blob: 36285ca48808a23495b7e98c71a55c28b937d177 [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 Iselyedb9dcb2009-03-07 00:37:10 -0300108typedef void (*pvr2_subdev_update_func)(struct pvr2_hdw *,
109 struct v4l2_subdev *);
110
111static const pvr2_subdev_update_func pvr2_module_update_functions[] = {
112 /* ????? */
113};
114
Mike Iselye9c64a72009-03-06 23:42:20 -0300115static const char *module_names[] = {
116 [PVR2_CLIENT_ID_MSP3400] = "msp3400",
117 [PVR2_CLIENT_ID_CX25840] = "cx25840",
118 [PVR2_CLIENT_ID_SAA7115] = "saa7115",
119 [PVR2_CLIENT_ID_TUNER] = "tuner",
120 [PVR2_CLIENT_ID_CS53132A] = "cs53132a",
121};
122
123
124static const unsigned char *module_i2c_addresses[] = {
125 [PVR2_CLIENT_ID_TUNER] = "\x60\x61\x62\x63",
126};
127
128
Mike Iselyb30d2442006-06-25 20:05:01 -0300129/* Define the list of additional controls we'll dynamically construct based
130 on query of the cx2341x module. */
131struct pvr2_mpeg_ids {
132 const char *strid;
133 int id;
134};
135static const struct pvr2_mpeg_ids mpeg_ids[] = {
136 {
137 .strid = "audio_layer",
138 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
139 },{
140 .strid = "audio_bitrate",
141 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
142 },{
143 /* Already using audio_mode elsewhere :-( */
144 .strid = "mpeg_audio_mode",
145 .id = V4L2_CID_MPEG_AUDIO_MODE,
146 },{
147 .strid = "mpeg_audio_mode_extension",
148 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
149 },{
150 .strid = "audio_emphasis",
151 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
152 },{
153 .strid = "audio_crc",
154 .id = V4L2_CID_MPEG_AUDIO_CRC,
155 },{
156 .strid = "video_aspect",
157 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
158 },{
159 .strid = "video_b_frames",
160 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
161 },{
162 .strid = "video_gop_size",
163 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
164 },{
165 .strid = "video_gop_closure",
166 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
167 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300168 .strid = "video_bitrate_mode",
169 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
170 },{
171 .strid = "video_bitrate",
172 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
173 },{
174 .strid = "video_bitrate_peak",
175 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
176 },{
177 .strid = "video_temporal_decimation",
178 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
179 },{
180 .strid = "stream_type",
181 .id = V4L2_CID_MPEG_STREAM_TYPE,
182 },{
183 .strid = "video_spatial_filter_mode",
184 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
185 },{
186 .strid = "video_spatial_filter",
187 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
188 },{
189 .strid = "video_luma_spatial_filter_type",
190 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
191 },{
192 .strid = "video_chroma_spatial_filter_type",
193 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
194 },{
195 .strid = "video_temporal_filter_mode",
196 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
197 },{
198 .strid = "video_temporal_filter",
199 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
200 },{
201 .strid = "video_median_filter_type",
202 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
203 },{
204 .strid = "video_luma_median_filter_top",
205 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
206 },{
207 .strid = "video_luma_median_filter_bottom",
208 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
209 },{
210 .strid = "video_chroma_median_filter_top",
211 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
212 },{
213 .strid = "video_chroma_median_filter_bottom",
214 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
215 }
216};
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300217#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
Mike Iselyc05c0462006-06-25 20:04:25 -0300218
Mike Iselyd8554972006-06-26 20:58:46 -0300219
Mike Isely434449f2006-08-08 09:10:06 -0300220static const char *control_values_srate[] = {
221 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
222 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
223 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
224};
Mike Iselyd8554972006-06-26 20:58:46 -0300225
Mike Iselyd8554972006-06-26 20:58:46 -0300226
227
228static const char *control_values_input[] = {
229 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
Mike Isely29bf5b12008-04-22 14:45:37 -0300230 [PVR2_CVAL_INPUT_DTV] = "dtv",
Mike Iselyd8554972006-06-26 20:58:46 -0300231 [PVR2_CVAL_INPUT_RADIO] = "radio",
232 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
233 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
234};
235
236
237static const char *control_values_audiomode[] = {
238 [V4L2_TUNER_MODE_MONO] = "Mono",
239 [V4L2_TUNER_MODE_STEREO] = "Stereo",
240 [V4L2_TUNER_MODE_LANG1] = "Lang1",
241 [V4L2_TUNER_MODE_LANG2] = "Lang2",
242 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
243};
244
245
246static const char *control_values_hsm[] = {
247 [PVR2_CVAL_HSM_FAIL] = "Fail",
248 [PVR2_CVAL_HSM_HIGH] = "High",
249 [PVR2_CVAL_HSM_FULL] = "Full",
250};
251
252
Mike Isely681c7392007-11-26 01:48:52 -0300253static const char *pvr2_state_names[] = {
254 [PVR2_STATE_NONE] = "none",
255 [PVR2_STATE_DEAD] = "dead",
256 [PVR2_STATE_COLD] = "cold",
257 [PVR2_STATE_WARM] = "warm",
258 [PVR2_STATE_ERROR] = "error",
259 [PVR2_STATE_READY] = "ready",
260 [PVR2_STATE_RUN] = "run",
Mike Iselyd8554972006-06-26 20:58:46 -0300261};
262
Mike Isely681c7392007-11-26 01:48:52 -0300263
Mike Isely694dca2b2008-03-28 05:42:10 -0300264struct pvr2_fx2cmd_descdef {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300265 unsigned char id;
266 unsigned char *desc;
267};
268
Mike Isely694dca2b2008-03-28 05:42:10 -0300269static const struct pvr2_fx2cmd_descdef pvr2_fx2cmd_desc[] = {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300270 {FX2CMD_MEM_WRITE_DWORD, "write encoder dword"},
271 {FX2CMD_MEM_READ_DWORD, "read encoder dword"},
Mike Isely31335b12008-07-25 19:35:31 -0300272 {FX2CMD_HCW_ZILOG_RESET, "zilog IR reset control"},
Mike Isely1c9d10d2008-03-28 05:38:54 -0300273 {FX2CMD_MEM_READ_64BYTES, "read encoder 64bytes"},
274 {FX2CMD_REG_WRITE, "write encoder register"},
275 {FX2CMD_REG_READ, "read encoder register"},
276 {FX2CMD_MEMSEL, "encoder memsel"},
277 {FX2CMD_I2C_WRITE, "i2c write"},
278 {FX2CMD_I2C_READ, "i2c read"},
279 {FX2CMD_GET_USB_SPEED, "get USB speed"},
280 {FX2CMD_STREAMING_ON, "stream on"},
281 {FX2CMD_STREAMING_OFF, "stream off"},
282 {FX2CMD_FWPOST1, "fwpost1"},
283 {FX2CMD_POWER_OFF, "power off"},
284 {FX2CMD_POWER_ON, "power on"},
285 {FX2CMD_DEEP_RESET, "deep reset"},
286 {FX2CMD_GET_EEPROM_ADDR, "get rom addr"},
287 {FX2CMD_GET_IR_CODE, "get IR code"},
288 {FX2CMD_HCW_DEMOD_RESETIN, "hcw demod resetin"},
289 {FX2CMD_HCW_DTV_STREAMING_ON, "hcw dtv stream on"},
290 {FX2CMD_HCW_DTV_STREAMING_OFF, "hcw dtv stream off"},
291 {FX2CMD_ONAIR_DTV_STREAMING_ON, "onair dtv stream on"},
292 {FX2CMD_ONAIR_DTV_STREAMING_OFF, "onair dtv stream off"},
293 {FX2CMD_ONAIR_DTV_POWER_ON, "onair dtv power on"},
294 {FX2CMD_ONAIR_DTV_POWER_OFF, "onair dtv power off"},
295};
296
297
Mike Isely1cb03b72008-04-21 03:47:43 -0300298static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v);
Mike Isely681c7392007-11-26 01:48:52 -0300299static void pvr2_hdw_state_sched(struct pvr2_hdw *);
300static int pvr2_hdw_state_eval(struct pvr2_hdw *);
Mike Isely1bde0282006-12-27 23:30:13 -0300301static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300302static void pvr2_hdw_worker_i2c(struct work_struct *work);
303static void pvr2_hdw_worker_poll(struct work_struct *work);
Mike Isely681c7392007-11-26 01:48:52 -0300304static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
305static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
306static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300307static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300308static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300309static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300310static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
311static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300312static void pvr2_hdw_quiescent_timeout(unsigned long);
313static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Mike Iselyd913d632008-04-06 04:04:35 -0300314static void pvr2_hdw_encoder_run_timeout(unsigned long);
Mike Isely1c9d10d2008-03-28 05:38:54 -0300315static int pvr2_issue_simple_cmd(struct pvr2_hdw *,u32);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300316static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
317 unsigned int timeout,int probe_fl,
318 void *write_data,unsigned int write_len,
319 void *read_data,unsigned int read_len);
Mike Isely432907f2008-08-31 21:02:20 -0300320static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300321
Mike Isely681c7392007-11-26 01:48:52 -0300322
323static void trace_stbit(const char *name,int val)
324{
325 pvr2_trace(PVR2_TRACE_STBITS,
326 "State bit %s <-- %s",
327 name,(val ? "true" : "false"));
328}
329
Mike Iselyd8554972006-06-26 20:58:46 -0300330static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
331{
332 struct pvr2_hdw *hdw = cptr->hdw;
333 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
334 *vp = hdw->freqTable[hdw->freqProgSlot-1];
335 } else {
336 *vp = 0;
337 }
338 return 0;
339}
340
341static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
342{
343 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300344 unsigned int slotId = hdw->freqProgSlot;
345 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
346 hdw->freqTable[slotId-1] = v;
347 /* Handle side effects correctly - if we're tuned to this
348 slot, then forgot the slot id relation since the stored
349 frequency has been changed. */
350 if (hdw->freqSelector) {
351 if (hdw->freqSlotRadio == slotId) {
352 hdw->freqSlotRadio = 0;
353 }
354 } else {
355 if (hdw->freqSlotTelevision == slotId) {
356 hdw->freqSlotTelevision = 0;
357 }
358 }
Mike Iselyd8554972006-06-26 20:58:46 -0300359 }
360 return 0;
361}
362
363static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
364{
365 *vp = cptr->hdw->freqProgSlot;
366 return 0;
367}
368
369static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
370{
371 struct pvr2_hdw *hdw = cptr->hdw;
372 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
373 hdw->freqProgSlot = v;
374 }
375 return 0;
376}
377
378static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
379{
Mike Isely1bde0282006-12-27 23:30:13 -0300380 struct pvr2_hdw *hdw = cptr->hdw;
381 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300382 return 0;
383}
384
Mike Isely1bde0282006-12-27 23:30:13 -0300385static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300386{
387 unsigned freq = 0;
388 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300389 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
390 if (slotId > 0) {
391 freq = hdw->freqTable[slotId-1];
392 if (!freq) return 0;
393 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300394 }
Mike Isely1bde0282006-12-27 23:30:13 -0300395 if (hdw->freqSelector) {
396 hdw->freqSlotRadio = slotId;
397 } else {
398 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300399 }
400 return 0;
401}
402
403static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
404{
Mike Isely1bde0282006-12-27 23:30:13 -0300405 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300406 return 0;
407}
408
409static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
410{
411 return cptr->hdw->freqDirty != 0;
412}
413
414static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
415{
416 cptr->hdw->freqDirty = 0;
417}
418
419static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
420{
Mike Isely1bde0282006-12-27 23:30:13 -0300421 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300422 return 0;
423}
424
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300425static int ctrl_cropl_min_get(struct pvr2_ctrl *cptr, int *left)
426{
Mike Isely432907f2008-08-31 21:02:20 -0300427 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
428 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
429 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300430 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300431 }
Mike Isely432907f2008-08-31 21:02:20 -0300432 *left = cap->bounds.left;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300433 return 0;
434}
435
436static int ctrl_cropl_max_get(struct pvr2_ctrl *cptr, int *left)
437{
Mike Isely432907f2008-08-31 21:02:20 -0300438 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
439 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
440 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300441 return stat;
442 }
443 *left = cap->bounds.left;
444 if (cap->bounds.width > cptr->hdw->cropw_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300445 *left += cap->bounds.width - cptr->hdw->cropw_val;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300446 }
447 return 0;
448}
449
450static int ctrl_cropt_min_get(struct pvr2_ctrl *cptr, int *top)
451{
Mike Isely432907f2008-08-31 21:02:20 -0300452 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
453 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
454 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300455 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300456 }
Mike Isely432907f2008-08-31 21:02:20 -0300457 *top = cap->bounds.top;
458 return 0;
459}
460
461static int ctrl_cropt_max_get(struct pvr2_ctrl *cptr, int *top)
462{
463 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
464 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
465 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300466 return stat;
467 }
468 *top = cap->bounds.top;
469 if (cap->bounds.height > cptr->hdw->croph_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300470 *top += cap->bounds.height - cptr->hdw->croph_val;
471 }
472 return 0;
473}
474
475static int ctrl_cropw_max_get(struct pvr2_ctrl *cptr, int *val)
476{
477 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
478 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
479 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300480 return stat;
481 }
482 *val = 0;
483 if (cap->bounds.width > cptr->hdw->cropl_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300484 *val = cap->bounds.width - cptr->hdw->cropl_val;
485 }
486 return 0;
487}
488
489static int ctrl_croph_max_get(struct pvr2_ctrl *cptr, int *val)
490{
491 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
492 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
493 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300494 return stat;
495 }
496 *val = 0;
497 if (cap->bounds.height > cptr->hdw->cropt_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300498 *val = cap->bounds.height - cptr->hdw->cropt_val;
499 }
500 return 0;
501}
502
503static int ctrl_get_cropcapbl(struct pvr2_ctrl *cptr, int *val)
504{
505 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
506 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
507 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300508 return stat;
509 }
510 *val = cap->bounds.left;
511 return 0;
512}
513
514static int ctrl_get_cropcapbt(struct pvr2_ctrl *cptr, int *val)
515{
516 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
517 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
518 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300519 return stat;
520 }
521 *val = cap->bounds.top;
522 return 0;
523}
524
525static int ctrl_get_cropcapbw(struct pvr2_ctrl *cptr, int *val)
526{
527 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
528 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
529 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300530 return stat;
531 }
532 *val = cap->bounds.width;
533 return 0;
534}
535
536static int ctrl_get_cropcapbh(struct pvr2_ctrl *cptr, int *val)
537{
538 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
539 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
540 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300541 return stat;
542 }
543 *val = cap->bounds.height;
544 return 0;
545}
546
547static int ctrl_get_cropcapdl(struct pvr2_ctrl *cptr, int *val)
548{
549 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
550 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
551 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300552 return stat;
553 }
554 *val = cap->defrect.left;
555 return 0;
556}
557
558static int ctrl_get_cropcapdt(struct pvr2_ctrl *cptr, int *val)
559{
560 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
561 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
562 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300563 return stat;
564 }
565 *val = cap->defrect.top;
566 return 0;
567}
568
569static int ctrl_get_cropcapdw(struct pvr2_ctrl *cptr, int *val)
570{
571 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
572 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
573 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300574 return stat;
575 }
576 *val = cap->defrect.width;
577 return 0;
578}
579
580static int ctrl_get_cropcapdh(struct pvr2_ctrl *cptr, int *val)
581{
582 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
583 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
584 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300585 return stat;
586 }
587 *val = cap->defrect.height;
588 return 0;
589}
590
591static int ctrl_get_cropcappan(struct pvr2_ctrl *cptr, int *val)
592{
593 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
594 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
595 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300596 return stat;
597 }
598 *val = cap->pixelaspect.numerator;
599 return 0;
600}
601
602static int ctrl_get_cropcappad(struct pvr2_ctrl *cptr, int *val)
603{
604 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
605 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
606 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300607 return stat;
608 }
609 *val = cap->pixelaspect.denominator;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300610 return 0;
611}
612
Mike Isely3ad9fc32006-09-02 22:37:52 -0300613static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
614{
615 /* Actual maximum depends on the video standard in effect. */
616 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
617 *vp = 480;
618 } else {
619 *vp = 576;
620 }
621 return 0;
622}
623
624static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
625{
Mike Isely989eb152007-11-26 01:53:12 -0300626 /* Actual minimum depends on device digitizer type. */
627 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300628 *vp = 75;
629 } else {
630 *vp = 17;
631 }
632 return 0;
633}
634
Mike Isely1bde0282006-12-27 23:30:13 -0300635static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
636{
637 *vp = cptr->hdw->input_val;
638 return 0;
639}
640
Mike Isely29bf5b12008-04-22 14:45:37 -0300641static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
642{
Mike Isely1cb03b72008-04-21 03:47:43 -0300643 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300644}
645
Mike Isely1bde0282006-12-27 23:30:13 -0300646static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
647{
Mike Isely1cb03b72008-04-21 03:47:43 -0300648 return pvr2_hdw_set_input(cptr->hdw,v);
Mike Isely1bde0282006-12-27 23:30:13 -0300649}
650
651static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
652{
653 return cptr->hdw->input_dirty != 0;
654}
655
656static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
657{
658 cptr->hdw->input_dirty = 0;
659}
660
Mike Isely5549f542006-12-27 23:28:54 -0300661
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300662static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
663{
Mike Isely644afdb2007-01-20 00:19:23 -0300664 unsigned long fv;
665 struct pvr2_hdw *hdw = cptr->hdw;
666 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -0300667 pvr2_hdw_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300668 }
Mike Isely644afdb2007-01-20 00:19:23 -0300669 fv = hdw->tuner_signal_info.rangehigh;
670 if (!fv) {
671 /* Safety fallback */
672 *vp = TV_MAX_FREQ;
673 return 0;
674 }
675 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
676 fv = (fv * 125) / 2;
677 } else {
678 fv = fv * 62500;
679 }
680 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300681 return 0;
682}
683
684static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
685{
Mike Isely644afdb2007-01-20 00:19:23 -0300686 unsigned long fv;
687 struct pvr2_hdw *hdw = cptr->hdw;
688 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -0300689 pvr2_hdw_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300690 }
Mike Isely644afdb2007-01-20 00:19:23 -0300691 fv = hdw->tuner_signal_info.rangelow;
692 if (!fv) {
693 /* Safety fallback */
694 *vp = TV_MIN_FREQ;
695 return 0;
696 }
697 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
698 fv = (fv * 125) / 2;
699 } else {
700 fv = fv * 62500;
701 }
702 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300703 return 0;
704}
705
Mike Iselyb30d2442006-06-25 20:05:01 -0300706static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
707{
708 return cptr->hdw->enc_stale != 0;
709}
710
711static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
712{
713 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300714 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300715}
716
717static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
718{
719 int ret;
720 struct v4l2_ext_controls cs;
721 struct v4l2_ext_control c1;
722 memset(&cs,0,sizeof(cs));
723 memset(&c1,0,sizeof(c1));
724 cs.controls = &c1;
725 cs.count = 1;
726 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300727 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300728 VIDIOC_G_EXT_CTRLS);
729 if (ret) return ret;
730 *vp = c1.value;
731 return 0;
732}
733
734static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
735{
736 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300737 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300738 struct v4l2_ext_controls cs;
739 struct v4l2_ext_control c1;
740 memset(&cs,0,sizeof(cs));
741 memset(&c1,0,sizeof(c1));
742 cs.controls = &c1;
743 cs.count = 1;
744 c1.id = cptr->info->v4l_id;
745 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300746 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
747 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300748 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300749 if (ret == -EBUSY) {
750 /* Oops. cx2341x is telling us it's not safe to change
751 this control while we're capturing. Make a note of this
752 fact so that the pipeline will be stopped the next time
753 controls are committed. Then go on ahead and store this
754 change anyway. */
755 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
756 0, &cs,
757 VIDIOC_S_EXT_CTRLS);
758 if (!ret) hdw->enc_unsafe_stale = !0;
759 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300760 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300761 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300762 return 0;
763}
764
765static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
766{
767 struct v4l2_queryctrl qctrl;
768 struct pvr2_ctl_info *info;
769 qctrl.id = cptr->info->v4l_id;
770 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
771 /* Strip out the const so we can adjust a function pointer. It's
772 OK to do this here because we know this is a dynamically created
773 control, so the underlying storage for the info pointer is (a)
774 private to us, and (b) not in read-only storage. Either we do
775 this or we significantly complicate the underlying control
776 implementation. */
777 info = (struct pvr2_ctl_info *)(cptr->info);
778 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
779 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300780 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300781 }
782 } else {
783 if (!(info->set_value)) {
784 info->set_value = ctrl_cx2341x_set;
785 }
786 }
787 return qctrl.flags;
788}
789
Mike Iselyd8554972006-06-26 20:58:46 -0300790static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
791{
Mike Isely681c7392007-11-26 01:48:52 -0300792 *vp = cptr->hdw->state_pipeline_req;
793 return 0;
794}
795
796static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
797{
798 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300799 return 0;
800}
801
802static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
803{
804 int result = pvr2_hdw_is_hsm(cptr->hdw);
805 *vp = PVR2_CVAL_HSM_FULL;
806 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
807 if (result) *vp = PVR2_CVAL_HSM_HIGH;
808 return 0;
809}
810
811static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
812{
813 *vp = cptr->hdw->std_mask_avail;
814 return 0;
815}
816
817static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
818{
819 struct pvr2_hdw *hdw = cptr->hdw;
820 v4l2_std_id ns;
821 ns = hdw->std_mask_avail;
822 ns = (ns & ~m) | (v & m);
823 if (ns == hdw->std_mask_avail) return 0;
824 hdw->std_mask_avail = ns;
825 pvr2_hdw_internal_set_std_avail(hdw);
826 pvr2_hdw_internal_find_stdenum(hdw);
827 return 0;
828}
829
830static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
831 char *bufPtr,unsigned int bufSize,
832 unsigned int *len)
833{
834 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
835 return 0;
836}
837
838static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
839 const char *bufPtr,unsigned int bufSize,
840 int *mskp,int *valp)
841{
842 int ret;
843 v4l2_std_id id;
844 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
845 if (ret < 0) return ret;
846 if (mskp) *mskp = id;
847 if (valp) *valp = id;
848 return 0;
849}
850
851static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
852{
853 *vp = cptr->hdw->std_mask_cur;
854 return 0;
855}
856
857static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
858{
859 struct pvr2_hdw *hdw = cptr->hdw;
860 v4l2_std_id ns;
861 ns = hdw->std_mask_cur;
862 ns = (ns & ~m) | (v & m);
863 if (ns == hdw->std_mask_cur) return 0;
864 hdw->std_mask_cur = ns;
865 hdw->std_dirty = !0;
866 pvr2_hdw_internal_find_stdenum(hdw);
867 return 0;
868}
869
870static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
871{
872 return cptr->hdw->std_dirty != 0;
873}
874
875static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
876{
877 cptr->hdw->std_dirty = 0;
878}
879
880static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
881{
Mike Isely18103c572007-01-20 00:09:47 -0300882 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselya51f5002009-03-06 23:30:37 -0300883 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300884 *vp = hdw->tuner_signal_info.signal;
885 return 0;
886}
887
888static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
889{
890 int val = 0;
891 unsigned int subchan;
892 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselya51f5002009-03-06 23:30:37 -0300893 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300894 subchan = hdw->tuner_signal_info.rxsubchans;
895 if (subchan & V4L2_TUNER_SUB_MONO) {
896 val |= (1 << V4L2_TUNER_MODE_MONO);
897 }
898 if (subchan & V4L2_TUNER_SUB_STEREO) {
899 val |= (1 << V4L2_TUNER_MODE_STEREO);
900 }
901 if (subchan & V4L2_TUNER_SUB_LANG1) {
902 val |= (1 << V4L2_TUNER_MODE_LANG1);
903 }
904 if (subchan & V4L2_TUNER_SUB_LANG2) {
905 val |= (1 << V4L2_TUNER_MODE_LANG2);
906 }
907 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300908 return 0;
909}
910
Mike Iselyd8554972006-06-26 20:58:46 -0300911
912static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
913{
914 struct pvr2_hdw *hdw = cptr->hdw;
915 if (v < 0) return -EINVAL;
916 if (v > hdw->std_enum_cnt) return -EINVAL;
917 hdw->std_enum_cur = v;
918 if (!v) return 0;
919 v--;
920 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
921 hdw->std_mask_cur = hdw->std_defs[v].id;
922 hdw->std_dirty = !0;
923 return 0;
924}
925
926
927static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
928{
929 *vp = cptr->hdw->std_enum_cur;
930 return 0;
931}
932
933
934static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
935{
936 return cptr->hdw->std_dirty != 0;
937}
938
939
940static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
941{
942 cptr->hdw->std_dirty = 0;
943}
944
945
946#define DEFINT(vmin,vmax) \
947 .type = pvr2_ctl_int, \
948 .def.type_int.min_value = vmin, \
949 .def.type_int.max_value = vmax
950
951#define DEFENUM(tab) \
952 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300953 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300954 .def.type_enum.value_names = tab
955
Mike Isely33213962006-06-25 20:04:40 -0300956#define DEFBOOL \
957 .type = pvr2_ctl_bool
958
Mike Iselyd8554972006-06-26 20:58:46 -0300959#define DEFMASK(msk,tab) \
960 .type = pvr2_ctl_bitmask, \
961 .def.type_bitmask.valid_bits = msk, \
962 .def.type_bitmask.bit_names = tab
963
964#define DEFREF(vname) \
965 .set_value = ctrl_set_##vname, \
966 .get_value = ctrl_get_##vname, \
967 .is_dirty = ctrl_isdirty_##vname, \
968 .clear_dirty = ctrl_cleardirty_##vname
969
970
971#define VCREATE_FUNCS(vname) \
972static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
973{*vp = cptr->hdw->vname##_val; return 0;} \
974static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
975{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
976static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
977{return cptr->hdw->vname##_dirty != 0;} \
978static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
979{cptr->hdw->vname##_dirty = 0;}
980
981VCREATE_FUNCS(brightness)
982VCREATE_FUNCS(contrast)
983VCREATE_FUNCS(saturation)
984VCREATE_FUNCS(hue)
985VCREATE_FUNCS(volume)
986VCREATE_FUNCS(balance)
987VCREATE_FUNCS(bass)
988VCREATE_FUNCS(treble)
989VCREATE_FUNCS(mute)
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300990VCREATE_FUNCS(cropl)
991VCREATE_FUNCS(cropt)
992VCREATE_FUNCS(cropw)
993VCREATE_FUNCS(croph)
Mike Iselyc05c0462006-06-25 20:04:25 -0300994VCREATE_FUNCS(audiomode)
995VCREATE_FUNCS(res_hor)
996VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300997VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -0300998
Mike Iselyd8554972006-06-26 20:58:46 -0300999/* Table definition of all controls which can be manipulated */
1000static const struct pvr2_ctl_info control_defs[] = {
1001 {
1002 .v4l_id = V4L2_CID_BRIGHTNESS,
1003 .desc = "Brightness",
1004 .name = "brightness",
1005 .default_value = 128,
1006 DEFREF(brightness),
1007 DEFINT(0,255),
1008 },{
1009 .v4l_id = V4L2_CID_CONTRAST,
1010 .desc = "Contrast",
1011 .name = "contrast",
1012 .default_value = 68,
1013 DEFREF(contrast),
1014 DEFINT(0,127),
1015 },{
1016 .v4l_id = V4L2_CID_SATURATION,
1017 .desc = "Saturation",
1018 .name = "saturation",
1019 .default_value = 64,
1020 DEFREF(saturation),
1021 DEFINT(0,127),
1022 },{
1023 .v4l_id = V4L2_CID_HUE,
1024 .desc = "Hue",
1025 .name = "hue",
1026 .default_value = 0,
1027 DEFREF(hue),
1028 DEFINT(-128,127),
1029 },{
1030 .v4l_id = V4L2_CID_AUDIO_VOLUME,
1031 .desc = "Volume",
1032 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -03001033 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -03001034 DEFREF(volume),
1035 DEFINT(0,65535),
1036 },{
1037 .v4l_id = V4L2_CID_AUDIO_BALANCE,
1038 .desc = "Balance",
1039 .name = "balance",
1040 .default_value = 0,
1041 DEFREF(balance),
1042 DEFINT(-32768,32767),
1043 },{
1044 .v4l_id = V4L2_CID_AUDIO_BASS,
1045 .desc = "Bass",
1046 .name = "bass",
1047 .default_value = 0,
1048 DEFREF(bass),
1049 DEFINT(-32768,32767),
1050 },{
1051 .v4l_id = V4L2_CID_AUDIO_TREBLE,
1052 .desc = "Treble",
1053 .name = "treble",
1054 .default_value = 0,
1055 DEFREF(treble),
1056 DEFINT(-32768,32767),
1057 },{
1058 .v4l_id = V4L2_CID_AUDIO_MUTE,
1059 .desc = "Mute",
1060 .name = "mute",
1061 .default_value = 0,
1062 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -03001063 DEFBOOL,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001064 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001065 .desc = "Capture crop left margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001066 .name = "crop_left",
1067 .internal_id = PVR2_CID_CROPL,
1068 .default_value = 0,
1069 DEFREF(cropl),
1070 DEFINT(-129, 340),
1071 .get_min_value = ctrl_cropl_min_get,
1072 .get_max_value = ctrl_cropl_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001073 .get_def_value = ctrl_get_cropcapdl,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001074 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001075 .desc = "Capture crop top margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001076 .name = "crop_top",
1077 .internal_id = PVR2_CID_CROPT,
1078 .default_value = 0,
1079 DEFREF(cropt),
1080 DEFINT(-35, 544),
1081 .get_min_value = ctrl_cropt_min_get,
1082 .get_max_value = ctrl_cropt_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001083 .get_def_value = ctrl_get_cropcapdt,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001084 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001085 .desc = "Capture crop width",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001086 .name = "crop_width",
1087 .internal_id = PVR2_CID_CROPW,
1088 .default_value = 720,
1089 DEFREF(cropw),
Mike Isely432907f2008-08-31 21:02:20 -03001090 .get_max_value = ctrl_cropw_max_get,
1091 .get_def_value = ctrl_get_cropcapdw,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001092 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001093 .desc = "Capture crop height",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001094 .name = "crop_height",
1095 .internal_id = PVR2_CID_CROPH,
1096 .default_value = 480,
1097 DEFREF(croph),
Mike Isely432907f2008-08-31 21:02:20 -03001098 .get_max_value = ctrl_croph_max_get,
1099 .get_def_value = ctrl_get_cropcapdh,
1100 }, {
1101 .desc = "Capture capability pixel aspect numerator",
1102 .name = "cropcap_pixel_numerator",
1103 .internal_id = PVR2_CID_CROPCAPPAN,
1104 .get_value = ctrl_get_cropcappan,
1105 }, {
1106 .desc = "Capture capability pixel aspect denominator",
1107 .name = "cropcap_pixel_denominator",
1108 .internal_id = PVR2_CID_CROPCAPPAD,
1109 .get_value = ctrl_get_cropcappad,
1110 }, {
1111 .desc = "Capture capability bounds top",
1112 .name = "cropcap_bounds_top",
1113 .internal_id = PVR2_CID_CROPCAPBT,
1114 .get_value = ctrl_get_cropcapbt,
1115 }, {
1116 .desc = "Capture capability bounds left",
1117 .name = "cropcap_bounds_left",
1118 .internal_id = PVR2_CID_CROPCAPBL,
1119 .get_value = ctrl_get_cropcapbl,
1120 }, {
1121 .desc = "Capture capability bounds width",
1122 .name = "cropcap_bounds_width",
1123 .internal_id = PVR2_CID_CROPCAPBW,
1124 .get_value = ctrl_get_cropcapbw,
1125 }, {
1126 .desc = "Capture capability bounds height",
1127 .name = "cropcap_bounds_height",
1128 .internal_id = PVR2_CID_CROPCAPBH,
1129 .get_value = ctrl_get_cropcapbh,
Mike Iselyd8554972006-06-26 20:58:46 -03001130 },{
Mike Iselyc05c0462006-06-25 20:04:25 -03001131 .desc = "Video Source",
1132 .name = "input",
1133 .internal_id = PVR2_CID_INPUT,
1134 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -03001135 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -03001136 DEFREF(input),
1137 DEFENUM(control_values_input),
1138 },{
1139 .desc = "Audio Mode",
1140 .name = "audio_mode",
1141 .internal_id = PVR2_CID_AUDIOMODE,
1142 .default_value = V4L2_TUNER_MODE_STEREO,
1143 DEFREF(audiomode),
1144 DEFENUM(control_values_audiomode),
1145 },{
1146 .desc = "Horizontal capture resolution",
1147 .name = "resolution_hor",
1148 .internal_id = PVR2_CID_HRES,
1149 .default_value = 720,
1150 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001151 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -03001152 },{
1153 .desc = "Vertical capture resolution",
1154 .name = "resolution_ver",
1155 .internal_id = PVR2_CID_VRES,
1156 .default_value = 480,
1157 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001158 DEFINT(17,576),
1159 /* Hook in check for video standard and adjust maximum
1160 depending on the standard. */
1161 .get_max_value = ctrl_vres_max_get,
1162 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -03001163 },{
Mike Iselyb30d2442006-06-25 20:05:01 -03001164 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -03001165 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
1166 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -03001167 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -03001168 DEFREF(srate),
1169 DEFENUM(control_values_srate),
1170 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001171 .desc = "Tuner Frequency (Hz)",
1172 .name = "frequency",
1173 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -03001174 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -03001175 .set_value = ctrl_freq_set,
1176 .get_value = ctrl_freq_get,
1177 .is_dirty = ctrl_freq_is_dirty,
1178 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -03001179 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -03001180 /* Hook in check for input value (tv/radio) and adjust
1181 max/min values accordingly */
1182 .get_max_value = ctrl_freq_max_get,
1183 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001184 },{
1185 .desc = "Channel",
1186 .name = "channel",
1187 .set_value = ctrl_channel_set,
1188 .get_value = ctrl_channel_get,
1189 DEFINT(0,FREQTABLE_SIZE),
1190 },{
1191 .desc = "Channel Program Frequency",
1192 .name = "freq_table_value",
1193 .set_value = ctrl_channelfreq_set,
1194 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -03001195 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -03001196 /* Hook in check for input value (tv/radio) and adjust
1197 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -03001198 .get_max_value = ctrl_freq_max_get,
1199 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001200 },{
1201 .desc = "Channel Program ID",
1202 .name = "freq_table_channel",
1203 .set_value = ctrl_channelprog_set,
1204 .get_value = ctrl_channelprog_get,
1205 DEFINT(0,FREQTABLE_SIZE),
1206 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001207 .desc = "Streaming Enabled",
1208 .name = "streaming_enabled",
1209 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -03001210 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -03001211 },{
1212 .desc = "USB Speed",
1213 .name = "usb_speed",
1214 .get_value = ctrl_hsm_get,
1215 DEFENUM(control_values_hsm),
1216 },{
Mike Isely681c7392007-11-26 01:48:52 -03001217 .desc = "Master State",
1218 .name = "master_state",
1219 .get_value = ctrl_masterstate_get,
1220 DEFENUM(pvr2_state_names),
1221 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001222 .desc = "Signal Present",
1223 .name = "signal_present",
1224 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -03001225 DEFINT(0,65535),
1226 },{
1227 .desc = "Audio Modes Present",
1228 .name = "audio_modes_present",
1229 .get_value = ctrl_audio_modes_present_get,
1230 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
1231 v4l. Nothing outside of this module cares about this,
1232 but I reuse it in order to also reuse the
1233 control_values_audiomode string table. */
1234 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
1235 (1 << V4L2_TUNER_MODE_STEREO)|
1236 (1 << V4L2_TUNER_MODE_LANG1)|
1237 (1 << V4L2_TUNER_MODE_LANG2)),
1238 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -03001239 },{
1240 .desc = "Video Standards Available Mask",
1241 .name = "video_standard_mask_available",
1242 .internal_id = PVR2_CID_STDAVAIL,
1243 .skip_init = !0,
1244 .get_value = ctrl_stdavail_get,
1245 .set_value = ctrl_stdavail_set,
1246 .val_to_sym = ctrl_std_val_to_sym,
1247 .sym_to_val = ctrl_std_sym_to_val,
1248 .type = pvr2_ctl_bitmask,
1249 },{
1250 .desc = "Video Standards In Use Mask",
1251 .name = "video_standard_mask_active",
1252 .internal_id = PVR2_CID_STDCUR,
1253 .skip_init = !0,
1254 .get_value = ctrl_stdcur_get,
1255 .set_value = ctrl_stdcur_set,
1256 .is_dirty = ctrl_stdcur_is_dirty,
1257 .clear_dirty = ctrl_stdcur_clear_dirty,
1258 .val_to_sym = ctrl_std_val_to_sym,
1259 .sym_to_val = ctrl_std_sym_to_val,
1260 .type = pvr2_ctl_bitmask,
1261 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001262 .desc = "Video Standard Name",
1263 .name = "video_standard",
1264 .internal_id = PVR2_CID_STDENUM,
1265 .skip_init = !0,
1266 .get_value = ctrl_stdenumcur_get,
1267 .set_value = ctrl_stdenumcur_set,
1268 .is_dirty = ctrl_stdenumcur_is_dirty,
1269 .clear_dirty = ctrl_stdenumcur_clear_dirty,
1270 .type = pvr2_ctl_enum,
1271 }
1272};
1273
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001274#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -03001275
1276
1277const char *pvr2_config_get_name(enum pvr2_config cfg)
1278{
1279 switch (cfg) {
1280 case pvr2_config_empty: return "empty";
1281 case pvr2_config_mpeg: return "mpeg";
1282 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -03001283 case pvr2_config_pcm: return "pcm";
1284 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -03001285 }
1286 return "<unknown>";
1287}
1288
1289
1290struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1291{
1292 return hdw->usb_dev;
1293}
1294
1295
1296unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1297{
1298 return hdw->serial_number;
1299}
1300
Mike Isely31a18542007-04-08 01:11:47 -03001301
1302const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1303{
1304 return hdw->bus_info;
1305}
1306
1307
Mike Isely13a88792009-01-14 04:22:56 -03001308const char *pvr2_hdw_get_device_identifier(struct pvr2_hdw *hdw)
1309{
1310 return hdw->identifier;
1311}
1312
1313
Mike Isely1bde0282006-12-27 23:30:13 -03001314unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1315{
1316 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1317}
1318
1319/* Set the currently tuned frequency and account for all possible
1320 driver-core side effects of this action. */
Adrian Bunkf55a8712008-04-18 05:38:56 -03001321static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
Mike Isely1bde0282006-12-27 23:30:13 -03001322{
Mike Isely7c74e572007-01-20 00:15:41 -03001323 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001324 if (hdw->freqSelector) {
1325 /* Swing over to radio frequency selection */
1326 hdw->freqSelector = 0;
1327 hdw->freqDirty = !0;
1328 }
Mike Isely1bde0282006-12-27 23:30:13 -03001329 if (hdw->freqValRadio != val) {
1330 hdw->freqValRadio = val;
1331 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001332 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001333 }
Mike Isely7c74e572007-01-20 00:15:41 -03001334 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001335 if (!(hdw->freqSelector)) {
1336 /* Swing over to television frequency selection */
1337 hdw->freqSelector = 1;
1338 hdw->freqDirty = !0;
1339 }
Mike Isely1bde0282006-12-27 23:30:13 -03001340 if (hdw->freqValTelevision != val) {
1341 hdw->freqValTelevision = val;
1342 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001343 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001344 }
Mike Isely1bde0282006-12-27 23:30:13 -03001345 }
1346}
1347
Mike Iselyd8554972006-06-26 20:58:46 -03001348int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1349{
1350 return hdw->unit_number;
1351}
1352
1353
1354/* Attempt to locate one of the given set of files. Messages are logged
1355 appropriate to what has been found. The return value will be 0 or
1356 greater on success (it will be the index of the file name found) and
1357 fw_entry will be filled in. Otherwise a negative error is returned on
1358 failure. If the return value is -ENOENT then no viable firmware file
1359 could be located. */
1360static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1361 const struct firmware **fw_entry,
1362 const char *fwtypename,
1363 unsigned int fwcount,
1364 const char *fwnames[])
1365{
1366 unsigned int idx;
1367 int ret = -EINVAL;
1368 for (idx = 0; idx < fwcount; idx++) {
1369 ret = request_firmware(fw_entry,
1370 fwnames[idx],
1371 &hdw->usb_dev->dev);
1372 if (!ret) {
1373 trace_firmware("Located %s firmware: %s;"
1374 " uploading...",
1375 fwtypename,
1376 fwnames[idx]);
1377 return idx;
1378 }
1379 if (ret == -ENOENT) continue;
1380 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1381 "request_firmware fatal error with code=%d",ret);
1382 return ret;
1383 }
1384 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1385 "***WARNING***"
1386 " Device %s firmware"
1387 " seems to be missing.",
1388 fwtypename);
1389 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1390 "Did you install the pvrusb2 firmware files"
1391 " in their proper location?");
1392 if (fwcount == 1) {
1393 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1394 "request_firmware unable to locate %s file %s",
1395 fwtypename,fwnames[0]);
1396 } else {
1397 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1398 "request_firmware unable to locate"
1399 " one of the following %s files:",
1400 fwtypename);
1401 for (idx = 0; idx < fwcount; idx++) {
1402 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1403 "request_firmware: Failed to find %s",
1404 fwnames[idx]);
1405 }
1406 }
1407 return ret;
1408}
1409
1410
1411/*
1412 * pvr2_upload_firmware1().
1413 *
1414 * Send the 8051 firmware to the device. After the upload, arrange for
1415 * device to re-enumerate.
1416 *
1417 * NOTE : the pointer to the firmware data given by request_firmware()
1418 * is not suitable for an usb transaction.
1419 *
1420 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001421static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001422{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001423 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001424 void *fw_ptr;
1425 unsigned int pipe;
1426 int ret;
1427 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001428
Mike Isely989eb152007-11-26 01:53:12 -03001429 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001430 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001431 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1432 "Connected device type defines"
1433 " no firmware to upload; ignoring firmware");
1434 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001435 }
1436
Mike Iselyd8554972006-06-26 20:58:46 -03001437 hdw->fw1_state = FW1_STATE_FAILED; // default result
1438
1439 trace_firmware("pvr2_upload_firmware1");
1440
1441 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001442 hdw->hdw_desc->fx2_firmware.cnt,
1443 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001444 if (ret < 0) {
1445 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1446 return ret;
1447 }
1448
1449 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1450 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1451
1452 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1453
1454 if (fw_entry->size != 0x2000){
1455 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1456 release_firmware(fw_entry);
1457 return -ENOMEM;
1458 }
1459
1460 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1461 if (fw_ptr == NULL){
1462 release_firmware(fw_entry);
1463 return -ENOMEM;
1464 }
1465
1466 /* We have to hold the CPU during firmware upload. */
1467 pvr2_hdw_cpureset_assert(hdw,1);
1468
1469 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1470 chunk. */
1471
1472 ret = 0;
1473 for(address = 0; address < fw_entry->size; address += 0x800) {
1474 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1475 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1476 0, fw_ptr, 0x800, HZ);
1477 }
1478
1479 trace_firmware("Upload done, releasing device's CPU");
1480
1481 /* Now release the CPU. It will disconnect and reconnect later. */
1482 pvr2_hdw_cpureset_assert(hdw,0);
1483
1484 kfree(fw_ptr);
1485 release_firmware(fw_entry);
1486
1487 trace_firmware("Upload done (%d bytes sent)",ret);
1488
1489 /* We should have written 8192 bytes */
1490 if (ret == 8192) {
1491 hdw->fw1_state = FW1_STATE_RELOAD;
1492 return 0;
1493 }
1494
1495 return -EIO;
1496}
1497
1498
1499/*
1500 * pvr2_upload_firmware2()
1501 *
1502 * This uploads encoder firmware on endpoint 2.
1503 *
1504 */
1505
1506int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1507{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001508 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001509 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001510 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001511 int actual_length;
1512 int ret = 0;
1513 int fwidx;
1514 static const char *fw_files[] = {
1515 CX2341X_FIRM_ENC_FILENAME,
1516 };
1517
Mike Isely989eb152007-11-26 01:53:12 -03001518 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001519 return 0;
1520 }
1521
Mike Iselyd8554972006-06-26 20:58:46 -03001522 trace_firmware("pvr2_upload_firmware2");
1523
1524 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001525 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001526 if (ret < 0) return ret;
1527 fwidx = ret;
1528 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001529 /* Since we're about to completely reinitialize the encoder,
1530 invalidate our cached copy of its configuration state. Next
1531 time we configure the encoder, then we'll fully configure it. */
1532 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001533
Mike Iselyd913d632008-04-06 04:04:35 -03001534 /* Encoder is about to be reset so note that as far as we're
1535 concerned now, the encoder has never been run. */
1536 del_timer_sync(&hdw->encoder_run_timer);
1537 if (hdw->state_encoder_runok) {
1538 hdw->state_encoder_runok = 0;
1539 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1540 }
1541
Mike Iselyd8554972006-06-26 20:58:46 -03001542 /* First prepare firmware loading */
1543 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1544 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1545 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1546 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1547 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1548 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1549 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1550 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1551 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1552 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1553 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1554 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1555 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1556 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1557 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1558 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001559 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1560 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001561
1562 if (ret) {
1563 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1564 "firmware2 upload prep failed, ret=%d",ret);
1565 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001566 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001567 }
1568
1569 /* Now send firmware */
1570
1571 fw_len = fw_entry->size;
1572
Mike Isely90060d32007-02-08 02:02:53 -03001573 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001574 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1575 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001576 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001577 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001578 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001579 ret = -EINVAL;
1580 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001581 }
1582
1583 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1584 if (fw_ptr == NULL){
1585 release_firmware(fw_entry);
1586 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1587 "failed to allocate memory for firmware2 upload");
Mike Isely21684ba2008-04-21 03:49:33 -03001588 ret = -ENOMEM;
1589 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001590 }
1591
1592 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1593
Mike Isely90060d32007-02-08 02:02:53 -03001594 fw_done = 0;
1595 for (fw_done = 0; fw_done < fw_len;) {
1596 bcnt = fw_len - fw_done;
1597 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1598 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1599 /* Usbsnoop log shows that we must swap bytes... */
Mike Isely5f33df12008-08-30 15:09:31 -03001600 /* Some background info: The data being swapped here is a
1601 firmware image destined for the mpeg encoder chip that
1602 lives at the other end of a USB endpoint. The encoder
1603 chip always talks in 32 bit chunks and its storage is
1604 organized into 32 bit words. However from the file
1605 system to the encoder chip everything is purely a byte
1606 stream. The firmware file's contents are always 32 bit
1607 swapped from what the encoder expects. Thus the need
1608 always exists to swap the bytes regardless of the endian
1609 type of the host processor and therefore swab32() makes
1610 the most sense. */
Mike Isely90060d32007-02-08 02:02:53 -03001611 for (icnt = 0; icnt < bcnt/4 ; icnt++)
Harvey Harrison513edce2008-08-18 17:38:01 -03001612 ((u32 *)fw_ptr)[icnt] = swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001613
Mike Isely90060d32007-02-08 02:02:53 -03001614 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001615 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001616 ret |= (actual_length != bcnt);
1617 if (ret) break;
1618 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001619 }
1620
1621 trace_firmware("upload of %s : %i / %i ",
1622 fw_files[fwidx],fw_done,fw_len);
1623
1624 kfree(fw_ptr);
1625 release_firmware(fw_entry);
1626
1627 if (ret) {
1628 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1629 "firmware2 upload transfer failure");
Mike Isely21684ba2008-04-21 03:49:33 -03001630 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001631 }
1632
1633 /* Finish upload */
1634
1635 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1636 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001637 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001638
1639 if (ret) {
1640 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1641 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001642 }
Mike Isely21684ba2008-04-21 03:49:33 -03001643
1644 done:
Mike Isely1df59f02008-04-21 03:50:39 -03001645 if (hdw->hdw_desc->signal_routing_scheme ==
1646 PVR2_ROUTING_SCHEME_GOTVIEW) {
1647 /* Ensure that GPIO 11 is set to output for GOTVIEW
1648 hardware. */
1649 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1650 }
Mike Iselyd8554972006-06-26 20:58:46 -03001651 return ret;
1652}
1653
1654
Mike Isely681c7392007-11-26 01:48:52 -03001655static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001656{
Mike Isely681c7392007-11-26 01:48:52 -03001657 if (st < ARRAY_SIZE(pvr2_state_names)) {
1658 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001659 }
Mike Isely681c7392007-11-26 01:48:52 -03001660 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001661}
1662
Mike Isely681c7392007-11-26 01:48:52 -03001663static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001664{
Mike Iselyaf78e162009-03-07 00:21:30 -03001665 if (hdw->decoder_ctrl) {
1666 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt, enablefl);
1667 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001668 }
Mike Iselyaf78e162009-03-07 00:21:30 -03001669 /* Even though we really only care about the video decoder chip at
1670 this point, we'll broadcast stream on/off to all sub-devices
1671 anyway, just in case somebody else wants to hear the
1672 command... */
1673 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_stream, enablefl);
1674 if (hdw->decoder_client_id) {
1675 /* We get here if the encoder has been noticed. Otherwise
1676 we'll issue a warning to the user (which should
1677 normally never happen). */
1678 return 0;
1679 }
1680 if (!hdw->flag_decoder_missed) {
1681 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1682 "WARNING: No decoder present");
1683 hdw->flag_decoder_missed = !0;
1684 trace_stbit("flag_decoder_missed",
1685 hdw->flag_decoder_missed);
1686 }
1687 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001688}
1689
1690
Mike Isely681c7392007-11-26 01:48:52 -03001691void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1692{
1693 if (hdw->decoder_ctrl == ptr) return;
1694 hdw->decoder_ctrl = ptr;
1695 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1696 hdw->flag_decoder_missed = 0;
1697 trace_stbit("flag_decoder_missed",
1698 hdw->flag_decoder_missed);
1699 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1700 "Decoder has appeared");
1701 pvr2_hdw_state_sched(hdw);
1702 }
1703}
1704
1705
1706int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1707{
1708 return hdw->master_state;
1709}
1710
1711
1712static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1713{
1714 if (!hdw->flag_tripped) return 0;
1715 hdw->flag_tripped = 0;
1716 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1717 "Clearing driver error statuss");
1718 return !0;
1719}
1720
1721
1722int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1723{
1724 int fl;
1725 LOCK_TAKE(hdw->big_lock); do {
1726 fl = pvr2_hdw_untrip_unlocked(hdw);
1727 } while (0); LOCK_GIVE(hdw->big_lock);
1728 if (fl) pvr2_hdw_state_sched(hdw);
1729 return 0;
1730}
1731
1732
Mike Isely681c7392007-11-26 01:48:52 -03001733
1734
Mike Iselyd8554972006-06-26 20:58:46 -03001735int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1736{
Mike Isely681c7392007-11-26 01:48:52 -03001737 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001738}
1739
1740
1741int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1742{
Mike Isely681c7392007-11-26 01:48:52 -03001743 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001744 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001745 pvr2_hdw_untrip_unlocked(hdw);
1746 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1747 hdw->state_pipeline_req = enable_flag != 0;
1748 pvr2_trace(PVR2_TRACE_START_STOP,
1749 "/*--TRACE_STREAM--*/ %s",
1750 enable_flag ? "enable" : "disable");
1751 }
1752 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001753 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001754 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1755 if (enable_flag) {
1756 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1757 if (st != PVR2_STATE_READY) return -EIO;
1758 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1759 }
1760 }
Mike Iselyd8554972006-06-26 20:58:46 -03001761 return 0;
1762}
1763
1764
1765int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1766{
Mike Isely681c7392007-11-26 01:48:52 -03001767 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001768 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001769 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1770 hdw->desired_stream_type = config;
1771 hdw->state_pipeline_config = 0;
1772 trace_stbit("state_pipeline_config",
1773 hdw->state_pipeline_config);
1774 pvr2_hdw_state_sched(hdw);
1775 }
Mike Iselyd8554972006-06-26 20:58:46 -03001776 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001777 if (fl) return 0;
1778 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001779}
1780
1781
1782static int get_default_tuner_type(struct pvr2_hdw *hdw)
1783{
1784 int unit_number = hdw->unit_number;
1785 int tp = -1;
1786 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1787 tp = tuner[unit_number];
1788 }
1789 if (tp < 0) return -EINVAL;
1790 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001791 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001792 return 0;
1793}
1794
1795
1796static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1797{
1798 int unit_number = hdw->unit_number;
1799 int tp = 0;
1800 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1801 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001802 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001803 }
Mike Isely6a540252007-12-02 23:51:34 -03001804 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001805}
1806
1807
1808static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1809{
1810 int unit_number = hdw->unit_number;
1811 int tp = 0;
1812 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1813 tp = tolerance[unit_number];
1814 }
1815 return tp;
1816}
1817
1818
1819static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1820{
1821 /* Try a harmless request to fetch the eeprom's address over
1822 endpoint 1. See what happens. Only the full FX2 image can
1823 respond to this. If this probe fails then likely the FX2
1824 firmware needs be loaded. */
1825 int result;
1826 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001827 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001828 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1829 hdw->cmd_buffer,1,
1830 hdw->cmd_buffer,1);
1831 if (result < 0) break;
1832 } while(0); LOCK_GIVE(hdw->ctl_lock);
1833 if (result) {
1834 pvr2_trace(PVR2_TRACE_INIT,
1835 "Probe of device endpoint 1 result status %d",
1836 result);
1837 } else {
1838 pvr2_trace(PVR2_TRACE_INIT,
1839 "Probe of device endpoint 1 succeeded");
1840 }
1841 return result == 0;
1842}
1843
Mike Isely9f66d4e2007-09-08 22:28:51 -03001844struct pvr2_std_hack {
1845 v4l2_std_id pat; /* Pattern to match */
1846 v4l2_std_id msk; /* Which bits we care about */
1847 v4l2_std_id std; /* What additional standards or default to set */
1848};
1849
1850/* This data structure labels specific combinations of standards from
1851 tveeprom that we'll try to recognize. If we recognize one, then assume
1852 a specified default standard to use. This is here because tveeprom only
1853 tells us about available standards not the intended default standard (if
1854 any) for the device in question. We guess the default based on what has
1855 been reported as available. Note that this is only for guessing a
1856 default - which can always be overridden explicitly - and if the user
1857 has otherwise named a default then that default will always be used in
1858 place of this table. */
Tobias Klauserebff0332008-04-22 14:45:45 -03001859static const struct pvr2_std_hack std_eeprom_maps[] = {
Mike Isely9f66d4e2007-09-08 22:28:51 -03001860 { /* PAL(B/G) */
1861 .pat = V4L2_STD_B|V4L2_STD_GH,
1862 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1863 },
1864 { /* NTSC(M) */
1865 .pat = V4L2_STD_MN,
1866 .std = V4L2_STD_NTSC_M,
1867 },
1868 { /* PAL(I) */
1869 .pat = V4L2_STD_PAL_I,
1870 .std = V4L2_STD_PAL_I,
1871 },
1872 { /* SECAM(L/L') */
1873 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1874 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1875 },
1876 { /* PAL(D/D1/K) */
1877 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001878 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001879 },
1880};
1881
Mike Iselyd8554972006-06-26 20:58:46 -03001882static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1883{
1884 char buf[40];
1885 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001886 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001887
1888 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001889 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001890
1891 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001892 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001893 "Supported video standard(s) reported available"
1894 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001895 bcnt,buf);
1896
1897 hdw->std_mask_avail = hdw->std_mask_eeprom;
1898
Mike Isely3d290bd2007-12-03 01:47:12 -03001899 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001900 if (std2) {
1901 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001902 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001903 "Expanding supported video standards"
1904 " to include: %.*s",
1905 bcnt,buf);
1906 hdw->std_mask_avail |= std2;
1907 }
1908
1909 pvr2_hdw_internal_set_std_avail(hdw);
1910
1911 if (std1) {
1912 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001913 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001914 "Initial video standard forced to %.*s",
1915 bcnt,buf);
1916 hdw->std_mask_cur = std1;
1917 hdw->std_dirty = !0;
1918 pvr2_hdw_internal_find_stdenum(hdw);
1919 return;
1920 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001921 if (std3) {
1922 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1923 pvr2_trace(PVR2_TRACE_STD,
1924 "Initial video standard"
1925 " (determined by device type): %.*s",bcnt,buf);
1926 hdw->std_mask_cur = std3;
1927 hdw->std_dirty = !0;
1928 pvr2_hdw_internal_find_stdenum(hdw);
1929 return;
1930 }
Mike Iselyd8554972006-06-26 20:58:46 -03001931
Mike Isely9f66d4e2007-09-08 22:28:51 -03001932 {
1933 unsigned int idx;
1934 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1935 if (std_eeprom_maps[idx].msk ?
1936 ((std_eeprom_maps[idx].pat ^
1937 hdw->std_mask_eeprom) &
1938 std_eeprom_maps[idx].msk) :
1939 (std_eeprom_maps[idx].pat !=
1940 hdw->std_mask_eeprom)) continue;
1941 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1942 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001943 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001944 "Initial video standard guessed as %.*s",
1945 bcnt,buf);
1946 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1947 hdw->std_dirty = !0;
1948 pvr2_hdw_internal_find_stdenum(hdw);
1949 return;
1950 }
1951 }
1952
Mike Iselyd8554972006-06-26 20:58:46 -03001953 if (hdw->std_enum_cnt > 1) {
1954 // Autoselect the first listed standard
1955 hdw->std_enum_cur = 1;
1956 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1957 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001958 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001959 "Initial video standard auto-selected to %s",
1960 hdw->std_defs[hdw->std_enum_cur-1].name);
1961 return;
1962 }
1963
Mike Isely0885ba12006-06-25 21:30:47 -03001964 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001965 "Unable to select a viable initial video standard");
1966}
1967
1968
Mike Iselye9c64a72009-03-06 23:42:20 -03001969static unsigned int pvr2_copy_i2c_addr_list(
1970 unsigned short *dst, const unsigned char *src,
1971 unsigned int dst_max)
1972{
1973 unsigned int cnt;
1974 if (!src) return 0;
1975 while (src[cnt] && (cnt + 1) < dst_max) {
1976 dst[cnt] = src[cnt];
1977 cnt++;
1978 }
1979 dst[cnt] = I2C_CLIENT_END;
1980 return cnt;
1981}
1982
1983
Mike Isely1ab5e742009-03-07 00:24:24 -03001984static int pvr2_hdw_load_subdev(struct pvr2_hdw *hdw,
1985 const struct pvr2_device_client_desc *cd)
Mike Iselye9c64a72009-03-06 23:42:20 -03001986{
1987 const char *fname;
1988 unsigned char mid;
1989 struct v4l2_subdev *sd;
1990 unsigned int i2ccnt;
1991 const unsigned char *p;
1992 /* Arbitrary count - max # i2c addresses we will probe */
1993 unsigned short i2caddr[25];
1994
1995 mid = cd->module_id;
1996 fname = (mid < ARRAY_SIZE(module_names)) ? module_names[mid] : NULL;
1997 if (!fname) {
1998 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03001999 "Module ID %u for device %s has no name",
Mike Iselye9c64a72009-03-06 23:42:20 -03002000 mid,
2001 hdw->hdw_desc->description);
Mike Isely1ab5e742009-03-07 00:24:24 -03002002 return -EINVAL;
Mike Iselye9c64a72009-03-06 23:42:20 -03002003 }
2004
2005 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, cd->i2c_address_list,
2006 ARRAY_SIZE(i2caddr));
2007 if (!i2ccnt && ((p = (mid < ARRAY_SIZE(module_i2c_addresses)) ?
2008 module_i2c_addresses[mid] : NULL) != NULL)) {
2009 /* Second chance: Try default i2c address list */
2010 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, p,
2011 ARRAY_SIZE(i2caddr));
2012 }
2013
2014 if (!i2ccnt) {
2015 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03002016 "Module ID %u (%s) for device %s:"
2017 " No i2c addresses",
2018 mid, fname, hdw->hdw_desc->description);
2019 return -EINVAL;
Mike Iselye9c64a72009-03-06 23:42:20 -03002020 }
2021
2022 /* Note how the 2nd and 3rd arguments are the same for both
2023 * v4l2_i2c_new_subdev() and v4l2_i2c_new_probed_subdev(). Why?
2024 * Well the 2nd argument is the module name to load, while the 3rd
2025 * argument is documented in the framework as being the "chipid" -
2026 * and every other place where I can find examples of this, the
2027 * "chipid" appears to just be the module name again. So here we
2028 * just do the same thing. */
2029 if (i2ccnt == 1) {
2030 sd = v4l2_i2c_new_subdev(&hdw->i2c_adap,
2031 fname, fname,
2032 i2caddr[0]);
2033 } else {
2034 sd = v4l2_i2c_new_probed_subdev(&hdw->i2c_adap,
2035 fname, fname,
2036 i2caddr);
2037 }
2038
Mike Isely446dfdc2009-03-06 23:58:15 -03002039 if (!sd) {
2040 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03002041 "Module ID %u (%s) for device %s failed to load",
2042 mid, fname, hdw->hdw_desc->description);
2043 return -EIO;
Mike Isely446dfdc2009-03-06 23:58:15 -03002044 }
2045
2046 /* Tag this sub-device instance with the module ID we know about.
2047 In other places we'll use that tag to determine if the instance
2048 requires special handling. */
2049 sd->grp_id = mid;
2050
Mike Iselya932f502009-03-06 23:47:10 -03002051 /* If we have both old and new i2c layers enabled, make sure that
2052 old layer isn't also tracking this module. This is a debugging
2053 aid, in normal situations there's no reason for both mechanisms
2054 to be enabled. */
2055 pvr2_i2c_untrack_subdev(hdw, sd);
Mike Isely446dfdc2009-03-06 23:58:15 -03002056 pvr2_trace(PVR2_TRACE_INIT, "Attached sub-driver %s", fname);
Mike Iselya932f502009-03-06 23:47:10 -03002057
Mike Iselye9c64a72009-03-06 23:42:20 -03002058
Mike Isely00e5f732009-03-07 00:17:11 -03002059 /* client-specific setup... */
2060 switch (mid) {
2061 case PVR2_CLIENT_ID_CX25840:
2062 hdw->decoder_client_id = mid;
2063 {
2064 /*
2065 Mike Isely <isely@pobox.com> 19-Nov-2006 - This
2066 bit of nuttiness for cx25840 causes that module
2067 to correctly set up its video scaling. This is
2068 really a problem in the cx25840 module itself,
2069 but we work around it here. The problem has not
2070 been seen in ivtv because there VBI is supported
2071 and set up. We don't do VBI here (at least not
2072 yet) and thus we never attempted to even set it
2073 up.
2074 */
2075 struct v4l2_format fmt;
2076 memset(&fmt, 0, sizeof(fmt));
2077 fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
2078 v4l2_device_call_all(&hdw->v4l2_dev, mid,
2079 video, s_fmt, &fmt);
2080 }
2081 break;
2082 case PVR2_CLIENT_ID_SAA7115:
2083 hdw->decoder_client_id = mid;
2084 break;
2085 default: break;
2086 }
Mike Isely1ab5e742009-03-07 00:24:24 -03002087
2088 return 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002089}
2090
2091
2092static void pvr2_hdw_load_modules(struct pvr2_hdw *hdw)
2093{
2094 unsigned int idx;
2095 const struct pvr2_string_table *cm;
2096 const struct pvr2_device_client_table *ct;
Mike Isely1ab5e742009-03-07 00:24:24 -03002097 int okFl = !0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002098
2099 cm = &hdw->hdw_desc->client_modules;
2100 for (idx = 0; idx < cm->cnt; idx++) {
2101 request_module(cm->lst[idx]);
2102 }
2103
2104 ct = &hdw->hdw_desc->client_table;
2105 for (idx = 0; idx < ct->cnt; idx++) {
Mike Isely1ab5e742009-03-07 00:24:24 -03002106 if (!pvr2_hdw_load_subdev(hdw, &ct->lst[idx])) okFl = 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002107 }
Mike Isely1ab5e742009-03-07 00:24:24 -03002108 if (!okFl) pvr2_hdw_render_useless(hdw);
Mike Iselye9c64a72009-03-06 23:42:20 -03002109}
2110
2111
Mike Iselyd8554972006-06-26 20:58:46 -03002112static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
2113{
2114 int ret;
2115 unsigned int idx;
2116 struct pvr2_ctrl *cptr;
2117 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03002118 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03002119 if (!reloadFl) {
2120 reloadFl =
2121 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
2122 == 0);
2123 if (reloadFl) {
2124 pvr2_trace(PVR2_TRACE_INIT,
2125 "USB endpoint config looks strange"
2126 "; possibly firmware needs to be"
2127 " loaded");
2128 }
2129 }
2130 if (!reloadFl) {
2131 reloadFl = !pvr2_hdw_check_firmware(hdw);
2132 if (reloadFl) {
2133 pvr2_trace(PVR2_TRACE_INIT,
2134 "Check for FX2 firmware failed"
2135 "; possibly firmware needs to be"
2136 " loaded");
2137 }
2138 }
Mike Iselyd8554972006-06-26 20:58:46 -03002139 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03002140 if (pvr2_upload_firmware1(hdw) != 0) {
2141 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2142 "Failure uploading firmware1");
2143 }
2144 return;
Mike Iselyd8554972006-06-26 20:58:46 -03002145 }
2146 }
Mike Iselyd8554972006-06-26 20:58:46 -03002147 hdw->fw1_state = FW1_STATE_OK;
2148
Mike Iselyd8554972006-06-26 20:58:46 -03002149 if (!pvr2_hdw_dev_ok(hdw)) return;
2150
Mike Isely989eb152007-11-26 01:53:12 -03002151 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03002152 pvr2_hdw_cmd_powerup(hdw);
2153 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002154 }
2155
Mike Isely31335b12008-07-25 19:35:31 -03002156 /* Take the IR chip out of reset, if appropriate */
2157 if (hdw->hdw_desc->ir_scheme == PVR2_IR_SCHEME_ZILOG) {
2158 pvr2_issue_simple_cmd(hdw,
2159 FX2CMD_HCW_ZILOG_RESET |
2160 (1 << 8) |
2161 ((0) << 16));
2162 }
2163
Mike Iselyd8554972006-06-26 20:58:46 -03002164 // This step MUST happen after the earlier powerup step.
Mike Isely59af3362009-03-07 03:06:09 -03002165 pvr2_i2c_track_init(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002166 pvr2_i2c_core_init(hdw);
2167 if (!pvr2_hdw_dev_ok(hdw)) return;
2168
Mike Iselye9c64a72009-03-06 23:42:20 -03002169 pvr2_hdw_load_modules(hdw);
Mike Isely1ab5e742009-03-07 00:24:24 -03002170 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselye9c64a72009-03-06 23:42:20 -03002171
Mike Iselyc05c0462006-06-25 20:04:25 -03002172 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002173 cptr = hdw->controls + idx;
2174 if (cptr->info->skip_init) continue;
2175 if (!cptr->info->set_value) continue;
2176 cptr->info->set_value(cptr,~0,cptr->info->default_value);
2177 }
2178
Mike Isely1bde0282006-12-27 23:30:13 -03002179 /* Set up special default values for the television and radio
2180 frequencies here. It's not really important what these defaults
2181 are, but I set them to something usable in the Chicago area just
2182 to make driver testing a little easier. */
2183
Michael Krufky5a4f5da62008-05-11 16:37:50 -03002184 hdw->freqValTelevision = default_tv_freq;
2185 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03002186
Mike Iselyd8554972006-06-26 20:58:46 -03002187 // Do not use pvr2_reset_ctl_endpoints() here. It is not
2188 // thread-safe against the normal pvr2_send_request() mechanism.
2189 // (We should make it thread safe).
2190
Mike Iselyaaf78842007-11-26 02:04:11 -03002191 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
2192 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002193 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03002194 if (ret < 0) {
2195 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2196 "Unable to determine location of eeprom,"
2197 " skipping");
2198 } else {
2199 hdw->eeprom_addr = ret;
2200 pvr2_eeprom_analyze(hdw);
2201 if (!pvr2_hdw_dev_ok(hdw)) return;
2202 }
2203 } else {
2204 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
2205 hdw->tuner_updated = !0;
2206 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03002207 }
2208
Mike Isely13a88792009-01-14 04:22:56 -03002209 if (hdw->serial_number) {
2210 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2211 "sn-%lu", hdw->serial_number);
2212 } else if (hdw->unit_number >= 0) {
2213 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2214 "unit-%c",
2215 hdw->unit_number + 'a');
2216 } else {
2217 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2218 "unit-??");
2219 }
2220 hdw->identifier[idx] = 0;
2221
Mike Iselyd8554972006-06-26 20:58:46 -03002222 pvr2_hdw_setup_std(hdw);
2223
2224 if (!get_default_tuner_type(hdw)) {
2225 pvr2_trace(PVR2_TRACE_INIT,
2226 "pvr2_hdw_setup: Tuner type overridden to %d",
2227 hdw->tuner_type);
2228 }
2229
Mike Iselyd8554972006-06-26 20:58:46 -03002230 pvr2_i2c_core_check_stale(hdw);
2231 hdw->tuner_updated = 0;
2232
2233 if (!pvr2_hdw_dev_ok(hdw)) return;
2234
Mike Isely1df59f02008-04-21 03:50:39 -03002235 if (hdw->hdw_desc->signal_routing_scheme ==
2236 PVR2_ROUTING_SCHEME_GOTVIEW) {
2237 /* Ensure that GPIO 11 is set to output for GOTVIEW
2238 hardware. */
2239 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
2240 }
2241
Mike Isely681c7392007-11-26 01:48:52 -03002242 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002243
2244 hdw->vid_stream = pvr2_stream_create();
2245 if (!pvr2_hdw_dev_ok(hdw)) return;
2246 pvr2_trace(PVR2_TRACE_INIT,
2247 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
2248 if (hdw->vid_stream) {
2249 idx = get_default_error_tolerance(hdw);
2250 if (idx) {
2251 pvr2_trace(PVR2_TRACE_INIT,
2252 "pvr2_hdw_setup: video stream %p"
2253 " setting tolerance %u",
2254 hdw->vid_stream,idx);
2255 }
2256 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
2257 PVR2_VID_ENDPOINT,idx);
2258 }
2259
2260 if (!pvr2_hdw_dev_ok(hdw)) return;
2261
Mike Iselyd8554972006-06-26 20:58:46 -03002262 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03002263
2264 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002265}
2266
2267
Mike Isely681c7392007-11-26 01:48:52 -03002268/* Set up the structure and attempt to put the device into a usable state.
2269 This can be a time-consuming operation, which is why it is not done
2270 internally as part of the create() step. */
2271static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002272{
2273 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002274 do {
Mike Iselyd8554972006-06-26 20:58:46 -03002275 pvr2_hdw_setup_low(hdw);
2276 pvr2_trace(PVR2_TRACE_INIT,
2277 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03002278 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03002279 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03002280 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03002281 pvr2_trace(
2282 PVR2_TRACE_INFO,
2283 "Device initialization"
2284 " completed successfully.");
2285 break;
2286 }
2287 if (hdw->fw1_state == FW1_STATE_RELOAD) {
2288 pvr2_trace(
2289 PVR2_TRACE_INFO,
2290 "Device microcontroller firmware"
2291 " (re)loaded; it should now reset"
2292 " and reconnect.");
2293 break;
2294 }
2295 pvr2_trace(
2296 PVR2_TRACE_ERROR_LEGS,
2297 "Device initialization was not successful.");
2298 if (hdw->fw1_state == FW1_STATE_MISSING) {
2299 pvr2_trace(
2300 PVR2_TRACE_ERROR_LEGS,
2301 "Giving up since device"
2302 " microcontroller firmware"
2303 " appears to be missing.");
2304 break;
2305 }
2306 }
2307 if (procreload) {
2308 pvr2_trace(
2309 PVR2_TRACE_ERROR_LEGS,
2310 "Attempting pvrusb2 recovery by reloading"
2311 " primary firmware.");
2312 pvr2_trace(
2313 PVR2_TRACE_ERROR_LEGS,
2314 "If this works, device should disconnect"
2315 " and reconnect in a sane state.");
2316 hdw->fw1_state = FW1_STATE_UNKNOWN;
2317 pvr2_upload_firmware1(hdw);
2318 } else {
2319 pvr2_trace(
2320 PVR2_TRACE_ERROR_LEGS,
2321 "***WARNING*** pvrusb2 device hardware"
2322 " appears to be jammed"
2323 " and I can't clear it.");
2324 pvr2_trace(
2325 PVR2_TRACE_ERROR_LEGS,
2326 "You might need to power cycle"
2327 " the pvrusb2 device"
2328 " in order to recover.");
2329 }
Mike Isely681c7392007-11-26 01:48:52 -03002330 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03002331 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002332}
2333
2334
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002335/* Perform second stage initialization. Set callback pointer first so that
2336 we can avoid a possible initialization race (if the kernel thread runs
2337 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03002338int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
2339 void (*callback_func)(void *),
2340 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002341{
2342 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03002343 if (hdw->flag_disconnected) {
2344 /* Handle a race here: If we're already
2345 disconnected by this point, then give up. If we
2346 get past this then we'll remain connected for
2347 the duration of initialization since the entire
2348 initialization sequence is now protected by the
2349 big_lock. */
2350 break;
2351 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002352 hdw->state_data = callback_data;
2353 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03002354 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002355 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03002356 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002357}
2358
2359
2360/* Create, set up, and return a structure for interacting with the
2361 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03002362struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2363 const struct usb_device_id *devid)
2364{
Mike Isely7fb20fa2008-04-22 14:45:37 -03002365 unsigned int idx,cnt1,cnt2,m;
Mike Iselyfe15f132008-08-30 18:11:40 -03002366 struct pvr2_hdw *hdw = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002367 int valid_std_mask;
2368 struct pvr2_ctrl *cptr;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002369 struct usb_device *usb_dev;
Mike Isely989eb152007-11-26 01:53:12 -03002370 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03002371 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03002372 struct v4l2_queryctrl qctrl;
2373 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03002374
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002375 usb_dev = interface_to_usbdev(intf);
2376
Mike Iselyd130fa82007-12-08 17:20:06 -03002377 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03002378
Mike Iselyfe15f132008-08-30 18:11:40 -03002379 if (hdw_desc == NULL) {
2380 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
2381 " No device description pointer,"
2382 " unable to continue.");
2383 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
2384 " please contact Mike Isely <isely@pobox.com>"
2385 " to get it included in the driver\n");
2386 goto fail;
2387 }
2388
Mike Iselyca545f72007-01-20 00:37:11 -03002389 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03002390 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03002391 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03002392 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03002393
2394 init_timer(&hdw->quiescent_timer);
2395 hdw->quiescent_timer.data = (unsigned long)hdw;
2396 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
2397
2398 init_timer(&hdw->encoder_wait_timer);
2399 hdw->encoder_wait_timer.data = (unsigned long)hdw;
2400 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
2401
Mike Iselyd913d632008-04-06 04:04:35 -03002402 init_timer(&hdw->encoder_run_timer);
2403 hdw->encoder_run_timer.data = (unsigned long)hdw;
2404 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
2405
Mike Isely681c7392007-11-26 01:48:52 -03002406 hdw->master_state = PVR2_STATE_DEAD;
2407
2408 init_waitqueue_head(&hdw->state_wait_data);
2409
Mike Isely18103c572007-01-20 00:09:47 -03002410 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03002411 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03002412
Mike Isely7fb20fa2008-04-22 14:45:37 -03002413 /* Calculate which inputs are OK */
2414 m = 0;
2415 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03002416 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
2417 m |= 1 << PVR2_CVAL_INPUT_DTV;
2418 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03002419 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
2420 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
2421 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
2422 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03002423 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03002424
Mike Isely62433e32008-04-22 14:45:40 -03002425 /* If not a hybrid device, pathway_state never changes. So
2426 initialize it here to what it should forever be. */
2427 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
2428 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
2429 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
2430 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
2431 }
2432
Mike Iselyc05c0462006-06-25 20:04:25 -03002433 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03002434 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03002435 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03002436 GFP_KERNEL);
2437 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03002438 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03002439 for (idx = 0; idx < hdw->control_cnt; idx++) {
2440 cptr = hdw->controls + idx;
2441 cptr->hdw = hdw;
2442 }
Mike Iselyd8554972006-06-26 20:58:46 -03002443 for (idx = 0; idx < 32; idx++) {
2444 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
2445 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002446 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002447 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03002448 cptr->info = control_defs+idx;
2449 }
Mike Iselydbc40a02008-04-22 14:45:39 -03002450
2451 /* Ensure that default input choice is a valid one. */
2452 m = hdw->input_avail_mask;
2453 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2454 if (!((1 << idx) & m)) continue;
2455 hdw->input_val = idx;
2456 break;
2457 }
2458
Mike Iselyb30d2442006-06-25 20:05:01 -03002459 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03002460 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03002461 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
2462 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03002463 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2464 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2465 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2466 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2467 ciptr->name = mpeg_ids[idx].strid;
2468 ciptr->v4l_id = mpeg_ids[idx].id;
2469 ciptr->skip_init = !0;
2470 ciptr->get_value = ctrl_cx2341x_get;
2471 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2472 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2473 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2474 qctrl.id = ciptr->v4l_id;
2475 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2476 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2477 ciptr->set_value = ctrl_cx2341x_set;
2478 }
2479 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2480 PVR2_CTLD_INFO_DESC_SIZE);
2481 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2482 ciptr->default_value = qctrl.default_value;
2483 switch (qctrl.type) {
2484 default:
2485 case V4L2_CTRL_TYPE_INTEGER:
2486 ciptr->type = pvr2_ctl_int;
2487 ciptr->def.type_int.min_value = qctrl.minimum;
2488 ciptr->def.type_int.max_value = qctrl.maximum;
2489 break;
2490 case V4L2_CTRL_TYPE_BOOLEAN:
2491 ciptr->type = pvr2_ctl_bool;
2492 break;
2493 case V4L2_CTRL_TYPE_MENU:
2494 ciptr->type = pvr2_ctl_enum;
2495 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002496 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2497 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002498 for (cnt1 = 0;
2499 ciptr->def.type_enum.value_names[cnt1] != NULL;
2500 cnt1++) { }
2501 ciptr->def.type_enum.count = cnt1;
2502 break;
2503 }
2504 cptr->info = ciptr;
2505 }
Mike Iselyd8554972006-06-26 20:58:46 -03002506
2507 // Initialize video standard enum dynamic control
2508 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2509 if (cptr) {
2510 memcpy(&hdw->std_info_enum,cptr->info,
2511 sizeof(hdw->std_info_enum));
2512 cptr->info = &hdw->std_info_enum;
2513
2514 }
2515 // Initialize control data regarding video standard masks
2516 valid_std_mask = pvr2_std_get_usable();
2517 for (idx = 0; idx < 32; idx++) {
2518 if (!(valid_std_mask & (1 << idx))) continue;
2519 cnt1 = pvr2_std_id_to_str(
2520 hdw->std_mask_names[idx],
2521 sizeof(hdw->std_mask_names[idx])-1,
2522 1 << idx);
2523 hdw->std_mask_names[idx][cnt1] = 0;
2524 }
2525 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2526 if (cptr) {
2527 memcpy(&hdw->std_info_avail,cptr->info,
2528 sizeof(hdw->std_info_avail));
2529 cptr->info = &hdw->std_info_avail;
2530 hdw->std_info_avail.def.type_bitmask.bit_names =
2531 hdw->std_mask_ptrs;
2532 hdw->std_info_avail.def.type_bitmask.valid_bits =
2533 valid_std_mask;
2534 }
2535 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2536 if (cptr) {
2537 memcpy(&hdw->std_info_cur,cptr->info,
2538 sizeof(hdw->std_info_cur));
2539 cptr->info = &hdw->std_info_cur;
2540 hdw->std_info_cur.def.type_bitmask.bit_names =
2541 hdw->std_mask_ptrs;
2542 hdw->std_info_avail.def.type_bitmask.valid_bits =
2543 valid_std_mask;
2544 }
2545
Mike Isely432907f2008-08-31 21:02:20 -03002546 hdw->cropcap_stale = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002547 hdw->eeprom_addr = -1;
2548 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002549 hdw->v4l_minor_number_video = -1;
2550 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002551 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002552 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2553 if (!hdw->ctl_write_buffer) goto fail;
2554 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2555 if (!hdw->ctl_read_buffer) goto fail;
2556 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2557 if (!hdw->ctl_write_urb) goto fail;
2558 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2559 if (!hdw->ctl_read_urb) goto fail;
2560
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002561 if (v4l2_device_register(&usb_dev->dev, &hdw->v4l2_dev) != 0) {
2562 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2563 "Error registering with v4l core, giving up");
2564 goto fail;
2565 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002566 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002567 for (idx = 0; idx < PVR_NUM; idx++) {
2568 if (unit_pointers[idx]) continue;
2569 hdw->unit_number = idx;
2570 unit_pointers[idx] = hdw;
2571 break;
2572 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002573 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002574
2575 cnt1 = 0;
2576 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2577 cnt1 += cnt2;
2578 if (hdw->unit_number >= 0) {
2579 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2580 ('a' + hdw->unit_number));
2581 cnt1 += cnt2;
2582 }
2583 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2584 hdw->name[cnt1] = 0;
2585
Mike Isely681c7392007-11-26 01:48:52 -03002586 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2587 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2588 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002589
Mike Iselyd8554972006-06-26 20:58:46 -03002590 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2591 hdw->unit_number,hdw->name);
2592
2593 hdw->tuner_type = -1;
2594 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002595
2596 hdw->usb_intf = intf;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002597 hdw->usb_dev = usb_dev;
Mike Iselyd8554972006-06-26 20:58:46 -03002598
Mike Isely87e34952009-01-23 01:20:24 -03002599 usb_make_path(hdw->usb_dev, hdw->bus_info, sizeof(hdw->bus_info));
Mike Isely31a18542007-04-08 01:11:47 -03002600
Mike Iselyd8554972006-06-26 20:58:46 -03002601 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2602 usb_set_interface(hdw->usb_dev,ifnum,0);
2603
2604 mutex_init(&hdw->ctl_lock_mutex);
2605 mutex_init(&hdw->big_lock_mutex);
2606
2607 return hdw;
2608 fail:
2609 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002610 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002611 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002612 del_timer_sync(&hdw->encoder_wait_timer);
2613 if (hdw->workqueue) {
2614 flush_workqueue(hdw->workqueue);
2615 destroy_workqueue(hdw->workqueue);
2616 hdw->workqueue = NULL;
2617 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002618 usb_free_urb(hdw->ctl_read_urb);
2619 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002620 kfree(hdw->ctl_read_buffer);
2621 kfree(hdw->ctl_write_buffer);
2622 kfree(hdw->controls);
2623 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002624 kfree(hdw->std_defs);
2625 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002626 kfree(hdw);
2627 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002628 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002629}
2630
2631
2632/* Remove _all_ associations between this driver and the underlying USB
2633 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002634static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002635{
2636 if (hdw->flag_disconnected) return;
2637 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2638 if (hdw->ctl_read_urb) {
2639 usb_kill_urb(hdw->ctl_read_urb);
2640 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002641 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002642 }
2643 if (hdw->ctl_write_urb) {
2644 usb_kill_urb(hdw->ctl_write_urb);
2645 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002646 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002647 }
2648 if (hdw->ctl_read_buffer) {
2649 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002650 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002651 }
2652 if (hdw->ctl_write_buffer) {
2653 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002654 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002655 }
Mike Iselyd8554972006-06-26 20:58:46 -03002656 hdw->flag_disconnected = !0;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002657 /* If we don't do this, then there will be a dangling struct device
2658 reference to our disappearing device persisting inside the V4L
2659 core... */
2660 if (hdw->v4l2_dev.dev) {
2661 dev_set_drvdata(hdw->v4l2_dev.dev, NULL);
2662 hdw->v4l2_dev.dev = NULL;
2663 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002664 hdw->usb_dev = NULL;
2665 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002666 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002667}
2668
2669
2670/* Destroy hardware interaction structure */
2671void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2672{
Mike Isely401c27c2007-09-08 22:11:46 -03002673 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002674 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002675 if (hdw->workqueue) {
2676 flush_workqueue(hdw->workqueue);
2677 destroy_workqueue(hdw->workqueue);
2678 hdw->workqueue = NULL;
2679 }
Mike Isely8f591002008-04-22 14:45:45 -03002680 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002681 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002682 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002683 if (hdw->fw_buffer) {
2684 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002685 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002686 }
2687 if (hdw->vid_stream) {
2688 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002689 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002690 }
Mike Iselyd8554972006-06-26 20:58:46 -03002691 if (hdw->decoder_ctrl) {
2692 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2693 }
2694 pvr2_i2c_core_done(hdw);
Mike Isely59af3362009-03-07 03:06:09 -03002695 pvr2_i2c_track_done(hdw);
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002696 v4l2_device_unregister(&hdw->v4l2_dev);
Mike Iselyd8554972006-06-26 20:58:46 -03002697 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002698 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002699 if ((hdw->unit_number >= 0) &&
2700 (hdw->unit_number < PVR_NUM) &&
2701 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002702 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002703 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002704 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002705 kfree(hdw->controls);
2706 kfree(hdw->mpeg_ctrl_info);
2707 kfree(hdw->std_defs);
2708 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002709 kfree(hdw);
2710}
2711
2712
Mike Iselyd8554972006-06-26 20:58:46 -03002713int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2714{
2715 return (hdw && hdw->flag_ok);
2716}
2717
2718
2719/* Called when hardware has been unplugged */
2720void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2721{
2722 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2723 LOCK_TAKE(hdw->big_lock);
2724 LOCK_TAKE(hdw->ctl_lock);
2725 pvr2_hdw_remove_usb_stuff(hdw);
2726 LOCK_GIVE(hdw->ctl_lock);
2727 LOCK_GIVE(hdw->big_lock);
2728}
2729
2730
2731// Attempt to autoselect an appropriate value for std_enum_cur given
2732// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002733static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002734{
2735 unsigned int idx;
2736 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2737 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2738 hdw->std_enum_cur = idx;
2739 return;
2740 }
2741 }
2742 hdw->std_enum_cur = 0;
2743}
2744
2745
2746// Calculate correct set of enumerated standards based on currently known
2747// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002748static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002749{
2750 struct v4l2_standard *newstd;
2751 unsigned int std_cnt;
2752 unsigned int idx;
2753
2754 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2755
2756 if (hdw->std_defs) {
2757 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002758 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002759 }
2760 hdw->std_enum_cnt = 0;
2761 if (hdw->std_enum_names) {
2762 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002763 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002764 }
2765
2766 if (!std_cnt) {
2767 pvr2_trace(
2768 PVR2_TRACE_ERROR_LEGS,
2769 "WARNING: Failed to identify any viable standards");
2770 }
2771 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2772 hdw->std_enum_names[0] = "none";
2773 for (idx = 0; idx < std_cnt; idx++) {
2774 hdw->std_enum_names[idx+1] =
2775 newstd[idx].name;
2776 }
2777 // Set up the dynamic control for this standard
2778 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2779 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2780 hdw->std_defs = newstd;
2781 hdw->std_enum_cnt = std_cnt+1;
2782 hdw->std_enum_cur = 0;
2783 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2784}
2785
2786
2787int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2788 struct v4l2_standard *std,
2789 unsigned int idx)
2790{
2791 int ret = -EINVAL;
2792 if (!idx) return ret;
2793 LOCK_TAKE(hdw->big_lock); do {
2794 if (idx >= hdw->std_enum_cnt) break;
2795 idx--;
2796 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2797 ret = 0;
2798 } while (0); LOCK_GIVE(hdw->big_lock);
2799 return ret;
2800}
2801
2802
2803/* Get the number of defined controls */
2804unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2805{
Mike Iselyc05c0462006-06-25 20:04:25 -03002806 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002807}
2808
2809
2810/* Retrieve a control handle given its index (0..count-1) */
2811struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2812 unsigned int idx)
2813{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002814 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002815 return hdw->controls + idx;
2816}
2817
2818
2819/* Retrieve a control handle given its index (0..count-1) */
2820struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2821 unsigned int ctl_id)
2822{
2823 struct pvr2_ctrl *cptr;
2824 unsigned int idx;
2825 int i;
2826
2827 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002828 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002829 cptr = hdw->controls + idx;
2830 i = cptr->info->internal_id;
2831 if (i && (i == ctl_id)) return cptr;
2832 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002833 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002834}
2835
2836
Mike Iselya761f432006-06-25 20:04:44 -03002837/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002838struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2839{
2840 struct pvr2_ctrl *cptr;
2841 unsigned int idx;
2842 int i;
2843
2844 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002845 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002846 cptr = hdw->controls + idx;
2847 i = cptr->info->v4l_id;
2848 if (i && (i == ctl_id)) return cptr;
2849 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002850 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002851}
2852
2853
Mike Iselya761f432006-06-25 20:04:44 -03002854/* Given a V4L ID for its immediate predecessor, retrieve the control
2855 structure associated with it. */
2856struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2857 unsigned int ctl_id)
2858{
2859 struct pvr2_ctrl *cptr,*cp2;
2860 unsigned int idx;
2861 int i;
2862
2863 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002864 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002865 for (idx = 0; idx < hdw->control_cnt; idx++) {
2866 cptr = hdw->controls + idx;
2867 i = cptr->info->v4l_id;
2868 if (!i) continue;
2869 if (i <= ctl_id) continue;
2870 if (cp2 && (cp2->info->v4l_id < i)) continue;
2871 cp2 = cptr;
2872 }
2873 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002874 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002875}
2876
2877
Mike Iselyd8554972006-06-26 20:58:46 -03002878static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2879{
2880 switch (tp) {
2881 case pvr2_ctl_int: return "integer";
2882 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002883 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002884 case pvr2_ctl_bitmask: return "bitmask";
2885 }
2886 return "";
2887}
2888
2889
Mike Isely2641df32009-03-07 00:13:25 -03002890static void pvr2_subdev_set_control(struct pvr2_hdw *hdw, int id,
2891 const char *name, int val)
2892{
2893 struct v4l2_control ctrl;
2894 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 %s=%d", name, val);
2895 memset(&ctrl, 0, sizeof(ctrl));
2896 ctrl.id = id;
2897 ctrl.value = val;
2898 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, s_ctrl, &ctrl);
2899}
2900
2901#define PVR2_SUBDEV_SET_CONTROL(hdw, id, lab) \
2902 if ((hdw)->lab##_dirty) { \
2903 pvr2_subdev_set_control(hdw, id, #lab, (hdw)->lab##_val); \
2904 }
2905
Mike Isely5ceaad12009-03-07 00:01:20 -03002906/* Execute whatever commands are required to update the state of all the
Mike Isely2641df32009-03-07 00:13:25 -03002907 sub-devices so that they match our current control values. */
Mike Isely5ceaad12009-03-07 00:01:20 -03002908static void pvr2_subdev_update(struct pvr2_hdw *hdw)
2909{
Mike Iselyedb9dcb2009-03-07 00:37:10 -03002910 struct v4l2_subdev *sd;
2911 unsigned int id;
2912 pvr2_subdev_update_func fp;
2913
Mike Isely2641df32009-03-07 00:13:25 -03002914 if (hdw->input_dirty || hdw->std_dirty) {
2915 pvr2_trace(PVR2_TRACE_CHIPS,"subdev v4l2 set_standard");
2916 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2917 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2918 tuner, s_radio);
2919 } else {
2920 v4l2_std_id vs;
2921 vs = hdw->std_mask_cur;
2922 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2923 tuner, s_std, vs);
2924 }
2925 hdw->tuner_signal_stale = !0;
2926 hdw->cropcap_stale = !0;
2927 }
2928
2929 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_BRIGHTNESS, brightness);
2930 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_CONTRAST, contrast);
2931 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_SATURATION, saturation);
2932 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_HUE, hue);
2933 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_MUTE, mute);
2934 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_VOLUME, volume);
2935 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BALANCE, balance);
2936 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BASS, bass);
2937 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_TREBLE, treble);
2938
2939 if (hdw->input_dirty || hdw->audiomode_dirty) {
2940 struct v4l2_tuner vt;
2941 memset(&vt, 0, sizeof(vt));
2942 vt.audmode = hdw->audiomode_val;
2943 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, s_tuner, &vt);
2944 }
2945
2946 if (hdw->freqDirty) {
2947 unsigned long fv;
2948 struct v4l2_frequency freq;
2949 fv = pvr2_hdw_get_cur_freq(hdw);
2950 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_freq(%lu)", fv);
2951 if (hdw->tuner_signal_stale) pvr2_hdw_status_poll(hdw);
2952 memset(&freq, 0, sizeof(freq));
2953 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
2954 /* ((fv * 1000) / 62500) */
2955 freq.frequency = (fv * 2) / 125;
2956 } else {
2957 freq.frequency = fv / 62500;
2958 }
2959 /* tuner-core currently doesn't seem to care about this, but
2960 let's set it anyway for completeness. */
2961 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2962 freq.type = V4L2_TUNER_RADIO;
2963 } else {
2964 freq.type = V4L2_TUNER_ANALOG_TV;
2965 }
2966 freq.tuner = 0;
2967 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner,
2968 s_frequency, &freq);
2969 }
2970
2971 if (hdw->res_hor_dirty || hdw->res_ver_dirty) {
2972 struct v4l2_format fmt;
2973 memset(&fmt, 0, sizeof(fmt));
2974 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2975 fmt.fmt.pix.width = hdw->res_hor_val;
2976 fmt.fmt.pix.height = hdw->res_ver_val;
2977 pvr2_trace(PVR2_TRACE_CHIPS,"subdev v4l2 set_size(%dx%d)",
2978 fmt.fmt.pix.width, fmt.fmt.pix.height);
2979 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_fmt, &fmt);
2980 }
2981
2982 /* Unable to set crop parameters; there is apparently no equivalent
2983 for VIDIOC_S_CROP */
2984
Mike Iselyedb9dcb2009-03-07 00:37:10 -03002985 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
2986 id = sd->grp_id;
2987 if (id >= ARRAY_SIZE(pvr2_module_update_functions)) continue;
2988 fp = pvr2_module_update_functions[id];
2989 if (!fp) continue;
2990 (*fp)(hdw, sd);
2991 }
Mike Isely2641df32009-03-07 00:13:25 -03002992
2993 if (hdw->tuner_signal_stale && hdw->cropcap_stale) {
2994 pvr2_hdw_status_poll(hdw);
2995 }
Mike Isely5ceaad12009-03-07 00:01:20 -03002996}
2997
2998
Mike Isely681c7392007-11-26 01:48:52 -03002999/* Figure out if we need to commit control changes. If so, mark internal
3000 state flags to indicate this fact and return true. Otherwise do nothing
3001 else and return false. */
3002static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003003{
Mike Iselyd8554972006-06-26 20:58:46 -03003004 unsigned int idx;
3005 struct pvr2_ctrl *cptr;
3006 int value;
3007 int commit_flag = 0;
3008 char buf[100];
3009 unsigned int bcnt,ccnt;
3010
Mike Iselyc05c0462006-06-25 20:04:25 -03003011 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03003012 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00003013 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003014 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03003015 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003016
Mike Iselyfe23a282007-01-20 00:10:55 -03003017 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003018 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
3019 cptr->info->name);
3020 value = 0;
3021 cptr->info->get_value(cptr,&value);
3022 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
3023 buf+bcnt,
3024 sizeof(buf)-bcnt,&ccnt);
3025 bcnt += ccnt;
3026 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
3027 get_ctrl_typename(cptr->info->type));
3028 pvr2_trace(PVR2_TRACE_CTL,
3029 "/*--TRACE_COMMIT--*/ %.*s",
3030 bcnt,buf);
3031 }
3032
3033 if (!commit_flag) {
3034 /* Nothing has changed */
3035 return 0;
3036 }
3037
Mike Isely681c7392007-11-26 01:48:52 -03003038 hdw->state_pipeline_config = 0;
3039 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3040 pvr2_hdw_state_sched(hdw);
3041
3042 return !0;
3043}
3044
3045
3046/* Perform all operations needed to commit all control changes. This must
3047 be performed in synchronization with the pipeline state and is thus
3048 expected to be called as part of the driver's worker thread. Return
3049 true if commit successful, otherwise return false to indicate that
3050 commit isn't possible at this time. */
3051static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
3052{
3053 unsigned int idx;
3054 struct pvr2_ctrl *cptr;
3055 int disruptive_change;
3056
Mike Iselyab062fe2008-06-30 03:32:35 -03003057 /* Handle some required side effects when the video standard is
3058 changed.... */
Mike Iselyd8554972006-06-26 20:58:46 -03003059 if (hdw->std_dirty) {
Mike Iselyd8554972006-06-26 20:58:46 -03003060 int nvres;
Mike Isely00528d92008-06-30 03:35:52 -03003061 int gop_size;
Mike Iselyd8554972006-06-26 20:58:46 -03003062 if (hdw->std_mask_cur & V4L2_STD_525_60) {
3063 nvres = 480;
Mike Isely00528d92008-06-30 03:35:52 -03003064 gop_size = 15;
Mike Iselyd8554972006-06-26 20:58:46 -03003065 } else {
3066 nvres = 576;
Mike Isely00528d92008-06-30 03:35:52 -03003067 gop_size = 12;
Mike Iselyd8554972006-06-26 20:58:46 -03003068 }
Mike Isely00528d92008-06-30 03:35:52 -03003069 /* Rewrite the vertical resolution to be appropriate to the
3070 video standard that has been selected. */
Mike Iselyd8554972006-06-26 20:58:46 -03003071 if (nvres != hdw->res_ver_val) {
3072 hdw->res_ver_val = nvres;
3073 hdw->res_ver_dirty = !0;
3074 }
Mike Isely00528d92008-06-30 03:35:52 -03003075 /* Rewrite the GOP size to be appropriate to the video
3076 standard that has been selected. */
3077 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
3078 struct v4l2_ext_controls cs;
3079 struct v4l2_ext_control c1;
3080 memset(&cs, 0, sizeof(cs));
3081 memset(&c1, 0, sizeof(c1));
3082 cs.controls = &c1;
3083 cs.count = 1;
3084 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
3085 c1.value = gop_size;
3086 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
3087 VIDIOC_S_EXT_CTRLS);
3088 }
Mike Iselyd8554972006-06-26 20:58:46 -03003089 }
3090
Mike Isely38d9a2c2008-03-28 05:30:48 -03003091 if (hdw->input_dirty && hdw->state_pathway_ok &&
Mike Isely62433e32008-04-22 14:45:40 -03003092 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
3093 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
3094 hdw->pathway_state)) {
3095 /* Change of mode being asked for... */
3096 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003097 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003098 }
3099 if (!hdw->state_pathway_ok) {
3100 /* Can't commit anything until pathway is ok. */
3101 return 0;
3102 }
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03003103 /* The broadcast decoder can only scale down, so if
3104 * res_*_dirty && crop window < output format ==> enlarge crop.
3105 *
3106 * The mpeg encoder receives fields of res_hor_val dots and
3107 * res_ver_val halflines. Limits: hor<=720, ver<=576.
3108 */
3109 if (hdw->res_hor_dirty && hdw->cropw_val < hdw->res_hor_val) {
3110 hdw->cropw_val = hdw->res_hor_val;
3111 hdw->cropw_dirty = !0;
3112 } else if (hdw->cropw_dirty) {
3113 hdw->res_hor_dirty = !0; /* must rescale */
3114 hdw->res_hor_val = min(720, hdw->cropw_val);
3115 }
3116 if (hdw->res_ver_dirty && hdw->croph_val < hdw->res_ver_val) {
3117 hdw->croph_val = hdw->res_ver_val;
3118 hdw->croph_dirty = !0;
3119 } else if (hdw->croph_dirty) {
3120 int nvres = hdw->std_mask_cur & V4L2_STD_525_60 ? 480 : 576;
3121 hdw->res_ver_dirty = !0;
3122 hdw->res_ver_val = min(nvres, hdw->croph_val);
3123 }
3124
Mike Isely681c7392007-11-26 01:48:52 -03003125 /* If any of the below has changed, then we can't do the update
3126 while the pipeline is running. Pipeline must be paused first
3127 and decoder -> encoder connection be made quiescent before we
3128 can proceed. */
3129 disruptive_change =
3130 (hdw->std_dirty ||
3131 hdw->enc_unsafe_stale ||
3132 hdw->srate_dirty ||
3133 hdw->res_ver_dirty ||
3134 hdw->res_hor_dirty ||
Mike Isely755879c2008-08-31 20:50:59 -03003135 hdw->cropw_dirty ||
3136 hdw->croph_dirty ||
Mike Isely681c7392007-11-26 01:48:52 -03003137 hdw->input_dirty ||
3138 (hdw->active_stream_type != hdw->desired_stream_type));
3139 if (disruptive_change && !hdw->state_pipeline_idle) {
3140 /* Pipeline is not idle; we can't proceed. Arrange to
3141 cause pipeline to stop so that we can try this again
3142 later.... */
3143 hdw->state_pipeline_pause = !0;
3144 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003145 }
3146
Mike Iselyb30d2442006-06-25 20:05:01 -03003147 if (hdw->srate_dirty) {
3148 /* Write new sample rate into control structure since
3149 * the master copy is stale. We must track srate
3150 * separate from the mpeg control structure because
3151 * other logic also uses this value. */
3152 struct v4l2_ext_controls cs;
3153 struct v4l2_ext_control c1;
3154 memset(&cs,0,sizeof(cs));
3155 memset(&c1,0,sizeof(c1));
3156 cs.controls = &c1;
3157 cs.count = 1;
3158 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
3159 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03003160 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03003161 }
Mike Iselyc05c0462006-06-25 20:04:25 -03003162
Mike Iselyd8554972006-06-26 20:58:46 -03003163 /* Scan i2c core at this point - before we clear all the dirty
3164 bits. Various parts of the i2c core will notice dirty bits as
3165 appropriate and arrange to broadcast or directly send updates to
3166 the client drivers in order to keep everything in sync */
3167 pvr2_i2c_core_check_stale(hdw);
3168
Mike Isely681c7392007-11-26 01:48:52 -03003169 if (hdw->active_stream_type != hdw->desired_stream_type) {
3170 /* Handle any side effects of stream config here */
3171 hdw->active_stream_type = hdw->desired_stream_type;
3172 }
3173
Mike Isely1df59f02008-04-21 03:50:39 -03003174 if (hdw->hdw_desc->signal_routing_scheme ==
3175 PVR2_ROUTING_SCHEME_GOTVIEW) {
3176 u32 b;
3177 /* Handle GOTVIEW audio switching */
3178 pvr2_hdw_gpio_get_out(hdw,&b);
3179 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3180 /* Set GPIO 11 */
3181 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
3182 } else {
3183 /* Clear GPIO 11 */
3184 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
3185 }
3186 }
3187
Mike Isely5ceaad12009-03-07 00:01:20 -03003188 for (idx = 0; idx < hdw->control_cnt; idx++) {
3189 cptr = hdw->controls + idx;
3190 if (!cptr->info->clear_dirty) continue;
3191 cptr->info->clear_dirty(cptr);
3192 }
3193
3194 /* Check and update state for all sub-devices. */
3195 pvr2_subdev_update(hdw);
3196
Mike Iselyd8554972006-06-26 20:58:46 -03003197 /* Now execute i2c core update */
3198 pvr2_i2c_core_sync(hdw);
3199
Mike Isely62433e32008-04-22 14:45:40 -03003200 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
3201 hdw->state_encoder_run) {
3202 /* If encoder isn't running or it can't be touched, then
3203 this will get worked out later when we start the
3204 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03003205 if (pvr2_encoder_adjust(hdw) < 0) return !0;
3206 }
Mike Iselyd8554972006-06-26 20:58:46 -03003207
Mike Isely681c7392007-11-26 01:48:52 -03003208 hdw->state_pipeline_config = !0;
Mike Isely432907f2008-08-31 21:02:20 -03003209 /* Hardware state may have changed in a way to cause the cropping
3210 capabilities to have changed. So mark it stale, which will
3211 cause a later re-fetch. */
Mike Isely681c7392007-11-26 01:48:52 -03003212 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3213 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003214}
3215
3216
3217int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
3218{
Mike Isely681c7392007-11-26 01:48:52 -03003219 int fl;
3220 LOCK_TAKE(hdw->big_lock);
3221 fl = pvr2_hdw_commit_setup(hdw);
3222 LOCK_GIVE(hdw->big_lock);
3223 if (!fl) return 0;
3224 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003225}
3226
3227
Mike Isely681c7392007-11-26 01:48:52 -03003228static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003229{
Mike Isely681c7392007-11-26 01:48:52 -03003230 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03003231 LOCK_TAKE(hdw->big_lock); do {
3232 pvr2_i2c_core_sync(hdw);
3233 } while (0); LOCK_GIVE(hdw->big_lock);
3234}
3235
3236
Mike Isely681c7392007-11-26 01:48:52 -03003237static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003238{
Mike Isely681c7392007-11-26 01:48:52 -03003239 int fl = 0;
3240 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03003241 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03003242 fl = pvr2_hdw_state_eval(hdw);
3243 } while (0); LOCK_GIVE(hdw->big_lock);
3244 if (fl && hdw->state_func) {
3245 hdw->state_func(hdw->state_data);
3246 }
3247}
3248
3249
Mike Isely681c7392007-11-26 01:48:52 -03003250static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03003251{
Mike Isely681c7392007-11-26 01:48:52 -03003252 return wait_event_interruptible(
3253 hdw->state_wait_data,
3254 (hdw->state_stale == 0) &&
3255 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03003256}
3257
Mike Isely681c7392007-11-26 01:48:52 -03003258
Mike Iselyd8554972006-06-26 20:58:46 -03003259/* Return name for this driver instance */
3260const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
3261{
3262 return hdw->name;
3263}
3264
3265
Mike Isely78a47102007-11-26 01:58:20 -03003266const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
3267{
3268 return hdw->hdw_desc->description;
3269}
3270
3271
3272const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
3273{
3274 return hdw->hdw_desc->shortname;
3275}
3276
3277
Mike Iselyd8554972006-06-26 20:58:46 -03003278int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
3279{
3280 int result;
3281 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03003282 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03003283 result = pvr2_send_request(hdw,
3284 hdw->cmd_buffer,1,
3285 hdw->cmd_buffer,1);
3286 if (result < 0) break;
3287 result = (hdw->cmd_buffer[0] != 0);
3288 } while(0); LOCK_GIVE(hdw->ctl_lock);
3289 return result;
3290}
3291
3292
Mike Isely18103c572007-01-20 00:09:47 -03003293/* Execute poll of tuner status */
3294void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003295{
Mike Iselyd8554972006-06-26 20:58:46 -03003296 LOCK_TAKE(hdw->big_lock); do {
Mike Iselya51f5002009-03-06 23:30:37 -03003297 pvr2_hdw_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003298 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03003299}
3300
3301
Mike Isely432907f2008-08-31 21:02:20 -03003302static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw)
3303{
3304 if (!hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003305 return 0;
3306 }
Mike Iselya51f5002009-03-06 23:30:37 -03003307 pvr2_hdw_status_poll(hdw);
Mike Isely432907f2008-08-31 21:02:20 -03003308 if (hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003309 return -EIO;
3310 }
3311 return 0;
3312}
3313
3314
3315/* Return information about cropping capabilities */
3316int pvr2_hdw_get_cropcap(struct pvr2_hdw *hdw, struct v4l2_cropcap *pp)
3317{
3318 int stat = 0;
3319 LOCK_TAKE(hdw->big_lock);
3320 stat = pvr2_hdw_check_cropcap(hdw);
3321 if (!stat) {
Mike Isely432907f2008-08-31 21:02:20 -03003322 memcpy(pp, &hdw->cropcap_info, sizeof(hdw->cropcap_info));
3323 }
3324 LOCK_GIVE(hdw->big_lock);
3325 return stat;
3326}
3327
3328
Mike Isely18103c572007-01-20 00:09:47 -03003329/* Return information about the tuner */
3330int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
3331{
3332 LOCK_TAKE(hdw->big_lock); do {
3333 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -03003334 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -03003335 }
3336 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
3337 } while (0); LOCK_GIVE(hdw->big_lock);
3338 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003339}
3340
3341
3342/* Get handle to video output stream */
3343struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
3344{
3345 return hp->vid_stream;
3346}
3347
3348
3349void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
3350{
Mike Isely4f1a3e52006-06-25 20:04:31 -03003351 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003352 LOCK_TAKE(hdw->big_lock); do {
Mike Isely4f1a3e52006-06-25 20:04:31 -03003353 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Isely5ceaad12009-03-07 00:01:20 -03003354 hdw->log_requested = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003355 pvr2_i2c_core_check_stale(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003356 pvr2_i2c_core_sync(hdw);
Mike Iselya51f5002009-03-06 23:30:37 -03003357 hdw->log_requested = 0;
Mike Iselyed3261a2009-03-07 00:02:33 -03003358 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, log_status);
Mike Iselyb30d2442006-06-25 20:05:01 -03003359 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03003360 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03003361 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03003362 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003363 } while (0); LOCK_GIVE(hdw->big_lock);
3364}
3365
Mike Isely4db666c2007-09-08 22:16:27 -03003366
3367/* Grab EEPROM contents, needed for direct method. */
3368#define EEPROM_SIZE 8192
3369#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
3370static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
3371{
3372 struct i2c_msg msg[2];
3373 u8 *eeprom;
3374 u8 iadd[2];
3375 u8 addr;
3376 u16 eepromSize;
3377 unsigned int offs;
3378 int ret;
3379 int mode16 = 0;
3380 unsigned pcnt,tcnt;
3381 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
3382 if (!eeprom) {
3383 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3384 "Failed to allocate memory"
3385 " required to read eeprom");
3386 return NULL;
3387 }
3388
3389 trace_eeprom("Value for eeprom addr from controller was 0x%x",
3390 hdw->eeprom_addr);
3391 addr = hdw->eeprom_addr;
3392 /* Seems that if the high bit is set, then the *real* eeprom
3393 address is shifted right now bit position (noticed this in
3394 newer PVR USB2 hardware) */
3395 if (addr & 0x80) addr >>= 1;
3396
3397 /* FX2 documentation states that a 16bit-addressed eeprom is
3398 expected if the I2C address is an odd number (yeah, this is
3399 strange but it's what they do) */
3400 mode16 = (addr & 1);
3401 eepromSize = (mode16 ? EEPROM_SIZE : 256);
3402 trace_eeprom("Examining %d byte eeprom at location 0x%x"
3403 " using %d bit addressing",eepromSize,addr,
3404 mode16 ? 16 : 8);
3405
3406 msg[0].addr = addr;
3407 msg[0].flags = 0;
3408 msg[0].len = mode16 ? 2 : 1;
3409 msg[0].buf = iadd;
3410 msg[1].addr = addr;
3411 msg[1].flags = I2C_M_RD;
3412
3413 /* We have to do the actual eeprom data fetch ourselves, because
3414 (1) we're only fetching part of the eeprom, and (2) if we were
3415 getting the whole thing our I2C driver can't grab it in one
3416 pass - which is what tveeprom is otherwise going to attempt */
3417 memset(eeprom,0,EEPROM_SIZE);
3418 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
3419 pcnt = 16;
3420 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
3421 offs = tcnt + (eepromSize - EEPROM_SIZE);
3422 if (mode16) {
3423 iadd[0] = offs >> 8;
3424 iadd[1] = offs;
3425 } else {
3426 iadd[0] = offs;
3427 }
3428 msg[1].len = pcnt;
3429 msg[1].buf = eeprom+tcnt;
3430 if ((ret = i2c_transfer(&hdw->i2c_adap,
3431 msg,ARRAY_SIZE(msg))) != 2) {
3432 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3433 "eeprom fetch set offs err=%d",ret);
3434 kfree(eeprom);
3435 return NULL;
3436 }
3437 }
3438 return eeprom;
3439}
3440
3441
3442void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
3443 int prom_flag,
3444 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003445{
3446 int ret;
3447 u16 address;
3448 unsigned int pipe;
3449 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00003450 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03003451
3452 if (!enable_flag) {
3453 pvr2_trace(PVR2_TRACE_FIRMWARE,
3454 "Cleaning up after CPU firmware fetch");
3455 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003456 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003457 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03003458 if (hdw->fw_cpu_flag) {
3459 /* Now release the CPU. It will disconnect
3460 and reconnect later. */
3461 pvr2_hdw_cpureset_assert(hdw,0);
3462 }
Mike Iselyd8554972006-06-26 20:58:46 -03003463 break;
3464 }
3465
Mike Isely4db666c2007-09-08 22:16:27 -03003466 hdw->fw_cpu_flag = (prom_flag == 0);
3467 if (hdw->fw_cpu_flag) {
3468 pvr2_trace(PVR2_TRACE_FIRMWARE,
3469 "Preparing to suck out CPU firmware");
3470 hdw->fw_size = 0x2000;
3471 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
3472 if (!hdw->fw_buffer) {
3473 hdw->fw_size = 0;
3474 break;
3475 }
3476
3477 /* We have to hold the CPU during firmware upload. */
3478 pvr2_hdw_cpureset_assert(hdw,1);
3479
3480 /* download the firmware from address 0000-1fff in 2048
3481 (=0x800) bytes chunk. */
3482
3483 pvr2_trace(PVR2_TRACE_FIRMWARE,
3484 "Grabbing CPU firmware");
3485 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
3486 for(address = 0; address < hdw->fw_size;
3487 address += 0x800) {
3488 ret = usb_control_msg(hdw->usb_dev,pipe,
3489 0xa0,0xc0,
3490 address,0,
3491 hdw->fw_buffer+address,
3492 0x800,HZ);
3493 if (ret < 0) break;
3494 }
3495
3496 pvr2_trace(PVR2_TRACE_FIRMWARE,
3497 "Done grabbing CPU firmware");
3498 } else {
3499 pvr2_trace(PVR2_TRACE_FIRMWARE,
3500 "Sucking down EEPROM contents");
3501 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
3502 if (!hdw->fw_buffer) {
3503 pvr2_trace(PVR2_TRACE_FIRMWARE,
3504 "EEPROM content suck failed.");
3505 break;
3506 }
3507 hdw->fw_size = EEPROM_SIZE;
3508 pvr2_trace(PVR2_TRACE_FIRMWARE,
3509 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03003510 }
3511
Mike Iselyd8554972006-06-26 20:58:46 -03003512 } while (0); LOCK_GIVE(hdw->big_lock);
3513}
3514
3515
3516/* Return true if we're in a mode for retrieval CPU firmware */
3517int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
3518{
Al Viro5fa12472008-03-29 03:07:38 +00003519 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003520}
3521
3522
3523int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
3524 char *buf,unsigned int cnt)
3525{
3526 int ret = -EINVAL;
3527 LOCK_TAKE(hdw->big_lock); do {
3528 if (!buf) break;
3529 if (!cnt) break;
3530
3531 if (!hdw->fw_buffer) {
3532 ret = -EIO;
3533 break;
3534 }
3535
3536 if (offs >= hdw->fw_size) {
3537 pvr2_trace(PVR2_TRACE_FIRMWARE,
3538 "Read firmware data offs=%d EOF",
3539 offs);
3540 ret = 0;
3541 break;
3542 }
3543
3544 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
3545
3546 memcpy(buf,hdw->fw_buffer+offs,cnt);
3547
3548 pvr2_trace(PVR2_TRACE_FIRMWARE,
3549 "Read firmware data offs=%d cnt=%d",
3550 offs,cnt);
3551 ret = cnt;
3552 } while (0); LOCK_GIVE(hdw->big_lock);
3553
3554 return ret;
3555}
3556
3557
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003558int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003559 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03003560{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003561 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003562 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
3563 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
3564 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003565 default: return -1;
3566 }
Mike Iselyd8554972006-06-26 20:58:46 -03003567}
3568
3569
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03003570/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003571void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003572 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03003573{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003574 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003575 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
3576 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
3577 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003578 default: break;
3579 }
Mike Iselyd8554972006-06-26 20:58:46 -03003580}
3581
3582
David Howells7d12e782006-10-05 14:55:46 +01003583static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003584{
3585 struct pvr2_hdw *hdw = urb->context;
3586 hdw->ctl_write_pend_flag = 0;
3587 if (hdw->ctl_read_pend_flag) return;
3588 complete(&hdw->ctl_done);
3589}
3590
3591
David Howells7d12e782006-10-05 14:55:46 +01003592static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003593{
3594 struct pvr2_hdw *hdw = urb->context;
3595 hdw->ctl_read_pend_flag = 0;
3596 if (hdw->ctl_write_pend_flag) return;
3597 complete(&hdw->ctl_done);
3598}
3599
3600
3601static void pvr2_ctl_timeout(unsigned long data)
3602{
3603 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3604 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3605 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003606 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003607 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003608 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003609 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03003610 }
3611}
3612
3613
Mike Iselye61b6fc2006-07-18 22:42:18 -03003614/* Issue a command and get a response from the device. This extended
3615 version includes a probe flag (which if set means that device errors
3616 should not be logged or treated as fatal) and a timeout in jiffies.
3617 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003618static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3619 unsigned int timeout,int probe_fl,
3620 void *write_data,unsigned int write_len,
3621 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03003622{
3623 unsigned int idx;
3624 int status = 0;
3625 struct timer_list timer;
3626 if (!hdw->ctl_lock_held) {
3627 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3628 "Attempted to execute control transfer"
3629 " without lock!!");
3630 return -EDEADLK;
3631 }
Mike Isely681c7392007-11-26 01:48:52 -03003632 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03003633 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3634 "Attempted to execute control transfer"
3635 " when device not ok");
3636 return -EIO;
3637 }
3638 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3639 if (!probe_fl) {
3640 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3641 "Attempted to execute control transfer"
3642 " when USB is disconnected");
3643 }
3644 return -ENOTTY;
3645 }
3646
3647 /* Ensure that we have sane parameters */
3648 if (!write_data) write_len = 0;
3649 if (!read_data) read_len = 0;
3650 if (write_len > PVR2_CTL_BUFFSIZE) {
3651 pvr2_trace(
3652 PVR2_TRACE_ERROR_LEGS,
3653 "Attempted to execute %d byte"
3654 " control-write transfer (limit=%d)",
3655 write_len,PVR2_CTL_BUFFSIZE);
3656 return -EINVAL;
3657 }
3658 if (read_len > PVR2_CTL_BUFFSIZE) {
3659 pvr2_trace(
3660 PVR2_TRACE_ERROR_LEGS,
3661 "Attempted to execute %d byte"
3662 " control-read transfer (limit=%d)",
3663 write_len,PVR2_CTL_BUFFSIZE);
3664 return -EINVAL;
3665 }
3666 if ((!write_len) && (!read_len)) {
3667 pvr2_trace(
3668 PVR2_TRACE_ERROR_LEGS,
3669 "Attempted to execute null control transfer?");
3670 return -EINVAL;
3671 }
3672
3673
3674 hdw->cmd_debug_state = 1;
3675 if (write_len) {
3676 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3677 } else {
3678 hdw->cmd_debug_code = 0;
3679 }
3680 hdw->cmd_debug_write_len = write_len;
3681 hdw->cmd_debug_read_len = read_len;
3682
3683 /* Initialize common stuff */
3684 init_completion(&hdw->ctl_done);
3685 hdw->ctl_timeout_flag = 0;
3686 hdw->ctl_write_pend_flag = 0;
3687 hdw->ctl_read_pend_flag = 0;
3688 init_timer(&timer);
3689 timer.expires = jiffies + timeout;
3690 timer.data = (unsigned long)hdw;
3691 timer.function = pvr2_ctl_timeout;
3692
3693 if (write_len) {
3694 hdw->cmd_debug_state = 2;
3695 /* Transfer write data to internal buffer */
3696 for (idx = 0; idx < write_len; idx++) {
3697 hdw->ctl_write_buffer[idx] =
3698 ((unsigned char *)write_data)[idx];
3699 }
3700 /* Initiate a write request */
3701 usb_fill_bulk_urb(hdw->ctl_write_urb,
3702 hdw->usb_dev,
3703 usb_sndbulkpipe(hdw->usb_dev,
3704 PVR2_CTL_WRITE_ENDPOINT),
3705 hdw->ctl_write_buffer,
3706 write_len,
3707 pvr2_ctl_write_complete,
3708 hdw);
3709 hdw->ctl_write_urb->actual_length = 0;
3710 hdw->ctl_write_pend_flag = !0;
3711 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3712 if (status < 0) {
3713 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3714 "Failed to submit write-control"
3715 " URB status=%d",status);
3716 hdw->ctl_write_pend_flag = 0;
3717 goto done;
3718 }
3719 }
3720
3721 if (read_len) {
3722 hdw->cmd_debug_state = 3;
3723 memset(hdw->ctl_read_buffer,0x43,read_len);
3724 /* Initiate a read request */
3725 usb_fill_bulk_urb(hdw->ctl_read_urb,
3726 hdw->usb_dev,
3727 usb_rcvbulkpipe(hdw->usb_dev,
3728 PVR2_CTL_READ_ENDPOINT),
3729 hdw->ctl_read_buffer,
3730 read_len,
3731 pvr2_ctl_read_complete,
3732 hdw);
3733 hdw->ctl_read_urb->actual_length = 0;
3734 hdw->ctl_read_pend_flag = !0;
3735 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3736 if (status < 0) {
3737 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3738 "Failed to submit read-control"
3739 " URB status=%d",status);
3740 hdw->ctl_read_pend_flag = 0;
3741 goto done;
3742 }
3743 }
3744
3745 /* Start timer */
3746 add_timer(&timer);
3747
3748 /* Now wait for all I/O to complete */
3749 hdw->cmd_debug_state = 4;
3750 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3751 wait_for_completion(&hdw->ctl_done);
3752 }
3753 hdw->cmd_debug_state = 5;
3754
3755 /* Stop timer */
3756 del_timer_sync(&timer);
3757
3758 hdw->cmd_debug_state = 6;
3759 status = 0;
3760
3761 if (hdw->ctl_timeout_flag) {
3762 status = -ETIMEDOUT;
3763 if (!probe_fl) {
3764 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3765 "Timed out control-write");
3766 }
3767 goto done;
3768 }
3769
3770 if (write_len) {
3771 /* Validate results of write request */
3772 if ((hdw->ctl_write_urb->status != 0) &&
3773 (hdw->ctl_write_urb->status != -ENOENT) &&
3774 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3775 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3776 /* USB subsystem is reporting some kind of failure
3777 on the write */
3778 status = hdw->ctl_write_urb->status;
3779 if (!probe_fl) {
3780 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3781 "control-write URB failure,"
3782 " status=%d",
3783 status);
3784 }
3785 goto done;
3786 }
3787 if (hdw->ctl_write_urb->actual_length < write_len) {
3788 /* Failed to write enough data */
3789 status = -EIO;
3790 if (!probe_fl) {
3791 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3792 "control-write URB short,"
3793 " expected=%d got=%d",
3794 write_len,
3795 hdw->ctl_write_urb->actual_length);
3796 }
3797 goto done;
3798 }
3799 }
3800 if (read_len) {
3801 /* Validate results of read request */
3802 if ((hdw->ctl_read_urb->status != 0) &&
3803 (hdw->ctl_read_urb->status != -ENOENT) &&
3804 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3805 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3806 /* USB subsystem is reporting some kind of failure
3807 on the read */
3808 status = hdw->ctl_read_urb->status;
3809 if (!probe_fl) {
3810 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3811 "control-read URB failure,"
3812 " status=%d",
3813 status);
3814 }
3815 goto done;
3816 }
3817 if (hdw->ctl_read_urb->actual_length < read_len) {
3818 /* Failed to read enough data */
3819 status = -EIO;
3820 if (!probe_fl) {
3821 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3822 "control-read URB short,"
3823 " expected=%d got=%d",
3824 read_len,
3825 hdw->ctl_read_urb->actual_length);
3826 }
3827 goto done;
3828 }
3829 /* Transfer retrieved data out from internal buffer */
3830 for (idx = 0; idx < read_len; idx++) {
3831 ((unsigned char *)read_data)[idx] =
3832 hdw->ctl_read_buffer[idx];
3833 }
3834 }
3835
3836 done:
3837
3838 hdw->cmd_debug_state = 0;
3839 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003840 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003841 }
3842 return status;
3843}
3844
3845
3846int pvr2_send_request(struct pvr2_hdw *hdw,
3847 void *write_data,unsigned int write_len,
3848 void *read_data,unsigned int read_len)
3849{
3850 return pvr2_send_request_ex(hdw,HZ*4,0,
3851 write_data,write_len,
3852 read_data,read_len);
3853}
3854
Mike Isely1c9d10d2008-03-28 05:38:54 -03003855
3856static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3857{
3858 int ret;
3859 unsigned int cnt = 1;
3860 unsigned int args = 0;
3861 LOCK_TAKE(hdw->ctl_lock);
3862 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3863 args = (cmdcode >> 8) & 0xffu;
3864 args = (args > 2) ? 2 : args;
3865 if (args) {
3866 cnt += args;
3867 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3868 if (args > 1) {
3869 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3870 }
3871 }
3872 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3873 unsigned int idx;
3874 unsigned int ccnt,bcnt;
3875 char tbuf[50];
3876 cmdcode &= 0xffu;
3877 bcnt = 0;
3878 ccnt = scnprintf(tbuf+bcnt,
3879 sizeof(tbuf)-bcnt,
3880 "Sending FX2 command 0x%x",cmdcode);
3881 bcnt += ccnt;
3882 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3883 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3884 ccnt = scnprintf(tbuf+bcnt,
3885 sizeof(tbuf)-bcnt,
3886 " \"%s\"",
3887 pvr2_fx2cmd_desc[idx].desc);
3888 bcnt += ccnt;
3889 break;
3890 }
3891 }
3892 if (args) {
3893 ccnt = scnprintf(tbuf+bcnt,
3894 sizeof(tbuf)-bcnt,
3895 " (%u",hdw->cmd_buffer[1]);
3896 bcnt += ccnt;
3897 if (args > 1) {
3898 ccnt = scnprintf(tbuf+bcnt,
3899 sizeof(tbuf)-bcnt,
3900 ",%u",hdw->cmd_buffer[2]);
3901 bcnt += ccnt;
3902 }
3903 ccnt = scnprintf(tbuf+bcnt,
3904 sizeof(tbuf)-bcnt,
3905 ")");
3906 bcnt += ccnt;
3907 }
3908 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
3909 }
3910 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
3911 LOCK_GIVE(hdw->ctl_lock);
3912 return ret;
3913}
3914
3915
Mike Iselyd8554972006-06-26 20:58:46 -03003916int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3917{
3918 int ret;
3919
3920 LOCK_TAKE(hdw->ctl_lock);
3921
Michael Krufky8d364362007-01-22 02:17:55 -03003922 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003923 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3924 hdw->cmd_buffer[5] = 0;
3925 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3926 hdw->cmd_buffer[7] = reg & 0xff;
3927
3928
3929 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3930
3931 LOCK_GIVE(hdw->ctl_lock);
3932
3933 return ret;
3934}
3935
3936
Adrian Bunk07e337e2006-06-30 11:30:20 -03003937static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003938{
3939 int ret = 0;
3940
3941 LOCK_TAKE(hdw->ctl_lock);
3942
Michael Krufky8d364362007-01-22 02:17:55 -03003943 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003944 hdw->cmd_buffer[1] = 0;
3945 hdw->cmd_buffer[2] = 0;
3946 hdw->cmd_buffer[3] = 0;
3947 hdw->cmd_buffer[4] = 0;
3948 hdw->cmd_buffer[5] = 0;
3949 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3950 hdw->cmd_buffer[7] = reg & 0xff;
3951
3952 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3953 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3954
3955 LOCK_GIVE(hdw->ctl_lock);
3956
3957 return ret;
3958}
3959
3960
Mike Isely681c7392007-11-26 01:48:52 -03003961void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003962{
3963 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003964 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3965 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003966 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003967 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003968 }
Mike Isely681c7392007-11-26 01:48:52 -03003969 hdw->flag_ok = 0;
3970 trace_stbit("flag_ok",hdw->flag_ok);
3971 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003972}
3973
3974
3975void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3976{
3977 int ret;
3978 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003979 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Alan Stern011b15d2008-11-04 11:29:27 -05003980 if (ret == 0) {
Mike Iselyd8554972006-06-26 20:58:46 -03003981 ret = usb_reset_device(hdw->usb_dev);
3982 usb_unlock_device(hdw->usb_dev);
3983 } else {
3984 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3985 "Failed to lock USB device ret=%d",ret);
3986 }
3987 if (init_pause_msec) {
3988 pvr2_trace(PVR2_TRACE_INFO,
3989 "Waiting %u msec for hardware to settle",
3990 init_pause_msec);
3991 msleep(init_pause_msec);
3992 }
3993
3994}
3995
3996
3997void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3998{
3999 char da[1];
4000 unsigned int pipe;
4001 int ret;
4002
4003 if (!hdw->usb_dev) return;
4004
4005 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
4006
4007 da[0] = val ? 0x01 : 0x00;
4008
4009 /* Write the CPUCS register on the 8051. The lsb of the register
4010 is the reset bit; a 1 asserts reset while a 0 clears it. */
4011 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
4012 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
4013 if (ret < 0) {
4014 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4015 "cpureset_assert(%d) error=%d",val,ret);
4016 pvr2_hdw_render_useless(hdw);
4017 }
4018}
4019
4020
4021int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
4022{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004023 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03004024}
4025
4026
Michael Krufkye1edb192008-04-22 14:45:39 -03004027int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
4028{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004029 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03004030}
4031
Mike Isely1c9d10d2008-03-28 05:38:54 -03004032
Michael Krufkye1edb192008-04-22 14:45:39 -03004033int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
4034{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004035 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03004036}
4037
Mike Iselyd8554972006-06-26 20:58:46 -03004038
4039int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
4040{
Mike Iselyd8554972006-06-26 20:58:46 -03004041 pvr2_trace(PVR2_TRACE_INIT,
4042 "Requesting decoder reset");
Mike Iselyaf78e162009-03-07 00:21:30 -03004043 if (hdw->decoder_ctrl) {
4044 if (!hdw->decoder_ctrl->force_reset) {
4045 pvr2_trace(PVR2_TRACE_INIT,
4046 "Unable to reset decoder: not implemented");
4047 return -ENOTTY;
4048 }
4049 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
4050 return 0;
4051 } else {
4052 }
4053 if (hdw->decoder_client_id) {
4054 v4l2_device_call_all(&hdw->v4l2_dev, hdw->decoder_client_id,
4055 core, reset, 0);
4056 return 0;
4057 }
4058 pvr2_trace(PVR2_TRACE_INIT,
4059 "Unable to reset decoder: nothing attached");
4060 return -ENOTTY;
Mike Iselyd8554972006-06-26 20:58:46 -03004061}
4062
4063
Mike Isely62433e32008-04-22 14:45:40 -03004064static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004065{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004066 hdw->flag_ok = !0;
4067 return pvr2_issue_simple_cmd(hdw,
4068 FX2CMD_HCW_DEMOD_RESETIN |
4069 (1 << 8) |
4070 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03004071}
4072
Mike Isely84147f32008-04-22 14:45:40 -03004073
Mike Isely62433e32008-04-22 14:45:40 -03004074static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004075{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004076 hdw->flag_ok = !0;
4077 return pvr2_issue_simple_cmd(hdw,(onoff ?
4078 FX2CMD_ONAIR_DTV_POWER_ON :
4079 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004080}
4081
Mike Isely62433e32008-04-22 14:45:40 -03004082
4083static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
4084 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004085{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004086 return pvr2_issue_simple_cmd(hdw,(onoff ?
4087 FX2CMD_ONAIR_DTV_STREAMING_ON :
4088 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004089}
4090
Mike Isely62433e32008-04-22 14:45:40 -03004091
4092static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
4093{
4094 int cmode;
4095 /* Compare digital/analog desired setting with current setting. If
4096 they don't match, fix it... */
4097 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
4098 if (cmode == hdw->pathway_state) {
4099 /* They match; nothing to do */
4100 return;
4101 }
4102
4103 switch (hdw->hdw_desc->digital_control_scheme) {
4104 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4105 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
4106 if (cmode == PVR2_PATHWAY_ANALOG) {
4107 /* If moving to analog mode, also force the decoder
4108 to reset. If no decoder is attached, then it's
4109 ok to ignore this because if/when the decoder
4110 attaches, it will reset itself at that time. */
4111 pvr2_hdw_cmd_decoder_reset(hdw);
4112 }
4113 break;
4114 case PVR2_DIGITAL_SCHEME_ONAIR:
4115 /* Supposedly we should always have the power on whether in
4116 digital or analog mode. But for now do what appears to
4117 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004118 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03004119 break;
4120 default: break;
4121 }
4122
Mike Isely1b9c18c2008-04-22 14:45:41 -03004123 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03004124 hdw->pathway_state = cmode;
4125}
4126
4127
Adrian Bunke9b59f62008-05-10 04:35:24 -03004128static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03004129{
4130 /* change some GPIO data
4131 *
4132 * note: bit d7 of dir appears to control the LED,
4133 * so we shut it off here.
4134 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03004135 */
Mike Isely40381cb2008-04-22 14:45:42 -03004136 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004137 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03004138 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004139 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03004140 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03004141 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03004142}
Mike Iselyc55a97d2008-04-22 14:45:41 -03004143
Mike Isely40381cb2008-04-22 14:45:42 -03004144
4145typedef void (*led_method_func)(struct pvr2_hdw *,int);
4146
4147static led_method_func led_methods[] = {
4148 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
4149};
4150
4151
4152/* Toggle LED */
4153static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
4154{
4155 unsigned int scheme_id;
4156 led_method_func fp;
4157
4158 if ((!onoff) == (!hdw->led_on)) return;
4159
4160 hdw->led_on = onoff != 0;
4161
4162 scheme_id = hdw->hdw_desc->led_scheme;
4163 if (scheme_id < ARRAY_SIZE(led_methods)) {
4164 fp = led_methods[scheme_id];
4165 } else {
4166 fp = NULL;
4167 }
4168
4169 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03004170}
4171
4172
Mike Iselye61b6fc2006-07-18 22:42:18 -03004173/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004174static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03004175{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004176 int ret;
4177
4178 /* If we're in analog mode, then just issue the usual analog
4179 command. */
4180 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4181 return pvr2_issue_simple_cmd(hdw,
4182 (runFl ?
4183 FX2CMD_STREAMING_ON :
4184 FX2CMD_STREAMING_OFF));
4185 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03004186 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004187
4188 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
4189 /* Whoops, we don't know what mode we're in... */
4190 return -EINVAL;
4191 }
4192
4193 /* To get here we have to be in digital mode. The mechanism here
4194 is unfortunately different for different vendors. So we switch
4195 on the device's digital scheme attribute in order to figure out
4196 what to do. */
4197 switch (hdw->hdw_desc->digital_control_scheme) {
4198 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4199 return pvr2_issue_simple_cmd(hdw,
4200 (runFl ?
4201 FX2CMD_HCW_DTV_STREAMING_ON :
4202 FX2CMD_HCW_DTV_STREAMING_OFF));
4203 case PVR2_DIGITAL_SCHEME_ONAIR:
4204 ret = pvr2_issue_simple_cmd(hdw,
4205 (runFl ?
4206 FX2CMD_STREAMING_ON :
4207 FX2CMD_STREAMING_OFF));
4208 if (ret) return ret;
4209 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
4210 default:
4211 return -EINVAL;
4212 }
Mike Iselyd8554972006-06-26 20:58:46 -03004213}
4214
4215
Mike Isely62433e32008-04-22 14:45:40 -03004216/* Evaluate whether or not state_pathway_ok can change */
4217static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
4218{
4219 if (hdw->state_pathway_ok) {
4220 /* Nothing to do if pathway is already ok */
4221 return 0;
4222 }
4223 if (!hdw->state_pipeline_idle) {
4224 /* Not allowed to change anything if pipeline is not idle */
4225 return 0;
4226 }
4227 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
4228 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03004229 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03004230 return !0;
4231}
4232
4233
Mike Isely681c7392007-11-26 01:48:52 -03004234/* Evaluate whether or not state_encoder_ok can change */
4235static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
4236{
4237 if (hdw->state_encoder_ok) return 0;
4238 if (hdw->flag_tripped) return 0;
4239 if (hdw->state_encoder_run) return 0;
4240 if (hdw->state_encoder_config) return 0;
4241 if (hdw->state_decoder_run) return 0;
4242 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004243 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
4244 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
4245 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
4246 return 0;
4247 }
4248
Mike Isely681c7392007-11-26 01:48:52 -03004249 if (pvr2_upload_firmware2(hdw) < 0) {
4250 hdw->flag_tripped = !0;
4251 trace_stbit("flag_tripped",hdw->flag_tripped);
4252 return !0;
4253 }
4254 hdw->state_encoder_ok = !0;
4255 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
4256 return !0;
4257}
4258
4259
4260/* Evaluate whether or not state_encoder_config can change */
4261static int state_eval_encoder_config(struct pvr2_hdw *hdw)
4262{
4263 if (hdw->state_encoder_config) {
4264 if (hdw->state_encoder_ok) {
4265 if (hdw->state_pipeline_req &&
4266 !hdw->state_pipeline_pause) return 0;
4267 }
4268 hdw->state_encoder_config = 0;
4269 hdw->state_encoder_waitok = 0;
4270 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4271 /* paranoia - solve race if timer just completed */
4272 del_timer_sync(&hdw->encoder_wait_timer);
4273 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004274 if (!hdw->state_pathway_ok ||
4275 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4276 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03004277 !hdw->state_pipeline_idle ||
4278 hdw->state_pipeline_pause ||
4279 !hdw->state_pipeline_req ||
4280 !hdw->state_pipeline_config) {
4281 /* We must reset the enforced wait interval if
4282 anything has happened that might have disturbed
4283 the encoder. This should be a rare case. */
4284 if (timer_pending(&hdw->encoder_wait_timer)) {
4285 del_timer_sync(&hdw->encoder_wait_timer);
4286 }
4287 if (hdw->state_encoder_waitok) {
4288 /* Must clear the state - therefore we did
4289 something to a state bit and must also
4290 return true. */
4291 hdw->state_encoder_waitok = 0;
4292 trace_stbit("state_encoder_waitok",
4293 hdw->state_encoder_waitok);
4294 return !0;
4295 }
4296 return 0;
4297 }
4298 if (!hdw->state_encoder_waitok) {
4299 if (!timer_pending(&hdw->encoder_wait_timer)) {
4300 /* waitok flag wasn't set and timer isn't
4301 running. Check flag once more to avoid
4302 a race then start the timer. This is
4303 the point when we measure out a minimal
4304 quiet interval before doing something to
4305 the encoder. */
4306 if (!hdw->state_encoder_waitok) {
4307 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004308 jiffies +
4309 (HZ * TIME_MSEC_ENCODER_WAIT
4310 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004311 add_timer(&hdw->encoder_wait_timer);
4312 }
4313 }
4314 /* We can't continue until we know we have been
4315 quiet for the interval measured by this
4316 timer. */
4317 return 0;
4318 }
4319 pvr2_encoder_configure(hdw);
4320 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
4321 }
4322 trace_stbit("state_encoder_config",hdw->state_encoder_config);
4323 return !0;
4324}
4325
4326
Mike Iselyd913d632008-04-06 04:04:35 -03004327/* Return true if the encoder should not be running. */
4328static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
4329{
4330 if (!hdw->state_encoder_ok) {
4331 /* Encoder isn't healthy at the moment, so stop it. */
4332 return !0;
4333 }
4334 if (!hdw->state_pathway_ok) {
4335 /* Mode is not understood at the moment (i.e. it wants to
4336 change), so encoder must be stopped. */
4337 return !0;
4338 }
4339
4340 switch (hdw->pathway_state) {
4341 case PVR2_PATHWAY_ANALOG:
4342 if (!hdw->state_decoder_run) {
4343 /* We're in analog mode and the decoder is not
4344 running; thus the encoder should be stopped as
4345 well. */
4346 return !0;
4347 }
4348 break;
4349 case PVR2_PATHWAY_DIGITAL:
4350 if (hdw->state_encoder_runok) {
4351 /* This is a funny case. We're in digital mode so
4352 really the encoder should be stopped. However
4353 if it really is running, only kill it after
4354 runok has been set. This gives a chance for the
4355 onair quirk to function (encoder must run
4356 briefly first, at least once, before onair
4357 digital streaming can work). */
4358 return !0;
4359 }
4360 break;
4361 default:
4362 /* Unknown mode; so encoder should be stopped. */
4363 return !0;
4364 }
4365
4366 /* If we get here, we haven't found a reason to stop the
4367 encoder. */
4368 return 0;
4369}
4370
4371
4372/* Return true if the encoder should be running. */
4373static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
4374{
4375 if (!hdw->state_encoder_ok) {
4376 /* Don't run the encoder if it isn't healthy... */
4377 return 0;
4378 }
4379 if (!hdw->state_pathway_ok) {
4380 /* Don't run the encoder if we don't (yet) know what mode
4381 we need to be in... */
4382 return 0;
4383 }
4384
4385 switch (hdw->pathway_state) {
4386 case PVR2_PATHWAY_ANALOG:
4387 if (hdw->state_decoder_run) {
4388 /* In analog mode, if the decoder is running, then
4389 run the encoder. */
4390 return !0;
4391 }
4392 break;
4393 case PVR2_PATHWAY_DIGITAL:
4394 if ((hdw->hdw_desc->digital_control_scheme ==
4395 PVR2_DIGITAL_SCHEME_ONAIR) &&
4396 !hdw->state_encoder_runok) {
4397 /* This is a quirk. OnAir hardware won't stream
4398 digital until the encoder has been run at least
4399 once, for a minimal period of time (empiricially
4400 measured to be 1/4 second). So if we're on
4401 OnAir hardware and the encoder has never been
4402 run at all, then start the encoder. Normal
4403 state machine logic in the driver will
4404 automatically handle the remaining bits. */
4405 return !0;
4406 }
4407 break;
4408 default:
4409 /* For completeness (unknown mode; encoder won't run ever) */
4410 break;
4411 }
4412 /* If we get here, then we haven't found any reason to run the
4413 encoder, so don't run it. */
4414 return 0;
4415}
4416
4417
Mike Isely681c7392007-11-26 01:48:52 -03004418/* Evaluate whether or not state_encoder_run can change */
4419static int state_eval_encoder_run(struct pvr2_hdw *hdw)
4420{
4421 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03004422 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004423 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03004424 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03004425 if (pvr2_encoder_stop(hdw) < 0) return !0;
4426 }
4427 hdw->state_encoder_run = 0;
4428 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03004429 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004430 if (pvr2_encoder_start(hdw) < 0) return !0;
4431 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03004432 if (!hdw->state_encoder_runok) {
4433 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004434 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03004435 add_timer(&hdw->encoder_run_timer);
4436 }
Mike Isely681c7392007-11-26 01:48:52 -03004437 }
4438 trace_stbit("state_encoder_run",hdw->state_encoder_run);
4439 return !0;
4440}
4441
4442
4443/* Timeout function for quiescent timer. */
4444static void pvr2_hdw_quiescent_timeout(unsigned long data)
4445{
4446 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4447 hdw->state_decoder_quiescent = !0;
4448 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4449 hdw->state_stale = !0;
4450 queue_work(hdw->workqueue,&hdw->workpoll);
4451}
4452
4453
4454/* Timeout function for encoder wait timer. */
4455static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
4456{
4457 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4458 hdw->state_encoder_waitok = !0;
4459 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4460 hdw->state_stale = !0;
4461 queue_work(hdw->workqueue,&hdw->workpoll);
4462}
4463
4464
Mike Iselyd913d632008-04-06 04:04:35 -03004465/* Timeout function for encoder run timer. */
4466static void pvr2_hdw_encoder_run_timeout(unsigned long data)
4467{
4468 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4469 if (!hdw->state_encoder_runok) {
4470 hdw->state_encoder_runok = !0;
4471 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
4472 hdw->state_stale = !0;
4473 queue_work(hdw->workqueue,&hdw->workpoll);
4474 }
4475}
4476
4477
Mike Isely681c7392007-11-26 01:48:52 -03004478/* Evaluate whether or not state_decoder_run can change */
4479static int state_eval_decoder_run(struct pvr2_hdw *hdw)
4480{
4481 if (hdw->state_decoder_run) {
4482 if (hdw->state_encoder_ok) {
4483 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03004484 !hdw->state_pipeline_pause &&
4485 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004486 }
4487 if (!hdw->flag_decoder_missed) {
4488 pvr2_decoder_enable(hdw,0);
4489 }
4490 hdw->state_decoder_quiescent = 0;
4491 hdw->state_decoder_run = 0;
4492 /* paranoia - solve race if timer just completed */
4493 del_timer_sync(&hdw->quiescent_timer);
4494 } else {
4495 if (!hdw->state_decoder_quiescent) {
4496 if (!timer_pending(&hdw->quiescent_timer)) {
4497 /* We don't do something about the
4498 quiescent timer until right here because
4499 we also want to catch cases where the
4500 decoder was already not running (like
4501 after initialization) as opposed to
4502 knowing that we had just stopped it.
4503 The second flag check is here to cover a
4504 race - the timer could have run and set
4505 this flag just after the previous check
4506 but before we did the pending check. */
4507 if (!hdw->state_decoder_quiescent) {
4508 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004509 jiffies +
4510 (HZ * TIME_MSEC_DECODER_WAIT
4511 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004512 add_timer(&hdw->quiescent_timer);
4513 }
4514 }
4515 /* Don't allow decoder to start again until it has
4516 been quiesced first. This little detail should
4517 hopefully further stabilize the encoder. */
4518 return 0;
4519 }
Mike Isely62433e32008-04-22 14:45:40 -03004520 if (!hdw->state_pathway_ok ||
4521 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4522 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03004523 hdw->state_pipeline_pause ||
4524 !hdw->state_pipeline_config ||
4525 !hdw->state_encoder_config ||
4526 !hdw->state_encoder_ok) return 0;
4527 del_timer_sync(&hdw->quiescent_timer);
4528 if (hdw->flag_decoder_missed) return 0;
4529 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
4530 hdw->state_decoder_quiescent = 0;
4531 hdw->state_decoder_run = !0;
4532 }
4533 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4534 trace_stbit("state_decoder_run",hdw->state_decoder_run);
4535 return !0;
4536}
4537
4538
4539/* Evaluate whether or not state_usbstream_run can change */
4540static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
4541{
4542 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03004543 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03004544 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03004545 fl = (hdw->state_encoder_ok &&
4546 hdw->state_encoder_run);
4547 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4548 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4549 fl = hdw->state_encoder_ok;
4550 }
4551 if (fl &&
4552 hdw->state_pipeline_req &&
4553 !hdw->state_pipeline_pause &&
4554 hdw->state_pathway_ok) {
4555 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004556 }
4557 pvr2_hdw_cmd_usbstream(hdw,0);
4558 hdw->state_usbstream_run = 0;
4559 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004560 if (!hdw->state_pipeline_req ||
4561 hdw->state_pipeline_pause ||
4562 !hdw->state_pathway_ok) return 0;
4563 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4564 if (!hdw->state_encoder_ok ||
4565 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004566 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4567 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4568 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03004569 if (hdw->state_encoder_run) return 0;
4570 if (hdw->hdw_desc->digital_control_scheme ==
4571 PVR2_DIGITAL_SCHEME_ONAIR) {
4572 /* OnAir digital receivers won't stream
4573 unless the analog encoder has run first.
4574 Why? I have no idea. But don't even
4575 try until we know the analog side is
4576 known to have run. */
4577 if (!hdw->state_encoder_runok) return 0;
4578 }
Mike Isely62433e32008-04-22 14:45:40 -03004579 }
Mike Isely681c7392007-11-26 01:48:52 -03004580 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
4581 hdw->state_usbstream_run = !0;
4582 }
4583 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
4584 return !0;
4585}
4586
4587
4588/* Attempt to configure pipeline, if needed */
4589static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
4590{
4591 if (hdw->state_pipeline_config ||
4592 hdw->state_pipeline_pause) return 0;
4593 pvr2_hdw_commit_execute(hdw);
4594 return !0;
4595}
4596
4597
4598/* Update pipeline idle and pipeline pause tracking states based on other
4599 inputs. This must be called whenever the other relevant inputs have
4600 changed. */
4601static int state_update_pipeline_state(struct pvr2_hdw *hdw)
4602{
4603 unsigned int st;
4604 int updatedFl = 0;
4605 /* Update pipeline state */
4606 st = !(hdw->state_encoder_run ||
4607 hdw->state_decoder_run ||
4608 hdw->state_usbstream_run ||
4609 (!hdw->state_decoder_quiescent));
4610 if (!st != !hdw->state_pipeline_idle) {
4611 hdw->state_pipeline_idle = st;
4612 updatedFl = !0;
4613 }
4614 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
4615 hdw->state_pipeline_pause = 0;
4616 updatedFl = !0;
4617 }
4618 return updatedFl;
4619}
4620
4621
4622typedef int (*state_eval_func)(struct pvr2_hdw *);
4623
4624/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03004625static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03004626 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03004627 state_eval_pipeline_config,
4628 state_eval_encoder_ok,
4629 state_eval_encoder_config,
4630 state_eval_decoder_run,
4631 state_eval_encoder_run,
4632 state_eval_usbstream_run,
4633};
4634
4635
4636/* Process various states and return true if we did anything interesting. */
4637static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4638{
4639 unsigned int i;
4640 int state_updated = 0;
4641 int check_flag;
4642
4643 if (!hdw->state_stale) return 0;
4644 if ((hdw->fw1_state != FW1_STATE_OK) ||
4645 !hdw->flag_ok) {
4646 hdw->state_stale = 0;
4647 return !0;
4648 }
4649 /* This loop is the heart of the entire driver. It keeps trying to
4650 evaluate various bits of driver state until nothing changes for
4651 one full iteration. Each "bit of state" tracks some global
4652 aspect of the driver, e.g. whether decoder should run, if
4653 pipeline is configured, usb streaming is on, etc. We separately
4654 evaluate each of those questions based on other driver state to
4655 arrive at the correct running configuration. */
4656 do {
4657 check_flag = 0;
4658 state_update_pipeline_state(hdw);
4659 /* Iterate over each bit of state */
4660 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4661 if ((*eval_funcs[i])(hdw)) {
4662 check_flag = !0;
4663 state_updated = !0;
4664 state_update_pipeline_state(hdw);
4665 }
4666 }
4667 } while (check_flag && hdw->flag_ok);
4668 hdw->state_stale = 0;
4669 trace_stbit("state_stale",hdw->state_stale);
4670 return state_updated;
4671}
4672
4673
Mike Isely1cb03b72008-04-21 03:47:43 -03004674static unsigned int print_input_mask(unsigned int msk,
4675 char *buf,unsigned int acnt)
4676{
4677 unsigned int idx,ccnt;
4678 unsigned int tcnt = 0;
4679 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4680 if (!((1 << idx) & msk)) continue;
4681 ccnt = scnprintf(buf+tcnt,
4682 acnt-tcnt,
4683 "%s%s",
4684 (tcnt ? ", " : ""),
4685 control_values_input[idx]);
4686 tcnt += ccnt;
4687 }
4688 return tcnt;
4689}
4690
4691
Mike Isely62433e32008-04-22 14:45:40 -03004692static const char *pvr2_pathway_state_name(int id)
4693{
4694 switch (id) {
4695 case PVR2_PATHWAY_ANALOG: return "analog";
4696 case PVR2_PATHWAY_DIGITAL: return "digital";
4697 default: return "unknown";
4698 }
4699}
4700
4701
Mike Isely681c7392007-11-26 01:48:52 -03004702static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4703 char *buf,unsigned int acnt)
4704{
4705 switch (which) {
4706 case 0:
4707 return scnprintf(
4708 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004709 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004710 (hdw->flag_ok ? " <ok>" : " <fail>"),
4711 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4712 (hdw->flag_disconnected ? " <disconnected>" :
4713 " <connected>"),
4714 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004715 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4716 pvr2_pathway_state_name(hdw->pathway_state));
4717
Mike Isely681c7392007-11-26 01:48:52 -03004718 case 1:
4719 return scnprintf(
4720 buf,acnt,
4721 "pipeline:%s%s%s%s",
4722 (hdw->state_pipeline_idle ? " <idle>" : ""),
4723 (hdw->state_pipeline_config ?
4724 " <configok>" : " <stale>"),
4725 (hdw->state_pipeline_req ? " <req>" : ""),
4726 (hdw->state_pipeline_pause ? " <pause>" : ""));
4727 case 2:
4728 return scnprintf(
4729 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004730 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004731 (hdw->state_decoder_run ?
4732 " <decode:run>" :
4733 (hdw->state_decoder_quiescent ?
4734 "" : " <decode:stop>")),
4735 (hdw->state_decoder_quiescent ?
4736 " <decode:quiescent>" : ""),
4737 (hdw->state_encoder_ok ?
4738 "" : " <encode:init>"),
4739 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004740 (hdw->state_encoder_runok ?
4741 " <encode:run>" :
4742 " <encode:firstrun>") :
4743 (hdw->state_encoder_runok ?
4744 " <encode:stop>" :
4745 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004746 (hdw->state_encoder_config ?
4747 " <encode:configok>" :
4748 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004749 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004750 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004751 " <usb:run>" : " <usb:stop>"),
4752 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004753 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004754 case 3:
4755 return scnprintf(
4756 buf,acnt,
4757 "state: %s",
4758 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004759 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004760 unsigned int tcnt = 0;
4761 unsigned int ccnt;
4762
4763 ccnt = scnprintf(buf,
4764 acnt,
4765 "Hardware supported inputs: ");
4766 tcnt += ccnt;
4767 tcnt += print_input_mask(hdw->input_avail_mask,
4768 buf+tcnt,
4769 acnt-tcnt);
4770 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4771 ccnt = scnprintf(buf+tcnt,
4772 acnt-tcnt,
4773 "; allowed inputs: ");
4774 tcnt += ccnt;
4775 tcnt += print_input_mask(hdw->input_allowed_mask,
4776 buf+tcnt,
4777 acnt-tcnt);
4778 }
4779 return tcnt;
4780 }
4781 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004782 struct pvr2_stream_stats stats;
4783 if (!hdw->vid_stream) break;
4784 pvr2_stream_get_stats(hdw->vid_stream,
4785 &stats,
4786 0);
4787 return scnprintf(
4788 buf,acnt,
4789 "Bytes streamed=%u"
4790 " URBs: queued=%u idle=%u ready=%u"
4791 " processed=%u failed=%u",
4792 stats.bytes_processed,
4793 stats.buffers_in_queue,
4794 stats.buffers_in_idle,
4795 stats.buffers_in_ready,
4796 stats.buffers_processed,
4797 stats.buffers_failed);
4798 }
Mike Isely858f9102009-03-07 00:28:28 -03004799 case 6: {
4800 struct v4l2_subdev *sd;
4801 unsigned int tcnt = 0;
4802 unsigned int ccnt;
4803 const char *p;
4804 unsigned int id;
4805 ccnt = scnprintf(buf,
4806 acnt,
4807 "Associted v4l2_subdev drivers:");
4808 tcnt += ccnt;
4809 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
4810 id = sd->grp_id;
4811 p = NULL;
4812 if (id < ARRAY_SIZE(module_names)) {
4813 p = module_names[id];
4814 }
4815 if (!p) p = "(unknown)";
4816 ccnt = scnprintf(buf + tcnt,
4817 acnt - tcnt,
4818 " %s (%u)", p, id);
4819 }
4820 return tcnt;
4821 }
Mike Isely681c7392007-11-26 01:48:52 -03004822 default: break;
4823 }
4824 return 0;
4825}
4826
4827
4828unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4829 char *buf,unsigned int acnt)
4830{
4831 unsigned int bcnt,ccnt,idx;
4832 bcnt = 0;
4833 LOCK_TAKE(hdw->big_lock);
4834 for (idx = 0; ; idx++) {
4835 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
4836 if (!ccnt) break;
4837 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4838 if (!acnt) break;
4839 buf[0] = '\n'; ccnt = 1;
4840 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4841 }
4842 LOCK_GIVE(hdw->big_lock);
4843 return bcnt;
4844}
4845
4846
4847static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
4848{
4849 char buf[128];
4850 unsigned int idx,ccnt;
4851
4852 for (idx = 0; ; idx++) {
4853 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
4854 if (!ccnt) break;
4855 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
4856 }
4857}
4858
4859
4860/* Evaluate and update the driver's current state, taking various actions
4861 as appropriate for the update. */
4862static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
4863{
4864 unsigned int st;
4865 int state_updated = 0;
4866 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004867 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03004868
4869 pvr2_trace(PVR2_TRACE_STBITS,
4870 "Drive state check START");
4871 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4872 pvr2_hdw_state_log_state(hdw);
4873 }
4874
4875 /* Process all state and get back over disposition */
4876 state_updated = pvr2_hdw_state_update(hdw);
4877
Mike Isely1b9c18c2008-04-22 14:45:41 -03004878 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
4879
Mike Isely681c7392007-11-26 01:48:52 -03004880 /* Update master state based upon all other states. */
4881 if (!hdw->flag_ok) {
4882 st = PVR2_STATE_DEAD;
4883 } else if (hdw->fw1_state != FW1_STATE_OK) {
4884 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03004885 } else if ((analog_mode ||
4886 hdw->hdw_desc->flag_digital_requires_cx23416) &&
4887 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03004888 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004889 } else if (hdw->flag_tripped ||
4890 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03004891 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03004892 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03004893 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03004894 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03004895 st = PVR2_STATE_RUN;
4896 } else {
4897 st = PVR2_STATE_READY;
4898 }
4899 if (hdw->master_state != st) {
4900 pvr2_trace(PVR2_TRACE_STATE,
4901 "Device state change from %s to %s",
4902 pvr2_get_state_name(hdw->master_state),
4903 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03004904 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03004905 hdw->master_state = st;
4906 state_updated = !0;
4907 callback_flag = !0;
4908 }
4909 if (state_updated) {
4910 /* Trigger anyone waiting on any state changes here. */
4911 wake_up(&hdw->state_wait_data);
4912 }
4913
4914 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4915 pvr2_hdw_state_log_state(hdw);
4916 }
4917 pvr2_trace(PVR2_TRACE_STBITS,
4918 "Drive state check DONE callback=%d",callback_flag);
4919
4920 return callback_flag;
4921}
4922
4923
4924/* Cause kernel thread to check / update driver state */
4925static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
4926{
4927 if (hdw->state_stale) return;
4928 hdw->state_stale = !0;
4929 trace_stbit("state_stale",hdw->state_stale);
4930 queue_work(hdw->workqueue,&hdw->workpoll);
4931}
4932
4933
Mike Iselyd8554972006-06-26 20:58:46 -03004934int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
4935{
4936 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
4937}
4938
4939
4940int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
4941{
4942 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
4943}
4944
4945
4946int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
4947{
4948 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
4949}
4950
4951
4952int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
4953{
4954 u32 cval,nval;
4955 int ret;
4956 if (~msk) {
4957 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
4958 if (ret) return ret;
4959 nval = (cval & ~msk) | (val & msk);
4960 pvr2_trace(PVR2_TRACE_GPIO,
4961 "GPIO direction changing 0x%x:0x%x"
4962 " from 0x%x to 0x%x",
4963 msk,val,cval,nval);
4964 } else {
4965 nval = val;
4966 pvr2_trace(PVR2_TRACE_GPIO,
4967 "GPIO direction changing to 0x%x",nval);
4968 }
4969 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
4970}
4971
4972
4973int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
4974{
4975 u32 cval,nval;
4976 int ret;
4977 if (~msk) {
4978 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
4979 if (ret) return ret;
4980 nval = (cval & ~msk) | (val & msk);
4981 pvr2_trace(PVR2_TRACE_GPIO,
4982 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
4983 msk,val,cval,nval);
4984 } else {
4985 nval = val;
4986 pvr2_trace(PVR2_TRACE_GPIO,
4987 "GPIO output changing to 0x%x",nval);
4988 }
4989 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
4990}
4991
4992
Mike Iselya51f5002009-03-06 23:30:37 -03004993void pvr2_hdw_status_poll(struct pvr2_hdw *hdw)
4994{
Mike Isely40f07112009-03-07 00:08:17 -03004995 struct v4l2_tuner *vtp = &hdw->tuner_signal_info;
4996 memset(vtp, 0, sizeof(*vtp));
Mike Isely2641df32009-03-07 00:13:25 -03004997 hdw->tuner_signal_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03004998 pvr2_i2c_core_status_poll(hdw);
Mike Isely40f07112009-03-07 00:08:17 -03004999 /* Note: There apparently is no replacement for VIDIOC_CROPCAP
5000 using v4l2-subdev - therefore we can't support that AT ALL right
5001 now. (Of course, no sub-drivers seem to implement it either.
5002 But now it's a a chicken and egg problem...) */
5003 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, g_tuner,
5004 &hdw->tuner_signal_info);
Mike Isely2641df32009-03-07 00:13:25 -03005005 pvr2_trace(PVR2_TRACE_CHIPS, "subdev status poll"
Mike Isely40f07112009-03-07 00:08:17 -03005006 " type=%u strength=%u audio=0x%x cap=0x%x"
5007 " low=%u hi=%u",
5008 vtp->type,
5009 vtp->signal, vtp->rxsubchans, vtp->capability,
5010 vtp->rangelow, vtp->rangehigh);
Mike Isely2641df32009-03-07 00:13:25 -03005011
5012 /* We have to do this to avoid getting into constant polling if
5013 there's nobody to answer a poll of cropcap info. */
5014 hdw->cropcap_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03005015}
5016
5017
Mike Isely7fb20fa2008-04-22 14:45:37 -03005018unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
5019{
5020 return hdw->input_avail_mask;
5021}
5022
5023
Mike Isely1cb03b72008-04-21 03:47:43 -03005024unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
5025{
5026 return hdw->input_allowed_mask;
5027}
5028
5029
5030static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
5031{
5032 if (hdw->input_val != v) {
5033 hdw->input_val = v;
5034 hdw->input_dirty = !0;
5035 }
5036
5037 /* Handle side effects - if we switch to a mode that needs the RF
5038 tuner, then select the right frequency choice as well and mark
5039 it dirty. */
5040 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
5041 hdw->freqSelector = 0;
5042 hdw->freqDirty = !0;
5043 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
5044 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
5045 hdw->freqSelector = 1;
5046 hdw->freqDirty = !0;
5047 }
5048 return 0;
5049}
5050
5051
5052int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
5053 unsigned int change_mask,
5054 unsigned int change_val)
5055{
5056 int ret = 0;
5057 unsigned int nv,m,idx;
5058 LOCK_TAKE(hdw->big_lock);
5059 do {
5060 nv = hdw->input_allowed_mask & ~change_mask;
5061 nv |= (change_val & change_mask);
5062 nv &= hdw->input_avail_mask;
5063 if (!nv) {
5064 /* No legal modes left; return error instead. */
5065 ret = -EPERM;
5066 break;
5067 }
5068 hdw->input_allowed_mask = nv;
5069 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
5070 /* Current mode is still in the allowed mask, so
5071 we're done. */
5072 break;
5073 }
5074 /* Select and switch to a mode that is still in the allowed
5075 mask */
5076 if (!hdw->input_allowed_mask) {
5077 /* Nothing legal; give up */
5078 break;
5079 }
5080 m = hdw->input_allowed_mask;
5081 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
5082 if (!((1 << idx) & m)) continue;
5083 pvr2_hdw_set_input(hdw,idx);
5084 break;
5085 }
5086 } while (0);
5087 LOCK_GIVE(hdw->big_lock);
5088 return ret;
5089}
5090
5091
Mike Iselye61b6fc2006-07-18 22:42:18 -03005092/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03005093static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03005094{
5095 int result;
5096 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03005097 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03005098 result = pvr2_send_request(hdw,
5099 hdw->cmd_buffer,1,
5100 hdw->cmd_buffer,1);
5101 if (result < 0) break;
5102 result = hdw->cmd_buffer[0];
5103 } while(0); LOCK_GIVE(hdw->ctl_lock);
5104 return result;
5105}
5106
5107
Mike Isely32ffa9a2006-09-23 22:26:52 -03005108int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005109 struct v4l2_dbg_match *match, u64 reg_id,
5110 int setFl, u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03005111{
5112#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03005113 struct pvr2_i2c_client *cp;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005114 struct v4l2_dbg_register req;
Mike Isely6d988162006-09-28 17:53:49 -03005115 int stat = 0;
5116 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005117
Mike Isely201f5c92007-01-28 16:08:36 -03005118 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
5119
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005120 req.match = *match;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005121 req.reg = reg_id;
5122 if (setFl) req.val = *val_ptr;
Mike Iselyd8f5b9b2009-03-07 00:05:00 -03005123 /* It would be nice to know if a sub-device answered the request */
5124 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, g_register, &req);
5125 if (!setFl) *val_ptr = req.val;
5126 if (!okFl) mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03005127 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03005128 if (!v4l2_chip_match_i2c_client(
5129 cp->client,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005130 &req.match)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03005131 continue;
5132 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005133 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03005134 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
5135 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03005136 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03005137 okFl = !0;
5138 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005139 }
5140 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03005141 if (okFl) {
5142 return stat;
5143 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005144 return -EINVAL;
5145#else
5146 return -ENOSYS;
5147#endif
5148}
5149
5150
Mike Iselyd8554972006-06-26 20:58:46 -03005151/*
5152 Stuff for Emacs to see, in order to encourage consistent editing style:
5153 *** Local Variables: ***
5154 *** mode: c ***
5155 *** fill-column: 75 ***
5156 *** tab-width: 8 ***
5157 *** c-basic-offset: 8 ***
5158 *** End: ***
5159 */