blob: bd4e374e37f804c9b87be1ed960100390c4271ed [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 Iselyd8554972006-06-26 20:58:46 -030027#include "pvrusb2.h"
28#include "pvrusb2-std.h"
29#include "pvrusb2-util.h"
30#include "pvrusb2-hdw.h"
31#include "pvrusb2-i2c-core.h"
Mike Isely59af3362009-03-07 03:06:09 -030032#include "pvrusb2-i2c-track.h"
Mike Iselyd8554972006-06-26 20:58:46 -030033#include "pvrusb2-tuner.h"
34#include "pvrusb2-eeprom.h"
35#include "pvrusb2-hdw-internal.h"
36#include "pvrusb2-encoder.h"
37#include "pvrusb2-debug.h"
Michael Krufky8d364362007-01-22 02:17:55 -030038#include "pvrusb2-fx2-cmd.h"
Mike Iselyd8554972006-06-26 20:58:46 -030039
Mike Isely1bde0282006-12-27 23:30:13 -030040#define TV_MIN_FREQ 55250000L
41#define TV_MAX_FREQ 850000000L
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -030042
Mike Isely83ce57a2008-05-26 05:51:57 -030043/* This defines a minimum interval that the decoder must remain quiet
44 before we are allowed to start it running. */
45#define TIME_MSEC_DECODER_WAIT 50
46
47/* This defines a minimum interval that the encoder must remain quiet
Mike Iselyfa98e592008-05-26 05:54:24 -030048 before we are allowed to configure it. I had this originally set to
49 50msec, but Martin Dauskardt <martin.dauskardt@gmx.de> reports that
50 things work better when it's set to 100msec. */
51#define TIME_MSEC_ENCODER_WAIT 100
Mike Isely83ce57a2008-05-26 05:51:57 -030052
53/* This defines the minimum interval that the encoder must successfully run
54 before we consider that the encoder has run at least once since its
55 firmware has been loaded. This measurement is in important for cases
56 where we can't do something until we know that the encoder has been run
57 at least once. */
58#define TIME_MSEC_ENCODER_OK 250
59
Mike Iselya0fd1cb2006-06-30 11:35:28 -030060static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -030061static DEFINE_MUTEX(pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -030062
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030063static int ctlchg;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030064static int procreload;
Mike Iselyd8554972006-06-26 20:58:46 -030065static int tuner[PVR_NUM] = { [0 ... PVR_NUM-1] = -1 };
66static int tolerance[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
67static int video_std[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030068static int init_pause_msec;
Mike Iselyd8554972006-06-26 20:58:46 -030069
70module_param(ctlchg, int, S_IRUGO|S_IWUSR);
71MODULE_PARM_DESC(ctlchg, "0=optimize ctl change 1=always accept new ctl value");
72module_param(init_pause_msec, int, S_IRUGO|S_IWUSR);
73MODULE_PARM_DESC(init_pause_msec, "hardware initialization settling delay");
Mike Iselyd8554972006-06-26 20:58:46 -030074module_param(procreload, int, S_IRUGO|S_IWUSR);
75MODULE_PARM_DESC(procreload,
76 "Attempt init failure recovery with firmware reload");
77module_param_array(tuner, int, NULL, 0444);
78MODULE_PARM_DESC(tuner,"specify installed tuner type");
79module_param_array(video_std, int, NULL, 0444);
80MODULE_PARM_DESC(video_std,"specify initial video standard");
81module_param_array(tolerance, int, NULL, 0444);
82MODULE_PARM_DESC(tolerance,"specify stream error tolerance");
83
Michael Krufky5a4f5da62008-05-11 16:37:50 -030084/* US Broadcast channel 7 (175.25 MHz) */
85static int default_tv_freq = 175250000L;
86/* 104.3 MHz, a usable FM station for my area */
87static int default_radio_freq = 104300000L;
88
89module_param_named(tv_freq, default_tv_freq, int, 0444);
90MODULE_PARM_DESC(tv_freq, "specify initial television frequency");
91module_param_named(radio_freq, default_radio_freq, int, 0444);
92MODULE_PARM_DESC(radio_freq, "specify initial radio frequency");
93
Mike Iselyd8554972006-06-26 20:58:46 -030094#define PVR2_CTL_WRITE_ENDPOINT 0x01
95#define PVR2_CTL_READ_ENDPOINT 0x81
96
97#define PVR2_GPIO_IN 0x9008
98#define PVR2_GPIO_OUT 0x900c
99#define PVR2_GPIO_DIR 0x9020
100
101#define trace_firmware(...) pvr2_trace(PVR2_TRACE_FIRMWARE,__VA_ARGS__)
102
103#define PVR2_FIRMWARE_ENDPOINT 0x02
104
105/* size of a firmware chunk */
106#define FIRMWARE_CHUNK_SIZE 0x2000
107
Mike Iselye9c64a72009-03-06 23:42:20 -0300108static const char *module_names[] = {
109 [PVR2_CLIENT_ID_MSP3400] = "msp3400",
110 [PVR2_CLIENT_ID_CX25840] = "cx25840",
111 [PVR2_CLIENT_ID_SAA7115] = "saa7115",
112 [PVR2_CLIENT_ID_TUNER] = "tuner",
113 [PVR2_CLIENT_ID_CS53132A] = "cs53132a",
114};
115
116
117static const unsigned char *module_i2c_addresses[] = {
118 [PVR2_CLIENT_ID_TUNER] = "\x60\x61\x62\x63",
119};
120
121
Mike Iselyb30d2442006-06-25 20:05:01 -0300122/* Define the list of additional controls we'll dynamically construct based
123 on query of the cx2341x module. */
124struct pvr2_mpeg_ids {
125 const char *strid;
126 int id;
127};
128static const struct pvr2_mpeg_ids mpeg_ids[] = {
129 {
130 .strid = "audio_layer",
131 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
132 },{
133 .strid = "audio_bitrate",
134 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
135 },{
136 /* Already using audio_mode elsewhere :-( */
137 .strid = "mpeg_audio_mode",
138 .id = V4L2_CID_MPEG_AUDIO_MODE,
139 },{
140 .strid = "mpeg_audio_mode_extension",
141 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
142 },{
143 .strid = "audio_emphasis",
144 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
145 },{
146 .strid = "audio_crc",
147 .id = V4L2_CID_MPEG_AUDIO_CRC,
148 },{
149 .strid = "video_aspect",
150 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
151 },{
152 .strid = "video_b_frames",
153 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
154 },{
155 .strid = "video_gop_size",
156 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
157 },{
158 .strid = "video_gop_closure",
159 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
160 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300161 .strid = "video_bitrate_mode",
162 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
163 },{
164 .strid = "video_bitrate",
165 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
166 },{
167 .strid = "video_bitrate_peak",
168 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
169 },{
170 .strid = "video_temporal_decimation",
171 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
172 },{
173 .strid = "stream_type",
174 .id = V4L2_CID_MPEG_STREAM_TYPE,
175 },{
176 .strid = "video_spatial_filter_mode",
177 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
178 },{
179 .strid = "video_spatial_filter",
180 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
181 },{
182 .strid = "video_luma_spatial_filter_type",
183 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
184 },{
185 .strid = "video_chroma_spatial_filter_type",
186 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
187 },{
188 .strid = "video_temporal_filter_mode",
189 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
190 },{
191 .strid = "video_temporal_filter",
192 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
193 },{
194 .strid = "video_median_filter_type",
195 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
196 },{
197 .strid = "video_luma_median_filter_top",
198 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
199 },{
200 .strid = "video_luma_median_filter_bottom",
201 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
202 },{
203 .strid = "video_chroma_median_filter_top",
204 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
205 },{
206 .strid = "video_chroma_median_filter_bottom",
207 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
208 }
209};
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300210#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
Mike Iselyc05c0462006-06-25 20:04:25 -0300211
Mike Iselyd8554972006-06-26 20:58:46 -0300212
Mike Isely434449f2006-08-08 09:10:06 -0300213static const char *control_values_srate[] = {
214 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
215 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
216 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
217};
Mike Iselyd8554972006-06-26 20:58:46 -0300218
Mike Iselyd8554972006-06-26 20:58:46 -0300219
220
221static const char *control_values_input[] = {
222 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
Mike Isely29bf5b12008-04-22 14:45:37 -0300223 [PVR2_CVAL_INPUT_DTV] = "dtv",
Mike Iselyd8554972006-06-26 20:58:46 -0300224 [PVR2_CVAL_INPUT_RADIO] = "radio",
225 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
226 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
227};
228
229
230static const char *control_values_audiomode[] = {
231 [V4L2_TUNER_MODE_MONO] = "Mono",
232 [V4L2_TUNER_MODE_STEREO] = "Stereo",
233 [V4L2_TUNER_MODE_LANG1] = "Lang1",
234 [V4L2_TUNER_MODE_LANG2] = "Lang2",
235 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
236};
237
238
239static const char *control_values_hsm[] = {
240 [PVR2_CVAL_HSM_FAIL] = "Fail",
241 [PVR2_CVAL_HSM_HIGH] = "High",
242 [PVR2_CVAL_HSM_FULL] = "Full",
243};
244
245
Mike Isely681c7392007-11-26 01:48:52 -0300246static const char *pvr2_state_names[] = {
247 [PVR2_STATE_NONE] = "none",
248 [PVR2_STATE_DEAD] = "dead",
249 [PVR2_STATE_COLD] = "cold",
250 [PVR2_STATE_WARM] = "warm",
251 [PVR2_STATE_ERROR] = "error",
252 [PVR2_STATE_READY] = "ready",
253 [PVR2_STATE_RUN] = "run",
Mike Iselyd8554972006-06-26 20:58:46 -0300254};
255
Mike Isely681c7392007-11-26 01:48:52 -0300256
Mike Isely694dca2b2008-03-28 05:42:10 -0300257struct pvr2_fx2cmd_descdef {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300258 unsigned char id;
259 unsigned char *desc;
260};
261
Mike Isely694dca2b2008-03-28 05:42:10 -0300262static const struct pvr2_fx2cmd_descdef pvr2_fx2cmd_desc[] = {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300263 {FX2CMD_MEM_WRITE_DWORD, "write encoder dword"},
264 {FX2CMD_MEM_READ_DWORD, "read encoder dword"},
Mike Isely31335b12008-07-25 19:35:31 -0300265 {FX2CMD_HCW_ZILOG_RESET, "zilog IR reset control"},
Mike Isely1c9d10d2008-03-28 05:38:54 -0300266 {FX2CMD_MEM_READ_64BYTES, "read encoder 64bytes"},
267 {FX2CMD_REG_WRITE, "write encoder register"},
268 {FX2CMD_REG_READ, "read encoder register"},
269 {FX2CMD_MEMSEL, "encoder memsel"},
270 {FX2CMD_I2C_WRITE, "i2c write"},
271 {FX2CMD_I2C_READ, "i2c read"},
272 {FX2CMD_GET_USB_SPEED, "get USB speed"},
273 {FX2CMD_STREAMING_ON, "stream on"},
274 {FX2CMD_STREAMING_OFF, "stream off"},
275 {FX2CMD_FWPOST1, "fwpost1"},
276 {FX2CMD_POWER_OFF, "power off"},
277 {FX2CMD_POWER_ON, "power on"},
278 {FX2CMD_DEEP_RESET, "deep reset"},
279 {FX2CMD_GET_EEPROM_ADDR, "get rom addr"},
280 {FX2CMD_GET_IR_CODE, "get IR code"},
281 {FX2CMD_HCW_DEMOD_RESETIN, "hcw demod resetin"},
282 {FX2CMD_HCW_DTV_STREAMING_ON, "hcw dtv stream on"},
283 {FX2CMD_HCW_DTV_STREAMING_OFF, "hcw dtv stream off"},
284 {FX2CMD_ONAIR_DTV_STREAMING_ON, "onair dtv stream on"},
285 {FX2CMD_ONAIR_DTV_STREAMING_OFF, "onair dtv stream off"},
286 {FX2CMD_ONAIR_DTV_POWER_ON, "onair dtv power on"},
287 {FX2CMD_ONAIR_DTV_POWER_OFF, "onair dtv power off"},
288};
289
290
Mike Isely1cb03b72008-04-21 03:47:43 -0300291static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v);
Mike Isely681c7392007-11-26 01:48:52 -0300292static void pvr2_hdw_state_sched(struct pvr2_hdw *);
293static int pvr2_hdw_state_eval(struct pvr2_hdw *);
Mike Isely1bde0282006-12-27 23:30:13 -0300294static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300295static void pvr2_hdw_worker_i2c(struct work_struct *work);
296static void pvr2_hdw_worker_poll(struct work_struct *work);
Mike Isely681c7392007-11-26 01:48:52 -0300297static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
298static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
299static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300300static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300301static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300302static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300303static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
304static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300305static void pvr2_hdw_quiescent_timeout(unsigned long);
306static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Mike Iselyd913d632008-04-06 04:04:35 -0300307static void pvr2_hdw_encoder_run_timeout(unsigned long);
Mike Isely1c9d10d2008-03-28 05:38:54 -0300308static int pvr2_issue_simple_cmd(struct pvr2_hdw *,u32);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300309static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
310 unsigned int timeout,int probe_fl,
311 void *write_data,unsigned int write_len,
312 void *read_data,unsigned int read_len);
Mike Isely432907f2008-08-31 21:02:20 -0300313static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300314
Mike Isely681c7392007-11-26 01:48:52 -0300315
316static void trace_stbit(const char *name,int val)
317{
318 pvr2_trace(PVR2_TRACE_STBITS,
319 "State bit %s <-- %s",
320 name,(val ? "true" : "false"));
321}
322
Mike Iselyd8554972006-06-26 20:58:46 -0300323static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
324{
325 struct pvr2_hdw *hdw = cptr->hdw;
326 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
327 *vp = hdw->freqTable[hdw->freqProgSlot-1];
328 } else {
329 *vp = 0;
330 }
331 return 0;
332}
333
334static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
335{
336 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300337 unsigned int slotId = hdw->freqProgSlot;
338 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
339 hdw->freqTable[slotId-1] = v;
340 /* Handle side effects correctly - if we're tuned to this
341 slot, then forgot the slot id relation since the stored
342 frequency has been changed. */
343 if (hdw->freqSelector) {
344 if (hdw->freqSlotRadio == slotId) {
345 hdw->freqSlotRadio = 0;
346 }
347 } else {
348 if (hdw->freqSlotTelevision == slotId) {
349 hdw->freqSlotTelevision = 0;
350 }
351 }
Mike Iselyd8554972006-06-26 20:58:46 -0300352 }
353 return 0;
354}
355
356static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
357{
358 *vp = cptr->hdw->freqProgSlot;
359 return 0;
360}
361
362static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
363{
364 struct pvr2_hdw *hdw = cptr->hdw;
365 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
366 hdw->freqProgSlot = v;
367 }
368 return 0;
369}
370
371static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
372{
Mike Isely1bde0282006-12-27 23:30:13 -0300373 struct pvr2_hdw *hdw = cptr->hdw;
374 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300375 return 0;
376}
377
Mike Isely1bde0282006-12-27 23:30:13 -0300378static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300379{
380 unsigned freq = 0;
381 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300382 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
383 if (slotId > 0) {
384 freq = hdw->freqTable[slotId-1];
385 if (!freq) return 0;
386 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300387 }
Mike Isely1bde0282006-12-27 23:30:13 -0300388 if (hdw->freqSelector) {
389 hdw->freqSlotRadio = slotId;
390 } else {
391 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300392 }
393 return 0;
394}
395
396static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
397{
Mike Isely1bde0282006-12-27 23:30:13 -0300398 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300399 return 0;
400}
401
402static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
403{
404 return cptr->hdw->freqDirty != 0;
405}
406
407static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
408{
409 cptr->hdw->freqDirty = 0;
410}
411
412static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
413{
Mike Isely1bde0282006-12-27 23:30:13 -0300414 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300415 return 0;
416}
417
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300418static int ctrl_cropl_min_get(struct pvr2_ctrl *cptr, int *left)
419{
Mike Isely432907f2008-08-31 21:02:20 -0300420 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
421 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
422 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300423 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300424 }
Mike Isely432907f2008-08-31 21:02:20 -0300425 *left = cap->bounds.left;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300426 return 0;
427}
428
429static int ctrl_cropl_max_get(struct pvr2_ctrl *cptr, int *left)
430{
Mike Isely432907f2008-08-31 21:02:20 -0300431 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
432 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
433 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300434 return stat;
435 }
436 *left = cap->bounds.left;
437 if (cap->bounds.width > cptr->hdw->cropw_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300438 *left += cap->bounds.width - cptr->hdw->cropw_val;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300439 }
440 return 0;
441}
442
443static int ctrl_cropt_min_get(struct pvr2_ctrl *cptr, int *top)
444{
Mike Isely432907f2008-08-31 21:02:20 -0300445 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
446 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
447 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300448 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300449 }
Mike Isely432907f2008-08-31 21:02:20 -0300450 *top = cap->bounds.top;
451 return 0;
452}
453
454static int ctrl_cropt_max_get(struct pvr2_ctrl *cptr, int *top)
455{
456 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
457 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
458 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300459 return stat;
460 }
461 *top = cap->bounds.top;
462 if (cap->bounds.height > cptr->hdw->croph_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300463 *top += cap->bounds.height - cptr->hdw->croph_val;
464 }
465 return 0;
466}
467
468static int ctrl_cropw_max_get(struct pvr2_ctrl *cptr, int *val)
469{
470 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
471 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
472 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300473 return stat;
474 }
475 *val = 0;
476 if (cap->bounds.width > cptr->hdw->cropl_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300477 *val = cap->bounds.width - cptr->hdw->cropl_val;
478 }
479 return 0;
480}
481
482static int ctrl_croph_max_get(struct pvr2_ctrl *cptr, int *val)
483{
484 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
485 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
486 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300487 return stat;
488 }
489 *val = 0;
490 if (cap->bounds.height > cptr->hdw->cropt_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300491 *val = cap->bounds.height - cptr->hdw->cropt_val;
492 }
493 return 0;
494}
495
496static int ctrl_get_cropcapbl(struct pvr2_ctrl *cptr, int *val)
497{
498 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
499 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
500 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300501 return stat;
502 }
503 *val = cap->bounds.left;
504 return 0;
505}
506
507static int ctrl_get_cropcapbt(struct pvr2_ctrl *cptr, int *val)
508{
509 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
510 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
511 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300512 return stat;
513 }
514 *val = cap->bounds.top;
515 return 0;
516}
517
518static int ctrl_get_cropcapbw(struct pvr2_ctrl *cptr, int *val)
519{
520 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
521 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
522 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300523 return stat;
524 }
525 *val = cap->bounds.width;
526 return 0;
527}
528
529static int ctrl_get_cropcapbh(struct pvr2_ctrl *cptr, int *val)
530{
531 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
532 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
533 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300534 return stat;
535 }
536 *val = cap->bounds.height;
537 return 0;
538}
539
540static int ctrl_get_cropcapdl(struct pvr2_ctrl *cptr, int *val)
541{
542 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
543 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
544 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300545 return stat;
546 }
547 *val = cap->defrect.left;
548 return 0;
549}
550
551static int ctrl_get_cropcapdt(struct pvr2_ctrl *cptr, int *val)
552{
553 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
554 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
555 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300556 return stat;
557 }
558 *val = cap->defrect.top;
559 return 0;
560}
561
562static int ctrl_get_cropcapdw(struct pvr2_ctrl *cptr, int *val)
563{
564 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
565 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
566 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300567 return stat;
568 }
569 *val = cap->defrect.width;
570 return 0;
571}
572
573static int ctrl_get_cropcapdh(struct pvr2_ctrl *cptr, int *val)
574{
575 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
576 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
577 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300578 return stat;
579 }
580 *val = cap->defrect.height;
581 return 0;
582}
583
584static int ctrl_get_cropcappan(struct pvr2_ctrl *cptr, int *val)
585{
586 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
587 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
588 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300589 return stat;
590 }
591 *val = cap->pixelaspect.numerator;
592 return 0;
593}
594
595static int ctrl_get_cropcappad(struct pvr2_ctrl *cptr, int *val)
596{
597 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
598 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
599 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300600 return stat;
601 }
602 *val = cap->pixelaspect.denominator;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300603 return 0;
604}
605
Mike Isely3ad9fc32006-09-02 22:37:52 -0300606static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
607{
608 /* Actual maximum depends on the video standard in effect. */
609 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
610 *vp = 480;
611 } else {
612 *vp = 576;
613 }
614 return 0;
615}
616
617static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
618{
Mike Isely989eb152007-11-26 01:53:12 -0300619 /* Actual minimum depends on device digitizer type. */
620 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300621 *vp = 75;
622 } else {
623 *vp = 17;
624 }
625 return 0;
626}
627
Mike Isely1bde0282006-12-27 23:30:13 -0300628static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
629{
630 *vp = cptr->hdw->input_val;
631 return 0;
632}
633
Mike Isely29bf5b12008-04-22 14:45:37 -0300634static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
635{
Mike Isely1cb03b72008-04-21 03:47:43 -0300636 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300637}
638
Mike Isely1bde0282006-12-27 23:30:13 -0300639static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
640{
Mike Isely1cb03b72008-04-21 03:47:43 -0300641 return pvr2_hdw_set_input(cptr->hdw,v);
Mike Isely1bde0282006-12-27 23:30:13 -0300642}
643
644static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
645{
646 return cptr->hdw->input_dirty != 0;
647}
648
649static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
650{
651 cptr->hdw->input_dirty = 0;
652}
653
Mike Isely5549f542006-12-27 23:28:54 -0300654
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300655static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
656{
Mike Isely644afdb2007-01-20 00:19:23 -0300657 unsigned long fv;
658 struct pvr2_hdw *hdw = cptr->hdw;
659 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -0300660 pvr2_hdw_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300661 }
Mike Isely644afdb2007-01-20 00:19:23 -0300662 fv = hdw->tuner_signal_info.rangehigh;
663 if (!fv) {
664 /* Safety fallback */
665 *vp = TV_MAX_FREQ;
666 return 0;
667 }
668 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
669 fv = (fv * 125) / 2;
670 } else {
671 fv = fv * 62500;
672 }
673 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300674 return 0;
675}
676
677static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
678{
Mike Isely644afdb2007-01-20 00:19:23 -0300679 unsigned long fv;
680 struct pvr2_hdw *hdw = cptr->hdw;
681 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -0300682 pvr2_hdw_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300683 }
Mike Isely644afdb2007-01-20 00:19:23 -0300684 fv = hdw->tuner_signal_info.rangelow;
685 if (!fv) {
686 /* Safety fallback */
687 *vp = TV_MIN_FREQ;
688 return 0;
689 }
690 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
691 fv = (fv * 125) / 2;
692 } else {
693 fv = fv * 62500;
694 }
695 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300696 return 0;
697}
698
Mike Iselyb30d2442006-06-25 20:05:01 -0300699static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
700{
701 return cptr->hdw->enc_stale != 0;
702}
703
704static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
705{
706 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300707 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300708}
709
710static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
711{
712 int ret;
713 struct v4l2_ext_controls cs;
714 struct v4l2_ext_control c1;
715 memset(&cs,0,sizeof(cs));
716 memset(&c1,0,sizeof(c1));
717 cs.controls = &c1;
718 cs.count = 1;
719 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300720 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300721 VIDIOC_G_EXT_CTRLS);
722 if (ret) return ret;
723 *vp = c1.value;
724 return 0;
725}
726
727static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
728{
729 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300730 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300731 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;
738 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300739 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
740 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300741 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300742 if (ret == -EBUSY) {
743 /* Oops. cx2341x is telling us it's not safe to change
744 this control while we're capturing. Make a note of this
745 fact so that the pipeline will be stopped the next time
746 controls are committed. Then go on ahead and store this
747 change anyway. */
748 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
749 0, &cs,
750 VIDIOC_S_EXT_CTRLS);
751 if (!ret) hdw->enc_unsafe_stale = !0;
752 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300753 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300754 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300755 return 0;
756}
757
758static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
759{
760 struct v4l2_queryctrl qctrl;
761 struct pvr2_ctl_info *info;
762 qctrl.id = cptr->info->v4l_id;
763 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
764 /* Strip out the const so we can adjust a function pointer. It's
765 OK to do this here because we know this is a dynamically created
766 control, so the underlying storage for the info pointer is (a)
767 private to us, and (b) not in read-only storage. Either we do
768 this or we significantly complicate the underlying control
769 implementation. */
770 info = (struct pvr2_ctl_info *)(cptr->info);
771 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
772 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300773 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300774 }
775 } else {
776 if (!(info->set_value)) {
777 info->set_value = ctrl_cx2341x_set;
778 }
779 }
780 return qctrl.flags;
781}
782
Mike Iselyd8554972006-06-26 20:58:46 -0300783static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
784{
Mike Isely681c7392007-11-26 01:48:52 -0300785 *vp = cptr->hdw->state_pipeline_req;
786 return 0;
787}
788
789static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
790{
791 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300792 return 0;
793}
794
795static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
796{
797 int result = pvr2_hdw_is_hsm(cptr->hdw);
798 *vp = PVR2_CVAL_HSM_FULL;
799 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
800 if (result) *vp = PVR2_CVAL_HSM_HIGH;
801 return 0;
802}
803
804static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
805{
806 *vp = cptr->hdw->std_mask_avail;
807 return 0;
808}
809
810static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
811{
812 struct pvr2_hdw *hdw = cptr->hdw;
813 v4l2_std_id ns;
814 ns = hdw->std_mask_avail;
815 ns = (ns & ~m) | (v & m);
816 if (ns == hdw->std_mask_avail) return 0;
817 hdw->std_mask_avail = ns;
818 pvr2_hdw_internal_set_std_avail(hdw);
819 pvr2_hdw_internal_find_stdenum(hdw);
820 return 0;
821}
822
823static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
824 char *bufPtr,unsigned int bufSize,
825 unsigned int *len)
826{
827 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
828 return 0;
829}
830
831static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
832 const char *bufPtr,unsigned int bufSize,
833 int *mskp,int *valp)
834{
835 int ret;
836 v4l2_std_id id;
837 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
838 if (ret < 0) return ret;
839 if (mskp) *mskp = id;
840 if (valp) *valp = id;
841 return 0;
842}
843
844static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
845{
846 *vp = cptr->hdw->std_mask_cur;
847 return 0;
848}
849
850static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
851{
852 struct pvr2_hdw *hdw = cptr->hdw;
853 v4l2_std_id ns;
854 ns = hdw->std_mask_cur;
855 ns = (ns & ~m) | (v & m);
856 if (ns == hdw->std_mask_cur) return 0;
857 hdw->std_mask_cur = ns;
858 hdw->std_dirty = !0;
859 pvr2_hdw_internal_find_stdenum(hdw);
860 return 0;
861}
862
863static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
864{
865 return cptr->hdw->std_dirty != 0;
866}
867
868static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
869{
870 cptr->hdw->std_dirty = 0;
871}
872
873static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
874{
Mike Isely18103c572007-01-20 00:09:47 -0300875 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselya51f5002009-03-06 23:30:37 -0300876 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300877 *vp = hdw->tuner_signal_info.signal;
878 return 0;
879}
880
881static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
882{
883 int val = 0;
884 unsigned int subchan;
885 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselya51f5002009-03-06 23:30:37 -0300886 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300887 subchan = hdw->tuner_signal_info.rxsubchans;
888 if (subchan & V4L2_TUNER_SUB_MONO) {
889 val |= (1 << V4L2_TUNER_MODE_MONO);
890 }
891 if (subchan & V4L2_TUNER_SUB_STEREO) {
892 val |= (1 << V4L2_TUNER_MODE_STEREO);
893 }
894 if (subchan & V4L2_TUNER_SUB_LANG1) {
895 val |= (1 << V4L2_TUNER_MODE_LANG1);
896 }
897 if (subchan & V4L2_TUNER_SUB_LANG2) {
898 val |= (1 << V4L2_TUNER_MODE_LANG2);
899 }
900 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300901 return 0;
902}
903
Mike Iselyd8554972006-06-26 20:58:46 -0300904
905static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
906{
907 struct pvr2_hdw *hdw = cptr->hdw;
908 if (v < 0) return -EINVAL;
909 if (v > hdw->std_enum_cnt) return -EINVAL;
910 hdw->std_enum_cur = v;
911 if (!v) return 0;
912 v--;
913 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
914 hdw->std_mask_cur = hdw->std_defs[v].id;
915 hdw->std_dirty = !0;
916 return 0;
917}
918
919
920static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
921{
922 *vp = cptr->hdw->std_enum_cur;
923 return 0;
924}
925
926
927static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
928{
929 return cptr->hdw->std_dirty != 0;
930}
931
932
933static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
934{
935 cptr->hdw->std_dirty = 0;
936}
937
938
939#define DEFINT(vmin,vmax) \
940 .type = pvr2_ctl_int, \
941 .def.type_int.min_value = vmin, \
942 .def.type_int.max_value = vmax
943
944#define DEFENUM(tab) \
945 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300946 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300947 .def.type_enum.value_names = tab
948
Mike Isely33213962006-06-25 20:04:40 -0300949#define DEFBOOL \
950 .type = pvr2_ctl_bool
951
Mike Iselyd8554972006-06-26 20:58:46 -0300952#define DEFMASK(msk,tab) \
953 .type = pvr2_ctl_bitmask, \
954 .def.type_bitmask.valid_bits = msk, \
955 .def.type_bitmask.bit_names = tab
956
957#define DEFREF(vname) \
958 .set_value = ctrl_set_##vname, \
959 .get_value = ctrl_get_##vname, \
960 .is_dirty = ctrl_isdirty_##vname, \
961 .clear_dirty = ctrl_cleardirty_##vname
962
963
964#define VCREATE_FUNCS(vname) \
965static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
966{*vp = cptr->hdw->vname##_val; return 0;} \
967static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
968{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
969static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
970{return cptr->hdw->vname##_dirty != 0;} \
971static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
972{cptr->hdw->vname##_dirty = 0;}
973
974VCREATE_FUNCS(brightness)
975VCREATE_FUNCS(contrast)
976VCREATE_FUNCS(saturation)
977VCREATE_FUNCS(hue)
978VCREATE_FUNCS(volume)
979VCREATE_FUNCS(balance)
980VCREATE_FUNCS(bass)
981VCREATE_FUNCS(treble)
982VCREATE_FUNCS(mute)
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300983VCREATE_FUNCS(cropl)
984VCREATE_FUNCS(cropt)
985VCREATE_FUNCS(cropw)
986VCREATE_FUNCS(croph)
Mike Iselyc05c0462006-06-25 20:04:25 -0300987VCREATE_FUNCS(audiomode)
988VCREATE_FUNCS(res_hor)
989VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300990VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -0300991
Mike Iselyd8554972006-06-26 20:58:46 -0300992/* Table definition of all controls which can be manipulated */
993static const struct pvr2_ctl_info control_defs[] = {
994 {
995 .v4l_id = V4L2_CID_BRIGHTNESS,
996 .desc = "Brightness",
997 .name = "brightness",
998 .default_value = 128,
999 DEFREF(brightness),
1000 DEFINT(0,255),
1001 },{
1002 .v4l_id = V4L2_CID_CONTRAST,
1003 .desc = "Contrast",
1004 .name = "contrast",
1005 .default_value = 68,
1006 DEFREF(contrast),
1007 DEFINT(0,127),
1008 },{
1009 .v4l_id = V4L2_CID_SATURATION,
1010 .desc = "Saturation",
1011 .name = "saturation",
1012 .default_value = 64,
1013 DEFREF(saturation),
1014 DEFINT(0,127),
1015 },{
1016 .v4l_id = V4L2_CID_HUE,
1017 .desc = "Hue",
1018 .name = "hue",
1019 .default_value = 0,
1020 DEFREF(hue),
1021 DEFINT(-128,127),
1022 },{
1023 .v4l_id = V4L2_CID_AUDIO_VOLUME,
1024 .desc = "Volume",
1025 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -03001026 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -03001027 DEFREF(volume),
1028 DEFINT(0,65535),
1029 },{
1030 .v4l_id = V4L2_CID_AUDIO_BALANCE,
1031 .desc = "Balance",
1032 .name = "balance",
1033 .default_value = 0,
1034 DEFREF(balance),
1035 DEFINT(-32768,32767),
1036 },{
1037 .v4l_id = V4L2_CID_AUDIO_BASS,
1038 .desc = "Bass",
1039 .name = "bass",
1040 .default_value = 0,
1041 DEFREF(bass),
1042 DEFINT(-32768,32767),
1043 },{
1044 .v4l_id = V4L2_CID_AUDIO_TREBLE,
1045 .desc = "Treble",
1046 .name = "treble",
1047 .default_value = 0,
1048 DEFREF(treble),
1049 DEFINT(-32768,32767),
1050 },{
1051 .v4l_id = V4L2_CID_AUDIO_MUTE,
1052 .desc = "Mute",
1053 .name = "mute",
1054 .default_value = 0,
1055 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -03001056 DEFBOOL,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001057 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001058 .desc = "Capture crop left margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001059 .name = "crop_left",
1060 .internal_id = PVR2_CID_CROPL,
1061 .default_value = 0,
1062 DEFREF(cropl),
1063 DEFINT(-129, 340),
1064 .get_min_value = ctrl_cropl_min_get,
1065 .get_max_value = ctrl_cropl_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001066 .get_def_value = ctrl_get_cropcapdl,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001067 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001068 .desc = "Capture crop top margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001069 .name = "crop_top",
1070 .internal_id = PVR2_CID_CROPT,
1071 .default_value = 0,
1072 DEFREF(cropt),
1073 DEFINT(-35, 544),
1074 .get_min_value = ctrl_cropt_min_get,
1075 .get_max_value = ctrl_cropt_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001076 .get_def_value = ctrl_get_cropcapdt,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001077 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001078 .desc = "Capture crop width",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001079 .name = "crop_width",
1080 .internal_id = PVR2_CID_CROPW,
1081 .default_value = 720,
1082 DEFREF(cropw),
Mike Isely432907f2008-08-31 21:02:20 -03001083 .get_max_value = ctrl_cropw_max_get,
1084 .get_def_value = ctrl_get_cropcapdw,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001085 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001086 .desc = "Capture crop height",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001087 .name = "crop_height",
1088 .internal_id = PVR2_CID_CROPH,
1089 .default_value = 480,
1090 DEFREF(croph),
Mike Isely432907f2008-08-31 21:02:20 -03001091 .get_max_value = ctrl_croph_max_get,
1092 .get_def_value = ctrl_get_cropcapdh,
1093 }, {
1094 .desc = "Capture capability pixel aspect numerator",
1095 .name = "cropcap_pixel_numerator",
1096 .internal_id = PVR2_CID_CROPCAPPAN,
1097 .get_value = ctrl_get_cropcappan,
1098 }, {
1099 .desc = "Capture capability pixel aspect denominator",
1100 .name = "cropcap_pixel_denominator",
1101 .internal_id = PVR2_CID_CROPCAPPAD,
1102 .get_value = ctrl_get_cropcappad,
1103 }, {
1104 .desc = "Capture capability bounds top",
1105 .name = "cropcap_bounds_top",
1106 .internal_id = PVR2_CID_CROPCAPBT,
1107 .get_value = ctrl_get_cropcapbt,
1108 }, {
1109 .desc = "Capture capability bounds left",
1110 .name = "cropcap_bounds_left",
1111 .internal_id = PVR2_CID_CROPCAPBL,
1112 .get_value = ctrl_get_cropcapbl,
1113 }, {
1114 .desc = "Capture capability bounds width",
1115 .name = "cropcap_bounds_width",
1116 .internal_id = PVR2_CID_CROPCAPBW,
1117 .get_value = ctrl_get_cropcapbw,
1118 }, {
1119 .desc = "Capture capability bounds height",
1120 .name = "cropcap_bounds_height",
1121 .internal_id = PVR2_CID_CROPCAPBH,
1122 .get_value = ctrl_get_cropcapbh,
Mike Iselyd8554972006-06-26 20:58:46 -03001123 },{
Mike Iselyc05c0462006-06-25 20:04:25 -03001124 .desc = "Video Source",
1125 .name = "input",
1126 .internal_id = PVR2_CID_INPUT,
1127 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -03001128 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -03001129 DEFREF(input),
1130 DEFENUM(control_values_input),
1131 },{
1132 .desc = "Audio Mode",
1133 .name = "audio_mode",
1134 .internal_id = PVR2_CID_AUDIOMODE,
1135 .default_value = V4L2_TUNER_MODE_STEREO,
1136 DEFREF(audiomode),
1137 DEFENUM(control_values_audiomode),
1138 },{
1139 .desc = "Horizontal capture resolution",
1140 .name = "resolution_hor",
1141 .internal_id = PVR2_CID_HRES,
1142 .default_value = 720,
1143 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001144 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -03001145 },{
1146 .desc = "Vertical capture resolution",
1147 .name = "resolution_ver",
1148 .internal_id = PVR2_CID_VRES,
1149 .default_value = 480,
1150 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001151 DEFINT(17,576),
1152 /* Hook in check for video standard and adjust maximum
1153 depending on the standard. */
1154 .get_max_value = ctrl_vres_max_get,
1155 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -03001156 },{
Mike Iselyb30d2442006-06-25 20:05:01 -03001157 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -03001158 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
1159 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -03001160 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -03001161 DEFREF(srate),
1162 DEFENUM(control_values_srate),
1163 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001164 .desc = "Tuner Frequency (Hz)",
1165 .name = "frequency",
1166 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -03001167 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -03001168 .set_value = ctrl_freq_set,
1169 .get_value = ctrl_freq_get,
1170 .is_dirty = ctrl_freq_is_dirty,
1171 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -03001172 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -03001173 /* Hook in check for input value (tv/radio) and adjust
1174 max/min values accordingly */
1175 .get_max_value = ctrl_freq_max_get,
1176 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001177 },{
1178 .desc = "Channel",
1179 .name = "channel",
1180 .set_value = ctrl_channel_set,
1181 .get_value = ctrl_channel_get,
1182 DEFINT(0,FREQTABLE_SIZE),
1183 },{
1184 .desc = "Channel Program Frequency",
1185 .name = "freq_table_value",
1186 .set_value = ctrl_channelfreq_set,
1187 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -03001188 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -03001189 /* Hook in check for input value (tv/radio) and adjust
1190 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -03001191 .get_max_value = ctrl_freq_max_get,
1192 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001193 },{
1194 .desc = "Channel Program ID",
1195 .name = "freq_table_channel",
1196 .set_value = ctrl_channelprog_set,
1197 .get_value = ctrl_channelprog_get,
1198 DEFINT(0,FREQTABLE_SIZE),
1199 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001200 .desc = "Streaming Enabled",
1201 .name = "streaming_enabled",
1202 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -03001203 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -03001204 },{
1205 .desc = "USB Speed",
1206 .name = "usb_speed",
1207 .get_value = ctrl_hsm_get,
1208 DEFENUM(control_values_hsm),
1209 },{
Mike Isely681c7392007-11-26 01:48:52 -03001210 .desc = "Master State",
1211 .name = "master_state",
1212 .get_value = ctrl_masterstate_get,
1213 DEFENUM(pvr2_state_names),
1214 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001215 .desc = "Signal Present",
1216 .name = "signal_present",
1217 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -03001218 DEFINT(0,65535),
1219 },{
1220 .desc = "Audio Modes Present",
1221 .name = "audio_modes_present",
1222 .get_value = ctrl_audio_modes_present_get,
1223 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
1224 v4l. Nothing outside of this module cares about this,
1225 but I reuse it in order to also reuse the
1226 control_values_audiomode string table. */
1227 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
1228 (1 << V4L2_TUNER_MODE_STEREO)|
1229 (1 << V4L2_TUNER_MODE_LANG1)|
1230 (1 << V4L2_TUNER_MODE_LANG2)),
1231 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -03001232 },{
1233 .desc = "Video Standards Available Mask",
1234 .name = "video_standard_mask_available",
1235 .internal_id = PVR2_CID_STDAVAIL,
1236 .skip_init = !0,
1237 .get_value = ctrl_stdavail_get,
1238 .set_value = ctrl_stdavail_set,
1239 .val_to_sym = ctrl_std_val_to_sym,
1240 .sym_to_val = ctrl_std_sym_to_val,
1241 .type = pvr2_ctl_bitmask,
1242 },{
1243 .desc = "Video Standards In Use Mask",
1244 .name = "video_standard_mask_active",
1245 .internal_id = PVR2_CID_STDCUR,
1246 .skip_init = !0,
1247 .get_value = ctrl_stdcur_get,
1248 .set_value = ctrl_stdcur_set,
1249 .is_dirty = ctrl_stdcur_is_dirty,
1250 .clear_dirty = ctrl_stdcur_clear_dirty,
1251 .val_to_sym = ctrl_std_val_to_sym,
1252 .sym_to_val = ctrl_std_sym_to_val,
1253 .type = pvr2_ctl_bitmask,
1254 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001255 .desc = "Video Standard Name",
1256 .name = "video_standard",
1257 .internal_id = PVR2_CID_STDENUM,
1258 .skip_init = !0,
1259 .get_value = ctrl_stdenumcur_get,
1260 .set_value = ctrl_stdenumcur_set,
1261 .is_dirty = ctrl_stdenumcur_is_dirty,
1262 .clear_dirty = ctrl_stdenumcur_clear_dirty,
1263 .type = pvr2_ctl_enum,
1264 }
1265};
1266
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001267#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -03001268
1269
1270const char *pvr2_config_get_name(enum pvr2_config cfg)
1271{
1272 switch (cfg) {
1273 case pvr2_config_empty: return "empty";
1274 case pvr2_config_mpeg: return "mpeg";
1275 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -03001276 case pvr2_config_pcm: return "pcm";
1277 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -03001278 }
1279 return "<unknown>";
1280}
1281
1282
1283struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1284{
1285 return hdw->usb_dev;
1286}
1287
1288
1289unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1290{
1291 return hdw->serial_number;
1292}
1293
Mike Isely31a18542007-04-08 01:11:47 -03001294
1295const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1296{
1297 return hdw->bus_info;
1298}
1299
1300
Mike Isely13a88792009-01-14 04:22:56 -03001301const char *pvr2_hdw_get_device_identifier(struct pvr2_hdw *hdw)
1302{
1303 return hdw->identifier;
1304}
1305
1306
Mike Isely1bde0282006-12-27 23:30:13 -03001307unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1308{
1309 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1310}
1311
1312/* Set the currently tuned frequency and account for all possible
1313 driver-core side effects of this action. */
Adrian Bunkf55a8712008-04-18 05:38:56 -03001314static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
Mike Isely1bde0282006-12-27 23:30:13 -03001315{
Mike Isely7c74e572007-01-20 00:15:41 -03001316 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001317 if (hdw->freqSelector) {
1318 /* Swing over to radio frequency selection */
1319 hdw->freqSelector = 0;
1320 hdw->freqDirty = !0;
1321 }
Mike Isely1bde0282006-12-27 23:30:13 -03001322 if (hdw->freqValRadio != val) {
1323 hdw->freqValRadio = val;
1324 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001325 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001326 }
Mike Isely7c74e572007-01-20 00:15:41 -03001327 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001328 if (!(hdw->freqSelector)) {
1329 /* Swing over to television frequency selection */
1330 hdw->freqSelector = 1;
1331 hdw->freqDirty = !0;
1332 }
Mike Isely1bde0282006-12-27 23:30:13 -03001333 if (hdw->freqValTelevision != val) {
1334 hdw->freqValTelevision = val;
1335 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001336 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001337 }
Mike Isely1bde0282006-12-27 23:30:13 -03001338 }
1339}
1340
Mike Iselyd8554972006-06-26 20:58:46 -03001341int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1342{
1343 return hdw->unit_number;
1344}
1345
1346
1347/* Attempt to locate one of the given set of files. Messages are logged
1348 appropriate to what has been found. The return value will be 0 or
1349 greater on success (it will be the index of the file name found) and
1350 fw_entry will be filled in. Otherwise a negative error is returned on
1351 failure. If the return value is -ENOENT then no viable firmware file
1352 could be located. */
1353static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1354 const struct firmware **fw_entry,
1355 const char *fwtypename,
1356 unsigned int fwcount,
1357 const char *fwnames[])
1358{
1359 unsigned int idx;
1360 int ret = -EINVAL;
1361 for (idx = 0; idx < fwcount; idx++) {
1362 ret = request_firmware(fw_entry,
1363 fwnames[idx],
1364 &hdw->usb_dev->dev);
1365 if (!ret) {
1366 trace_firmware("Located %s firmware: %s;"
1367 " uploading...",
1368 fwtypename,
1369 fwnames[idx]);
1370 return idx;
1371 }
1372 if (ret == -ENOENT) continue;
1373 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1374 "request_firmware fatal error with code=%d",ret);
1375 return ret;
1376 }
1377 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1378 "***WARNING***"
1379 " Device %s firmware"
1380 " seems to be missing.",
1381 fwtypename);
1382 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1383 "Did you install the pvrusb2 firmware files"
1384 " in their proper location?");
1385 if (fwcount == 1) {
1386 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1387 "request_firmware unable to locate %s file %s",
1388 fwtypename,fwnames[0]);
1389 } else {
1390 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1391 "request_firmware unable to locate"
1392 " one of the following %s files:",
1393 fwtypename);
1394 for (idx = 0; idx < fwcount; idx++) {
1395 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1396 "request_firmware: Failed to find %s",
1397 fwnames[idx]);
1398 }
1399 }
1400 return ret;
1401}
1402
1403
1404/*
1405 * pvr2_upload_firmware1().
1406 *
1407 * Send the 8051 firmware to the device. After the upload, arrange for
1408 * device to re-enumerate.
1409 *
1410 * NOTE : the pointer to the firmware data given by request_firmware()
1411 * is not suitable for an usb transaction.
1412 *
1413 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001414static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001415{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001416 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001417 void *fw_ptr;
1418 unsigned int pipe;
1419 int ret;
1420 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001421
Mike Isely989eb152007-11-26 01:53:12 -03001422 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001423 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001424 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1425 "Connected device type defines"
1426 " no firmware to upload; ignoring firmware");
1427 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001428 }
1429
Mike Iselyd8554972006-06-26 20:58:46 -03001430 hdw->fw1_state = FW1_STATE_FAILED; // default result
1431
1432 trace_firmware("pvr2_upload_firmware1");
1433
1434 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001435 hdw->hdw_desc->fx2_firmware.cnt,
1436 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001437 if (ret < 0) {
1438 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1439 return ret;
1440 }
1441
1442 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1443 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1444
1445 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1446
1447 if (fw_entry->size != 0x2000){
1448 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1449 release_firmware(fw_entry);
1450 return -ENOMEM;
1451 }
1452
1453 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1454 if (fw_ptr == NULL){
1455 release_firmware(fw_entry);
1456 return -ENOMEM;
1457 }
1458
1459 /* We have to hold the CPU during firmware upload. */
1460 pvr2_hdw_cpureset_assert(hdw,1);
1461
1462 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1463 chunk. */
1464
1465 ret = 0;
1466 for(address = 0; address < fw_entry->size; address += 0x800) {
1467 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1468 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1469 0, fw_ptr, 0x800, HZ);
1470 }
1471
1472 trace_firmware("Upload done, releasing device's CPU");
1473
1474 /* Now release the CPU. It will disconnect and reconnect later. */
1475 pvr2_hdw_cpureset_assert(hdw,0);
1476
1477 kfree(fw_ptr);
1478 release_firmware(fw_entry);
1479
1480 trace_firmware("Upload done (%d bytes sent)",ret);
1481
1482 /* We should have written 8192 bytes */
1483 if (ret == 8192) {
1484 hdw->fw1_state = FW1_STATE_RELOAD;
1485 return 0;
1486 }
1487
1488 return -EIO;
1489}
1490
1491
1492/*
1493 * pvr2_upload_firmware2()
1494 *
1495 * This uploads encoder firmware on endpoint 2.
1496 *
1497 */
1498
1499int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1500{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001501 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001502 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001503 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001504 int actual_length;
1505 int ret = 0;
1506 int fwidx;
1507 static const char *fw_files[] = {
1508 CX2341X_FIRM_ENC_FILENAME,
1509 };
1510
Mike Isely989eb152007-11-26 01:53:12 -03001511 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001512 return 0;
1513 }
1514
Mike Iselyd8554972006-06-26 20:58:46 -03001515 trace_firmware("pvr2_upload_firmware2");
1516
1517 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001518 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001519 if (ret < 0) return ret;
1520 fwidx = ret;
1521 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001522 /* Since we're about to completely reinitialize the encoder,
1523 invalidate our cached copy of its configuration state. Next
1524 time we configure the encoder, then we'll fully configure it. */
1525 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001526
Mike Iselyd913d632008-04-06 04:04:35 -03001527 /* Encoder is about to be reset so note that as far as we're
1528 concerned now, the encoder has never been run. */
1529 del_timer_sync(&hdw->encoder_run_timer);
1530 if (hdw->state_encoder_runok) {
1531 hdw->state_encoder_runok = 0;
1532 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1533 }
1534
Mike Iselyd8554972006-06-26 20:58:46 -03001535 /* First prepare firmware loading */
1536 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1537 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1538 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1539 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1540 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1541 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1542 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1543 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1544 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1545 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1546 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1547 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1548 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1549 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1550 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1551 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001552 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1553 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001554
1555 if (ret) {
1556 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1557 "firmware2 upload prep failed, ret=%d",ret);
1558 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001559 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001560 }
1561
1562 /* Now send firmware */
1563
1564 fw_len = fw_entry->size;
1565
Mike Isely90060d32007-02-08 02:02:53 -03001566 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001567 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1568 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001569 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001570 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001571 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001572 ret = -EINVAL;
1573 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001574 }
1575
1576 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1577 if (fw_ptr == NULL){
1578 release_firmware(fw_entry);
1579 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1580 "failed to allocate memory for firmware2 upload");
Mike Isely21684ba2008-04-21 03:49:33 -03001581 ret = -ENOMEM;
1582 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001583 }
1584
1585 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1586
Mike Isely90060d32007-02-08 02:02:53 -03001587 fw_done = 0;
1588 for (fw_done = 0; fw_done < fw_len;) {
1589 bcnt = fw_len - fw_done;
1590 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1591 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1592 /* Usbsnoop log shows that we must swap bytes... */
Mike Isely5f33df12008-08-30 15:09:31 -03001593 /* Some background info: The data being swapped here is a
1594 firmware image destined for the mpeg encoder chip that
1595 lives at the other end of a USB endpoint. The encoder
1596 chip always talks in 32 bit chunks and its storage is
1597 organized into 32 bit words. However from the file
1598 system to the encoder chip everything is purely a byte
1599 stream. The firmware file's contents are always 32 bit
1600 swapped from what the encoder expects. Thus the need
1601 always exists to swap the bytes regardless of the endian
1602 type of the host processor and therefore swab32() makes
1603 the most sense. */
Mike Isely90060d32007-02-08 02:02:53 -03001604 for (icnt = 0; icnt < bcnt/4 ; icnt++)
Harvey Harrison513edce2008-08-18 17:38:01 -03001605 ((u32 *)fw_ptr)[icnt] = swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001606
Mike Isely90060d32007-02-08 02:02:53 -03001607 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001608 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001609 ret |= (actual_length != bcnt);
1610 if (ret) break;
1611 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001612 }
1613
1614 trace_firmware("upload of %s : %i / %i ",
1615 fw_files[fwidx],fw_done,fw_len);
1616
1617 kfree(fw_ptr);
1618 release_firmware(fw_entry);
1619
1620 if (ret) {
1621 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1622 "firmware2 upload transfer failure");
Mike Isely21684ba2008-04-21 03:49:33 -03001623 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001624 }
1625
1626 /* Finish upload */
1627
1628 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1629 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001630 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001631
1632 if (ret) {
1633 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1634 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001635 }
Mike Isely21684ba2008-04-21 03:49:33 -03001636
1637 done:
Mike Isely1df59f02008-04-21 03:50:39 -03001638 if (hdw->hdw_desc->signal_routing_scheme ==
1639 PVR2_ROUTING_SCHEME_GOTVIEW) {
1640 /* Ensure that GPIO 11 is set to output for GOTVIEW
1641 hardware. */
1642 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1643 }
Mike Iselyd8554972006-06-26 20:58:46 -03001644 return ret;
1645}
1646
1647
Mike Isely681c7392007-11-26 01:48:52 -03001648static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001649{
Mike Isely681c7392007-11-26 01:48:52 -03001650 if (st < ARRAY_SIZE(pvr2_state_names)) {
1651 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001652 }
Mike Isely681c7392007-11-26 01:48:52 -03001653 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001654}
1655
Mike Isely681c7392007-11-26 01:48:52 -03001656static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001657{
Mike Iselyaf78e162009-03-07 00:21:30 -03001658 if (hdw->decoder_ctrl) {
1659 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt, enablefl);
1660 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001661 }
Mike Iselyaf78e162009-03-07 00:21:30 -03001662 /* Even though we really only care about the video decoder chip at
1663 this point, we'll broadcast stream on/off to all sub-devices
1664 anyway, just in case somebody else wants to hear the
1665 command... */
1666 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_stream, enablefl);
1667 if (hdw->decoder_client_id) {
1668 /* We get here if the encoder has been noticed. Otherwise
1669 we'll issue a warning to the user (which should
1670 normally never happen). */
1671 return 0;
1672 }
1673 if (!hdw->flag_decoder_missed) {
1674 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1675 "WARNING: No decoder present");
1676 hdw->flag_decoder_missed = !0;
1677 trace_stbit("flag_decoder_missed",
1678 hdw->flag_decoder_missed);
1679 }
1680 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001681}
1682
1683
Mike Isely681c7392007-11-26 01:48:52 -03001684void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1685{
1686 if (hdw->decoder_ctrl == ptr) return;
1687 hdw->decoder_ctrl = ptr;
1688 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1689 hdw->flag_decoder_missed = 0;
1690 trace_stbit("flag_decoder_missed",
1691 hdw->flag_decoder_missed);
1692 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1693 "Decoder has appeared");
1694 pvr2_hdw_state_sched(hdw);
1695 }
1696}
1697
1698
1699int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1700{
1701 return hdw->master_state;
1702}
1703
1704
1705static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1706{
1707 if (!hdw->flag_tripped) return 0;
1708 hdw->flag_tripped = 0;
1709 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1710 "Clearing driver error statuss");
1711 return !0;
1712}
1713
1714
1715int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1716{
1717 int fl;
1718 LOCK_TAKE(hdw->big_lock); do {
1719 fl = pvr2_hdw_untrip_unlocked(hdw);
1720 } while (0); LOCK_GIVE(hdw->big_lock);
1721 if (fl) pvr2_hdw_state_sched(hdw);
1722 return 0;
1723}
1724
1725
Mike Isely681c7392007-11-26 01:48:52 -03001726
1727
Mike Iselyd8554972006-06-26 20:58:46 -03001728int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1729{
Mike Isely681c7392007-11-26 01:48:52 -03001730 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001731}
1732
1733
1734int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1735{
Mike Isely681c7392007-11-26 01:48:52 -03001736 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001737 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001738 pvr2_hdw_untrip_unlocked(hdw);
1739 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1740 hdw->state_pipeline_req = enable_flag != 0;
1741 pvr2_trace(PVR2_TRACE_START_STOP,
1742 "/*--TRACE_STREAM--*/ %s",
1743 enable_flag ? "enable" : "disable");
1744 }
1745 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001746 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001747 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1748 if (enable_flag) {
1749 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1750 if (st != PVR2_STATE_READY) return -EIO;
1751 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1752 }
1753 }
Mike Iselyd8554972006-06-26 20:58:46 -03001754 return 0;
1755}
1756
1757
1758int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1759{
Mike Isely681c7392007-11-26 01:48:52 -03001760 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001761 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001762 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1763 hdw->desired_stream_type = config;
1764 hdw->state_pipeline_config = 0;
1765 trace_stbit("state_pipeline_config",
1766 hdw->state_pipeline_config);
1767 pvr2_hdw_state_sched(hdw);
1768 }
Mike Iselyd8554972006-06-26 20:58:46 -03001769 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001770 if (fl) return 0;
1771 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001772}
1773
1774
1775static int get_default_tuner_type(struct pvr2_hdw *hdw)
1776{
1777 int unit_number = hdw->unit_number;
1778 int tp = -1;
1779 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1780 tp = tuner[unit_number];
1781 }
1782 if (tp < 0) return -EINVAL;
1783 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001784 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001785 return 0;
1786}
1787
1788
1789static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1790{
1791 int unit_number = hdw->unit_number;
1792 int tp = 0;
1793 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1794 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001795 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001796 }
Mike Isely6a540252007-12-02 23:51:34 -03001797 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001798}
1799
1800
1801static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1802{
1803 int unit_number = hdw->unit_number;
1804 int tp = 0;
1805 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1806 tp = tolerance[unit_number];
1807 }
1808 return tp;
1809}
1810
1811
1812static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1813{
1814 /* Try a harmless request to fetch the eeprom's address over
1815 endpoint 1. See what happens. Only the full FX2 image can
1816 respond to this. If this probe fails then likely the FX2
1817 firmware needs be loaded. */
1818 int result;
1819 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001820 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001821 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1822 hdw->cmd_buffer,1,
1823 hdw->cmd_buffer,1);
1824 if (result < 0) break;
1825 } while(0); LOCK_GIVE(hdw->ctl_lock);
1826 if (result) {
1827 pvr2_trace(PVR2_TRACE_INIT,
1828 "Probe of device endpoint 1 result status %d",
1829 result);
1830 } else {
1831 pvr2_trace(PVR2_TRACE_INIT,
1832 "Probe of device endpoint 1 succeeded");
1833 }
1834 return result == 0;
1835}
1836
Mike Isely9f66d4e2007-09-08 22:28:51 -03001837struct pvr2_std_hack {
1838 v4l2_std_id pat; /* Pattern to match */
1839 v4l2_std_id msk; /* Which bits we care about */
1840 v4l2_std_id std; /* What additional standards or default to set */
1841};
1842
1843/* This data structure labels specific combinations of standards from
1844 tveeprom that we'll try to recognize. If we recognize one, then assume
1845 a specified default standard to use. This is here because tveeprom only
1846 tells us about available standards not the intended default standard (if
1847 any) for the device in question. We guess the default based on what has
1848 been reported as available. Note that this is only for guessing a
1849 default - which can always be overridden explicitly - and if the user
1850 has otherwise named a default then that default will always be used in
1851 place of this table. */
Tobias Klauserebff0332008-04-22 14:45:45 -03001852static const struct pvr2_std_hack std_eeprom_maps[] = {
Mike Isely9f66d4e2007-09-08 22:28:51 -03001853 { /* PAL(B/G) */
1854 .pat = V4L2_STD_B|V4L2_STD_GH,
1855 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1856 },
1857 { /* NTSC(M) */
1858 .pat = V4L2_STD_MN,
1859 .std = V4L2_STD_NTSC_M,
1860 },
1861 { /* PAL(I) */
1862 .pat = V4L2_STD_PAL_I,
1863 .std = V4L2_STD_PAL_I,
1864 },
1865 { /* SECAM(L/L') */
1866 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1867 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1868 },
1869 { /* PAL(D/D1/K) */
1870 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001871 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001872 },
1873};
1874
Mike Iselyd8554972006-06-26 20:58:46 -03001875static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1876{
1877 char buf[40];
1878 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001879 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001880
1881 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001882 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001883
1884 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001885 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001886 "Supported video standard(s) reported available"
1887 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001888 bcnt,buf);
1889
1890 hdw->std_mask_avail = hdw->std_mask_eeprom;
1891
Mike Isely3d290bd2007-12-03 01:47:12 -03001892 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001893 if (std2) {
1894 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001895 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001896 "Expanding supported video standards"
1897 " to include: %.*s",
1898 bcnt,buf);
1899 hdw->std_mask_avail |= std2;
1900 }
1901
1902 pvr2_hdw_internal_set_std_avail(hdw);
1903
1904 if (std1) {
1905 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001906 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001907 "Initial video standard forced to %.*s",
1908 bcnt,buf);
1909 hdw->std_mask_cur = std1;
1910 hdw->std_dirty = !0;
1911 pvr2_hdw_internal_find_stdenum(hdw);
1912 return;
1913 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001914 if (std3) {
1915 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1916 pvr2_trace(PVR2_TRACE_STD,
1917 "Initial video standard"
1918 " (determined by device type): %.*s",bcnt,buf);
1919 hdw->std_mask_cur = std3;
1920 hdw->std_dirty = !0;
1921 pvr2_hdw_internal_find_stdenum(hdw);
1922 return;
1923 }
Mike Iselyd8554972006-06-26 20:58:46 -03001924
Mike Isely9f66d4e2007-09-08 22:28:51 -03001925 {
1926 unsigned int idx;
1927 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1928 if (std_eeprom_maps[idx].msk ?
1929 ((std_eeprom_maps[idx].pat ^
1930 hdw->std_mask_eeprom) &
1931 std_eeprom_maps[idx].msk) :
1932 (std_eeprom_maps[idx].pat !=
1933 hdw->std_mask_eeprom)) continue;
1934 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1935 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001936 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001937 "Initial video standard guessed as %.*s",
1938 bcnt,buf);
1939 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1940 hdw->std_dirty = !0;
1941 pvr2_hdw_internal_find_stdenum(hdw);
1942 return;
1943 }
1944 }
1945
Mike Iselyd8554972006-06-26 20:58:46 -03001946 if (hdw->std_enum_cnt > 1) {
1947 // Autoselect the first listed standard
1948 hdw->std_enum_cur = 1;
1949 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1950 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001951 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001952 "Initial video standard auto-selected to %s",
1953 hdw->std_defs[hdw->std_enum_cur-1].name);
1954 return;
1955 }
1956
Mike Isely0885ba12006-06-25 21:30:47 -03001957 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001958 "Unable to select a viable initial video standard");
1959}
1960
1961
Mike Iselye9c64a72009-03-06 23:42:20 -03001962static unsigned int pvr2_copy_i2c_addr_list(
1963 unsigned short *dst, const unsigned char *src,
1964 unsigned int dst_max)
1965{
1966 unsigned int cnt;
1967 if (!src) return 0;
1968 while (src[cnt] && (cnt + 1) < dst_max) {
1969 dst[cnt] = src[cnt];
1970 cnt++;
1971 }
1972 dst[cnt] = I2C_CLIENT_END;
1973 return cnt;
1974}
1975
1976
Mike Isely1ab5e742009-03-07 00:24:24 -03001977static int pvr2_hdw_load_subdev(struct pvr2_hdw *hdw,
1978 const struct pvr2_device_client_desc *cd)
Mike Iselye9c64a72009-03-06 23:42:20 -03001979{
1980 const char *fname;
1981 unsigned char mid;
1982 struct v4l2_subdev *sd;
1983 unsigned int i2ccnt;
1984 const unsigned char *p;
1985 /* Arbitrary count - max # i2c addresses we will probe */
1986 unsigned short i2caddr[25];
1987
1988 mid = cd->module_id;
1989 fname = (mid < ARRAY_SIZE(module_names)) ? module_names[mid] : NULL;
1990 if (!fname) {
1991 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03001992 "Module ID %u for device %s has no name",
Mike Iselye9c64a72009-03-06 23:42:20 -03001993 mid,
1994 hdw->hdw_desc->description);
Mike Isely1ab5e742009-03-07 00:24:24 -03001995 return -EINVAL;
Mike Iselye9c64a72009-03-06 23:42:20 -03001996 }
1997
1998 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, cd->i2c_address_list,
1999 ARRAY_SIZE(i2caddr));
2000 if (!i2ccnt && ((p = (mid < ARRAY_SIZE(module_i2c_addresses)) ?
2001 module_i2c_addresses[mid] : NULL) != NULL)) {
2002 /* Second chance: Try default i2c address list */
2003 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, p,
2004 ARRAY_SIZE(i2caddr));
2005 }
2006
2007 if (!i2ccnt) {
2008 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03002009 "Module ID %u (%s) for device %s:"
2010 " No i2c addresses",
2011 mid, fname, hdw->hdw_desc->description);
2012 return -EINVAL;
Mike Iselye9c64a72009-03-06 23:42:20 -03002013 }
2014
2015 /* Note how the 2nd and 3rd arguments are the same for both
2016 * v4l2_i2c_new_subdev() and v4l2_i2c_new_probed_subdev(). Why?
2017 * Well the 2nd argument is the module name to load, while the 3rd
2018 * argument is documented in the framework as being the "chipid" -
2019 * and every other place where I can find examples of this, the
2020 * "chipid" appears to just be the module name again. So here we
2021 * just do the same thing. */
2022 if (i2ccnt == 1) {
2023 sd = v4l2_i2c_new_subdev(&hdw->i2c_adap,
2024 fname, fname,
2025 i2caddr[0]);
2026 } else {
2027 sd = v4l2_i2c_new_probed_subdev(&hdw->i2c_adap,
2028 fname, fname,
2029 i2caddr);
2030 }
2031
Mike Isely446dfdc2009-03-06 23:58:15 -03002032 if (!sd) {
2033 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03002034 "Module ID %u (%s) for device %s failed to load",
2035 mid, fname, hdw->hdw_desc->description);
2036 return -EIO;
Mike Isely446dfdc2009-03-06 23:58:15 -03002037 }
2038
2039 /* Tag this sub-device instance with the module ID we know about.
2040 In other places we'll use that tag to determine if the instance
2041 requires special handling. */
2042 sd->grp_id = mid;
2043
Mike Iselya932f502009-03-06 23:47:10 -03002044 /* If we have both old and new i2c layers enabled, make sure that
2045 old layer isn't also tracking this module. This is a debugging
2046 aid, in normal situations there's no reason for both mechanisms
2047 to be enabled. */
2048 pvr2_i2c_untrack_subdev(hdw, sd);
Mike Isely446dfdc2009-03-06 23:58:15 -03002049 pvr2_trace(PVR2_TRACE_INIT, "Attached sub-driver %s", fname);
Mike Iselya932f502009-03-06 23:47:10 -03002050
Mike Iselye9c64a72009-03-06 23:42:20 -03002051
Mike Isely00e5f732009-03-07 00:17:11 -03002052 /* client-specific setup... */
2053 switch (mid) {
2054 case PVR2_CLIENT_ID_CX25840:
2055 hdw->decoder_client_id = mid;
2056 {
2057 /*
2058 Mike Isely <isely@pobox.com> 19-Nov-2006 - This
2059 bit of nuttiness for cx25840 causes that module
2060 to correctly set up its video scaling. This is
2061 really a problem in the cx25840 module itself,
2062 but we work around it here. The problem has not
2063 been seen in ivtv because there VBI is supported
2064 and set up. We don't do VBI here (at least not
2065 yet) and thus we never attempted to even set it
2066 up.
2067 */
2068 struct v4l2_format fmt;
2069 memset(&fmt, 0, sizeof(fmt));
2070 fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
2071 v4l2_device_call_all(&hdw->v4l2_dev, mid,
2072 video, s_fmt, &fmt);
2073 }
2074 break;
2075 case PVR2_CLIENT_ID_SAA7115:
2076 hdw->decoder_client_id = mid;
2077 break;
2078 default: break;
2079 }
Mike Isely1ab5e742009-03-07 00:24:24 -03002080
2081 return 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002082}
2083
2084
2085static void pvr2_hdw_load_modules(struct pvr2_hdw *hdw)
2086{
2087 unsigned int idx;
2088 const struct pvr2_string_table *cm;
2089 const struct pvr2_device_client_table *ct;
Mike Isely1ab5e742009-03-07 00:24:24 -03002090 int okFl = !0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002091
2092 cm = &hdw->hdw_desc->client_modules;
2093 for (idx = 0; idx < cm->cnt; idx++) {
2094 request_module(cm->lst[idx]);
2095 }
2096
2097 ct = &hdw->hdw_desc->client_table;
2098 for (idx = 0; idx < ct->cnt; idx++) {
Mike Isely1ab5e742009-03-07 00:24:24 -03002099 if (!pvr2_hdw_load_subdev(hdw, &ct->lst[idx])) okFl = 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002100 }
Mike Isely1ab5e742009-03-07 00:24:24 -03002101 if (!okFl) pvr2_hdw_render_useless(hdw);
Mike Iselye9c64a72009-03-06 23:42:20 -03002102}
2103
2104
Mike Iselyd8554972006-06-26 20:58:46 -03002105static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
2106{
2107 int ret;
2108 unsigned int idx;
2109 struct pvr2_ctrl *cptr;
2110 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03002111 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03002112 if (!reloadFl) {
2113 reloadFl =
2114 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
2115 == 0);
2116 if (reloadFl) {
2117 pvr2_trace(PVR2_TRACE_INIT,
2118 "USB endpoint config looks strange"
2119 "; possibly firmware needs to be"
2120 " loaded");
2121 }
2122 }
2123 if (!reloadFl) {
2124 reloadFl = !pvr2_hdw_check_firmware(hdw);
2125 if (reloadFl) {
2126 pvr2_trace(PVR2_TRACE_INIT,
2127 "Check for FX2 firmware failed"
2128 "; possibly firmware needs to be"
2129 " loaded");
2130 }
2131 }
Mike Iselyd8554972006-06-26 20:58:46 -03002132 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03002133 if (pvr2_upload_firmware1(hdw) != 0) {
2134 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2135 "Failure uploading firmware1");
2136 }
2137 return;
Mike Iselyd8554972006-06-26 20:58:46 -03002138 }
2139 }
Mike Iselyd8554972006-06-26 20:58:46 -03002140 hdw->fw1_state = FW1_STATE_OK;
2141
Mike Iselyd8554972006-06-26 20:58:46 -03002142 if (!pvr2_hdw_dev_ok(hdw)) return;
2143
Mike Isely989eb152007-11-26 01:53:12 -03002144 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03002145 pvr2_hdw_cmd_powerup(hdw);
2146 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002147 }
2148
Mike Isely31335b12008-07-25 19:35:31 -03002149 /* Take the IR chip out of reset, if appropriate */
2150 if (hdw->hdw_desc->ir_scheme == PVR2_IR_SCHEME_ZILOG) {
2151 pvr2_issue_simple_cmd(hdw,
2152 FX2CMD_HCW_ZILOG_RESET |
2153 (1 << 8) |
2154 ((0) << 16));
2155 }
2156
Mike Iselyd8554972006-06-26 20:58:46 -03002157 // This step MUST happen after the earlier powerup step.
Mike Isely59af3362009-03-07 03:06:09 -03002158 pvr2_i2c_track_init(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002159 pvr2_i2c_core_init(hdw);
2160 if (!pvr2_hdw_dev_ok(hdw)) return;
2161
Mike Iselye9c64a72009-03-06 23:42:20 -03002162 pvr2_hdw_load_modules(hdw);
Mike Isely1ab5e742009-03-07 00:24:24 -03002163 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselye9c64a72009-03-06 23:42:20 -03002164
Mike Iselyc05c0462006-06-25 20:04:25 -03002165 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002166 cptr = hdw->controls + idx;
2167 if (cptr->info->skip_init) continue;
2168 if (!cptr->info->set_value) continue;
2169 cptr->info->set_value(cptr,~0,cptr->info->default_value);
2170 }
2171
Mike Isely1bde0282006-12-27 23:30:13 -03002172 /* Set up special default values for the television and radio
2173 frequencies here. It's not really important what these defaults
2174 are, but I set them to something usable in the Chicago area just
2175 to make driver testing a little easier. */
2176
Michael Krufky5a4f5da62008-05-11 16:37:50 -03002177 hdw->freqValTelevision = default_tv_freq;
2178 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03002179
Mike Iselyd8554972006-06-26 20:58:46 -03002180 // Do not use pvr2_reset_ctl_endpoints() here. It is not
2181 // thread-safe against the normal pvr2_send_request() mechanism.
2182 // (We should make it thread safe).
2183
Mike Iselyaaf78842007-11-26 02:04:11 -03002184 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
2185 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002186 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03002187 if (ret < 0) {
2188 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2189 "Unable to determine location of eeprom,"
2190 " skipping");
2191 } else {
2192 hdw->eeprom_addr = ret;
2193 pvr2_eeprom_analyze(hdw);
2194 if (!pvr2_hdw_dev_ok(hdw)) return;
2195 }
2196 } else {
2197 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
2198 hdw->tuner_updated = !0;
2199 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03002200 }
2201
Mike Isely13a88792009-01-14 04:22:56 -03002202 if (hdw->serial_number) {
2203 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2204 "sn-%lu", hdw->serial_number);
2205 } else if (hdw->unit_number >= 0) {
2206 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2207 "unit-%c",
2208 hdw->unit_number + 'a');
2209 } else {
2210 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2211 "unit-??");
2212 }
2213 hdw->identifier[idx] = 0;
2214
Mike Iselyd8554972006-06-26 20:58:46 -03002215 pvr2_hdw_setup_std(hdw);
2216
2217 if (!get_default_tuner_type(hdw)) {
2218 pvr2_trace(PVR2_TRACE_INIT,
2219 "pvr2_hdw_setup: Tuner type overridden to %d",
2220 hdw->tuner_type);
2221 }
2222
Mike Iselyd8554972006-06-26 20:58:46 -03002223 pvr2_i2c_core_check_stale(hdw);
2224 hdw->tuner_updated = 0;
2225
2226 if (!pvr2_hdw_dev_ok(hdw)) return;
2227
Mike Isely1df59f02008-04-21 03:50:39 -03002228 if (hdw->hdw_desc->signal_routing_scheme ==
2229 PVR2_ROUTING_SCHEME_GOTVIEW) {
2230 /* Ensure that GPIO 11 is set to output for GOTVIEW
2231 hardware. */
2232 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
2233 }
2234
Mike Isely681c7392007-11-26 01:48:52 -03002235 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002236
2237 hdw->vid_stream = pvr2_stream_create();
2238 if (!pvr2_hdw_dev_ok(hdw)) return;
2239 pvr2_trace(PVR2_TRACE_INIT,
2240 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
2241 if (hdw->vid_stream) {
2242 idx = get_default_error_tolerance(hdw);
2243 if (idx) {
2244 pvr2_trace(PVR2_TRACE_INIT,
2245 "pvr2_hdw_setup: video stream %p"
2246 " setting tolerance %u",
2247 hdw->vid_stream,idx);
2248 }
2249 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
2250 PVR2_VID_ENDPOINT,idx);
2251 }
2252
2253 if (!pvr2_hdw_dev_ok(hdw)) return;
2254
Mike Iselyd8554972006-06-26 20:58:46 -03002255 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03002256
2257 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002258}
2259
2260
Mike Isely681c7392007-11-26 01:48:52 -03002261/* Set up the structure and attempt to put the device into a usable state.
2262 This can be a time-consuming operation, which is why it is not done
2263 internally as part of the create() step. */
2264static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002265{
2266 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002267 do {
Mike Iselyd8554972006-06-26 20:58:46 -03002268 pvr2_hdw_setup_low(hdw);
2269 pvr2_trace(PVR2_TRACE_INIT,
2270 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03002271 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03002272 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03002273 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03002274 pvr2_trace(
2275 PVR2_TRACE_INFO,
2276 "Device initialization"
2277 " completed successfully.");
2278 break;
2279 }
2280 if (hdw->fw1_state == FW1_STATE_RELOAD) {
2281 pvr2_trace(
2282 PVR2_TRACE_INFO,
2283 "Device microcontroller firmware"
2284 " (re)loaded; it should now reset"
2285 " and reconnect.");
2286 break;
2287 }
2288 pvr2_trace(
2289 PVR2_TRACE_ERROR_LEGS,
2290 "Device initialization was not successful.");
2291 if (hdw->fw1_state == FW1_STATE_MISSING) {
2292 pvr2_trace(
2293 PVR2_TRACE_ERROR_LEGS,
2294 "Giving up since device"
2295 " microcontroller firmware"
2296 " appears to be missing.");
2297 break;
2298 }
2299 }
2300 if (procreload) {
2301 pvr2_trace(
2302 PVR2_TRACE_ERROR_LEGS,
2303 "Attempting pvrusb2 recovery by reloading"
2304 " primary firmware.");
2305 pvr2_trace(
2306 PVR2_TRACE_ERROR_LEGS,
2307 "If this works, device should disconnect"
2308 " and reconnect in a sane state.");
2309 hdw->fw1_state = FW1_STATE_UNKNOWN;
2310 pvr2_upload_firmware1(hdw);
2311 } else {
2312 pvr2_trace(
2313 PVR2_TRACE_ERROR_LEGS,
2314 "***WARNING*** pvrusb2 device hardware"
2315 " appears to be jammed"
2316 " and I can't clear it.");
2317 pvr2_trace(
2318 PVR2_TRACE_ERROR_LEGS,
2319 "You might need to power cycle"
2320 " the pvrusb2 device"
2321 " in order to recover.");
2322 }
Mike Isely681c7392007-11-26 01:48:52 -03002323 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03002324 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002325}
2326
2327
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002328/* Perform second stage initialization. Set callback pointer first so that
2329 we can avoid a possible initialization race (if the kernel thread runs
2330 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03002331int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
2332 void (*callback_func)(void *),
2333 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002334{
2335 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03002336 if (hdw->flag_disconnected) {
2337 /* Handle a race here: If we're already
2338 disconnected by this point, then give up. If we
2339 get past this then we'll remain connected for
2340 the duration of initialization since the entire
2341 initialization sequence is now protected by the
2342 big_lock. */
2343 break;
2344 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002345 hdw->state_data = callback_data;
2346 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03002347 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002348 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03002349 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002350}
2351
2352
2353/* Create, set up, and return a structure for interacting with the
2354 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03002355struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2356 const struct usb_device_id *devid)
2357{
Mike Isely7fb20fa2008-04-22 14:45:37 -03002358 unsigned int idx,cnt1,cnt2,m;
Mike Iselyfe15f132008-08-30 18:11:40 -03002359 struct pvr2_hdw *hdw = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002360 int valid_std_mask;
2361 struct pvr2_ctrl *cptr;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002362 struct usb_device *usb_dev;
Mike Isely989eb152007-11-26 01:53:12 -03002363 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03002364 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03002365 struct v4l2_queryctrl qctrl;
2366 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03002367
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002368 usb_dev = interface_to_usbdev(intf);
2369
Mike Iselyd130fa82007-12-08 17:20:06 -03002370 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03002371
Mike Iselyfe15f132008-08-30 18:11:40 -03002372 if (hdw_desc == NULL) {
2373 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
2374 " No device description pointer,"
2375 " unable to continue.");
2376 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
2377 " please contact Mike Isely <isely@pobox.com>"
2378 " to get it included in the driver\n");
2379 goto fail;
2380 }
2381
Mike Iselyca545f72007-01-20 00:37:11 -03002382 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03002383 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03002384 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03002385 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03002386
2387 init_timer(&hdw->quiescent_timer);
2388 hdw->quiescent_timer.data = (unsigned long)hdw;
2389 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
2390
2391 init_timer(&hdw->encoder_wait_timer);
2392 hdw->encoder_wait_timer.data = (unsigned long)hdw;
2393 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
2394
Mike Iselyd913d632008-04-06 04:04:35 -03002395 init_timer(&hdw->encoder_run_timer);
2396 hdw->encoder_run_timer.data = (unsigned long)hdw;
2397 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
2398
Mike Isely681c7392007-11-26 01:48:52 -03002399 hdw->master_state = PVR2_STATE_DEAD;
2400
2401 init_waitqueue_head(&hdw->state_wait_data);
2402
Mike Isely18103c572007-01-20 00:09:47 -03002403 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03002404 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03002405
Mike Isely7fb20fa2008-04-22 14:45:37 -03002406 /* Calculate which inputs are OK */
2407 m = 0;
2408 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03002409 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
2410 m |= 1 << PVR2_CVAL_INPUT_DTV;
2411 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03002412 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
2413 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
2414 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
2415 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03002416 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03002417
Mike Isely62433e32008-04-22 14:45:40 -03002418 /* If not a hybrid device, pathway_state never changes. So
2419 initialize it here to what it should forever be. */
2420 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
2421 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
2422 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
2423 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
2424 }
2425
Mike Iselyc05c0462006-06-25 20:04:25 -03002426 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03002427 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03002428 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03002429 GFP_KERNEL);
2430 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03002431 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03002432 for (idx = 0; idx < hdw->control_cnt; idx++) {
2433 cptr = hdw->controls + idx;
2434 cptr->hdw = hdw;
2435 }
Mike Iselyd8554972006-06-26 20:58:46 -03002436 for (idx = 0; idx < 32; idx++) {
2437 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
2438 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002439 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002440 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03002441 cptr->info = control_defs+idx;
2442 }
Mike Iselydbc40a02008-04-22 14:45:39 -03002443
2444 /* Ensure that default input choice is a valid one. */
2445 m = hdw->input_avail_mask;
2446 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2447 if (!((1 << idx) & m)) continue;
2448 hdw->input_val = idx;
2449 break;
2450 }
2451
Mike Iselyb30d2442006-06-25 20:05:01 -03002452 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03002453 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03002454 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
2455 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03002456 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2457 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2458 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2459 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2460 ciptr->name = mpeg_ids[idx].strid;
2461 ciptr->v4l_id = mpeg_ids[idx].id;
2462 ciptr->skip_init = !0;
2463 ciptr->get_value = ctrl_cx2341x_get;
2464 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2465 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2466 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2467 qctrl.id = ciptr->v4l_id;
2468 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2469 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2470 ciptr->set_value = ctrl_cx2341x_set;
2471 }
2472 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2473 PVR2_CTLD_INFO_DESC_SIZE);
2474 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2475 ciptr->default_value = qctrl.default_value;
2476 switch (qctrl.type) {
2477 default:
2478 case V4L2_CTRL_TYPE_INTEGER:
2479 ciptr->type = pvr2_ctl_int;
2480 ciptr->def.type_int.min_value = qctrl.minimum;
2481 ciptr->def.type_int.max_value = qctrl.maximum;
2482 break;
2483 case V4L2_CTRL_TYPE_BOOLEAN:
2484 ciptr->type = pvr2_ctl_bool;
2485 break;
2486 case V4L2_CTRL_TYPE_MENU:
2487 ciptr->type = pvr2_ctl_enum;
2488 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002489 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2490 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002491 for (cnt1 = 0;
2492 ciptr->def.type_enum.value_names[cnt1] != NULL;
2493 cnt1++) { }
2494 ciptr->def.type_enum.count = cnt1;
2495 break;
2496 }
2497 cptr->info = ciptr;
2498 }
Mike Iselyd8554972006-06-26 20:58:46 -03002499
2500 // Initialize video standard enum dynamic control
2501 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2502 if (cptr) {
2503 memcpy(&hdw->std_info_enum,cptr->info,
2504 sizeof(hdw->std_info_enum));
2505 cptr->info = &hdw->std_info_enum;
2506
2507 }
2508 // Initialize control data regarding video standard masks
2509 valid_std_mask = pvr2_std_get_usable();
2510 for (idx = 0; idx < 32; idx++) {
2511 if (!(valid_std_mask & (1 << idx))) continue;
2512 cnt1 = pvr2_std_id_to_str(
2513 hdw->std_mask_names[idx],
2514 sizeof(hdw->std_mask_names[idx])-1,
2515 1 << idx);
2516 hdw->std_mask_names[idx][cnt1] = 0;
2517 }
2518 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2519 if (cptr) {
2520 memcpy(&hdw->std_info_avail,cptr->info,
2521 sizeof(hdw->std_info_avail));
2522 cptr->info = &hdw->std_info_avail;
2523 hdw->std_info_avail.def.type_bitmask.bit_names =
2524 hdw->std_mask_ptrs;
2525 hdw->std_info_avail.def.type_bitmask.valid_bits =
2526 valid_std_mask;
2527 }
2528 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2529 if (cptr) {
2530 memcpy(&hdw->std_info_cur,cptr->info,
2531 sizeof(hdw->std_info_cur));
2532 cptr->info = &hdw->std_info_cur;
2533 hdw->std_info_cur.def.type_bitmask.bit_names =
2534 hdw->std_mask_ptrs;
2535 hdw->std_info_avail.def.type_bitmask.valid_bits =
2536 valid_std_mask;
2537 }
2538
Mike Isely432907f2008-08-31 21:02:20 -03002539 hdw->cropcap_stale = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002540 hdw->eeprom_addr = -1;
2541 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002542 hdw->v4l_minor_number_video = -1;
2543 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002544 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002545 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2546 if (!hdw->ctl_write_buffer) goto fail;
2547 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2548 if (!hdw->ctl_read_buffer) goto fail;
2549 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2550 if (!hdw->ctl_write_urb) goto fail;
2551 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2552 if (!hdw->ctl_read_urb) goto fail;
2553
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002554 if (v4l2_device_register(&usb_dev->dev, &hdw->v4l2_dev) != 0) {
2555 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2556 "Error registering with v4l core, giving up");
2557 goto fail;
2558 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002559 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002560 for (idx = 0; idx < PVR_NUM; idx++) {
2561 if (unit_pointers[idx]) continue;
2562 hdw->unit_number = idx;
2563 unit_pointers[idx] = hdw;
2564 break;
2565 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002566 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002567
2568 cnt1 = 0;
2569 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2570 cnt1 += cnt2;
2571 if (hdw->unit_number >= 0) {
2572 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2573 ('a' + hdw->unit_number));
2574 cnt1 += cnt2;
2575 }
2576 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2577 hdw->name[cnt1] = 0;
2578
Mike Isely681c7392007-11-26 01:48:52 -03002579 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2580 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2581 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002582
Mike Iselyd8554972006-06-26 20:58:46 -03002583 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2584 hdw->unit_number,hdw->name);
2585
2586 hdw->tuner_type = -1;
2587 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002588
2589 hdw->usb_intf = intf;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002590 hdw->usb_dev = usb_dev;
Mike Iselyd8554972006-06-26 20:58:46 -03002591
Mike Isely87e34952009-01-23 01:20:24 -03002592 usb_make_path(hdw->usb_dev, hdw->bus_info, sizeof(hdw->bus_info));
Mike Isely31a18542007-04-08 01:11:47 -03002593
Mike Iselyd8554972006-06-26 20:58:46 -03002594 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2595 usb_set_interface(hdw->usb_dev,ifnum,0);
2596
2597 mutex_init(&hdw->ctl_lock_mutex);
2598 mutex_init(&hdw->big_lock_mutex);
2599
2600 return hdw;
2601 fail:
2602 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002603 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002604 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002605 del_timer_sync(&hdw->encoder_wait_timer);
2606 if (hdw->workqueue) {
2607 flush_workqueue(hdw->workqueue);
2608 destroy_workqueue(hdw->workqueue);
2609 hdw->workqueue = NULL;
2610 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002611 usb_free_urb(hdw->ctl_read_urb);
2612 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002613 kfree(hdw->ctl_read_buffer);
2614 kfree(hdw->ctl_write_buffer);
2615 kfree(hdw->controls);
2616 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002617 kfree(hdw->std_defs);
2618 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002619 kfree(hdw);
2620 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002621 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002622}
2623
2624
2625/* Remove _all_ associations between this driver and the underlying USB
2626 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002627static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002628{
2629 if (hdw->flag_disconnected) return;
2630 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2631 if (hdw->ctl_read_urb) {
2632 usb_kill_urb(hdw->ctl_read_urb);
2633 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002634 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002635 }
2636 if (hdw->ctl_write_urb) {
2637 usb_kill_urb(hdw->ctl_write_urb);
2638 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002639 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002640 }
2641 if (hdw->ctl_read_buffer) {
2642 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002643 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002644 }
2645 if (hdw->ctl_write_buffer) {
2646 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002647 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002648 }
Mike Iselyd8554972006-06-26 20:58:46 -03002649 hdw->flag_disconnected = !0;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002650 /* If we don't do this, then there will be a dangling struct device
2651 reference to our disappearing device persisting inside the V4L
2652 core... */
2653 if (hdw->v4l2_dev.dev) {
2654 dev_set_drvdata(hdw->v4l2_dev.dev, NULL);
2655 hdw->v4l2_dev.dev = NULL;
2656 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002657 hdw->usb_dev = NULL;
2658 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002659 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002660}
2661
2662
2663/* Destroy hardware interaction structure */
2664void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2665{
Mike Isely401c27c2007-09-08 22:11:46 -03002666 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002667 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002668 if (hdw->workqueue) {
2669 flush_workqueue(hdw->workqueue);
2670 destroy_workqueue(hdw->workqueue);
2671 hdw->workqueue = NULL;
2672 }
Mike Isely8f591002008-04-22 14:45:45 -03002673 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002674 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002675 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002676 if (hdw->fw_buffer) {
2677 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002678 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002679 }
2680 if (hdw->vid_stream) {
2681 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002682 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002683 }
Mike Iselyd8554972006-06-26 20:58:46 -03002684 if (hdw->decoder_ctrl) {
2685 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2686 }
2687 pvr2_i2c_core_done(hdw);
Mike Isely59af3362009-03-07 03:06:09 -03002688 pvr2_i2c_track_done(hdw);
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002689 v4l2_device_unregister(&hdw->v4l2_dev);
Mike Iselyd8554972006-06-26 20:58:46 -03002690 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002691 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002692 if ((hdw->unit_number >= 0) &&
2693 (hdw->unit_number < PVR_NUM) &&
2694 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002695 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002696 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002697 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002698 kfree(hdw->controls);
2699 kfree(hdw->mpeg_ctrl_info);
2700 kfree(hdw->std_defs);
2701 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002702 kfree(hdw);
2703}
2704
2705
Mike Iselyd8554972006-06-26 20:58:46 -03002706int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2707{
2708 return (hdw && hdw->flag_ok);
2709}
2710
2711
2712/* Called when hardware has been unplugged */
2713void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2714{
2715 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2716 LOCK_TAKE(hdw->big_lock);
2717 LOCK_TAKE(hdw->ctl_lock);
2718 pvr2_hdw_remove_usb_stuff(hdw);
2719 LOCK_GIVE(hdw->ctl_lock);
2720 LOCK_GIVE(hdw->big_lock);
2721}
2722
2723
2724// Attempt to autoselect an appropriate value for std_enum_cur given
2725// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002726static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002727{
2728 unsigned int idx;
2729 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2730 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2731 hdw->std_enum_cur = idx;
2732 return;
2733 }
2734 }
2735 hdw->std_enum_cur = 0;
2736}
2737
2738
2739// Calculate correct set of enumerated standards based on currently known
2740// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002741static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002742{
2743 struct v4l2_standard *newstd;
2744 unsigned int std_cnt;
2745 unsigned int idx;
2746
2747 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2748
2749 if (hdw->std_defs) {
2750 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002751 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002752 }
2753 hdw->std_enum_cnt = 0;
2754 if (hdw->std_enum_names) {
2755 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002756 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002757 }
2758
2759 if (!std_cnt) {
2760 pvr2_trace(
2761 PVR2_TRACE_ERROR_LEGS,
2762 "WARNING: Failed to identify any viable standards");
2763 }
2764 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2765 hdw->std_enum_names[0] = "none";
2766 for (idx = 0; idx < std_cnt; idx++) {
2767 hdw->std_enum_names[idx+1] =
2768 newstd[idx].name;
2769 }
2770 // Set up the dynamic control for this standard
2771 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2772 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2773 hdw->std_defs = newstd;
2774 hdw->std_enum_cnt = std_cnt+1;
2775 hdw->std_enum_cur = 0;
2776 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2777}
2778
2779
2780int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2781 struct v4l2_standard *std,
2782 unsigned int idx)
2783{
2784 int ret = -EINVAL;
2785 if (!idx) return ret;
2786 LOCK_TAKE(hdw->big_lock); do {
2787 if (idx >= hdw->std_enum_cnt) break;
2788 idx--;
2789 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2790 ret = 0;
2791 } while (0); LOCK_GIVE(hdw->big_lock);
2792 return ret;
2793}
2794
2795
2796/* Get the number of defined controls */
2797unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2798{
Mike Iselyc05c0462006-06-25 20:04:25 -03002799 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002800}
2801
2802
2803/* Retrieve a control handle given its index (0..count-1) */
2804struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2805 unsigned int idx)
2806{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002807 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002808 return hdw->controls + idx;
2809}
2810
2811
2812/* Retrieve a control handle given its index (0..count-1) */
2813struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2814 unsigned int ctl_id)
2815{
2816 struct pvr2_ctrl *cptr;
2817 unsigned int idx;
2818 int i;
2819
2820 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002821 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002822 cptr = hdw->controls + idx;
2823 i = cptr->info->internal_id;
2824 if (i && (i == ctl_id)) return cptr;
2825 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002826 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002827}
2828
2829
Mike Iselya761f432006-06-25 20:04:44 -03002830/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002831struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2832{
2833 struct pvr2_ctrl *cptr;
2834 unsigned int idx;
2835 int i;
2836
2837 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002838 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002839 cptr = hdw->controls + idx;
2840 i = cptr->info->v4l_id;
2841 if (i && (i == ctl_id)) return cptr;
2842 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002843 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002844}
2845
2846
Mike Iselya761f432006-06-25 20:04:44 -03002847/* Given a V4L ID for its immediate predecessor, retrieve the control
2848 structure associated with it. */
2849struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2850 unsigned int ctl_id)
2851{
2852 struct pvr2_ctrl *cptr,*cp2;
2853 unsigned int idx;
2854 int i;
2855
2856 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002857 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002858 for (idx = 0; idx < hdw->control_cnt; idx++) {
2859 cptr = hdw->controls + idx;
2860 i = cptr->info->v4l_id;
2861 if (!i) continue;
2862 if (i <= ctl_id) continue;
2863 if (cp2 && (cp2->info->v4l_id < i)) continue;
2864 cp2 = cptr;
2865 }
2866 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002867 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002868}
2869
2870
Mike Iselyd8554972006-06-26 20:58:46 -03002871static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2872{
2873 switch (tp) {
2874 case pvr2_ctl_int: return "integer";
2875 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002876 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002877 case pvr2_ctl_bitmask: return "bitmask";
2878 }
2879 return "";
2880}
2881
2882
Mike Isely2641df32009-03-07 00:13:25 -03002883static void pvr2_subdev_set_control(struct pvr2_hdw *hdw, int id,
2884 const char *name, int val)
2885{
2886 struct v4l2_control ctrl;
2887 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 %s=%d", name, val);
2888 memset(&ctrl, 0, sizeof(ctrl));
2889 ctrl.id = id;
2890 ctrl.value = val;
2891 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, s_ctrl, &ctrl);
2892}
2893
2894#define PVR2_SUBDEV_SET_CONTROL(hdw, id, lab) \
2895 if ((hdw)->lab##_dirty) { \
2896 pvr2_subdev_set_control(hdw, id, #lab, (hdw)->lab##_val); \
2897 }
2898
Mike Isely5ceaad12009-03-07 00:01:20 -03002899/* Execute whatever commands are required to update the state of all the
Mike Isely2641df32009-03-07 00:13:25 -03002900 sub-devices so that they match our current control values. */
Mike Isely5ceaad12009-03-07 00:01:20 -03002901static void pvr2_subdev_update(struct pvr2_hdw *hdw)
2902{
Mike Isely2641df32009-03-07 00:13:25 -03002903 if (hdw->input_dirty || hdw->std_dirty) {
2904 pvr2_trace(PVR2_TRACE_CHIPS,"subdev v4l2 set_standard");
2905 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2906 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2907 tuner, s_radio);
2908 } else {
2909 v4l2_std_id vs;
2910 vs = hdw->std_mask_cur;
2911 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2912 tuner, s_std, vs);
2913 }
2914 hdw->tuner_signal_stale = !0;
2915 hdw->cropcap_stale = !0;
2916 }
2917
2918 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_BRIGHTNESS, brightness);
2919 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_CONTRAST, contrast);
2920 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_SATURATION, saturation);
2921 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_HUE, hue);
2922 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_MUTE, mute);
2923 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_VOLUME, volume);
2924 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BALANCE, balance);
2925 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BASS, bass);
2926 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_TREBLE, treble);
2927
2928 if (hdw->input_dirty || hdw->audiomode_dirty) {
2929 struct v4l2_tuner vt;
2930 memset(&vt, 0, sizeof(vt));
2931 vt.audmode = hdw->audiomode_val;
2932 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, s_tuner, &vt);
2933 }
2934
2935 if (hdw->freqDirty) {
2936 unsigned long fv;
2937 struct v4l2_frequency freq;
2938 fv = pvr2_hdw_get_cur_freq(hdw);
2939 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_freq(%lu)", fv);
2940 if (hdw->tuner_signal_stale) pvr2_hdw_status_poll(hdw);
2941 memset(&freq, 0, sizeof(freq));
2942 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
2943 /* ((fv * 1000) / 62500) */
2944 freq.frequency = (fv * 2) / 125;
2945 } else {
2946 freq.frequency = fv / 62500;
2947 }
2948 /* tuner-core currently doesn't seem to care about this, but
2949 let's set it anyway for completeness. */
2950 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2951 freq.type = V4L2_TUNER_RADIO;
2952 } else {
2953 freq.type = V4L2_TUNER_ANALOG_TV;
2954 }
2955 freq.tuner = 0;
2956 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner,
2957 s_frequency, &freq);
2958 }
2959
2960 if (hdw->res_hor_dirty || hdw->res_ver_dirty) {
2961 struct v4l2_format fmt;
2962 memset(&fmt, 0, sizeof(fmt));
2963 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2964 fmt.fmt.pix.width = hdw->res_hor_val;
2965 fmt.fmt.pix.height = hdw->res_ver_val;
2966 pvr2_trace(PVR2_TRACE_CHIPS,"subdev v4l2 set_size(%dx%d)",
2967 fmt.fmt.pix.width, fmt.fmt.pix.height);
2968 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_fmt, &fmt);
2969 }
2970
2971 /* Unable to set crop parameters; there is apparently no equivalent
2972 for VIDIOC_S_CROP */
2973
2974 /* ????? Cover special cases for specific sub-devices. */
2975
2976 if (hdw->tuner_signal_stale && hdw->cropcap_stale) {
2977 pvr2_hdw_status_poll(hdw);
2978 }
Mike Isely5ceaad12009-03-07 00:01:20 -03002979}
2980
2981
Mike Isely681c7392007-11-26 01:48:52 -03002982/* Figure out if we need to commit control changes. If so, mark internal
2983 state flags to indicate this fact and return true. Otherwise do nothing
2984 else and return false. */
2985static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002986{
Mike Iselyd8554972006-06-26 20:58:46 -03002987 unsigned int idx;
2988 struct pvr2_ctrl *cptr;
2989 int value;
2990 int commit_flag = 0;
2991 char buf[100];
2992 unsigned int bcnt,ccnt;
2993
Mike Iselyc05c0462006-06-25 20:04:25 -03002994 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002995 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00002996 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002997 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03002998 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002999
Mike Iselyfe23a282007-01-20 00:10:55 -03003000 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003001 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
3002 cptr->info->name);
3003 value = 0;
3004 cptr->info->get_value(cptr,&value);
3005 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
3006 buf+bcnt,
3007 sizeof(buf)-bcnt,&ccnt);
3008 bcnt += ccnt;
3009 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
3010 get_ctrl_typename(cptr->info->type));
3011 pvr2_trace(PVR2_TRACE_CTL,
3012 "/*--TRACE_COMMIT--*/ %.*s",
3013 bcnt,buf);
3014 }
3015
3016 if (!commit_flag) {
3017 /* Nothing has changed */
3018 return 0;
3019 }
3020
Mike Isely681c7392007-11-26 01:48:52 -03003021 hdw->state_pipeline_config = 0;
3022 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3023 pvr2_hdw_state_sched(hdw);
3024
3025 return !0;
3026}
3027
3028
3029/* Perform all operations needed to commit all control changes. This must
3030 be performed in synchronization with the pipeline state and is thus
3031 expected to be called as part of the driver's worker thread. Return
3032 true if commit successful, otherwise return false to indicate that
3033 commit isn't possible at this time. */
3034static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
3035{
3036 unsigned int idx;
3037 struct pvr2_ctrl *cptr;
3038 int disruptive_change;
3039
Mike Iselyab062fe2008-06-30 03:32:35 -03003040 /* Handle some required side effects when the video standard is
3041 changed.... */
Mike Iselyd8554972006-06-26 20:58:46 -03003042 if (hdw->std_dirty) {
Mike Iselyd8554972006-06-26 20:58:46 -03003043 int nvres;
Mike Isely00528d92008-06-30 03:35:52 -03003044 int gop_size;
Mike Iselyd8554972006-06-26 20:58:46 -03003045 if (hdw->std_mask_cur & V4L2_STD_525_60) {
3046 nvres = 480;
Mike Isely00528d92008-06-30 03:35:52 -03003047 gop_size = 15;
Mike Iselyd8554972006-06-26 20:58:46 -03003048 } else {
3049 nvres = 576;
Mike Isely00528d92008-06-30 03:35:52 -03003050 gop_size = 12;
Mike Iselyd8554972006-06-26 20:58:46 -03003051 }
Mike Isely00528d92008-06-30 03:35:52 -03003052 /* Rewrite the vertical resolution to be appropriate to the
3053 video standard that has been selected. */
Mike Iselyd8554972006-06-26 20:58:46 -03003054 if (nvres != hdw->res_ver_val) {
3055 hdw->res_ver_val = nvres;
3056 hdw->res_ver_dirty = !0;
3057 }
Mike Isely00528d92008-06-30 03:35:52 -03003058 /* Rewrite the GOP size to be appropriate to the video
3059 standard that has been selected. */
3060 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
3061 struct v4l2_ext_controls cs;
3062 struct v4l2_ext_control c1;
3063 memset(&cs, 0, sizeof(cs));
3064 memset(&c1, 0, sizeof(c1));
3065 cs.controls = &c1;
3066 cs.count = 1;
3067 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
3068 c1.value = gop_size;
3069 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
3070 VIDIOC_S_EXT_CTRLS);
3071 }
Mike Iselyd8554972006-06-26 20:58:46 -03003072 }
3073
Mike Isely38d9a2c2008-03-28 05:30:48 -03003074 if (hdw->input_dirty && hdw->state_pathway_ok &&
Mike Isely62433e32008-04-22 14:45:40 -03003075 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
3076 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
3077 hdw->pathway_state)) {
3078 /* Change of mode being asked for... */
3079 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003080 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003081 }
3082 if (!hdw->state_pathway_ok) {
3083 /* Can't commit anything until pathway is ok. */
3084 return 0;
3085 }
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03003086 /* The broadcast decoder can only scale down, so if
3087 * res_*_dirty && crop window < output format ==> enlarge crop.
3088 *
3089 * The mpeg encoder receives fields of res_hor_val dots and
3090 * res_ver_val halflines. Limits: hor<=720, ver<=576.
3091 */
3092 if (hdw->res_hor_dirty && hdw->cropw_val < hdw->res_hor_val) {
3093 hdw->cropw_val = hdw->res_hor_val;
3094 hdw->cropw_dirty = !0;
3095 } else if (hdw->cropw_dirty) {
3096 hdw->res_hor_dirty = !0; /* must rescale */
3097 hdw->res_hor_val = min(720, hdw->cropw_val);
3098 }
3099 if (hdw->res_ver_dirty && hdw->croph_val < hdw->res_ver_val) {
3100 hdw->croph_val = hdw->res_ver_val;
3101 hdw->croph_dirty = !0;
3102 } else if (hdw->croph_dirty) {
3103 int nvres = hdw->std_mask_cur & V4L2_STD_525_60 ? 480 : 576;
3104 hdw->res_ver_dirty = !0;
3105 hdw->res_ver_val = min(nvres, hdw->croph_val);
3106 }
3107
Mike Isely681c7392007-11-26 01:48:52 -03003108 /* If any of the below has changed, then we can't do the update
3109 while the pipeline is running. Pipeline must be paused first
3110 and decoder -> encoder connection be made quiescent before we
3111 can proceed. */
3112 disruptive_change =
3113 (hdw->std_dirty ||
3114 hdw->enc_unsafe_stale ||
3115 hdw->srate_dirty ||
3116 hdw->res_ver_dirty ||
3117 hdw->res_hor_dirty ||
Mike Isely755879c2008-08-31 20:50:59 -03003118 hdw->cropw_dirty ||
3119 hdw->croph_dirty ||
Mike Isely681c7392007-11-26 01:48:52 -03003120 hdw->input_dirty ||
3121 (hdw->active_stream_type != hdw->desired_stream_type));
3122 if (disruptive_change && !hdw->state_pipeline_idle) {
3123 /* Pipeline is not idle; we can't proceed. Arrange to
3124 cause pipeline to stop so that we can try this again
3125 later.... */
3126 hdw->state_pipeline_pause = !0;
3127 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003128 }
3129
Mike Iselyb30d2442006-06-25 20:05:01 -03003130 if (hdw->srate_dirty) {
3131 /* Write new sample rate into control structure since
3132 * the master copy is stale. We must track srate
3133 * separate from the mpeg control structure because
3134 * other logic also uses this value. */
3135 struct v4l2_ext_controls cs;
3136 struct v4l2_ext_control c1;
3137 memset(&cs,0,sizeof(cs));
3138 memset(&c1,0,sizeof(c1));
3139 cs.controls = &c1;
3140 cs.count = 1;
3141 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
3142 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03003143 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03003144 }
Mike Iselyc05c0462006-06-25 20:04:25 -03003145
Mike Iselyd8554972006-06-26 20:58:46 -03003146 /* Scan i2c core at this point - before we clear all the dirty
3147 bits. Various parts of the i2c core will notice dirty bits as
3148 appropriate and arrange to broadcast or directly send updates to
3149 the client drivers in order to keep everything in sync */
3150 pvr2_i2c_core_check_stale(hdw);
3151
Mike Isely681c7392007-11-26 01:48:52 -03003152 if (hdw->active_stream_type != hdw->desired_stream_type) {
3153 /* Handle any side effects of stream config here */
3154 hdw->active_stream_type = hdw->desired_stream_type;
3155 }
3156
Mike Isely1df59f02008-04-21 03:50:39 -03003157 if (hdw->hdw_desc->signal_routing_scheme ==
3158 PVR2_ROUTING_SCHEME_GOTVIEW) {
3159 u32 b;
3160 /* Handle GOTVIEW audio switching */
3161 pvr2_hdw_gpio_get_out(hdw,&b);
3162 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3163 /* Set GPIO 11 */
3164 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
3165 } else {
3166 /* Clear GPIO 11 */
3167 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
3168 }
3169 }
3170
Mike Isely5ceaad12009-03-07 00:01:20 -03003171 for (idx = 0; idx < hdw->control_cnt; idx++) {
3172 cptr = hdw->controls + idx;
3173 if (!cptr->info->clear_dirty) continue;
3174 cptr->info->clear_dirty(cptr);
3175 }
3176
3177 /* Check and update state for all sub-devices. */
3178 pvr2_subdev_update(hdw);
3179
Mike Iselyd8554972006-06-26 20:58:46 -03003180 /* Now execute i2c core update */
3181 pvr2_i2c_core_sync(hdw);
3182
Mike Isely62433e32008-04-22 14:45:40 -03003183 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
3184 hdw->state_encoder_run) {
3185 /* If encoder isn't running or it can't be touched, then
3186 this will get worked out later when we start the
3187 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03003188 if (pvr2_encoder_adjust(hdw) < 0) return !0;
3189 }
Mike Iselyd8554972006-06-26 20:58:46 -03003190
Mike Isely681c7392007-11-26 01:48:52 -03003191 hdw->state_pipeline_config = !0;
Mike Isely432907f2008-08-31 21:02:20 -03003192 /* Hardware state may have changed in a way to cause the cropping
3193 capabilities to have changed. So mark it stale, which will
3194 cause a later re-fetch. */
Mike Isely681c7392007-11-26 01:48:52 -03003195 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3196 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003197}
3198
3199
3200int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
3201{
Mike Isely681c7392007-11-26 01:48:52 -03003202 int fl;
3203 LOCK_TAKE(hdw->big_lock);
3204 fl = pvr2_hdw_commit_setup(hdw);
3205 LOCK_GIVE(hdw->big_lock);
3206 if (!fl) return 0;
3207 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003208}
3209
3210
Mike Isely681c7392007-11-26 01:48:52 -03003211static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003212{
Mike Isely681c7392007-11-26 01:48:52 -03003213 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03003214 LOCK_TAKE(hdw->big_lock); do {
3215 pvr2_i2c_core_sync(hdw);
3216 } while (0); LOCK_GIVE(hdw->big_lock);
3217}
3218
3219
Mike Isely681c7392007-11-26 01:48:52 -03003220static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003221{
Mike Isely681c7392007-11-26 01:48:52 -03003222 int fl = 0;
3223 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03003224 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03003225 fl = pvr2_hdw_state_eval(hdw);
3226 } while (0); LOCK_GIVE(hdw->big_lock);
3227 if (fl && hdw->state_func) {
3228 hdw->state_func(hdw->state_data);
3229 }
3230}
3231
3232
Mike Isely681c7392007-11-26 01:48:52 -03003233static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03003234{
Mike Isely681c7392007-11-26 01:48:52 -03003235 return wait_event_interruptible(
3236 hdw->state_wait_data,
3237 (hdw->state_stale == 0) &&
3238 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03003239}
3240
Mike Isely681c7392007-11-26 01:48:52 -03003241
Mike Iselyd8554972006-06-26 20:58:46 -03003242/* Return name for this driver instance */
3243const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
3244{
3245 return hdw->name;
3246}
3247
3248
Mike Isely78a47102007-11-26 01:58:20 -03003249const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
3250{
3251 return hdw->hdw_desc->description;
3252}
3253
3254
3255const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
3256{
3257 return hdw->hdw_desc->shortname;
3258}
3259
3260
Mike Iselyd8554972006-06-26 20:58:46 -03003261int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
3262{
3263 int result;
3264 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03003265 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03003266 result = pvr2_send_request(hdw,
3267 hdw->cmd_buffer,1,
3268 hdw->cmd_buffer,1);
3269 if (result < 0) break;
3270 result = (hdw->cmd_buffer[0] != 0);
3271 } while(0); LOCK_GIVE(hdw->ctl_lock);
3272 return result;
3273}
3274
3275
Mike Isely18103c572007-01-20 00:09:47 -03003276/* Execute poll of tuner status */
3277void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003278{
Mike Iselyd8554972006-06-26 20:58:46 -03003279 LOCK_TAKE(hdw->big_lock); do {
Mike Iselya51f5002009-03-06 23:30:37 -03003280 pvr2_hdw_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003281 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03003282}
3283
3284
Mike Isely432907f2008-08-31 21:02:20 -03003285static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw)
3286{
3287 if (!hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003288 return 0;
3289 }
Mike Iselya51f5002009-03-06 23:30:37 -03003290 pvr2_hdw_status_poll(hdw);
Mike Isely432907f2008-08-31 21:02:20 -03003291 if (hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003292 return -EIO;
3293 }
3294 return 0;
3295}
3296
3297
3298/* Return information about cropping capabilities */
3299int pvr2_hdw_get_cropcap(struct pvr2_hdw *hdw, struct v4l2_cropcap *pp)
3300{
3301 int stat = 0;
3302 LOCK_TAKE(hdw->big_lock);
3303 stat = pvr2_hdw_check_cropcap(hdw);
3304 if (!stat) {
Mike Isely432907f2008-08-31 21:02:20 -03003305 memcpy(pp, &hdw->cropcap_info, sizeof(hdw->cropcap_info));
3306 }
3307 LOCK_GIVE(hdw->big_lock);
3308 return stat;
3309}
3310
3311
Mike Isely18103c572007-01-20 00:09:47 -03003312/* Return information about the tuner */
3313int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
3314{
3315 LOCK_TAKE(hdw->big_lock); do {
3316 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -03003317 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -03003318 }
3319 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
3320 } while (0); LOCK_GIVE(hdw->big_lock);
3321 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003322}
3323
3324
3325/* Get handle to video output stream */
3326struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
3327{
3328 return hp->vid_stream;
3329}
3330
3331
3332void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
3333{
Mike Isely4f1a3e52006-06-25 20:04:31 -03003334 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003335 LOCK_TAKE(hdw->big_lock); do {
Mike Isely4f1a3e52006-06-25 20:04:31 -03003336 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Isely5ceaad12009-03-07 00:01:20 -03003337 hdw->log_requested = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003338 pvr2_i2c_core_check_stale(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003339 pvr2_i2c_core_sync(hdw);
Mike Iselya51f5002009-03-06 23:30:37 -03003340 hdw->log_requested = 0;
Mike Iselyed3261a2009-03-07 00:02:33 -03003341 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, log_status);
Mike Iselyb30d2442006-06-25 20:05:01 -03003342 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03003343 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03003344 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03003345 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003346 } while (0); LOCK_GIVE(hdw->big_lock);
3347}
3348
Mike Isely4db666c2007-09-08 22:16:27 -03003349
3350/* Grab EEPROM contents, needed for direct method. */
3351#define EEPROM_SIZE 8192
3352#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
3353static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
3354{
3355 struct i2c_msg msg[2];
3356 u8 *eeprom;
3357 u8 iadd[2];
3358 u8 addr;
3359 u16 eepromSize;
3360 unsigned int offs;
3361 int ret;
3362 int mode16 = 0;
3363 unsigned pcnt,tcnt;
3364 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
3365 if (!eeprom) {
3366 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3367 "Failed to allocate memory"
3368 " required to read eeprom");
3369 return NULL;
3370 }
3371
3372 trace_eeprom("Value for eeprom addr from controller was 0x%x",
3373 hdw->eeprom_addr);
3374 addr = hdw->eeprom_addr;
3375 /* Seems that if the high bit is set, then the *real* eeprom
3376 address is shifted right now bit position (noticed this in
3377 newer PVR USB2 hardware) */
3378 if (addr & 0x80) addr >>= 1;
3379
3380 /* FX2 documentation states that a 16bit-addressed eeprom is
3381 expected if the I2C address is an odd number (yeah, this is
3382 strange but it's what they do) */
3383 mode16 = (addr & 1);
3384 eepromSize = (mode16 ? EEPROM_SIZE : 256);
3385 trace_eeprom("Examining %d byte eeprom at location 0x%x"
3386 " using %d bit addressing",eepromSize,addr,
3387 mode16 ? 16 : 8);
3388
3389 msg[0].addr = addr;
3390 msg[0].flags = 0;
3391 msg[0].len = mode16 ? 2 : 1;
3392 msg[0].buf = iadd;
3393 msg[1].addr = addr;
3394 msg[1].flags = I2C_M_RD;
3395
3396 /* We have to do the actual eeprom data fetch ourselves, because
3397 (1) we're only fetching part of the eeprom, and (2) if we were
3398 getting the whole thing our I2C driver can't grab it in one
3399 pass - which is what tveeprom is otherwise going to attempt */
3400 memset(eeprom,0,EEPROM_SIZE);
3401 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
3402 pcnt = 16;
3403 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
3404 offs = tcnt + (eepromSize - EEPROM_SIZE);
3405 if (mode16) {
3406 iadd[0] = offs >> 8;
3407 iadd[1] = offs;
3408 } else {
3409 iadd[0] = offs;
3410 }
3411 msg[1].len = pcnt;
3412 msg[1].buf = eeprom+tcnt;
3413 if ((ret = i2c_transfer(&hdw->i2c_adap,
3414 msg,ARRAY_SIZE(msg))) != 2) {
3415 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3416 "eeprom fetch set offs err=%d",ret);
3417 kfree(eeprom);
3418 return NULL;
3419 }
3420 }
3421 return eeprom;
3422}
3423
3424
3425void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
3426 int prom_flag,
3427 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003428{
3429 int ret;
3430 u16 address;
3431 unsigned int pipe;
3432 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00003433 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03003434
3435 if (!enable_flag) {
3436 pvr2_trace(PVR2_TRACE_FIRMWARE,
3437 "Cleaning up after CPU firmware fetch");
3438 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003439 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003440 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03003441 if (hdw->fw_cpu_flag) {
3442 /* Now release the CPU. It will disconnect
3443 and reconnect later. */
3444 pvr2_hdw_cpureset_assert(hdw,0);
3445 }
Mike Iselyd8554972006-06-26 20:58:46 -03003446 break;
3447 }
3448
Mike Isely4db666c2007-09-08 22:16:27 -03003449 hdw->fw_cpu_flag = (prom_flag == 0);
3450 if (hdw->fw_cpu_flag) {
3451 pvr2_trace(PVR2_TRACE_FIRMWARE,
3452 "Preparing to suck out CPU firmware");
3453 hdw->fw_size = 0x2000;
3454 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
3455 if (!hdw->fw_buffer) {
3456 hdw->fw_size = 0;
3457 break;
3458 }
3459
3460 /* We have to hold the CPU during firmware upload. */
3461 pvr2_hdw_cpureset_assert(hdw,1);
3462
3463 /* download the firmware from address 0000-1fff in 2048
3464 (=0x800) bytes chunk. */
3465
3466 pvr2_trace(PVR2_TRACE_FIRMWARE,
3467 "Grabbing CPU firmware");
3468 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
3469 for(address = 0; address < hdw->fw_size;
3470 address += 0x800) {
3471 ret = usb_control_msg(hdw->usb_dev,pipe,
3472 0xa0,0xc0,
3473 address,0,
3474 hdw->fw_buffer+address,
3475 0x800,HZ);
3476 if (ret < 0) break;
3477 }
3478
3479 pvr2_trace(PVR2_TRACE_FIRMWARE,
3480 "Done grabbing CPU firmware");
3481 } else {
3482 pvr2_trace(PVR2_TRACE_FIRMWARE,
3483 "Sucking down EEPROM contents");
3484 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
3485 if (!hdw->fw_buffer) {
3486 pvr2_trace(PVR2_TRACE_FIRMWARE,
3487 "EEPROM content suck failed.");
3488 break;
3489 }
3490 hdw->fw_size = EEPROM_SIZE;
3491 pvr2_trace(PVR2_TRACE_FIRMWARE,
3492 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03003493 }
3494
Mike Iselyd8554972006-06-26 20:58:46 -03003495 } while (0); LOCK_GIVE(hdw->big_lock);
3496}
3497
3498
3499/* Return true if we're in a mode for retrieval CPU firmware */
3500int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
3501{
Al Viro5fa12472008-03-29 03:07:38 +00003502 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003503}
3504
3505
3506int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
3507 char *buf,unsigned int cnt)
3508{
3509 int ret = -EINVAL;
3510 LOCK_TAKE(hdw->big_lock); do {
3511 if (!buf) break;
3512 if (!cnt) break;
3513
3514 if (!hdw->fw_buffer) {
3515 ret = -EIO;
3516 break;
3517 }
3518
3519 if (offs >= hdw->fw_size) {
3520 pvr2_trace(PVR2_TRACE_FIRMWARE,
3521 "Read firmware data offs=%d EOF",
3522 offs);
3523 ret = 0;
3524 break;
3525 }
3526
3527 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
3528
3529 memcpy(buf,hdw->fw_buffer+offs,cnt);
3530
3531 pvr2_trace(PVR2_TRACE_FIRMWARE,
3532 "Read firmware data offs=%d cnt=%d",
3533 offs,cnt);
3534 ret = cnt;
3535 } while (0); LOCK_GIVE(hdw->big_lock);
3536
3537 return ret;
3538}
3539
3540
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003541int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003542 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03003543{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003544 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003545 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
3546 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
3547 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003548 default: return -1;
3549 }
Mike Iselyd8554972006-06-26 20:58:46 -03003550}
3551
3552
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03003553/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003554void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003555 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03003556{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003557 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003558 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
3559 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
3560 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003561 default: break;
3562 }
Mike Iselyd8554972006-06-26 20:58:46 -03003563}
3564
3565
David Howells7d12e782006-10-05 14:55:46 +01003566static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003567{
3568 struct pvr2_hdw *hdw = urb->context;
3569 hdw->ctl_write_pend_flag = 0;
3570 if (hdw->ctl_read_pend_flag) return;
3571 complete(&hdw->ctl_done);
3572}
3573
3574
David Howells7d12e782006-10-05 14:55:46 +01003575static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003576{
3577 struct pvr2_hdw *hdw = urb->context;
3578 hdw->ctl_read_pend_flag = 0;
3579 if (hdw->ctl_write_pend_flag) return;
3580 complete(&hdw->ctl_done);
3581}
3582
3583
3584static void pvr2_ctl_timeout(unsigned long data)
3585{
3586 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3587 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3588 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003589 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003590 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003591 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003592 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03003593 }
3594}
3595
3596
Mike Iselye61b6fc2006-07-18 22:42:18 -03003597/* Issue a command and get a response from the device. This extended
3598 version includes a probe flag (which if set means that device errors
3599 should not be logged or treated as fatal) and a timeout in jiffies.
3600 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003601static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3602 unsigned int timeout,int probe_fl,
3603 void *write_data,unsigned int write_len,
3604 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03003605{
3606 unsigned int idx;
3607 int status = 0;
3608 struct timer_list timer;
3609 if (!hdw->ctl_lock_held) {
3610 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3611 "Attempted to execute control transfer"
3612 " without lock!!");
3613 return -EDEADLK;
3614 }
Mike Isely681c7392007-11-26 01:48:52 -03003615 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03003616 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3617 "Attempted to execute control transfer"
3618 " when device not ok");
3619 return -EIO;
3620 }
3621 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3622 if (!probe_fl) {
3623 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3624 "Attempted to execute control transfer"
3625 " when USB is disconnected");
3626 }
3627 return -ENOTTY;
3628 }
3629
3630 /* Ensure that we have sane parameters */
3631 if (!write_data) write_len = 0;
3632 if (!read_data) read_len = 0;
3633 if (write_len > PVR2_CTL_BUFFSIZE) {
3634 pvr2_trace(
3635 PVR2_TRACE_ERROR_LEGS,
3636 "Attempted to execute %d byte"
3637 " control-write transfer (limit=%d)",
3638 write_len,PVR2_CTL_BUFFSIZE);
3639 return -EINVAL;
3640 }
3641 if (read_len > PVR2_CTL_BUFFSIZE) {
3642 pvr2_trace(
3643 PVR2_TRACE_ERROR_LEGS,
3644 "Attempted to execute %d byte"
3645 " control-read transfer (limit=%d)",
3646 write_len,PVR2_CTL_BUFFSIZE);
3647 return -EINVAL;
3648 }
3649 if ((!write_len) && (!read_len)) {
3650 pvr2_trace(
3651 PVR2_TRACE_ERROR_LEGS,
3652 "Attempted to execute null control transfer?");
3653 return -EINVAL;
3654 }
3655
3656
3657 hdw->cmd_debug_state = 1;
3658 if (write_len) {
3659 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3660 } else {
3661 hdw->cmd_debug_code = 0;
3662 }
3663 hdw->cmd_debug_write_len = write_len;
3664 hdw->cmd_debug_read_len = read_len;
3665
3666 /* Initialize common stuff */
3667 init_completion(&hdw->ctl_done);
3668 hdw->ctl_timeout_flag = 0;
3669 hdw->ctl_write_pend_flag = 0;
3670 hdw->ctl_read_pend_flag = 0;
3671 init_timer(&timer);
3672 timer.expires = jiffies + timeout;
3673 timer.data = (unsigned long)hdw;
3674 timer.function = pvr2_ctl_timeout;
3675
3676 if (write_len) {
3677 hdw->cmd_debug_state = 2;
3678 /* Transfer write data to internal buffer */
3679 for (idx = 0; idx < write_len; idx++) {
3680 hdw->ctl_write_buffer[idx] =
3681 ((unsigned char *)write_data)[idx];
3682 }
3683 /* Initiate a write request */
3684 usb_fill_bulk_urb(hdw->ctl_write_urb,
3685 hdw->usb_dev,
3686 usb_sndbulkpipe(hdw->usb_dev,
3687 PVR2_CTL_WRITE_ENDPOINT),
3688 hdw->ctl_write_buffer,
3689 write_len,
3690 pvr2_ctl_write_complete,
3691 hdw);
3692 hdw->ctl_write_urb->actual_length = 0;
3693 hdw->ctl_write_pend_flag = !0;
3694 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3695 if (status < 0) {
3696 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3697 "Failed to submit write-control"
3698 " URB status=%d",status);
3699 hdw->ctl_write_pend_flag = 0;
3700 goto done;
3701 }
3702 }
3703
3704 if (read_len) {
3705 hdw->cmd_debug_state = 3;
3706 memset(hdw->ctl_read_buffer,0x43,read_len);
3707 /* Initiate a read request */
3708 usb_fill_bulk_urb(hdw->ctl_read_urb,
3709 hdw->usb_dev,
3710 usb_rcvbulkpipe(hdw->usb_dev,
3711 PVR2_CTL_READ_ENDPOINT),
3712 hdw->ctl_read_buffer,
3713 read_len,
3714 pvr2_ctl_read_complete,
3715 hdw);
3716 hdw->ctl_read_urb->actual_length = 0;
3717 hdw->ctl_read_pend_flag = !0;
3718 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3719 if (status < 0) {
3720 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3721 "Failed to submit read-control"
3722 " URB status=%d",status);
3723 hdw->ctl_read_pend_flag = 0;
3724 goto done;
3725 }
3726 }
3727
3728 /* Start timer */
3729 add_timer(&timer);
3730
3731 /* Now wait for all I/O to complete */
3732 hdw->cmd_debug_state = 4;
3733 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3734 wait_for_completion(&hdw->ctl_done);
3735 }
3736 hdw->cmd_debug_state = 5;
3737
3738 /* Stop timer */
3739 del_timer_sync(&timer);
3740
3741 hdw->cmd_debug_state = 6;
3742 status = 0;
3743
3744 if (hdw->ctl_timeout_flag) {
3745 status = -ETIMEDOUT;
3746 if (!probe_fl) {
3747 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3748 "Timed out control-write");
3749 }
3750 goto done;
3751 }
3752
3753 if (write_len) {
3754 /* Validate results of write request */
3755 if ((hdw->ctl_write_urb->status != 0) &&
3756 (hdw->ctl_write_urb->status != -ENOENT) &&
3757 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3758 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3759 /* USB subsystem is reporting some kind of failure
3760 on the write */
3761 status = hdw->ctl_write_urb->status;
3762 if (!probe_fl) {
3763 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3764 "control-write URB failure,"
3765 " status=%d",
3766 status);
3767 }
3768 goto done;
3769 }
3770 if (hdw->ctl_write_urb->actual_length < write_len) {
3771 /* Failed to write enough data */
3772 status = -EIO;
3773 if (!probe_fl) {
3774 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3775 "control-write URB short,"
3776 " expected=%d got=%d",
3777 write_len,
3778 hdw->ctl_write_urb->actual_length);
3779 }
3780 goto done;
3781 }
3782 }
3783 if (read_len) {
3784 /* Validate results of read request */
3785 if ((hdw->ctl_read_urb->status != 0) &&
3786 (hdw->ctl_read_urb->status != -ENOENT) &&
3787 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3788 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3789 /* USB subsystem is reporting some kind of failure
3790 on the read */
3791 status = hdw->ctl_read_urb->status;
3792 if (!probe_fl) {
3793 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3794 "control-read URB failure,"
3795 " status=%d",
3796 status);
3797 }
3798 goto done;
3799 }
3800 if (hdw->ctl_read_urb->actual_length < read_len) {
3801 /* Failed to read enough data */
3802 status = -EIO;
3803 if (!probe_fl) {
3804 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3805 "control-read URB short,"
3806 " expected=%d got=%d",
3807 read_len,
3808 hdw->ctl_read_urb->actual_length);
3809 }
3810 goto done;
3811 }
3812 /* Transfer retrieved data out from internal buffer */
3813 for (idx = 0; idx < read_len; idx++) {
3814 ((unsigned char *)read_data)[idx] =
3815 hdw->ctl_read_buffer[idx];
3816 }
3817 }
3818
3819 done:
3820
3821 hdw->cmd_debug_state = 0;
3822 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003823 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003824 }
3825 return status;
3826}
3827
3828
3829int pvr2_send_request(struct pvr2_hdw *hdw,
3830 void *write_data,unsigned int write_len,
3831 void *read_data,unsigned int read_len)
3832{
3833 return pvr2_send_request_ex(hdw,HZ*4,0,
3834 write_data,write_len,
3835 read_data,read_len);
3836}
3837
Mike Isely1c9d10d2008-03-28 05:38:54 -03003838
3839static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3840{
3841 int ret;
3842 unsigned int cnt = 1;
3843 unsigned int args = 0;
3844 LOCK_TAKE(hdw->ctl_lock);
3845 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3846 args = (cmdcode >> 8) & 0xffu;
3847 args = (args > 2) ? 2 : args;
3848 if (args) {
3849 cnt += args;
3850 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3851 if (args > 1) {
3852 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3853 }
3854 }
3855 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3856 unsigned int idx;
3857 unsigned int ccnt,bcnt;
3858 char tbuf[50];
3859 cmdcode &= 0xffu;
3860 bcnt = 0;
3861 ccnt = scnprintf(tbuf+bcnt,
3862 sizeof(tbuf)-bcnt,
3863 "Sending FX2 command 0x%x",cmdcode);
3864 bcnt += ccnt;
3865 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3866 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3867 ccnt = scnprintf(tbuf+bcnt,
3868 sizeof(tbuf)-bcnt,
3869 " \"%s\"",
3870 pvr2_fx2cmd_desc[idx].desc);
3871 bcnt += ccnt;
3872 break;
3873 }
3874 }
3875 if (args) {
3876 ccnt = scnprintf(tbuf+bcnt,
3877 sizeof(tbuf)-bcnt,
3878 " (%u",hdw->cmd_buffer[1]);
3879 bcnt += ccnt;
3880 if (args > 1) {
3881 ccnt = scnprintf(tbuf+bcnt,
3882 sizeof(tbuf)-bcnt,
3883 ",%u",hdw->cmd_buffer[2]);
3884 bcnt += ccnt;
3885 }
3886 ccnt = scnprintf(tbuf+bcnt,
3887 sizeof(tbuf)-bcnt,
3888 ")");
3889 bcnt += ccnt;
3890 }
3891 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
3892 }
3893 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
3894 LOCK_GIVE(hdw->ctl_lock);
3895 return ret;
3896}
3897
3898
Mike Iselyd8554972006-06-26 20:58:46 -03003899int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3900{
3901 int ret;
3902
3903 LOCK_TAKE(hdw->ctl_lock);
3904
Michael Krufky8d364362007-01-22 02:17:55 -03003905 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003906 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3907 hdw->cmd_buffer[5] = 0;
3908 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3909 hdw->cmd_buffer[7] = reg & 0xff;
3910
3911
3912 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3913
3914 LOCK_GIVE(hdw->ctl_lock);
3915
3916 return ret;
3917}
3918
3919
Adrian Bunk07e337e2006-06-30 11:30:20 -03003920static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003921{
3922 int ret = 0;
3923
3924 LOCK_TAKE(hdw->ctl_lock);
3925
Michael Krufky8d364362007-01-22 02:17:55 -03003926 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003927 hdw->cmd_buffer[1] = 0;
3928 hdw->cmd_buffer[2] = 0;
3929 hdw->cmd_buffer[3] = 0;
3930 hdw->cmd_buffer[4] = 0;
3931 hdw->cmd_buffer[5] = 0;
3932 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3933 hdw->cmd_buffer[7] = reg & 0xff;
3934
3935 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3936 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3937
3938 LOCK_GIVE(hdw->ctl_lock);
3939
3940 return ret;
3941}
3942
3943
Mike Isely681c7392007-11-26 01:48:52 -03003944void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003945{
3946 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003947 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3948 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003949 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003950 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003951 }
Mike Isely681c7392007-11-26 01:48:52 -03003952 hdw->flag_ok = 0;
3953 trace_stbit("flag_ok",hdw->flag_ok);
3954 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003955}
3956
3957
3958void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3959{
3960 int ret;
3961 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003962 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Alan Stern011b15d2008-11-04 11:29:27 -05003963 if (ret == 0) {
Mike Iselyd8554972006-06-26 20:58:46 -03003964 ret = usb_reset_device(hdw->usb_dev);
3965 usb_unlock_device(hdw->usb_dev);
3966 } else {
3967 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3968 "Failed to lock USB device ret=%d",ret);
3969 }
3970 if (init_pause_msec) {
3971 pvr2_trace(PVR2_TRACE_INFO,
3972 "Waiting %u msec for hardware to settle",
3973 init_pause_msec);
3974 msleep(init_pause_msec);
3975 }
3976
3977}
3978
3979
3980void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3981{
3982 char da[1];
3983 unsigned int pipe;
3984 int ret;
3985
3986 if (!hdw->usb_dev) return;
3987
3988 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3989
3990 da[0] = val ? 0x01 : 0x00;
3991
3992 /* Write the CPUCS register on the 8051. The lsb of the register
3993 is the reset bit; a 1 asserts reset while a 0 clears it. */
3994 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3995 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3996 if (ret < 0) {
3997 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3998 "cpureset_assert(%d) error=%d",val,ret);
3999 pvr2_hdw_render_useless(hdw);
4000 }
4001}
4002
4003
4004int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
4005{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004006 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03004007}
4008
4009
Michael Krufkye1edb192008-04-22 14:45:39 -03004010int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
4011{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004012 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03004013}
4014
Mike Isely1c9d10d2008-03-28 05:38:54 -03004015
Michael Krufkye1edb192008-04-22 14:45:39 -03004016int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
4017{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004018 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03004019}
4020
Mike Iselyd8554972006-06-26 20:58:46 -03004021
4022int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
4023{
Mike Iselyd8554972006-06-26 20:58:46 -03004024 pvr2_trace(PVR2_TRACE_INIT,
4025 "Requesting decoder reset");
Mike Iselyaf78e162009-03-07 00:21:30 -03004026 if (hdw->decoder_ctrl) {
4027 if (!hdw->decoder_ctrl->force_reset) {
4028 pvr2_trace(PVR2_TRACE_INIT,
4029 "Unable to reset decoder: not implemented");
4030 return -ENOTTY;
4031 }
4032 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
4033 return 0;
4034 } else {
4035 }
4036 if (hdw->decoder_client_id) {
4037 v4l2_device_call_all(&hdw->v4l2_dev, hdw->decoder_client_id,
4038 core, reset, 0);
4039 return 0;
4040 }
4041 pvr2_trace(PVR2_TRACE_INIT,
4042 "Unable to reset decoder: nothing attached");
4043 return -ENOTTY;
Mike Iselyd8554972006-06-26 20:58:46 -03004044}
4045
4046
Mike Isely62433e32008-04-22 14:45:40 -03004047static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004048{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004049 hdw->flag_ok = !0;
4050 return pvr2_issue_simple_cmd(hdw,
4051 FX2CMD_HCW_DEMOD_RESETIN |
4052 (1 << 8) |
4053 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03004054}
4055
Mike Isely84147f32008-04-22 14:45:40 -03004056
Mike Isely62433e32008-04-22 14:45:40 -03004057static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004058{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004059 hdw->flag_ok = !0;
4060 return pvr2_issue_simple_cmd(hdw,(onoff ?
4061 FX2CMD_ONAIR_DTV_POWER_ON :
4062 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004063}
4064
Mike Isely62433e32008-04-22 14:45:40 -03004065
4066static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
4067 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004068{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004069 return pvr2_issue_simple_cmd(hdw,(onoff ?
4070 FX2CMD_ONAIR_DTV_STREAMING_ON :
4071 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004072}
4073
Mike Isely62433e32008-04-22 14:45:40 -03004074
4075static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
4076{
4077 int cmode;
4078 /* Compare digital/analog desired setting with current setting. If
4079 they don't match, fix it... */
4080 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
4081 if (cmode == hdw->pathway_state) {
4082 /* They match; nothing to do */
4083 return;
4084 }
4085
4086 switch (hdw->hdw_desc->digital_control_scheme) {
4087 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4088 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
4089 if (cmode == PVR2_PATHWAY_ANALOG) {
4090 /* If moving to analog mode, also force the decoder
4091 to reset. If no decoder is attached, then it's
4092 ok to ignore this because if/when the decoder
4093 attaches, it will reset itself at that time. */
4094 pvr2_hdw_cmd_decoder_reset(hdw);
4095 }
4096 break;
4097 case PVR2_DIGITAL_SCHEME_ONAIR:
4098 /* Supposedly we should always have the power on whether in
4099 digital or analog mode. But for now do what appears to
4100 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004101 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03004102 break;
4103 default: break;
4104 }
4105
Mike Isely1b9c18c2008-04-22 14:45:41 -03004106 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03004107 hdw->pathway_state = cmode;
4108}
4109
4110
Adrian Bunke9b59f62008-05-10 04:35:24 -03004111static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03004112{
4113 /* change some GPIO data
4114 *
4115 * note: bit d7 of dir appears to control the LED,
4116 * so we shut it off here.
4117 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03004118 */
Mike Isely40381cb2008-04-22 14:45:42 -03004119 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004120 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03004121 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004122 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03004123 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03004124 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03004125}
Mike Iselyc55a97d2008-04-22 14:45:41 -03004126
Mike Isely40381cb2008-04-22 14:45:42 -03004127
4128typedef void (*led_method_func)(struct pvr2_hdw *,int);
4129
4130static led_method_func led_methods[] = {
4131 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
4132};
4133
4134
4135/* Toggle LED */
4136static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
4137{
4138 unsigned int scheme_id;
4139 led_method_func fp;
4140
4141 if ((!onoff) == (!hdw->led_on)) return;
4142
4143 hdw->led_on = onoff != 0;
4144
4145 scheme_id = hdw->hdw_desc->led_scheme;
4146 if (scheme_id < ARRAY_SIZE(led_methods)) {
4147 fp = led_methods[scheme_id];
4148 } else {
4149 fp = NULL;
4150 }
4151
4152 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03004153}
4154
4155
Mike Iselye61b6fc2006-07-18 22:42:18 -03004156/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004157static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03004158{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004159 int ret;
4160
4161 /* If we're in analog mode, then just issue the usual analog
4162 command. */
4163 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4164 return pvr2_issue_simple_cmd(hdw,
4165 (runFl ?
4166 FX2CMD_STREAMING_ON :
4167 FX2CMD_STREAMING_OFF));
4168 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03004169 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004170
4171 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
4172 /* Whoops, we don't know what mode we're in... */
4173 return -EINVAL;
4174 }
4175
4176 /* To get here we have to be in digital mode. The mechanism here
4177 is unfortunately different for different vendors. So we switch
4178 on the device's digital scheme attribute in order to figure out
4179 what to do. */
4180 switch (hdw->hdw_desc->digital_control_scheme) {
4181 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4182 return pvr2_issue_simple_cmd(hdw,
4183 (runFl ?
4184 FX2CMD_HCW_DTV_STREAMING_ON :
4185 FX2CMD_HCW_DTV_STREAMING_OFF));
4186 case PVR2_DIGITAL_SCHEME_ONAIR:
4187 ret = pvr2_issue_simple_cmd(hdw,
4188 (runFl ?
4189 FX2CMD_STREAMING_ON :
4190 FX2CMD_STREAMING_OFF));
4191 if (ret) return ret;
4192 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
4193 default:
4194 return -EINVAL;
4195 }
Mike Iselyd8554972006-06-26 20:58:46 -03004196}
4197
4198
Mike Isely62433e32008-04-22 14:45:40 -03004199/* Evaluate whether or not state_pathway_ok can change */
4200static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
4201{
4202 if (hdw->state_pathway_ok) {
4203 /* Nothing to do if pathway is already ok */
4204 return 0;
4205 }
4206 if (!hdw->state_pipeline_idle) {
4207 /* Not allowed to change anything if pipeline is not idle */
4208 return 0;
4209 }
4210 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
4211 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03004212 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03004213 return !0;
4214}
4215
4216
Mike Isely681c7392007-11-26 01:48:52 -03004217/* Evaluate whether or not state_encoder_ok can change */
4218static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
4219{
4220 if (hdw->state_encoder_ok) return 0;
4221 if (hdw->flag_tripped) return 0;
4222 if (hdw->state_encoder_run) return 0;
4223 if (hdw->state_encoder_config) return 0;
4224 if (hdw->state_decoder_run) return 0;
4225 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004226 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
4227 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
4228 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
4229 return 0;
4230 }
4231
Mike Isely681c7392007-11-26 01:48:52 -03004232 if (pvr2_upload_firmware2(hdw) < 0) {
4233 hdw->flag_tripped = !0;
4234 trace_stbit("flag_tripped",hdw->flag_tripped);
4235 return !0;
4236 }
4237 hdw->state_encoder_ok = !0;
4238 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
4239 return !0;
4240}
4241
4242
4243/* Evaluate whether or not state_encoder_config can change */
4244static int state_eval_encoder_config(struct pvr2_hdw *hdw)
4245{
4246 if (hdw->state_encoder_config) {
4247 if (hdw->state_encoder_ok) {
4248 if (hdw->state_pipeline_req &&
4249 !hdw->state_pipeline_pause) return 0;
4250 }
4251 hdw->state_encoder_config = 0;
4252 hdw->state_encoder_waitok = 0;
4253 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4254 /* paranoia - solve race if timer just completed */
4255 del_timer_sync(&hdw->encoder_wait_timer);
4256 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004257 if (!hdw->state_pathway_ok ||
4258 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4259 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03004260 !hdw->state_pipeline_idle ||
4261 hdw->state_pipeline_pause ||
4262 !hdw->state_pipeline_req ||
4263 !hdw->state_pipeline_config) {
4264 /* We must reset the enforced wait interval if
4265 anything has happened that might have disturbed
4266 the encoder. This should be a rare case. */
4267 if (timer_pending(&hdw->encoder_wait_timer)) {
4268 del_timer_sync(&hdw->encoder_wait_timer);
4269 }
4270 if (hdw->state_encoder_waitok) {
4271 /* Must clear the state - therefore we did
4272 something to a state bit and must also
4273 return true. */
4274 hdw->state_encoder_waitok = 0;
4275 trace_stbit("state_encoder_waitok",
4276 hdw->state_encoder_waitok);
4277 return !0;
4278 }
4279 return 0;
4280 }
4281 if (!hdw->state_encoder_waitok) {
4282 if (!timer_pending(&hdw->encoder_wait_timer)) {
4283 /* waitok flag wasn't set and timer isn't
4284 running. Check flag once more to avoid
4285 a race then start the timer. This is
4286 the point when we measure out a minimal
4287 quiet interval before doing something to
4288 the encoder. */
4289 if (!hdw->state_encoder_waitok) {
4290 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004291 jiffies +
4292 (HZ * TIME_MSEC_ENCODER_WAIT
4293 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004294 add_timer(&hdw->encoder_wait_timer);
4295 }
4296 }
4297 /* We can't continue until we know we have been
4298 quiet for the interval measured by this
4299 timer. */
4300 return 0;
4301 }
4302 pvr2_encoder_configure(hdw);
4303 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
4304 }
4305 trace_stbit("state_encoder_config",hdw->state_encoder_config);
4306 return !0;
4307}
4308
4309
Mike Iselyd913d632008-04-06 04:04:35 -03004310/* Return true if the encoder should not be running. */
4311static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
4312{
4313 if (!hdw->state_encoder_ok) {
4314 /* Encoder isn't healthy at the moment, so stop it. */
4315 return !0;
4316 }
4317 if (!hdw->state_pathway_ok) {
4318 /* Mode is not understood at the moment (i.e. it wants to
4319 change), so encoder must be stopped. */
4320 return !0;
4321 }
4322
4323 switch (hdw->pathway_state) {
4324 case PVR2_PATHWAY_ANALOG:
4325 if (!hdw->state_decoder_run) {
4326 /* We're in analog mode and the decoder is not
4327 running; thus the encoder should be stopped as
4328 well. */
4329 return !0;
4330 }
4331 break;
4332 case PVR2_PATHWAY_DIGITAL:
4333 if (hdw->state_encoder_runok) {
4334 /* This is a funny case. We're in digital mode so
4335 really the encoder should be stopped. However
4336 if it really is running, only kill it after
4337 runok has been set. This gives a chance for the
4338 onair quirk to function (encoder must run
4339 briefly first, at least once, before onair
4340 digital streaming can work). */
4341 return !0;
4342 }
4343 break;
4344 default:
4345 /* Unknown mode; so encoder should be stopped. */
4346 return !0;
4347 }
4348
4349 /* If we get here, we haven't found a reason to stop the
4350 encoder. */
4351 return 0;
4352}
4353
4354
4355/* Return true if the encoder should be running. */
4356static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
4357{
4358 if (!hdw->state_encoder_ok) {
4359 /* Don't run the encoder if it isn't healthy... */
4360 return 0;
4361 }
4362 if (!hdw->state_pathway_ok) {
4363 /* Don't run the encoder if we don't (yet) know what mode
4364 we need to be in... */
4365 return 0;
4366 }
4367
4368 switch (hdw->pathway_state) {
4369 case PVR2_PATHWAY_ANALOG:
4370 if (hdw->state_decoder_run) {
4371 /* In analog mode, if the decoder is running, then
4372 run the encoder. */
4373 return !0;
4374 }
4375 break;
4376 case PVR2_PATHWAY_DIGITAL:
4377 if ((hdw->hdw_desc->digital_control_scheme ==
4378 PVR2_DIGITAL_SCHEME_ONAIR) &&
4379 !hdw->state_encoder_runok) {
4380 /* This is a quirk. OnAir hardware won't stream
4381 digital until the encoder has been run at least
4382 once, for a minimal period of time (empiricially
4383 measured to be 1/4 second). So if we're on
4384 OnAir hardware and the encoder has never been
4385 run at all, then start the encoder. Normal
4386 state machine logic in the driver will
4387 automatically handle the remaining bits. */
4388 return !0;
4389 }
4390 break;
4391 default:
4392 /* For completeness (unknown mode; encoder won't run ever) */
4393 break;
4394 }
4395 /* If we get here, then we haven't found any reason to run the
4396 encoder, so don't run it. */
4397 return 0;
4398}
4399
4400
Mike Isely681c7392007-11-26 01:48:52 -03004401/* Evaluate whether or not state_encoder_run can change */
4402static int state_eval_encoder_run(struct pvr2_hdw *hdw)
4403{
4404 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03004405 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004406 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03004407 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03004408 if (pvr2_encoder_stop(hdw) < 0) return !0;
4409 }
4410 hdw->state_encoder_run = 0;
4411 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03004412 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004413 if (pvr2_encoder_start(hdw) < 0) return !0;
4414 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03004415 if (!hdw->state_encoder_runok) {
4416 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004417 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03004418 add_timer(&hdw->encoder_run_timer);
4419 }
Mike Isely681c7392007-11-26 01:48:52 -03004420 }
4421 trace_stbit("state_encoder_run",hdw->state_encoder_run);
4422 return !0;
4423}
4424
4425
4426/* Timeout function for quiescent timer. */
4427static void pvr2_hdw_quiescent_timeout(unsigned long data)
4428{
4429 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4430 hdw->state_decoder_quiescent = !0;
4431 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4432 hdw->state_stale = !0;
4433 queue_work(hdw->workqueue,&hdw->workpoll);
4434}
4435
4436
4437/* Timeout function for encoder wait timer. */
4438static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
4439{
4440 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4441 hdw->state_encoder_waitok = !0;
4442 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4443 hdw->state_stale = !0;
4444 queue_work(hdw->workqueue,&hdw->workpoll);
4445}
4446
4447
Mike Iselyd913d632008-04-06 04:04:35 -03004448/* Timeout function for encoder run timer. */
4449static void pvr2_hdw_encoder_run_timeout(unsigned long data)
4450{
4451 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4452 if (!hdw->state_encoder_runok) {
4453 hdw->state_encoder_runok = !0;
4454 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
4455 hdw->state_stale = !0;
4456 queue_work(hdw->workqueue,&hdw->workpoll);
4457 }
4458}
4459
4460
Mike Isely681c7392007-11-26 01:48:52 -03004461/* Evaluate whether or not state_decoder_run can change */
4462static int state_eval_decoder_run(struct pvr2_hdw *hdw)
4463{
4464 if (hdw->state_decoder_run) {
4465 if (hdw->state_encoder_ok) {
4466 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03004467 !hdw->state_pipeline_pause &&
4468 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004469 }
4470 if (!hdw->flag_decoder_missed) {
4471 pvr2_decoder_enable(hdw,0);
4472 }
4473 hdw->state_decoder_quiescent = 0;
4474 hdw->state_decoder_run = 0;
4475 /* paranoia - solve race if timer just completed */
4476 del_timer_sync(&hdw->quiescent_timer);
4477 } else {
4478 if (!hdw->state_decoder_quiescent) {
4479 if (!timer_pending(&hdw->quiescent_timer)) {
4480 /* We don't do something about the
4481 quiescent timer until right here because
4482 we also want to catch cases where the
4483 decoder was already not running (like
4484 after initialization) as opposed to
4485 knowing that we had just stopped it.
4486 The second flag check is here to cover a
4487 race - the timer could have run and set
4488 this flag just after the previous check
4489 but before we did the pending check. */
4490 if (!hdw->state_decoder_quiescent) {
4491 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004492 jiffies +
4493 (HZ * TIME_MSEC_DECODER_WAIT
4494 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004495 add_timer(&hdw->quiescent_timer);
4496 }
4497 }
4498 /* Don't allow decoder to start again until it has
4499 been quiesced first. This little detail should
4500 hopefully further stabilize the encoder. */
4501 return 0;
4502 }
Mike Isely62433e32008-04-22 14:45:40 -03004503 if (!hdw->state_pathway_ok ||
4504 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4505 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03004506 hdw->state_pipeline_pause ||
4507 !hdw->state_pipeline_config ||
4508 !hdw->state_encoder_config ||
4509 !hdw->state_encoder_ok) return 0;
4510 del_timer_sync(&hdw->quiescent_timer);
4511 if (hdw->flag_decoder_missed) return 0;
4512 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
4513 hdw->state_decoder_quiescent = 0;
4514 hdw->state_decoder_run = !0;
4515 }
4516 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4517 trace_stbit("state_decoder_run",hdw->state_decoder_run);
4518 return !0;
4519}
4520
4521
4522/* Evaluate whether or not state_usbstream_run can change */
4523static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
4524{
4525 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03004526 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03004527 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03004528 fl = (hdw->state_encoder_ok &&
4529 hdw->state_encoder_run);
4530 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4531 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4532 fl = hdw->state_encoder_ok;
4533 }
4534 if (fl &&
4535 hdw->state_pipeline_req &&
4536 !hdw->state_pipeline_pause &&
4537 hdw->state_pathway_ok) {
4538 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004539 }
4540 pvr2_hdw_cmd_usbstream(hdw,0);
4541 hdw->state_usbstream_run = 0;
4542 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004543 if (!hdw->state_pipeline_req ||
4544 hdw->state_pipeline_pause ||
4545 !hdw->state_pathway_ok) return 0;
4546 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4547 if (!hdw->state_encoder_ok ||
4548 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004549 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4550 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4551 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03004552 if (hdw->state_encoder_run) return 0;
4553 if (hdw->hdw_desc->digital_control_scheme ==
4554 PVR2_DIGITAL_SCHEME_ONAIR) {
4555 /* OnAir digital receivers won't stream
4556 unless the analog encoder has run first.
4557 Why? I have no idea. But don't even
4558 try until we know the analog side is
4559 known to have run. */
4560 if (!hdw->state_encoder_runok) return 0;
4561 }
Mike Isely62433e32008-04-22 14:45:40 -03004562 }
Mike Isely681c7392007-11-26 01:48:52 -03004563 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
4564 hdw->state_usbstream_run = !0;
4565 }
4566 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
4567 return !0;
4568}
4569
4570
4571/* Attempt to configure pipeline, if needed */
4572static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
4573{
4574 if (hdw->state_pipeline_config ||
4575 hdw->state_pipeline_pause) return 0;
4576 pvr2_hdw_commit_execute(hdw);
4577 return !0;
4578}
4579
4580
4581/* Update pipeline idle and pipeline pause tracking states based on other
4582 inputs. This must be called whenever the other relevant inputs have
4583 changed. */
4584static int state_update_pipeline_state(struct pvr2_hdw *hdw)
4585{
4586 unsigned int st;
4587 int updatedFl = 0;
4588 /* Update pipeline state */
4589 st = !(hdw->state_encoder_run ||
4590 hdw->state_decoder_run ||
4591 hdw->state_usbstream_run ||
4592 (!hdw->state_decoder_quiescent));
4593 if (!st != !hdw->state_pipeline_idle) {
4594 hdw->state_pipeline_idle = st;
4595 updatedFl = !0;
4596 }
4597 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
4598 hdw->state_pipeline_pause = 0;
4599 updatedFl = !0;
4600 }
4601 return updatedFl;
4602}
4603
4604
4605typedef int (*state_eval_func)(struct pvr2_hdw *);
4606
4607/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03004608static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03004609 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03004610 state_eval_pipeline_config,
4611 state_eval_encoder_ok,
4612 state_eval_encoder_config,
4613 state_eval_decoder_run,
4614 state_eval_encoder_run,
4615 state_eval_usbstream_run,
4616};
4617
4618
4619/* Process various states and return true if we did anything interesting. */
4620static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4621{
4622 unsigned int i;
4623 int state_updated = 0;
4624 int check_flag;
4625
4626 if (!hdw->state_stale) return 0;
4627 if ((hdw->fw1_state != FW1_STATE_OK) ||
4628 !hdw->flag_ok) {
4629 hdw->state_stale = 0;
4630 return !0;
4631 }
4632 /* This loop is the heart of the entire driver. It keeps trying to
4633 evaluate various bits of driver state until nothing changes for
4634 one full iteration. Each "bit of state" tracks some global
4635 aspect of the driver, e.g. whether decoder should run, if
4636 pipeline is configured, usb streaming is on, etc. We separately
4637 evaluate each of those questions based on other driver state to
4638 arrive at the correct running configuration. */
4639 do {
4640 check_flag = 0;
4641 state_update_pipeline_state(hdw);
4642 /* Iterate over each bit of state */
4643 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4644 if ((*eval_funcs[i])(hdw)) {
4645 check_flag = !0;
4646 state_updated = !0;
4647 state_update_pipeline_state(hdw);
4648 }
4649 }
4650 } while (check_flag && hdw->flag_ok);
4651 hdw->state_stale = 0;
4652 trace_stbit("state_stale",hdw->state_stale);
4653 return state_updated;
4654}
4655
4656
Mike Isely1cb03b72008-04-21 03:47:43 -03004657static unsigned int print_input_mask(unsigned int msk,
4658 char *buf,unsigned int acnt)
4659{
4660 unsigned int idx,ccnt;
4661 unsigned int tcnt = 0;
4662 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4663 if (!((1 << idx) & msk)) continue;
4664 ccnt = scnprintf(buf+tcnt,
4665 acnt-tcnt,
4666 "%s%s",
4667 (tcnt ? ", " : ""),
4668 control_values_input[idx]);
4669 tcnt += ccnt;
4670 }
4671 return tcnt;
4672}
4673
4674
Mike Isely62433e32008-04-22 14:45:40 -03004675static const char *pvr2_pathway_state_name(int id)
4676{
4677 switch (id) {
4678 case PVR2_PATHWAY_ANALOG: return "analog";
4679 case PVR2_PATHWAY_DIGITAL: return "digital";
4680 default: return "unknown";
4681 }
4682}
4683
4684
Mike Isely681c7392007-11-26 01:48:52 -03004685static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4686 char *buf,unsigned int acnt)
4687{
4688 switch (which) {
4689 case 0:
4690 return scnprintf(
4691 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004692 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004693 (hdw->flag_ok ? " <ok>" : " <fail>"),
4694 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4695 (hdw->flag_disconnected ? " <disconnected>" :
4696 " <connected>"),
4697 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004698 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4699 pvr2_pathway_state_name(hdw->pathway_state));
4700
Mike Isely681c7392007-11-26 01:48:52 -03004701 case 1:
4702 return scnprintf(
4703 buf,acnt,
4704 "pipeline:%s%s%s%s",
4705 (hdw->state_pipeline_idle ? " <idle>" : ""),
4706 (hdw->state_pipeline_config ?
4707 " <configok>" : " <stale>"),
4708 (hdw->state_pipeline_req ? " <req>" : ""),
4709 (hdw->state_pipeline_pause ? " <pause>" : ""));
4710 case 2:
4711 return scnprintf(
4712 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004713 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004714 (hdw->state_decoder_run ?
4715 " <decode:run>" :
4716 (hdw->state_decoder_quiescent ?
4717 "" : " <decode:stop>")),
4718 (hdw->state_decoder_quiescent ?
4719 " <decode:quiescent>" : ""),
4720 (hdw->state_encoder_ok ?
4721 "" : " <encode:init>"),
4722 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004723 (hdw->state_encoder_runok ?
4724 " <encode:run>" :
4725 " <encode:firstrun>") :
4726 (hdw->state_encoder_runok ?
4727 " <encode:stop>" :
4728 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004729 (hdw->state_encoder_config ?
4730 " <encode:configok>" :
4731 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004732 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004733 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004734 " <usb:run>" : " <usb:stop>"),
4735 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004736 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004737 case 3:
4738 return scnprintf(
4739 buf,acnt,
4740 "state: %s",
4741 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004742 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004743 unsigned int tcnt = 0;
4744 unsigned int ccnt;
4745
4746 ccnt = scnprintf(buf,
4747 acnt,
4748 "Hardware supported inputs: ");
4749 tcnt += ccnt;
4750 tcnt += print_input_mask(hdw->input_avail_mask,
4751 buf+tcnt,
4752 acnt-tcnt);
4753 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4754 ccnt = scnprintf(buf+tcnt,
4755 acnt-tcnt,
4756 "; allowed inputs: ");
4757 tcnt += ccnt;
4758 tcnt += print_input_mask(hdw->input_allowed_mask,
4759 buf+tcnt,
4760 acnt-tcnt);
4761 }
4762 return tcnt;
4763 }
4764 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004765 struct pvr2_stream_stats stats;
4766 if (!hdw->vid_stream) break;
4767 pvr2_stream_get_stats(hdw->vid_stream,
4768 &stats,
4769 0);
4770 return scnprintf(
4771 buf,acnt,
4772 "Bytes streamed=%u"
4773 " URBs: queued=%u idle=%u ready=%u"
4774 " processed=%u failed=%u",
4775 stats.bytes_processed,
4776 stats.buffers_in_queue,
4777 stats.buffers_in_idle,
4778 stats.buffers_in_ready,
4779 stats.buffers_processed,
4780 stats.buffers_failed);
4781 }
Mike Isely858f9102009-03-07 00:28:28 -03004782 case 6: {
4783 struct v4l2_subdev *sd;
4784 unsigned int tcnt = 0;
4785 unsigned int ccnt;
4786 const char *p;
4787 unsigned int id;
4788 ccnt = scnprintf(buf,
4789 acnt,
4790 "Associted v4l2_subdev drivers:");
4791 tcnt += ccnt;
4792 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
4793 id = sd->grp_id;
4794 p = NULL;
4795 if (id < ARRAY_SIZE(module_names)) {
4796 p = module_names[id];
4797 }
4798 if (!p) p = "(unknown)";
4799 ccnt = scnprintf(buf + tcnt,
4800 acnt - tcnt,
4801 " %s (%u)", p, id);
4802 }
4803 return tcnt;
4804 }
Mike Isely681c7392007-11-26 01:48:52 -03004805 default: break;
4806 }
4807 return 0;
4808}
4809
4810
4811unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4812 char *buf,unsigned int acnt)
4813{
4814 unsigned int bcnt,ccnt,idx;
4815 bcnt = 0;
4816 LOCK_TAKE(hdw->big_lock);
4817 for (idx = 0; ; idx++) {
4818 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
4819 if (!ccnt) break;
4820 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4821 if (!acnt) break;
4822 buf[0] = '\n'; ccnt = 1;
4823 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4824 }
4825 LOCK_GIVE(hdw->big_lock);
4826 return bcnt;
4827}
4828
4829
4830static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
4831{
4832 char buf[128];
4833 unsigned int idx,ccnt;
4834
4835 for (idx = 0; ; idx++) {
4836 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
4837 if (!ccnt) break;
4838 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
4839 }
4840}
4841
4842
4843/* Evaluate and update the driver's current state, taking various actions
4844 as appropriate for the update. */
4845static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
4846{
4847 unsigned int st;
4848 int state_updated = 0;
4849 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004850 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03004851
4852 pvr2_trace(PVR2_TRACE_STBITS,
4853 "Drive state check START");
4854 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4855 pvr2_hdw_state_log_state(hdw);
4856 }
4857
4858 /* Process all state and get back over disposition */
4859 state_updated = pvr2_hdw_state_update(hdw);
4860
Mike Isely1b9c18c2008-04-22 14:45:41 -03004861 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
4862
Mike Isely681c7392007-11-26 01:48:52 -03004863 /* Update master state based upon all other states. */
4864 if (!hdw->flag_ok) {
4865 st = PVR2_STATE_DEAD;
4866 } else if (hdw->fw1_state != FW1_STATE_OK) {
4867 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03004868 } else if ((analog_mode ||
4869 hdw->hdw_desc->flag_digital_requires_cx23416) &&
4870 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03004871 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004872 } else if (hdw->flag_tripped ||
4873 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03004874 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03004875 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03004876 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03004877 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03004878 st = PVR2_STATE_RUN;
4879 } else {
4880 st = PVR2_STATE_READY;
4881 }
4882 if (hdw->master_state != st) {
4883 pvr2_trace(PVR2_TRACE_STATE,
4884 "Device state change from %s to %s",
4885 pvr2_get_state_name(hdw->master_state),
4886 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03004887 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03004888 hdw->master_state = st;
4889 state_updated = !0;
4890 callback_flag = !0;
4891 }
4892 if (state_updated) {
4893 /* Trigger anyone waiting on any state changes here. */
4894 wake_up(&hdw->state_wait_data);
4895 }
4896
4897 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4898 pvr2_hdw_state_log_state(hdw);
4899 }
4900 pvr2_trace(PVR2_TRACE_STBITS,
4901 "Drive state check DONE callback=%d",callback_flag);
4902
4903 return callback_flag;
4904}
4905
4906
4907/* Cause kernel thread to check / update driver state */
4908static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
4909{
4910 if (hdw->state_stale) return;
4911 hdw->state_stale = !0;
4912 trace_stbit("state_stale",hdw->state_stale);
4913 queue_work(hdw->workqueue,&hdw->workpoll);
4914}
4915
4916
Mike Iselyd8554972006-06-26 20:58:46 -03004917int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
4918{
4919 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
4920}
4921
4922
4923int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
4924{
4925 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
4926}
4927
4928
4929int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
4930{
4931 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
4932}
4933
4934
4935int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
4936{
4937 u32 cval,nval;
4938 int ret;
4939 if (~msk) {
4940 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
4941 if (ret) return ret;
4942 nval = (cval & ~msk) | (val & msk);
4943 pvr2_trace(PVR2_TRACE_GPIO,
4944 "GPIO direction changing 0x%x:0x%x"
4945 " from 0x%x to 0x%x",
4946 msk,val,cval,nval);
4947 } else {
4948 nval = val;
4949 pvr2_trace(PVR2_TRACE_GPIO,
4950 "GPIO direction changing to 0x%x",nval);
4951 }
4952 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
4953}
4954
4955
4956int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
4957{
4958 u32 cval,nval;
4959 int ret;
4960 if (~msk) {
4961 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
4962 if (ret) return ret;
4963 nval = (cval & ~msk) | (val & msk);
4964 pvr2_trace(PVR2_TRACE_GPIO,
4965 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
4966 msk,val,cval,nval);
4967 } else {
4968 nval = val;
4969 pvr2_trace(PVR2_TRACE_GPIO,
4970 "GPIO output changing to 0x%x",nval);
4971 }
4972 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
4973}
4974
4975
Mike Iselya51f5002009-03-06 23:30:37 -03004976void pvr2_hdw_status_poll(struct pvr2_hdw *hdw)
4977{
Mike Isely40f07112009-03-07 00:08:17 -03004978 struct v4l2_tuner *vtp = &hdw->tuner_signal_info;
4979 memset(vtp, 0, sizeof(*vtp));
Mike Isely2641df32009-03-07 00:13:25 -03004980 hdw->tuner_signal_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03004981 pvr2_i2c_core_status_poll(hdw);
Mike Isely40f07112009-03-07 00:08:17 -03004982 /* Note: There apparently is no replacement for VIDIOC_CROPCAP
4983 using v4l2-subdev - therefore we can't support that AT ALL right
4984 now. (Of course, no sub-drivers seem to implement it either.
4985 But now it's a a chicken and egg problem...) */
4986 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, g_tuner,
4987 &hdw->tuner_signal_info);
Mike Isely2641df32009-03-07 00:13:25 -03004988 pvr2_trace(PVR2_TRACE_CHIPS, "subdev status poll"
Mike Isely40f07112009-03-07 00:08:17 -03004989 " type=%u strength=%u audio=0x%x cap=0x%x"
4990 " low=%u hi=%u",
4991 vtp->type,
4992 vtp->signal, vtp->rxsubchans, vtp->capability,
4993 vtp->rangelow, vtp->rangehigh);
Mike Isely2641df32009-03-07 00:13:25 -03004994
4995 /* We have to do this to avoid getting into constant polling if
4996 there's nobody to answer a poll of cropcap info. */
4997 hdw->cropcap_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03004998}
4999
5000
Mike Isely7fb20fa2008-04-22 14:45:37 -03005001unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
5002{
5003 return hdw->input_avail_mask;
5004}
5005
5006
Mike Isely1cb03b72008-04-21 03:47:43 -03005007unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
5008{
5009 return hdw->input_allowed_mask;
5010}
5011
5012
5013static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
5014{
5015 if (hdw->input_val != v) {
5016 hdw->input_val = v;
5017 hdw->input_dirty = !0;
5018 }
5019
5020 /* Handle side effects - if we switch to a mode that needs the RF
5021 tuner, then select the right frequency choice as well and mark
5022 it dirty. */
5023 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
5024 hdw->freqSelector = 0;
5025 hdw->freqDirty = !0;
5026 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
5027 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
5028 hdw->freqSelector = 1;
5029 hdw->freqDirty = !0;
5030 }
5031 return 0;
5032}
5033
5034
5035int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
5036 unsigned int change_mask,
5037 unsigned int change_val)
5038{
5039 int ret = 0;
5040 unsigned int nv,m,idx;
5041 LOCK_TAKE(hdw->big_lock);
5042 do {
5043 nv = hdw->input_allowed_mask & ~change_mask;
5044 nv |= (change_val & change_mask);
5045 nv &= hdw->input_avail_mask;
5046 if (!nv) {
5047 /* No legal modes left; return error instead. */
5048 ret = -EPERM;
5049 break;
5050 }
5051 hdw->input_allowed_mask = nv;
5052 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
5053 /* Current mode is still in the allowed mask, so
5054 we're done. */
5055 break;
5056 }
5057 /* Select and switch to a mode that is still in the allowed
5058 mask */
5059 if (!hdw->input_allowed_mask) {
5060 /* Nothing legal; give up */
5061 break;
5062 }
5063 m = hdw->input_allowed_mask;
5064 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
5065 if (!((1 << idx) & m)) continue;
5066 pvr2_hdw_set_input(hdw,idx);
5067 break;
5068 }
5069 } while (0);
5070 LOCK_GIVE(hdw->big_lock);
5071 return ret;
5072}
5073
5074
Mike Iselye61b6fc2006-07-18 22:42:18 -03005075/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03005076static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03005077{
5078 int result;
5079 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03005080 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03005081 result = pvr2_send_request(hdw,
5082 hdw->cmd_buffer,1,
5083 hdw->cmd_buffer,1);
5084 if (result < 0) break;
5085 result = hdw->cmd_buffer[0];
5086 } while(0); LOCK_GIVE(hdw->ctl_lock);
5087 return result;
5088}
5089
5090
Mike Isely32ffa9a2006-09-23 22:26:52 -03005091int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005092 struct v4l2_dbg_match *match, u64 reg_id,
5093 int setFl, u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03005094{
5095#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03005096 struct pvr2_i2c_client *cp;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005097 struct v4l2_dbg_register req;
Mike Isely6d988162006-09-28 17:53:49 -03005098 int stat = 0;
5099 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005100
Mike Isely201f5c92007-01-28 16:08:36 -03005101 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
5102
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005103 req.match = *match;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005104 req.reg = reg_id;
5105 if (setFl) req.val = *val_ptr;
Mike Iselyd8f5b9b2009-03-07 00:05:00 -03005106 /* It would be nice to know if a sub-device answered the request */
5107 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, g_register, &req);
5108 if (!setFl) *val_ptr = req.val;
5109 if (!okFl) mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03005110 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03005111 if (!v4l2_chip_match_i2c_client(
5112 cp->client,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005113 &req.match)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03005114 continue;
5115 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005116 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03005117 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
5118 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03005119 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03005120 okFl = !0;
5121 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005122 }
5123 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03005124 if (okFl) {
5125 return stat;
5126 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005127 return -EINVAL;
5128#else
5129 return -ENOSYS;
5130#endif
5131}
5132
5133
Mike Iselyd8554972006-06-26 20:58:46 -03005134/*
5135 Stuff for Emacs to see, in order to encourage consistent editing style:
5136 *** Local Variables: ***
5137 *** mode: c ***
5138 *** fill-column: 75 ***
5139 *** tab-width: 8 ***
5140 *** c-basic-offset: 8 ***
5141 *** End: ***
5142 */