blob: ac5dad0c5fb04750d4f274fdadf8c62efcb7f2dd [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"
32#include "pvrusb2-tuner.h"
33#include "pvrusb2-eeprom.h"
34#include "pvrusb2-hdw-internal.h"
35#include "pvrusb2-encoder.h"
36#include "pvrusb2-debug.h"
Michael Krufky8d364362007-01-22 02:17:55 -030037#include "pvrusb2-fx2-cmd.h"
Mike Iselyd8554972006-06-26 20:58:46 -030038
Mike Isely1bde0282006-12-27 23:30:13 -030039#define TV_MIN_FREQ 55250000L
40#define TV_MAX_FREQ 850000000L
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -030041
Mike Isely83ce57a2008-05-26 05:51:57 -030042/* This defines a minimum interval that the decoder must remain quiet
43 before we are allowed to start it running. */
44#define TIME_MSEC_DECODER_WAIT 50
45
46/* This defines a minimum interval that the encoder must remain quiet
Mike Iselyfa98e592008-05-26 05:54:24 -030047 before we are allowed to configure it. I had this originally set to
48 50msec, but Martin Dauskardt <martin.dauskardt@gmx.de> reports that
49 things work better when it's set to 100msec. */
50#define TIME_MSEC_ENCODER_WAIT 100
Mike Isely83ce57a2008-05-26 05:51:57 -030051
52/* This defines the minimum interval that the encoder must successfully run
53 before we consider that the encoder has run at least once since its
54 firmware has been loaded. This measurement is in important for cases
55 where we can't do something until we know that the encoder has been run
56 at least once. */
57#define TIME_MSEC_ENCODER_OK 250
58
Mike Iselya0fd1cb2006-06-30 11:35:28 -030059static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -030060static DEFINE_MUTEX(pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -030061
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030062static int ctlchg;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030063static int procreload;
Mike Iselyd8554972006-06-26 20:58:46 -030064static int tuner[PVR_NUM] = { [0 ... PVR_NUM-1] = -1 };
65static int tolerance[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
66static int video_std[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030067static int init_pause_msec;
Mike Iselyd8554972006-06-26 20:58:46 -030068
69module_param(ctlchg, int, S_IRUGO|S_IWUSR);
70MODULE_PARM_DESC(ctlchg, "0=optimize ctl change 1=always accept new ctl value");
71module_param(init_pause_msec, int, S_IRUGO|S_IWUSR);
72MODULE_PARM_DESC(init_pause_msec, "hardware initialization settling delay");
Mike Iselyd8554972006-06-26 20:58:46 -030073module_param(procreload, int, S_IRUGO|S_IWUSR);
74MODULE_PARM_DESC(procreload,
75 "Attempt init failure recovery with firmware reload");
76module_param_array(tuner, int, NULL, 0444);
77MODULE_PARM_DESC(tuner,"specify installed tuner type");
78module_param_array(video_std, int, NULL, 0444);
79MODULE_PARM_DESC(video_std,"specify initial video standard");
80module_param_array(tolerance, int, NULL, 0444);
81MODULE_PARM_DESC(tolerance,"specify stream error tolerance");
82
Michael Krufky5a4f5da62008-05-11 16:37:50 -030083/* US Broadcast channel 7 (175.25 MHz) */
84static int default_tv_freq = 175250000L;
85/* 104.3 MHz, a usable FM station for my area */
86static int default_radio_freq = 104300000L;
87
88module_param_named(tv_freq, default_tv_freq, int, 0444);
89MODULE_PARM_DESC(tv_freq, "specify initial television frequency");
90module_param_named(radio_freq, default_radio_freq, int, 0444);
91MODULE_PARM_DESC(radio_freq, "specify initial radio frequency");
92
Mike Iselyd8554972006-06-26 20:58:46 -030093#define PVR2_CTL_WRITE_ENDPOINT 0x01
94#define PVR2_CTL_READ_ENDPOINT 0x81
95
96#define PVR2_GPIO_IN 0x9008
97#define PVR2_GPIO_OUT 0x900c
98#define PVR2_GPIO_DIR 0x9020
99
100#define trace_firmware(...) pvr2_trace(PVR2_TRACE_FIRMWARE,__VA_ARGS__)
101
102#define PVR2_FIRMWARE_ENDPOINT 0x02
103
104/* size of a firmware chunk */
105#define FIRMWARE_CHUNK_SIZE 0x2000
106
Mike Iselyb30d2442006-06-25 20:05:01 -0300107/* Define the list of additional controls we'll dynamically construct based
108 on query of the cx2341x module. */
109struct pvr2_mpeg_ids {
110 const char *strid;
111 int id;
112};
113static const struct pvr2_mpeg_ids mpeg_ids[] = {
114 {
115 .strid = "audio_layer",
116 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
117 },{
118 .strid = "audio_bitrate",
119 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
120 },{
121 /* Already using audio_mode elsewhere :-( */
122 .strid = "mpeg_audio_mode",
123 .id = V4L2_CID_MPEG_AUDIO_MODE,
124 },{
125 .strid = "mpeg_audio_mode_extension",
126 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
127 },{
128 .strid = "audio_emphasis",
129 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
130 },{
131 .strid = "audio_crc",
132 .id = V4L2_CID_MPEG_AUDIO_CRC,
133 },{
134 .strid = "video_aspect",
135 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
136 },{
137 .strid = "video_b_frames",
138 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
139 },{
140 .strid = "video_gop_size",
141 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
142 },{
143 .strid = "video_gop_closure",
144 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
145 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300146 .strid = "video_bitrate_mode",
147 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
148 },{
149 .strid = "video_bitrate",
150 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
151 },{
152 .strid = "video_bitrate_peak",
153 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
154 },{
155 .strid = "video_temporal_decimation",
156 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
157 },{
158 .strid = "stream_type",
159 .id = V4L2_CID_MPEG_STREAM_TYPE,
160 },{
161 .strid = "video_spatial_filter_mode",
162 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
163 },{
164 .strid = "video_spatial_filter",
165 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
166 },{
167 .strid = "video_luma_spatial_filter_type",
168 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
169 },{
170 .strid = "video_chroma_spatial_filter_type",
171 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
172 },{
173 .strid = "video_temporal_filter_mode",
174 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
175 },{
176 .strid = "video_temporal_filter",
177 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
178 },{
179 .strid = "video_median_filter_type",
180 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
181 },{
182 .strid = "video_luma_median_filter_top",
183 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
184 },{
185 .strid = "video_luma_median_filter_bottom",
186 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
187 },{
188 .strid = "video_chroma_median_filter_top",
189 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
190 },{
191 .strid = "video_chroma_median_filter_bottom",
192 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
193 }
194};
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300195#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
Mike Iselyc05c0462006-06-25 20:04:25 -0300196
Mike Iselyd8554972006-06-26 20:58:46 -0300197
Mike Isely434449f2006-08-08 09:10:06 -0300198static const char *control_values_srate[] = {
199 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
200 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
201 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
202};
Mike Iselyd8554972006-06-26 20:58:46 -0300203
Mike Iselyd8554972006-06-26 20:58:46 -0300204
205
206static const char *control_values_input[] = {
207 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
Mike Isely29bf5b12008-04-22 14:45:37 -0300208 [PVR2_CVAL_INPUT_DTV] = "dtv",
Mike Iselyd8554972006-06-26 20:58:46 -0300209 [PVR2_CVAL_INPUT_RADIO] = "radio",
210 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
211 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
212};
213
214
215static const char *control_values_audiomode[] = {
216 [V4L2_TUNER_MODE_MONO] = "Mono",
217 [V4L2_TUNER_MODE_STEREO] = "Stereo",
218 [V4L2_TUNER_MODE_LANG1] = "Lang1",
219 [V4L2_TUNER_MODE_LANG2] = "Lang2",
220 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
221};
222
223
224static const char *control_values_hsm[] = {
225 [PVR2_CVAL_HSM_FAIL] = "Fail",
226 [PVR2_CVAL_HSM_HIGH] = "High",
227 [PVR2_CVAL_HSM_FULL] = "Full",
228};
229
230
Mike Isely681c7392007-11-26 01:48:52 -0300231static const char *pvr2_state_names[] = {
232 [PVR2_STATE_NONE] = "none",
233 [PVR2_STATE_DEAD] = "dead",
234 [PVR2_STATE_COLD] = "cold",
235 [PVR2_STATE_WARM] = "warm",
236 [PVR2_STATE_ERROR] = "error",
237 [PVR2_STATE_READY] = "ready",
238 [PVR2_STATE_RUN] = "run",
Mike Iselyd8554972006-06-26 20:58:46 -0300239};
240
Mike Isely681c7392007-11-26 01:48:52 -0300241
Mike Isely694dca2b2008-03-28 05:42:10 -0300242struct pvr2_fx2cmd_descdef {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300243 unsigned char id;
244 unsigned char *desc;
245};
246
Mike Isely694dca2b2008-03-28 05:42:10 -0300247static const struct pvr2_fx2cmd_descdef pvr2_fx2cmd_desc[] = {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300248 {FX2CMD_MEM_WRITE_DWORD, "write encoder dword"},
249 {FX2CMD_MEM_READ_DWORD, "read encoder dword"},
Mike Isely31335b12008-07-25 19:35:31 -0300250 {FX2CMD_HCW_ZILOG_RESET, "zilog IR reset control"},
Mike Isely1c9d10d2008-03-28 05:38:54 -0300251 {FX2CMD_MEM_READ_64BYTES, "read encoder 64bytes"},
252 {FX2CMD_REG_WRITE, "write encoder register"},
253 {FX2CMD_REG_READ, "read encoder register"},
254 {FX2CMD_MEMSEL, "encoder memsel"},
255 {FX2CMD_I2C_WRITE, "i2c write"},
256 {FX2CMD_I2C_READ, "i2c read"},
257 {FX2CMD_GET_USB_SPEED, "get USB speed"},
258 {FX2CMD_STREAMING_ON, "stream on"},
259 {FX2CMD_STREAMING_OFF, "stream off"},
260 {FX2CMD_FWPOST1, "fwpost1"},
261 {FX2CMD_POWER_OFF, "power off"},
262 {FX2CMD_POWER_ON, "power on"},
263 {FX2CMD_DEEP_RESET, "deep reset"},
264 {FX2CMD_GET_EEPROM_ADDR, "get rom addr"},
265 {FX2CMD_GET_IR_CODE, "get IR code"},
266 {FX2CMD_HCW_DEMOD_RESETIN, "hcw demod resetin"},
267 {FX2CMD_HCW_DTV_STREAMING_ON, "hcw dtv stream on"},
268 {FX2CMD_HCW_DTV_STREAMING_OFF, "hcw dtv stream off"},
269 {FX2CMD_ONAIR_DTV_STREAMING_ON, "onair dtv stream on"},
270 {FX2CMD_ONAIR_DTV_STREAMING_OFF, "onair dtv stream off"},
271 {FX2CMD_ONAIR_DTV_POWER_ON, "onair dtv power on"},
272 {FX2CMD_ONAIR_DTV_POWER_OFF, "onair dtv power off"},
273};
274
275
Mike Isely1cb03b72008-04-21 03:47:43 -0300276static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v);
Mike Isely681c7392007-11-26 01:48:52 -0300277static void pvr2_hdw_state_sched(struct pvr2_hdw *);
278static int pvr2_hdw_state_eval(struct pvr2_hdw *);
Mike Isely1bde0282006-12-27 23:30:13 -0300279static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300280static void pvr2_hdw_worker_i2c(struct work_struct *work);
281static void pvr2_hdw_worker_poll(struct work_struct *work);
Mike Isely681c7392007-11-26 01:48:52 -0300282static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
283static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
284static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300285static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300286static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300287static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300288static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
289static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300290static void pvr2_hdw_quiescent_timeout(unsigned long);
291static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Mike Iselyd913d632008-04-06 04:04:35 -0300292static void pvr2_hdw_encoder_run_timeout(unsigned long);
Mike Isely1c9d10d2008-03-28 05:38:54 -0300293static int pvr2_issue_simple_cmd(struct pvr2_hdw *,u32);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300294static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
295 unsigned int timeout,int probe_fl,
296 void *write_data,unsigned int write_len,
297 void *read_data,unsigned int read_len);
Mike Isely432907f2008-08-31 21:02:20 -0300298static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300299
Mike Isely681c7392007-11-26 01:48:52 -0300300
301static void trace_stbit(const char *name,int val)
302{
303 pvr2_trace(PVR2_TRACE_STBITS,
304 "State bit %s <-- %s",
305 name,(val ? "true" : "false"));
306}
307
Mike Iselyd8554972006-06-26 20:58:46 -0300308static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
309{
310 struct pvr2_hdw *hdw = cptr->hdw;
311 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
312 *vp = hdw->freqTable[hdw->freqProgSlot-1];
313 } else {
314 *vp = 0;
315 }
316 return 0;
317}
318
319static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
320{
321 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300322 unsigned int slotId = hdw->freqProgSlot;
323 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
324 hdw->freqTable[slotId-1] = v;
325 /* Handle side effects correctly - if we're tuned to this
326 slot, then forgot the slot id relation since the stored
327 frequency has been changed. */
328 if (hdw->freqSelector) {
329 if (hdw->freqSlotRadio == slotId) {
330 hdw->freqSlotRadio = 0;
331 }
332 } else {
333 if (hdw->freqSlotTelevision == slotId) {
334 hdw->freqSlotTelevision = 0;
335 }
336 }
Mike Iselyd8554972006-06-26 20:58:46 -0300337 }
338 return 0;
339}
340
341static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
342{
343 *vp = cptr->hdw->freqProgSlot;
344 return 0;
345}
346
347static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
348{
349 struct pvr2_hdw *hdw = cptr->hdw;
350 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
351 hdw->freqProgSlot = v;
352 }
353 return 0;
354}
355
356static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
357{
Mike Isely1bde0282006-12-27 23:30:13 -0300358 struct pvr2_hdw *hdw = cptr->hdw;
359 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300360 return 0;
361}
362
Mike Isely1bde0282006-12-27 23:30:13 -0300363static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300364{
365 unsigned freq = 0;
366 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300367 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
368 if (slotId > 0) {
369 freq = hdw->freqTable[slotId-1];
370 if (!freq) return 0;
371 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300372 }
Mike Isely1bde0282006-12-27 23:30:13 -0300373 if (hdw->freqSelector) {
374 hdw->freqSlotRadio = slotId;
375 } else {
376 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300377 }
378 return 0;
379}
380
381static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
382{
Mike Isely1bde0282006-12-27 23:30:13 -0300383 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300384 return 0;
385}
386
387static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
388{
389 return cptr->hdw->freqDirty != 0;
390}
391
392static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
393{
394 cptr->hdw->freqDirty = 0;
395}
396
397static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
398{
Mike Isely1bde0282006-12-27 23:30:13 -0300399 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300400 return 0;
401}
402
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300403static int ctrl_cropl_min_get(struct pvr2_ctrl *cptr, int *left)
404{
Mike Isely432907f2008-08-31 21:02:20 -0300405 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
406 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
407 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300408 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300409 }
Mike Isely432907f2008-08-31 21:02:20 -0300410 *left = cap->bounds.left;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300411 return 0;
412}
413
414static int ctrl_cropl_max_get(struct pvr2_ctrl *cptr, int *left)
415{
Mike Isely432907f2008-08-31 21:02:20 -0300416 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
417 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
418 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300419 return stat;
420 }
421 *left = cap->bounds.left;
422 if (cap->bounds.width > cptr->hdw->cropw_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300423 *left += cap->bounds.width - cptr->hdw->cropw_val;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300424 }
425 return 0;
426}
427
428static int ctrl_cropt_min_get(struct pvr2_ctrl *cptr, int *top)
429{
Mike Isely432907f2008-08-31 21:02:20 -0300430 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
431 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
432 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300433 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300434 }
Mike Isely432907f2008-08-31 21:02:20 -0300435 *top = cap->bounds.top;
436 return 0;
437}
438
439static int ctrl_cropt_max_get(struct pvr2_ctrl *cptr, int *top)
440{
441 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
442 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
443 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300444 return stat;
445 }
446 *top = cap->bounds.top;
447 if (cap->bounds.height > cptr->hdw->croph_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300448 *top += cap->bounds.height - cptr->hdw->croph_val;
449 }
450 return 0;
451}
452
453static int ctrl_cropw_max_get(struct pvr2_ctrl *cptr, int *val)
454{
455 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
456 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
457 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300458 return stat;
459 }
460 *val = 0;
461 if (cap->bounds.width > cptr->hdw->cropl_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300462 *val = cap->bounds.width - cptr->hdw->cropl_val;
463 }
464 return 0;
465}
466
467static int ctrl_croph_max_get(struct pvr2_ctrl *cptr, int *val)
468{
469 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
470 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
471 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300472 return stat;
473 }
474 *val = 0;
475 if (cap->bounds.height > cptr->hdw->cropt_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300476 *val = cap->bounds.height - cptr->hdw->cropt_val;
477 }
478 return 0;
479}
480
481static int ctrl_get_cropcapbl(struct pvr2_ctrl *cptr, int *val)
482{
483 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
484 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
485 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300486 return stat;
487 }
488 *val = cap->bounds.left;
489 return 0;
490}
491
492static int ctrl_get_cropcapbt(struct pvr2_ctrl *cptr, int *val)
493{
494 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
495 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
496 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300497 return stat;
498 }
499 *val = cap->bounds.top;
500 return 0;
501}
502
503static int ctrl_get_cropcapbw(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.width;
511 return 0;
512}
513
514static int ctrl_get_cropcapbh(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.height;
522 return 0;
523}
524
525static int ctrl_get_cropcapdl(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->defrect.left;
533 return 0;
534}
535
536static int ctrl_get_cropcapdt(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->defrect.top;
544 return 0;
545}
546
547static int ctrl_get_cropcapdw(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.width;
555 return 0;
556}
557
558static int ctrl_get_cropcapdh(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.height;
566 return 0;
567}
568
569static int ctrl_get_cropcappan(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->pixelaspect.numerator;
577 return 0;
578}
579
580static int ctrl_get_cropcappad(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->pixelaspect.denominator;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300588 return 0;
589}
590
Mike Isely3ad9fc32006-09-02 22:37:52 -0300591static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
592{
593 /* Actual maximum depends on the video standard in effect. */
594 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
595 *vp = 480;
596 } else {
597 *vp = 576;
598 }
599 return 0;
600}
601
602static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
603{
Mike Isely989eb152007-11-26 01:53:12 -0300604 /* Actual minimum depends on device digitizer type. */
605 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300606 *vp = 75;
607 } else {
608 *vp = 17;
609 }
610 return 0;
611}
612
Mike Isely1bde0282006-12-27 23:30:13 -0300613static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
614{
615 *vp = cptr->hdw->input_val;
616 return 0;
617}
618
Mike Isely29bf5b12008-04-22 14:45:37 -0300619static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
620{
Mike Isely1cb03b72008-04-21 03:47:43 -0300621 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300622}
623
Mike Isely1bde0282006-12-27 23:30:13 -0300624static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
625{
Mike Isely1cb03b72008-04-21 03:47:43 -0300626 return pvr2_hdw_set_input(cptr->hdw,v);
Mike Isely1bde0282006-12-27 23:30:13 -0300627}
628
629static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
630{
631 return cptr->hdw->input_dirty != 0;
632}
633
634static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
635{
636 cptr->hdw->input_dirty = 0;
637}
638
Mike Isely5549f542006-12-27 23:28:54 -0300639
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300640static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
641{
Mike Isely644afdb2007-01-20 00:19:23 -0300642 unsigned long fv;
643 struct pvr2_hdw *hdw = cptr->hdw;
644 if (hdw->tuner_signal_stale) {
645 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300646 }
Mike Isely644afdb2007-01-20 00:19:23 -0300647 fv = hdw->tuner_signal_info.rangehigh;
648 if (!fv) {
649 /* Safety fallback */
650 *vp = TV_MAX_FREQ;
651 return 0;
652 }
653 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
654 fv = (fv * 125) / 2;
655 } else {
656 fv = fv * 62500;
657 }
658 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300659 return 0;
660}
661
662static int ctrl_freq_min_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) {
667 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300668 }
Mike Isely644afdb2007-01-20 00:19:23 -0300669 fv = hdw->tuner_signal_info.rangelow;
670 if (!fv) {
671 /* Safety fallback */
672 *vp = TV_MIN_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
Mike Iselyb30d2442006-06-25 20:05:01 -0300684static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
685{
686 return cptr->hdw->enc_stale != 0;
687}
688
689static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
690{
691 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300692 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300693}
694
695static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
696{
697 int ret;
698 struct v4l2_ext_controls cs;
699 struct v4l2_ext_control c1;
700 memset(&cs,0,sizeof(cs));
701 memset(&c1,0,sizeof(c1));
702 cs.controls = &c1;
703 cs.count = 1;
704 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300705 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300706 VIDIOC_G_EXT_CTRLS);
707 if (ret) return ret;
708 *vp = c1.value;
709 return 0;
710}
711
712static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
713{
714 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300715 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300716 struct v4l2_ext_controls cs;
717 struct v4l2_ext_control c1;
718 memset(&cs,0,sizeof(cs));
719 memset(&c1,0,sizeof(c1));
720 cs.controls = &c1;
721 cs.count = 1;
722 c1.id = cptr->info->v4l_id;
723 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300724 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
725 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300726 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300727 if (ret == -EBUSY) {
728 /* Oops. cx2341x is telling us it's not safe to change
729 this control while we're capturing. Make a note of this
730 fact so that the pipeline will be stopped the next time
731 controls are committed. Then go on ahead and store this
732 change anyway. */
733 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
734 0, &cs,
735 VIDIOC_S_EXT_CTRLS);
736 if (!ret) hdw->enc_unsafe_stale = !0;
737 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300738 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300739 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300740 return 0;
741}
742
743static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
744{
745 struct v4l2_queryctrl qctrl;
746 struct pvr2_ctl_info *info;
747 qctrl.id = cptr->info->v4l_id;
748 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
749 /* Strip out the const so we can adjust a function pointer. It's
750 OK to do this here because we know this is a dynamically created
751 control, so the underlying storage for the info pointer is (a)
752 private to us, and (b) not in read-only storage. Either we do
753 this or we significantly complicate the underlying control
754 implementation. */
755 info = (struct pvr2_ctl_info *)(cptr->info);
756 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
757 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300758 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300759 }
760 } else {
761 if (!(info->set_value)) {
762 info->set_value = ctrl_cx2341x_set;
763 }
764 }
765 return qctrl.flags;
766}
767
Mike Iselyd8554972006-06-26 20:58:46 -0300768static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
769{
Mike Isely681c7392007-11-26 01:48:52 -0300770 *vp = cptr->hdw->state_pipeline_req;
771 return 0;
772}
773
774static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
775{
776 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300777 return 0;
778}
779
780static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
781{
782 int result = pvr2_hdw_is_hsm(cptr->hdw);
783 *vp = PVR2_CVAL_HSM_FULL;
784 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
785 if (result) *vp = PVR2_CVAL_HSM_HIGH;
786 return 0;
787}
788
789static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
790{
791 *vp = cptr->hdw->std_mask_avail;
792 return 0;
793}
794
795static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
796{
797 struct pvr2_hdw *hdw = cptr->hdw;
798 v4l2_std_id ns;
799 ns = hdw->std_mask_avail;
800 ns = (ns & ~m) | (v & m);
801 if (ns == hdw->std_mask_avail) return 0;
802 hdw->std_mask_avail = ns;
803 pvr2_hdw_internal_set_std_avail(hdw);
804 pvr2_hdw_internal_find_stdenum(hdw);
805 return 0;
806}
807
808static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
809 char *bufPtr,unsigned int bufSize,
810 unsigned int *len)
811{
812 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
813 return 0;
814}
815
816static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
817 const char *bufPtr,unsigned int bufSize,
818 int *mskp,int *valp)
819{
820 int ret;
821 v4l2_std_id id;
822 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
823 if (ret < 0) return ret;
824 if (mskp) *mskp = id;
825 if (valp) *valp = id;
826 return 0;
827}
828
829static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
830{
831 *vp = cptr->hdw->std_mask_cur;
832 return 0;
833}
834
835static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
836{
837 struct pvr2_hdw *hdw = cptr->hdw;
838 v4l2_std_id ns;
839 ns = hdw->std_mask_cur;
840 ns = (ns & ~m) | (v & m);
841 if (ns == hdw->std_mask_cur) return 0;
842 hdw->std_mask_cur = ns;
843 hdw->std_dirty = !0;
844 pvr2_hdw_internal_find_stdenum(hdw);
845 return 0;
846}
847
848static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
849{
850 return cptr->hdw->std_dirty != 0;
851}
852
853static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
854{
855 cptr->hdw->std_dirty = 0;
856}
857
858static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
859{
Mike Isely18103c572007-01-20 00:09:47 -0300860 struct pvr2_hdw *hdw = cptr->hdw;
861 pvr2_i2c_core_status_poll(hdw);
862 *vp = hdw->tuner_signal_info.signal;
863 return 0;
864}
865
866static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
867{
868 int val = 0;
869 unsigned int subchan;
870 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely644afdb2007-01-20 00:19:23 -0300871 pvr2_i2c_core_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300872 subchan = hdw->tuner_signal_info.rxsubchans;
873 if (subchan & V4L2_TUNER_SUB_MONO) {
874 val |= (1 << V4L2_TUNER_MODE_MONO);
875 }
876 if (subchan & V4L2_TUNER_SUB_STEREO) {
877 val |= (1 << V4L2_TUNER_MODE_STEREO);
878 }
879 if (subchan & V4L2_TUNER_SUB_LANG1) {
880 val |= (1 << V4L2_TUNER_MODE_LANG1);
881 }
882 if (subchan & V4L2_TUNER_SUB_LANG2) {
883 val |= (1 << V4L2_TUNER_MODE_LANG2);
884 }
885 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300886 return 0;
887}
888
Mike Iselyd8554972006-06-26 20:58:46 -0300889
890static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
891{
892 struct pvr2_hdw *hdw = cptr->hdw;
893 if (v < 0) return -EINVAL;
894 if (v > hdw->std_enum_cnt) return -EINVAL;
895 hdw->std_enum_cur = v;
896 if (!v) return 0;
897 v--;
898 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
899 hdw->std_mask_cur = hdw->std_defs[v].id;
900 hdw->std_dirty = !0;
901 return 0;
902}
903
904
905static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
906{
907 *vp = cptr->hdw->std_enum_cur;
908 return 0;
909}
910
911
912static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
913{
914 return cptr->hdw->std_dirty != 0;
915}
916
917
918static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
919{
920 cptr->hdw->std_dirty = 0;
921}
922
923
924#define DEFINT(vmin,vmax) \
925 .type = pvr2_ctl_int, \
926 .def.type_int.min_value = vmin, \
927 .def.type_int.max_value = vmax
928
929#define DEFENUM(tab) \
930 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300931 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300932 .def.type_enum.value_names = tab
933
Mike Isely33213962006-06-25 20:04:40 -0300934#define DEFBOOL \
935 .type = pvr2_ctl_bool
936
Mike Iselyd8554972006-06-26 20:58:46 -0300937#define DEFMASK(msk,tab) \
938 .type = pvr2_ctl_bitmask, \
939 .def.type_bitmask.valid_bits = msk, \
940 .def.type_bitmask.bit_names = tab
941
942#define DEFREF(vname) \
943 .set_value = ctrl_set_##vname, \
944 .get_value = ctrl_get_##vname, \
945 .is_dirty = ctrl_isdirty_##vname, \
946 .clear_dirty = ctrl_cleardirty_##vname
947
948
949#define VCREATE_FUNCS(vname) \
950static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
951{*vp = cptr->hdw->vname##_val; return 0;} \
952static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
953{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
954static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
955{return cptr->hdw->vname##_dirty != 0;} \
956static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
957{cptr->hdw->vname##_dirty = 0;}
958
959VCREATE_FUNCS(brightness)
960VCREATE_FUNCS(contrast)
961VCREATE_FUNCS(saturation)
962VCREATE_FUNCS(hue)
963VCREATE_FUNCS(volume)
964VCREATE_FUNCS(balance)
965VCREATE_FUNCS(bass)
966VCREATE_FUNCS(treble)
967VCREATE_FUNCS(mute)
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300968VCREATE_FUNCS(cropl)
969VCREATE_FUNCS(cropt)
970VCREATE_FUNCS(cropw)
971VCREATE_FUNCS(croph)
Mike Iselyc05c0462006-06-25 20:04:25 -0300972VCREATE_FUNCS(audiomode)
973VCREATE_FUNCS(res_hor)
974VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300975VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -0300976
Mike Iselyd8554972006-06-26 20:58:46 -0300977/* Table definition of all controls which can be manipulated */
978static const struct pvr2_ctl_info control_defs[] = {
979 {
980 .v4l_id = V4L2_CID_BRIGHTNESS,
981 .desc = "Brightness",
982 .name = "brightness",
983 .default_value = 128,
984 DEFREF(brightness),
985 DEFINT(0,255),
986 },{
987 .v4l_id = V4L2_CID_CONTRAST,
988 .desc = "Contrast",
989 .name = "contrast",
990 .default_value = 68,
991 DEFREF(contrast),
992 DEFINT(0,127),
993 },{
994 .v4l_id = V4L2_CID_SATURATION,
995 .desc = "Saturation",
996 .name = "saturation",
997 .default_value = 64,
998 DEFREF(saturation),
999 DEFINT(0,127),
1000 },{
1001 .v4l_id = V4L2_CID_HUE,
1002 .desc = "Hue",
1003 .name = "hue",
1004 .default_value = 0,
1005 DEFREF(hue),
1006 DEFINT(-128,127),
1007 },{
1008 .v4l_id = V4L2_CID_AUDIO_VOLUME,
1009 .desc = "Volume",
1010 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -03001011 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -03001012 DEFREF(volume),
1013 DEFINT(0,65535),
1014 },{
1015 .v4l_id = V4L2_CID_AUDIO_BALANCE,
1016 .desc = "Balance",
1017 .name = "balance",
1018 .default_value = 0,
1019 DEFREF(balance),
1020 DEFINT(-32768,32767),
1021 },{
1022 .v4l_id = V4L2_CID_AUDIO_BASS,
1023 .desc = "Bass",
1024 .name = "bass",
1025 .default_value = 0,
1026 DEFREF(bass),
1027 DEFINT(-32768,32767),
1028 },{
1029 .v4l_id = V4L2_CID_AUDIO_TREBLE,
1030 .desc = "Treble",
1031 .name = "treble",
1032 .default_value = 0,
1033 DEFREF(treble),
1034 DEFINT(-32768,32767),
1035 },{
1036 .v4l_id = V4L2_CID_AUDIO_MUTE,
1037 .desc = "Mute",
1038 .name = "mute",
1039 .default_value = 0,
1040 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -03001041 DEFBOOL,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001042 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001043 .desc = "Capture crop left margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001044 .name = "crop_left",
1045 .internal_id = PVR2_CID_CROPL,
1046 .default_value = 0,
1047 DEFREF(cropl),
1048 DEFINT(-129, 340),
1049 .get_min_value = ctrl_cropl_min_get,
1050 .get_max_value = ctrl_cropl_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001051 .get_def_value = ctrl_get_cropcapdl,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001052 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001053 .desc = "Capture crop top margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001054 .name = "crop_top",
1055 .internal_id = PVR2_CID_CROPT,
1056 .default_value = 0,
1057 DEFREF(cropt),
1058 DEFINT(-35, 544),
1059 .get_min_value = ctrl_cropt_min_get,
1060 .get_max_value = ctrl_cropt_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001061 .get_def_value = ctrl_get_cropcapdt,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001062 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001063 .desc = "Capture crop width",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001064 .name = "crop_width",
1065 .internal_id = PVR2_CID_CROPW,
1066 .default_value = 720,
1067 DEFREF(cropw),
Mike Isely432907f2008-08-31 21:02:20 -03001068 .get_max_value = ctrl_cropw_max_get,
1069 .get_def_value = ctrl_get_cropcapdw,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001070 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001071 .desc = "Capture crop height",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001072 .name = "crop_height",
1073 .internal_id = PVR2_CID_CROPH,
1074 .default_value = 480,
1075 DEFREF(croph),
Mike Isely432907f2008-08-31 21:02:20 -03001076 .get_max_value = ctrl_croph_max_get,
1077 .get_def_value = ctrl_get_cropcapdh,
1078 }, {
1079 .desc = "Capture capability pixel aspect numerator",
1080 .name = "cropcap_pixel_numerator",
1081 .internal_id = PVR2_CID_CROPCAPPAN,
1082 .get_value = ctrl_get_cropcappan,
1083 }, {
1084 .desc = "Capture capability pixel aspect denominator",
1085 .name = "cropcap_pixel_denominator",
1086 .internal_id = PVR2_CID_CROPCAPPAD,
1087 .get_value = ctrl_get_cropcappad,
1088 }, {
1089 .desc = "Capture capability bounds top",
1090 .name = "cropcap_bounds_top",
1091 .internal_id = PVR2_CID_CROPCAPBT,
1092 .get_value = ctrl_get_cropcapbt,
1093 }, {
1094 .desc = "Capture capability bounds left",
1095 .name = "cropcap_bounds_left",
1096 .internal_id = PVR2_CID_CROPCAPBL,
1097 .get_value = ctrl_get_cropcapbl,
1098 }, {
1099 .desc = "Capture capability bounds width",
1100 .name = "cropcap_bounds_width",
1101 .internal_id = PVR2_CID_CROPCAPBW,
1102 .get_value = ctrl_get_cropcapbw,
1103 }, {
1104 .desc = "Capture capability bounds height",
1105 .name = "cropcap_bounds_height",
1106 .internal_id = PVR2_CID_CROPCAPBH,
1107 .get_value = ctrl_get_cropcapbh,
Mike Iselyd8554972006-06-26 20:58:46 -03001108 },{
Mike Iselyc05c0462006-06-25 20:04:25 -03001109 .desc = "Video Source",
1110 .name = "input",
1111 .internal_id = PVR2_CID_INPUT,
1112 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -03001113 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -03001114 DEFREF(input),
1115 DEFENUM(control_values_input),
1116 },{
1117 .desc = "Audio Mode",
1118 .name = "audio_mode",
1119 .internal_id = PVR2_CID_AUDIOMODE,
1120 .default_value = V4L2_TUNER_MODE_STEREO,
1121 DEFREF(audiomode),
1122 DEFENUM(control_values_audiomode),
1123 },{
1124 .desc = "Horizontal capture resolution",
1125 .name = "resolution_hor",
1126 .internal_id = PVR2_CID_HRES,
1127 .default_value = 720,
1128 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001129 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -03001130 },{
1131 .desc = "Vertical capture resolution",
1132 .name = "resolution_ver",
1133 .internal_id = PVR2_CID_VRES,
1134 .default_value = 480,
1135 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001136 DEFINT(17,576),
1137 /* Hook in check for video standard and adjust maximum
1138 depending on the standard. */
1139 .get_max_value = ctrl_vres_max_get,
1140 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -03001141 },{
Mike Iselyb30d2442006-06-25 20:05:01 -03001142 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -03001143 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
1144 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -03001145 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -03001146 DEFREF(srate),
1147 DEFENUM(control_values_srate),
1148 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001149 .desc = "Tuner Frequency (Hz)",
1150 .name = "frequency",
1151 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -03001152 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -03001153 .set_value = ctrl_freq_set,
1154 .get_value = ctrl_freq_get,
1155 .is_dirty = ctrl_freq_is_dirty,
1156 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -03001157 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -03001158 /* Hook in check for input value (tv/radio) and adjust
1159 max/min values accordingly */
1160 .get_max_value = ctrl_freq_max_get,
1161 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001162 },{
1163 .desc = "Channel",
1164 .name = "channel",
1165 .set_value = ctrl_channel_set,
1166 .get_value = ctrl_channel_get,
1167 DEFINT(0,FREQTABLE_SIZE),
1168 },{
1169 .desc = "Channel Program Frequency",
1170 .name = "freq_table_value",
1171 .set_value = ctrl_channelfreq_set,
1172 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -03001173 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -03001174 /* Hook in check for input value (tv/radio) and adjust
1175 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -03001176 .get_max_value = ctrl_freq_max_get,
1177 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001178 },{
1179 .desc = "Channel Program ID",
1180 .name = "freq_table_channel",
1181 .set_value = ctrl_channelprog_set,
1182 .get_value = ctrl_channelprog_get,
1183 DEFINT(0,FREQTABLE_SIZE),
1184 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001185 .desc = "Streaming Enabled",
1186 .name = "streaming_enabled",
1187 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -03001188 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -03001189 },{
1190 .desc = "USB Speed",
1191 .name = "usb_speed",
1192 .get_value = ctrl_hsm_get,
1193 DEFENUM(control_values_hsm),
1194 },{
Mike Isely681c7392007-11-26 01:48:52 -03001195 .desc = "Master State",
1196 .name = "master_state",
1197 .get_value = ctrl_masterstate_get,
1198 DEFENUM(pvr2_state_names),
1199 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001200 .desc = "Signal Present",
1201 .name = "signal_present",
1202 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -03001203 DEFINT(0,65535),
1204 },{
1205 .desc = "Audio Modes Present",
1206 .name = "audio_modes_present",
1207 .get_value = ctrl_audio_modes_present_get,
1208 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
1209 v4l. Nothing outside of this module cares about this,
1210 but I reuse it in order to also reuse the
1211 control_values_audiomode string table. */
1212 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
1213 (1 << V4L2_TUNER_MODE_STEREO)|
1214 (1 << V4L2_TUNER_MODE_LANG1)|
1215 (1 << V4L2_TUNER_MODE_LANG2)),
1216 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -03001217 },{
1218 .desc = "Video Standards Available Mask",
1219 .name = "video_standard_mask_available",
1220 .internal_id = PVR2_CID_STDAVAIL,
1221 .skip_init = !0,
1222 .get_value = ctrl_stdavail_get,
1223 .set_value = ctrl_stdavail_set,
1224 .val_to_sym = ctrl_std_val_to_sym,
1225 .sym_to_val = ctrl_std_sym_to_val,
1226 .type = pvr2_ctl_bitmask,
1227 },{
1228 .desc = "Video Standards In Use Mask",
1229 .name = "video_standard_mask_active",
1230 .internal_id = PVR2_CID_STDCUR,
1231 .skip_init = !0,
1232 .get_value = ctrl_stdcur_get,
1233 .set_value = ctrl_stdcur_set,
1234 .is_dirty = ctrl_stdcur_is_dirty,
1235 .clear_dirty = ctrl_stdcur_clear_dirty,
1236 .val_to_sym = ctrl_std_val_to_sym,
1237 .sym_to_val = ctrl_std_sym_to_val,
1238 .type = pvr2_ctl_bitmask,
1239 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001240 .desc = "Video Standard Name",
1241 .name = "video_standard",
1242 .internal_id = PVR2_CID_STDENUM,
1243 .skip_init = !0,
1244 .get_value = ctrl_stdenumcur_get,
1245 .set_value = ctrl_stdenumcur_set,
1246 .is_dirty = ctrl_stdenumcur_is_dirty,
1247 .clear_dirty = ctrl_stdenumcur_clear_dirty,
1248 .type = pvr2_ctl_enum,
1249 }
1250};
1251
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001252#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -03001253
1254
1255const char *pvr2_config_get_name(enum pvr2_config cfg)
1256{
1257 switch (cfg) {
1258 case pvr2_config_empty: return "empty";
1259 case pvr2_config_mpeg: return "mpeg";
1260 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -03001261 case pvr2_config_pcm: return "pcm";
1262 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -03001263 }
1264 return "<unknown>";
1265}
1266
1267
1268struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1269{
1270 return hdw->usb_dev;
1271}
1272
1273
1274unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1275{
1276 return hdw->serial_number;
1277}
1278
Mike Isely31a18542007-04-08 01:11:47 -03001279
1280const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1281{
1282 return hdw->bus_info;
1283}
1284
1285
Mike Isely13a88792009-01-14 04:22:56 -03001286const char *pvr2_hdw_get_device_identifier(struct pvr2_hdw *hdw)
1287{
1288 return hdw->identifier;
1289}
1290
1291
Mike Isely1bde0282006-12-27 23:30:13 -03001292unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1293{
1294 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1295}
1296
1297/* Set the currently tuned frequency and account for all possible
1298 driver-core side effects of this action. */
Adrian Bunkf55a8712008-04-18 05:38:56 -03001299static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
Mike Isely1bde0282006-12-27 23:30:13 -03001300{
Mike Isely7c74e572007-01-20 00:15:41 -03001301 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001302 if (hdw->freqSelector) {
1303 /* Swing over to radio frequency selection */
1304 hdw->freqSelector = 0;
1305 hdw->freqDirty = !0;
1306 }
Mike Isely1bde0282006-12-27 23:30:13 -03001307 if (hdw->freqValRadio != val) {
1308 hdw->freqValRadio = val;
1309 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001310 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001311 }
Mike Isely7c74e572007-01-20 00:15:41 -03001312 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001313 if (!(hdw->freqSelector)) {
1314 /* Swing over to television frequency selection */
1315 hdw->freqSelector = 1;
1316 hdw->freqDirty = !0;
1317 }
Mike Isely1bde0282006-12-27 23:30:13 -03001318 if (hdw->freqValTelevision != val) {
1319 hdw->freqValTelevision = val;
1320 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001321 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001322 }
Mike Isely1bde0282006-12-27 23:30:13 -03001323 }
1324}
1325
Mike Iselyd8554972006-06-26 20:58:46 -03001326int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1327{
1328 return hdw->unit_number;
1329}
1330
1331
1332/* Attempt to locate one of the given set of files. Messages are logged
1333 appropriate to what has been found. The return value will be 0 or
1334 greater on success (it will be the index of the file name found) and
1335 fw_entry will be filled in. Otherwise a negative error is returned on
1336 failure. If the return value is -ENOENT then no viable firmware file
1337 could be located. */
1338static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1339 const struct firmware **fw_entry,
1340 const char *fwtypename,
1341 unsigned int fwcount,
1342 const char *fwnames[])
1343{
1344 unsigned int idx;
1345 int ret = -EINVAL;
1346 for (idx = 0; idx < fwcount; idx++) {
1347 ret = request_firmware(fw_entry,
1348 fwnames[idx],
1349 &hdw->usb_dev->dev);
1350 if (!ret) {
1351 trace_firmware("Located %s firmware: %s;"
1352 " uploading...",
1353 fwtypename,
1354 fwnames[idx]);
1355 return idx;
1356 }
1357 if (ret == -ENOENT) continue;
1358 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1359 "request_firmware fatal error with code=%d",ret);
1360 return ret;
1361 }
1362 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1363 "***WARNING***"
1364 " Device %s firmware"
1365 " seems to be missing.",
1366 fwtypename);
1367 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1368 "Did you install the pvrusb2 firmware files"
1369 " in their proper location?");
1370 if (fwcount == 1) {
1371 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1372 "request_firmware unable to locate %s file %s",
1373 fwtypename,fwnames[0]);
1374 } else {
1375 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1376 "request_firmware unable to locate"
1377 " one of the following %s files:",
1378 fwtypename);
1379 for (idx = 0; idx < fwcount; idx++) {
1380 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1381 "request_firmware: Failed to find %s",
1382 fwnames[idx]);
1383 }
1384 }
1385 return ret;
1386}
1387
1388
1389/*
1390 * pvr2_upload_firmware1().
1391 *
1392 * Send the 8051 firmware to the device. After the upload, arrange for
1393 * device to re-enumerate.
1394 *
1395 * NOTE : the pointer to the firmware data given by request_firmware()
1396 * is not suitable for an usb transaction.
1397 *
1398 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001399static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001400{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001401 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001402 void *fw_ptr;
1403 unsigned int pipe;
1404 int ret;
1405 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001406
Mike Isely989eb152007-11-26 01:53:12 -03001407 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001408 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001409 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1410 "Connected device type defines"
1411 " no firmware to upload; ignoring firmware");
1412 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001413 }
1414
Mike Iselyd8554972006-06-26 20:58:46 -03001415 hdw->fw1_state = FW1_STATE_FAILED; // default result
1416
1417 trace_firmware("pvr2_upload_firmware1");
1418
1419 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001420 hdw->hdw_desc->fx2_firmware.cnt,
1421 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001422 if (ret < 0) {
1423 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1424 return ret;
1425 }
1426
1427 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1428 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1429
1430 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1431
1432 if (fw_entry->size != 0x2000){
1433 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1434 release_firmware(fw_entry);
1435 return -ENOMEM;
1436 }
1437
1438 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1439 if (fw_ptr == NULL){
1440 release_firmware(fw_entry);
1441 return -ENOMEM;
1442 }
1443
1444 /* We have to hold the CPU during firmware upload. */
1445 pvr2_hdw_cpureset_assert(hdw,1);
1446
1447 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1448 chunk. */
1449
1450 ret = 0;
1451 for(address = 0; address < fw_entry->size; address += 0x800) {
1452 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1453 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1454 0, fw_ptr, 0x800, HZ);
1455 }
1456
1457 trace_firmware("Upload done, releasing device's CPU");
1458
1459 /* Now release the CPU. It will disconnect and reconnect later. */
1460 pvr2_hdw_cpureset_assert(hdw,0);
1461
1462 kfree(fw_ptr);
1463 release_firmware(fw_entry);
1464
1465 trace_firmware("Upload done (%d bytes sent)",ret);
1466
1467 /* We should have written 8192 bytes */
1468 if (ret == 8192) {
1469 hdw->fw1_state = FW1_STATE_RELOAD;
1470 return 0;
1471 }
1472
1473 return -EIO;
1474}
1475
1476
1477/*
1478 * pvr2_upload_firmware2()
1479 *
1480 * This uploads encoder firmware on endpoint 2.
1481 *
1482 */
1483
1484int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1485{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001486 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001487 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001488 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001489 int actual_length;
1490 int ret = 0;
1491 int fwidx;
1492 static const char *fw_files[] = {
1493 CX2341X_FIRM_ENC_FILENAME,
1494 };
1495
Mike Isely989eb152007-11-26 01:53:12 -03001496 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001497 return 0;
1498 }
1499
Mike Iselyd8554972006-06-26 20:58:46 -03001500 trace_firmware("pvr2_upload_firmware2");
1501
1502 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001503 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001504 if (ret < 0) return ret;
1505 fwidx = ret;
1506 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001507 /* Since we're about to completely reinitialize the encoder,
1508 invalidate our cached copy of its configuration state. Next
1509 time we configure the encoder, then we'll fully configure it. */
1510 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001511
Mike Iselyd913d632008-04-06 04:04:35 -03001512 /* Encoder is about to be reset so note that as far as we're
1513 concerned now, the encoder has never been run. */
1514 del_timer_sync(&hdw->encoder_run_timer);
1515 if (hdw->state_encoder_runok) {
1516 hdw->state_encoder_runok = 0;
1517 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1518 }
1519
Mike Iselyd8554972006-06-26 20:58:46 -03001520 /* First prepare firmware loading */
1521 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1522 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1523 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1524 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1525 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1526 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1527 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1528 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1529 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1530 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1531 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1532 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1533 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1534 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1535 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1536 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001537 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1538 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001539
1540 if (ret) {
1541 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1542 "firmware2 upload prep failed, ret=%d",ret);
1543 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001544 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001545 }
1546
1547 /* Now send firmware */
1548
1549 fw_len = fw_entry->size;
1550
Mike Isely90060d32007-02-08 02:02:53 -03001551 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001552 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1553 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001554 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001555 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001556 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001557 ret = -EINVAL;
1558 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001559 }
1560
1561 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1562 if (fw_ptr == NULL){
1563 release_firmware(fw_entry);
1564 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1565 "failed to allocate memory for firmware2 upload");
Mike Isely21684ba2008-04-21 03:49:33 -03001566 ret = -ENOMEM;
1567 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001568 }
1569
1570 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1571
Mike Isely90060d32007-02-08 02:02:53 -03001572 fw_done = 0;
1573 for (fw_done = 0; fw_done < fw_len;) {
1574 bcnt = fw_len - fw_done;
1575 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1576 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1577 /* Usbsnoop log shows that we must swap bytes... */
Mike Isely5f33df12008-08-30 15:09:31 -03001578 /* Some background info: The data being swapped here is a
1579 firmware image destined for the mpeg encoder chip that
1580 lives at the other end of a USB endpoint. The encoder
1581 chip always talks in 32 bit chunks and its storage is
1582 organized into 32 bit words. However from the file
1583 system to the encoder chip everything is purely a byte
1584 stream. The firmware file's contents are always 32 bit
1585 swapped from what the encoder expects. Thus the need
1586 always exists to swap the bytes regardless of the endian
1587 type of the host processor and therefore swab32() makes
1588 the most sense. */
Mike Isely90060d32007-02-08 02:02:53 -03001589 for (icnt = 0; icnt < bcnt/4 ; icnt++)
Harvey Harrison513edce2008-08-18 17:38:01 -03001590 ((u32 *)fw_ptr)[icnt] = swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001591
Mike Isely90060d32007-02-08 02:02:53 -03001592 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001593 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001594 ret |= (actual_length != bcnt);
1595 if (ret) break;
1596 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001597 }
1598
1599 trace_firmware("upload of %s : %i / %i ",
1600 fw_files[fwidx],fw_done,fw_len);
1601
1602 kfree(fw_ptr);
1603 release_firmware(fw_entry);
1604
1605 if (ret) {
1606 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1607 "firmware2 upload transfer failure");
Mike Isely21684ba2008-04-21 03:49:33 -03001608 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001609 }
1610
1611 /* Finish upload */
1612
1613 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1614 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001615 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001616
1617 if (ret) {
1618 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1619 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001620 }
Mike Isely21684ba2008-04-21 03:49:33 -03001621
1622 done:
Mike Isely1df59f02008-04-21 03:50:39 -03001623 if (hdw->hdw_desc->signal_routing_scheme ==
1624 PVR2_ROUTING_SCHEME_GOTVIEW) {
1625 /* Ensure that GPIO 11 is set to output for GOTVIEW
1626 hardware. */
1627 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1628 }
Mike Iselyd8554972006-06-26 20:58:46 -03001629 return ret;
1630}
1631
1632
Mike Isely681c7392007-11-26 01:48:52 -03001633static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001634{
Mike Isely681c7392007-11-26 01:48:52 -03001635 if (st < ARRAY_SIZE(pvr2_state_names)) {
1636 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001637 }
Mike Isely681c7392007-11-26 01:48:52 -03001638 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001639}
1640
Mike Isely681c7392007-11-26 01:48:52 -03001641static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001642{
Mike Isely681c7392007-11-26 01:48:52 -03001643 if (!hdw->decoder_ctrl) {
1644 if (!hdw->flag_decoder_missed) {
1645 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1646 "WARNING: No decoder present");
1647 hdw->flag_decoder_missed = !0;
1648 trace_stbit("flag_decoder_missed",
1649 hdw->flag_decoder_missed);
1650 }
1651 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001652 }
Mike Isely681c7392007-11-26 01:48:52 -03001653 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt,enablefl);
Mike Iselyd8554972006-06-26 20:58:46 -03001654 return 0;
1655}
1656
1657
Mike Isely681c7392007-11-26 01:48:52 -03001658void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1659{
1660 if (hdw->decoder_ctrl == ptr) return;
1661 hdw->decoder_ctrl = ptr;
1662 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1663 hdw->flag_decoder_missed = 0;
1664 trace_stbit("flag_decoder_missed",
1665 hdw->flag_decoder_missed);
1666 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1667 "Decoder has appeared");
1668 pvr2_hdw_state_sched(hdw);
1669 }
1670}
1671
1672
1673int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1674{
1675 return hdw->master_state;
1676}
1677
1678
1679static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1680{
1681 if (!hdw->flag_tripped) return 0;
1682 hdw->flag_tripped = 0;
1683 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1684 "Clearing driver error statuss");
1685 return !0;
1686}
1687
1688
1689int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1690{
1691 int fl;
1692 LOCK_TAKE(hdw->big_lock); do {
1693 fl = pvr2_hdw_untrip_unlocked(hdw);
1694 } while (0); LOCK_GIVE(hdw->big_lock);
1695 if (fl) pvr2_hdw_state_sched(hdw);
1696 return 0;
1697}
1698
1699
Mike Isely681c7392007-11-26 01:48:52 -03001700
1701
Mike Iselyd8554972006-06-26 20:58:46 -03001702int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1703{
Mike Isely681c7392007-11-26 01:48:52 -03001704 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001705}
1706
1707
1708int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1709{
Mike Isely681c7392007-11-26 01:48:52 -03001710 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001711 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001712 pvr2_hdw_untrip_unlocked(hdw);
1713 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1714 hdw->state_pipeline_req = enable_flag != 0;
1715 pvr2_trace(PVR2_TRACE_START_STOP,
1716 "/*--TRACE_STREAM--*/ %s",
1717 enable_flag ? "enable" : "disable");
1718 }
1719 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001720 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001721 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1722 if (enable_flag) {
1723 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1724 if (st != PVR2_STATE_READY) return -EIO;
1725 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1726 }
1727 }
Mike Iselyd8554972006-06-26 20:58:46 -03001728 return 0;
1729}
1730
1731
1732int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1733{
Mike Isely681c7392007-11-26 01:48:52 -03001734 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001735 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001736 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1737 hdw->desired_stream_type = config;
1738 hdw->state_pipeline_config = 0;
1739 trace_stbit("state_pipeline_config",
1740 hdw->state_pipeline_config);
1741 pvr2_hdw_state_sched(hdw);
1742 }
Mike Iselyd8554972006-06-26 20:58:46 -03001743 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001744 if (fl) return 0;
1745 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001746}
1747
1748
1749static int get_default_tuner_type(struct pvr2_hdw *hdw)
1750{
1751 int unit_number = hdw->unit_number;
1752 int tp = -1;
1753 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1754 tp = tuner[unit_number];
1755 }
1756 if (tp < 0) return -EINVAL;
1757 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001758 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001759 return 0;
1760}
1761
1762
1763static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1764{
1765 int unit_number = hdw->unit_number;
1766 int tp = 0;
1767 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1768 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001769 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001770 }
Mike Isely6a540252007-12-02 23:51:34 -03001771 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001772}
1773
1774
1775static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1776{
1777 int unit_number = hdw->unit_number;
1778 int tp = 0;
1779 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1780 tp = tolerance[unit_number];
1781 }
1782 return tp;
1783}
1784
1785
1786static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1787{
1788 /* Try a harmless request to fetch the eeprom's address over
1789 endpoint 1. See what happens. Only the full FX2 image can
1790 respond to this. If this probe fails then likely the FX2
1791 firmware needs be loaded. */
1792 int result;
1793 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001794 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001795 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1796 hdw->cmd_buffer,1,
1797 hdw->cmd_buffer,1);
1798 if (result < 0) break;
1799 } while(0); LOCK_GIVE(hdw->ctl_lock);
1800 if (result) {
1801 pvr2_trace(PVR2_TRACE_INIT,
1802 "Probe of device endpoint 1 result status %d",
1803 result);
1804 } else {
1805 pvr2_trace(PVR2_TRACE_INIT,
1806 "Probe of device endpoint 1 succeeded");
1807 }
1808 return result == 0;
1809}
1810
Mike Isely9f66d4e2007-09-08 22:28:51 -03001811struct pvr2_std_hack {
1812 v4l2_std_id pat; /* Pattern to match */
1813 v4l2_std_id msk; /* Which bits we care about */
1814 v4l2_std_id std; /* What additional standards or default to set */
1815};
1816
1817/* This data structure labels specific combinations of standards from
1818 tveeprom that we'll try to recognize. If we recognize one, then assume
1819 a specified default standard to use. This is here because tveeprom only
1820 tells us about available standards not the intended default standard (if
1821 any) for the device in question. We guess the default based on what has
1822 been reported as available. Note that this is only for guessing a
1823 default - which can always be overridden explicitly - and if the user
1824 has otherwise named a default then that default will always be used in
1825 place of this table. */
Tobias Klauserebff0332008-04-22 14:45:45 -03001826static const struct pvr2_std_hack std_eeprom_maps[] = {
Mike Isely9f66d4e2007-09-08 22:28:51 -03001827 { /* PAL(B/G) */
1828 .pat = V4L2_STD_B|V4L2_STD_GH,
1829 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1830 },
1831 { /* NTSC(M) */
1832 .pat = V4L2_STD_MN,
1833 .std = V4L2_STD_NTSC_M,
1834 },
1835 { /* PAL(I) */
1836 .pat = V4L2_STD_PAL_I,
1837 .std = V4L2_STD_PAL_I,
1838 },
1839 { /* SECAM(L/L') */
1840 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1841 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1842 },
1843 { /* PAL(D/D1/K) */
1844 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001845 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001846 },
1847};
1848
Mike Iselyd8554972006-06-26 20:58:46 -03001849static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1850{
1851 char buf[40];
1852 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001853 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001854
1855 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001856 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001857
1858 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001859 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001860 "Supported video standard(s) reported available"
1861 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001862 bcnt,buf);
1863
1864 hdw->std_mask_avail = hdw->std_mask_eeprom;
1865
Mike Isely3d290bd2007-12-03 01:47:12 -03001866 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001867 if (std2) {
1868 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001869 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001870 "Expanding supported video standards"
1871 " to include: %.*s",
1872 bcnt,buf);
1873 hdw->std_mask_avail |= std2;
1874 }
1875
1876 pvr2_hdw_internal_set_std_avail(hdw);
1877
1878 if (std1) {
1879 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001880 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001881 "Initial video standard forced to %.*s",
1882 bcnt,buf);
1883 hdw->std_mask_cur = std1;
1884 hdw->std_dirty = !0;
1885 pvr2_hdw_internal_find_stdenum(hdw);
1886 return;
1887 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001888 if (std3) {
1889 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1890 pvr2_trace(PVR2_TRACE_STD,
1891 "Initial video standard"
1892 " (determined by device type): %.*s",bcnt,buf);
1893 hdw->std_mask_cur = std3;
1894 hdw->std_dirty = !0;
1895 pvr2_hdw_internal_find_stdenum(hdw);
1896 return;
1897 }
Mike Iselyd8554972006-06-26 20:58:46 -03001898
Mike Isely9f66d4e2007-09-08 22:28:51 -03001899 {
1900 unsigned int idx;
1901 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1902 if (std_eeprom_maps[idx].msk ?
1903 ((std_eeprom_maps[idx].pat ^
1904 hdw->std_mask_eeprom) &
1905 std_eeprom_maps[idx].msk) :
1906 (std_eeprom_maps[idx].pat !=
1907 hdw->std_mask_eeprom)) continue;
1908 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1909 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001910 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001911 "Initial video standard guessed as %.*s",
1912 bcnt,buf);
1913 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1914 hdw->std_dirty = !0;
1915 pvr2_hdw_internal_find_stdenum(hdw);
1916 return;
1917 }
1918 }
1919
Mike Iselyd8554972006-06-26 20:58:46 -03001920 if (hdw->std_enum_cnt > 1) {
1921 // Autoselect the first listed standard
1922 hdw->std_enum_cur = 1;
1923 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1924 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001925 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001926 "Initial video standard auto-selected to %s",
1927 hdw->std_defs[hdw->std_enum_cur-1].name);
1928 return;
1929 }
1930
Mike Isely0885ba12006-06-25 21:30:47 -03001931 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001932 "Unable to select a viable initial video standard");
1933}
1934
1935
1936static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1937{
1938 int ret;
1939 unsigned int idx;
1940 struct pvr2_ctrl *cptr;
1941 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03001942 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001943 if (!reloadFl) {
1944 reloadFl =
1945 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
1946 == 0);
1947 if (reloadFl) {
1948 pvr2_trace(PVR2_TRACE_INIT,
1949 "USB endpoint config looks strange"
1950 "; possibly firmware needs to be"
1951 " loaded");
1952 }
1953 }
1954 if (!reloadFl) {
1955 reloadFl = !pvr2_hdw_check_firmware(hdw);
1956 if (reloadFl) {
1957 pvr2_trace(PVR2_TRACE_INIT,
1958 "Check for FX2 firmware failed"
1959 "; possibly firmware needs to be"
1960 " loaded");
1961 }
1962 }
Mike Iselyd8554972006-06-26 20:58:46 -03001963 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03001964 if (pvr2_upload_firmware1(hdw) != 0) {
1965 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1966 "Failure uploading firmware1");
1967 }
1968 return;
Mike Iselyd8554972006-06-26 20:58:46 -03001969 }
1970 }
Mike Iselyd8554972006-06-26 20:58:46 -03001971 hdw->fw1_state = FW1_STATE_OK;
1972
Mike Iselyd8554972006-06-26 20:58:46 -03001973 if (!pvr2_hdw_dev_ok(hdw)) return;
1974
Mike Isely989eb152007-11-26 01:53:12 -03001975 for (idx = 0; idx < hdw->hdw_desc->client_modules.cnt; idx++) {
1976 request_module(hdw->hdw_desc->client_modules.lst[idx]);
Mike Iselyd8554972006-06-26 20:58:46 -03001977 }
1978
Mike Isely989eb152007-11-26 01:53:12 -03001979 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03001980 pvr2_hdw_cmd_powerup(hdw);
1981 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03001982 }
1983
Mike Isely31335b12008-07-25 19:35:31 -03001984 /* Take the IR chip out of reset, if appropriate */
1985 if (hdw->hdw_desc->ir_scheme == PVR2_IR_SCHEME_ZILOG) {
1986 pvr2_issue_simple_cmd(hdw,
1987 FX2CMD_HCW_ZILOG_RESET |
1988 (1 << 8) |
1989 ((0) << 16));
1990 }
1991
Mike Iselyd8554972006-06-26 20:58:46 -03001992 // This step MUST happen after the earlier powerup step.
1993 pvr2_i2c_core_init(hdw);
1994 if (!pvr2_hdw_dev_ok(hdw)) return;
1995
Mike Iselyc05c0462006-06-25 20:04:25 -03001996 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001997 cptr = hdw->controls + idx;
1998 if (cptr->info->skip_init) continue;
1999 if (!cptr->info->set_value) continue;
2000 cptr->info->set_value(cptr,~0,cptr->info->default_value);
2001 }
2002
Mike Isely1bde0282006-12-27 23:30:13 -03002003 /* Set up special default values for the television and radio
2004 frequencies here. It's not really important what these defaults
2005 are, but I set them to something usable in the Chicago area just
2006 to make driver testing a little easier. */
2007
Michael Krufky5a4f5da62008-05-11 16:37:50 -03002008 hdw->freqValTelevision = default_tv_freq;
2009 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03002010
Mike Iselyd8554972006-06-26 20:58:46 -03002011 // Do not use pvr2_reset_ctl_endpoints() here. It is not
2012 // thread-safe against the normal pvr2_send_request() mechanism.
2013 // (We should make it thread safe).
2014
Mike Iselyaaf78842007-11-26 02:04:11 -03002015 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
2016 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002017 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03002018 if (ret < 0) {
2019 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2020 "Unable to determine location of eeprom,"
2021 " skipping");
2022 } else {
2023 hdw->eeprom_addr = ret;
2024 pvr2_eeprom_analyze(hdw);
2025 if (!pvr2_hdw_dev_ok(hdw)) return;
2026 }
2027 } else {
2028 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
2029 hdw->tuner_updated = !0;
2030 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03002031 }
2032
Mike Isely13a88792009-01-14 04:22:56 -03002033 if (hdw->serial_number) {
2034 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2035 "sn-%lu", hdw->serial_number);
2036 } else if (hdw->unit_number >= 0) {
2037 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2038 "unit-%c",
2039 hdw->unit_number + 'a');
2040 } else {
2041 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2042 "unit-??");
2043 }
2044 hdw->identifier[idx] = 0;
2045
Mike Iselyd8554972006-06-26 20:58:46 -03002046 pvr2_hdw_setup_std(hdw);
2047
2048 if (!get_default_tuner_type(hdw)) {
2049 pvr2_trace(PVR2_TRACE_INIT,
2050 "pvr2_hdw_setup: Tuner type overridden to %d",
2051 hdw->tuner_type);
2052 }
2053
Mike Iselyd8554972006-06-26 20:58:46 -03002054 pvr2_i2c_core_check_stale(hdw);
2055 hdw->tuner_updated = 0;
2056
2057 if (!pvr2_hdw_dev_ok(hdw)) return;
2058
Mike Isely1df59f02008-04-21 03:50:39 -03002059 if (hdw->hdw_desc->signal_routing_scheme ==
2060 PVR2_ROUTING_SCHEME_GOTVIEW) {
2061 /* Ensure that GPIO 11 is set to output for GOTVIEW
2062 hardware. */
2063 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
2064 }
2065
Mike Isely681c7392007-11-26 01:48:52 -03002066 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002067
2068 hdw->vid_stream = pvr2_stream_create();
2069 if (!pvr2_hdw_dev_ok(hdw)) return;
2070 pvr2_trace(PVR2_TRACE_INIT,
2071 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
2072 if (hdw->vid_stream) {
2073 idx = get_default_error_tolerance(hdw);
2074 if (idx) {
2075 pvr2_trace(PVR2_TRACE_INIT,
2076 "pvr2_hdw_setup: video stream %p"
2077 " setting tolerance %u",
2078 hdw->vid_stream,idx);
2079 }
2080 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
2081 PVR2_VID_ENDPOINT,idx);
2082 }
2083
2084 if (!pvr2_hdw_dev_ok(hdw)) return;
2085
Mike Iselyd8554972006-06-26 20:58:46 -03002086 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03002087
2088 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002089}
2090
2091
Mike Isely681c7392007-11-26 01:48:52 -03002092/* Set up the structure and attempt to put the device into a usable state.
2093 This can be a time-consuming operation, which is why it is not done
2094 internally as part of the create() step. */
2095static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002096{
2097 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002098 do {
Mike Iselyd8554972006-06-26 20:58:46 -03002099 pvr2_hdw_setup_low(hdw);
2100 pvr2_trace(PVR2_TRACE_INIT,
2101 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03002102 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03002103 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03002104 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03002105 pvr2_trace(
2106 PVR2_TRACE_INFO,
2107 "Device initialization"
2108 " completed successfully.");
2109 break;
2110 }
2111 if (hdw->fw1_state == FW1_STATE_RELOAD) {
2112 pvr2_trace(
2113 PVR2_TRACE_INFO,
2114 "Device microcontroller firmware"
2115 " (re)loaded; it should now reset"
2116 " and reconnect.");
2117 break;
2118 }
2119 pvr2_trace(
2120 PVR2_TRACE_ERROR_LEGS,
2121 "Device initialization was not successful.");
2122 if (hdw->fw1_state == FW1_STATE_MISSING) {
2123 pvr2_trace(
2124 PVR2_TRACE_ERROR_LEGS,
2125 "Giving up since device"
2126 " microcontroller firmware"
2127 " appears to be missing.");
2128 break;
2129 }
2130 }
2131 if (procreload) {
2132 pvr2_trace(
2133 PVR2_TRACE_ERROR_LEGS,
2134 "Attempting pvrusb2 recovery by reloading"
2135 " primary firmware.");
2136 pvr2_trace(
2137 PVR2_TRACE_ERROR_LEGS,
2138 "If this works, device should disconnect"
2139 " and reconnect in a sane state.");
2140 hdw->fw1_state = FW1_STATE_UNKNOWN;
2141 pvr2_upload_firmware1(hdw);
2142 } else {
2143 pvr2_trace(
2144 PVR2_TRACE_ERROR_LEGS,
2145 "***WARNING*** pvrusb2 device hardware"
2146 " appears to be jammed"
2147 " and I can't clear it.");
2148 pvr2_trace(
2149 PVR2_TRACE_ERROR_LEGS,
2150 "You might need to power cycle"
2151 " the pvrusb2 device"
2152 " in order to recover.");
2153 }
Mike Isely681c7392007-11-26 01:48:52 -03002154 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03002155 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002156}
2157
2158
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002159/* Perform second stage initialization. Set callback pointer first so that
2160 we can avoid a possible initialization race (if the kernel thread runs
2161 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03002162int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
2163 void (*callback_func)(void *),
2164 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002165{
2166 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03002167 if (hdw->flag_disconnected) {
2168 /* Handle a race here: If we're already
2169 disconnected by this point, then give up. If we
2170 get past this then we'll remain connected for
2171 the duration of initialization since the entire
2172 initialization sequence is now protected by the
2173 big_lock. */
2174 break;
2175 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002176 hdw->state_data = callback_data;
2177 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03002178 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002179 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03002180 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002181}
2182
2183
2184/* Create, set up, and return a structure for interacting with the
2185 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03002186struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2187 const struct usb_device_id *devid)
2188{
Mike Isely7fb20fa2008-04-22 14:45:37 -03002189 unsigned int idx,cnt1,cnt2,m;
Mike Iselyfe15f132008-08-30 18:11:40 -03002190 struct pvr2_hdw *hdw = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002191 int valid_std_mask;
2192 struct pvr2_ctrl *cptr;
Mike Isely989eb152007-11-26 01:53:12 -03002193 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03002194 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03002195 struct v4l2_queryctrl qctrl;
2196 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03002197
Mike Iselyd130fa82007-12-08 17:20:06 -03002198 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03002199
Mike Iselyfe15f132008-08-30 18:11:40 -03002200 if (hdw_desc == NULL) {
2201 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
2202 " No device description pointer,"
2203 " unable to continue.");
2204 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
2205 " please contact Mike Isely <isely@pobox.com>"
2206 " to get it included in the driver\n");
2207 goto fail;
2208 }
2209
Mike Iselyca545f72007-01-20 00:37:11 -03002210 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03002211 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03002212 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03002213 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03002214
2215 init_timer(&hdw->quiescent_timer);
2216 hdw->quiescent_timer.data = (unsigned long)hdw;
2217 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
2218
2219 init_timer(&hdw->encoder_wait_timer);
2220 hdw->encoder_wait_timer.data = (unsigned long)hdw;
2221 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
2222
Mike Iselyd913d632008-04-06 04:04:35 -03002223 init_timer(&hdw->encoder_run_timer);
2224 hdw->encoder_run_timer.data = (unsigned long)hdw;
2225 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
2226
Mike Isely681c7392007-11-26 01:48:52 -03002227 hdw->master_state = PVR2_STATE_DEAD;
2228
2229 init_waitqueue_head(&hdw->state_wait_data);
2230
Mike Isely18103c572007-01-20 00:09:47 -03002231 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03002232 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03002233
Mike Isely7fb20fa2008-04-22 14:45:37 -03002234 /* Calculate which inputs are OK */
2235 m = 0;
2236 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03002237 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
2238 m |= 1 << PVR2_CVAL_INPUT_DTV;
2239 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03002240 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
2241 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
2242 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
2243 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03002244 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03002245
Mike Isely62433e32008-04-22 14:45:40 -03002246 /* If not a hybrid device, pathway_state never changes. So
2247 initialize it here to what it should forever be. */
2248 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
2249 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
2250 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
2251 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
2252 }
2253
Mike Iselyc05c0462006-06-25 20:04:25 -03002254 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03002255 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03002256 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03002257 GFP_KERNEL);
2258 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03002259 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03002260 for (idx = 0; idx < hdw->control_cnt; idx++) {
2261 cptr = hdw->controls + idx;
2262 cptr->hdw = hdw;
2263 }
Mike Iselyd8554972006-06-26 20:58:46 -03002264 for (idx = 0; idx < 32; idx++) {
2265 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
2266 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002267 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002268 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03002269 cptr->info = control_defs+idx;
2270 }
Mike Iselydbc40a02008-04-22 14:45:39 -03002271
2272 /* Ensure that default input choice is a valid one. */
2273 m = hdw->input_avail_mask;
2274 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2275 if (!((1 << idx) & m)) continue;
2276 hdw->input_val = idx;
2277 break;
2278 }
2279
Mike Iselyb30d2442006-06-25 20:05:01 -03002280 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03002281 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03002282 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
2283 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03002284 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2285 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2286 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2287 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2288 ciptr->name = mpeg_ids[idx].strid;
2289 ciptr->v4l_id = mpeg_ids[idx].id;
2290 ciptr->skip_init = !0;
2291 ciptr->get_value = ctrl_cx2341x_get;
2292 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2293 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2294 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2295 qctrl.id = ciptr->v4l_id;
2296 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2297 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2298 ciptr->set_value = ctrl_cx2341x_set;
2299 }
2300 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2301 PVR2_CTLD_INFO_DESC_SIZE);
2302 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2303 ciptr->default_value = qctrl.default_value;
2304 switch (qctrl.type) {
2305 default:
2306 case V4L2_CTRL_TYPE_INTEGER:
2307 ciptr->type = pvr2_ctl_int;
2308 ciptr->def.type_int.min_value = qctrl.minimum;
2309 ciptr->def.type_int.max_value = qctrl.maximum;
2310 break;
2311 case V4L2_CTRL_TYPE_BOOLEAN:
2312 ciptr->type = pvr2_ctl_bool;
2313 break;
2314 case V4L2_CTRL_TYPE_MENU:
2315 ciptr->type = pvr2_ctl_enum;
2316 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002317 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2318 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002319 for (cnt1 = 0;
2320 ciptr->def.type_enum.value_names[cnt1] != NULL;
2321 cnt1++) { }
2322 ciptr->def.type_enum.count = cnt1;
2323 break;
2324 }
2325 cptr->info = ciptr;
2326 }
Mike Iselyd8554972006-06-26 20:58:46 -03002327
2328 // Initialize video standard enum dynamic control
2329 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2330 if (cptr) {
2331 memcpy(&hdw->std_info_enum,cptr->info,
2332 sizeof(hdw->std_info_enum));
2333 cptr->info = &hdw->std_info_enum;
2334
2335 }
2336 // Initialize control data regarding video standard masks
2337 valid_std_mask = pvr2_std_get_usable();
2338 for (idx = 0; idx < 32; idx++) {
2339 if (!(valid_std_mask & (1 << idx))) continue;
2340 cnt1 = pvr2_std_id_to_str(
2341 hdw->std_mask_names[idx],
2342 sizeof(hdw->std_mask_names[idx])-1,
2343 1 << idx);
2344 hdw->std_mask_names[idx][cnt1] = 0;
2345 }
2346 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2347 if (cptr) {
2348 memcpy(&hdw->std_info_avail,cptr->info,
2349 sizeof(hdw->std_info_avail));
2350 cptr->info = &hdw->std_info_avail;
2351 hdw->std_info_avail.def.type_bitmask.bit_names =
2352 hdw->std_mask_ptrs;
2353 hdw->std_info_avail.def.type_bitmask.valid_bits =
2354 valid_std_mask;
2355 }
2356 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2357 if (cptr) {
2358 memcpy(&hdw->std_info_cur,cptr->info,
2359 sizeof(hdw->std_info_cur));
2360 cptr->info = &hdw->std_info_cur;
2361 hdw->std_info_cur.def.type_bitmask.bit_names =
2362 hdw->std_mask_ptrs;
2363 hdw->std_info_avail.def.type_bitmask.valid_bits =
2364 valid_std_mask;
2365 }
2366
Mike Isely432907f2008-08-31 21:02:20 -03002367 hdw->cropcap_stale = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002368 hdw->eeprom_addr = -1;
2369 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002370 hdw->v4l_minor_number_video = -1;
2371 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002372 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002373 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2374 if (!hdw->ctl_write_buffer) goto fail;
2375 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2376 if (!hdw->ctl_read_buffer) goto fail;
2377 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2378 if (!hdw->ctl_write_urb) goto fail;
2379 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2380 if (!hdw->ctl_read_urb) goto fail;
2381
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002382 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002383 for (idx = 0; idx < PVR_NUM; idx++) {
2384 if (unit_pointers[idx]) continue;
2385 hdw->unit_number = idx;
2386 unit_pointers[idx] = hdw;
2387 break;
2388 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002389 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002390
2391 cnt1 = 0;
2392 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2393 cnt1 += cnt2;
2394 if (hdw->unit_number >= 0) {
2395 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2396 ('a' + hdw->unit_number));
2397 cnt1 += cnt2;
2398 }
2399 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2400 hdw->name[cnt1] = 0;
2401
Mike Isely681c7392007-11-26 01:48:52 -03002402 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2403 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2404 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002405
Mike Iselyd8554972006-06-26 20:58:46 -03002406 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2407 hdw->unit_number,hdw->name);
2408
2409 hdw->tuner_type = -1;
2410 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002411
2412 hdw->usb_intf = intf;
2413 hdw->usb_dev = interface_to_usbdev(intf);
2414
Mike Isely31a18542007-04-08 01:11:47 -03002415 scnprintf(hdw->bus_info,sizeof(hdw->bus_info),
2416 "usb %s address %d",
Kay Sieversaf128a12008-10-30 00:51:46 -03002417 dev_name(&hdw->usb_dev->dev),
Mike Isely31a18542007-04-08 01:11:47 -03002418 hdw->usb_dev->devnum);
2419
Mike Iselyd8554972006-06-26 20:58:46 -03002420 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2421 usb_set_interface(hdw->usb_dev,ifnum,0);
2422
2423 mutex_init(&hdw->ctl_lock_mutex);
2424 mutex_init(&hdw->big_lock_mutex);
2425
2426 return hdw;
2427 fail:
2428 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002429 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002430 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002431 del_timer_sync(&hdw->encoder_wait_timer);
2432 if (hdw->workqueue) {
2433 flush_workqueue(hdw->workqueue);
2434 destroy_workqueue(hdw->workqueue);
2435 hdw->workqueue = NULL;
2436 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002437 usb_free_urb(hdw->ctl_read_urb);
2438 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002439 kfree(hdw->ctl_read_buffer);
2440 kfree(hdw->ctl_write_buffer);
2441 kfree(hdw->controls);
2442 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002443 kfree(hdw->std_defs);
2444 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002445 kfree(hdw);
2446 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002447 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002448}
2449
2450
2451/* Remove _all_ associations between this driver and the underlying USB
2452 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002453static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002454{
2455 if (hdw->flag_disconnected) return;
2456 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2457 if (hdw->ctl_read_urb) {
2458 usb_kill_urb(hdw->ctl_read_urb);
2459 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002460 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002461 }
2462 if (hdw->ctl_write_urb) {
2463 usb_kill_urb(hdw->ctl_write_urb);
2464 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002465 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002466 }
2467 if (hdw->ctl_read_buffer) {
2468 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002469 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002470 }
2471 if (hdw->ctl_write_buffer) {
2472 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002473 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002474 }
Mike Iselyd8554972006-06-26 20:58:46 -03002475 hdw->flag_disconnected = !0;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002476 hdw->usb_dev = NULL;
2477 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002478 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002479}
2480
2481
2482/* Destroy hardware interaction structure */
2483void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2484{
Mike Isely401c27c2007-09-08 22:11:46 -03002485 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002486 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002487 if (hdw->workqueue) {
2488 flush_workqueue(hdw->workqueue);
2489 destroy_workqueue(hdw->workqueue);
2490 hdw->workqueue = NULL;
2491 }
Mike Isely8f591002008-04-22 14:45:45 -03002492 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002493 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002494 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002495 if (hdw->fw_buffer) {
2496 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002497 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002498 }
2499 if (hdw->vid_stream) {
2500 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002501 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002502 }
Mike Iselyd8554972006-06-26 20:58:46 -03002503 if (hdw->decoder_ctrl) {
2504 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2505 }
2506 pvr2_i2c_core_done(hdw);
2507 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002508 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002509 if ((hdw->unit_number >= 0) &&
2510 (hdw->unit_number < PVR_NUM) &&
2511 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002512 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002513 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002514 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002515 kfree(hdw->controls);
2516 kfree(hdw->mpeg_ctrl_info);
2517 kfree(hdw->std_defs);
2518 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002519 kfree(hdw);
2520}
2521
2522
Mike Iselyd8554972006-06-26 20:58:46 -03002523int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2524{
2525 return (hdw && hdw->flag_ok);
2526}
2527
2528
2529/* Called when hardware has been unplugged */
2530void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2531{
2532 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2533 LOCK_TAKE(hdw->big_lock);
2534 LOCK_TAKE(hdw->ctl_lock);
2535 pvr2_hdw_remove_usb_stuff(hdw);
2536 LOCK_GIVE(hdw->ctl_lock);
2537 LOCK_GIVE(hdw->big_lock);
2538}
2539
2540
2541// Attempt to autoselect an appropriate value for std_enum_cur given
2542// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002543static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002544{
2545 unsigned int idx;
2546 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2547 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2548 hdw->std_enum_cur = idx;
2549 return;
2550 }
2551 }
2552 hdw->std_enum_cur = 0;
2553}
2554
2555
2556// Calculate correct set of enumerated standards based on currently known
2557// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002558static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002559{
2560 struct v4l2_standard *newstd;
2561 unsigned int std_cnt;
2562 unsigned int idx;
2563
2564 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2565
2566 if (hdw->std_defs) {
2567 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002568 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002569 }
2570 hdw->std_enum_cnt = 0;
2571 if (hdw->std_enum_names) {
2572 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002573 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002574 }
2575
2576 if (!std_cnt) {
2577 pvr2_trace(
2578 PVR2_TRACE_ERROR_LEGS,
2579 "WARNING: Failed to identify any viable standards");
2580 }
2581 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2582 hdw->std_enum_names[0] = "none";
2583 for (idx = 0; idx < std_cnt; idx++) {
2584 hdw->std_enum_names[idx+1] =
2585 newstd[idx].name;
2586 }
2587 // Set up the dynamic control for this standard
2588 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2589 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2590 hdw->std_defs = newstd;
2591 hdw->std_enum_cnt = std_cnt+1;
2592 hdw->std_enum_cur = 0;
2593 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2594}
2595
2596
2597int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2598 struct v4l2_standard *std,
2599 unsigned int idx)
2600{
2601 int ret = -EINVAL;
2602 if (!idx) return ret;
2603 LOCK_TAKE(hdw->big_lock); do {
2604 if (idx >= hdw->std_enum_cnt) break;
2605 idx--;
2606 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2607 ret = 0;
2608 } while (0); LOCK_GIVE(hdw->big_lock);
2609 return ret;
2610}
2611
2612
2613/* Get the number of defined controls */
2614unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2615{
Mike Iselyc05c0462006-06-25 20:04:25 -03002616 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002617}
2618
2619
2620/* Retrieve a control handle given its index (0..count-1) */
2621struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2622 unsigned int idx)
2623{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002624 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002625 return hdw->controls + idx;
2626}
2627
2628
2629/* Retrieve a control handle given its index (0..count-1) */
2630struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2631 unsigned int ctl_id)
2632{
2633 struct pvr2_ctrl *cptr;
2634 unsigned int idx;
2635 int i;
2636
2637 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002638 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002639 cptr = hdw->controls + idx;
2640 i = cptr->info->internal_id;
2641 if (i && (i == ctl_id)) return cptr;
2642 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002643 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002644}
2645
2646
Mike Iselya761f432006-06-25 20:04:44 -03002647/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002648struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2649{
2650 struct pvr2_ctrl *cptr;
2651 unsigned int idx;
2652 int i;
2653
2654 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002655 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002656 cptr = hdw->controls + idx;
2657 i = cptr->info->v4l_id;
2658 if (i && (i == ctl_id)) return cptr;
2659 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002660 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002661}
2662
2663
Mike Iselya761f432006-06-25 20:04:44 -03002664/* Given a V4L ID for its immediate predecessor, retrieve the control
2665 structure associated with it. */
2666struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2667 unsigned int ctl_id)
2668{
2669 struct pvr2_ctrl *cptr,*cp2;
2670 unsigned int idx;
2671 int i;
2672
2673 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002674 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002675 for (idx = 0; idx < hdw->control_cnt; idx++) {
2676 cptr = hdw->controls + idx;
2677 i = cptr->info->v4l_id;
2678 if (!i) continue;
2679 if (i <= ctl_id) continue;
2680 if (cp2 && (cp2->info->v4l_id < i)) continue;
2681 cp2 = cptr;
2682 }
2683 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002684 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002685}
2686
2687
Mike Iselyd8554972006-06-26 20:58:46 -03002688static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2689{
2690 switch (tp) {
2691 case pvr2_ctl_int: return "integer";
2692 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002693 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002694 case pvr2_ctl_bitmask: return "bitmask";
2695 }
2696 return "";
2697}
2698
2699
Mike Isely681c7392007-11-26 01:48:52 -03002700/* Figure out if we need to commit control changes. If so, mark internal
2701 state flags to indicate this fact and return true. Otherwise do nothing
2702 else and return false. */
2703static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002704{
Mike Iselyd8554972006-06-26 20:58:46 -03002705 unsigned int idx;
2706 struct pvr2_ctrl *cptr;
2707 int value;
2708 int commit_flag = 0;
2709 char buf[100];
2710 unsigned int bcnt,ccnt;
2711
Mike Iselyc05c0462006-06-25 20:04:25 -03002712 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002713 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00002714 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002715 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03002716 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002717
Mike Iselyfe23a282007-01-20 00:10:55 -03002718 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002719 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
2720 cptr->info->name);
2721 value = 0;
2722 cptr->info->get_value(cptr,&value);
2723 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
2724 buf+bcnt,
2725 sizeof(buf)-bcnt,&ccnt);
2726 bcnt += ccnt;
2727 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
2728 get_ctrl_typename(cptr->info->type));
2729 pvr2_trace(PVR2_TRACE_CTL,
2730 "/*--TRACE_COMMIT--*/ %.*s",
2731 bcnt,buf);
2732 }
2733
2734 if (!commit_flag) {
2735 /* Nothing has changed */
2736 return 0;
2737 }
2738
Mike Isely681c7392007-11-26 01:48:52 -03002739 hdw->state_pipeline_config = 0;
2740 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2741 pvr2_hdw_state_sched(hdw);
2742
2743 return !0;
2744}
2745
2746
2747/* Perform all operations needed to commit all control changes. This must
2748 be performed in synchronization with the pipeline state and is thus
2749 expected to be called as part of the driver's worker thread. Return
2750 true if commit successful, otherwise return false to indicate that
2751 commit isn't possible at this time. */
2752static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
2753{
2754 unsigned int idx;
2755 struct pvr2_ctrl *cptr;
2756 int disruptive_change;
2757
Mike Iselyab062fe2008-06-30 03:32:35 -03002758 /* Handle some required side effects when the video standard is
2759 changed.... */
Mike Iselyd8554972006-06-26 20:58:46 -03002760 if (hdw->std_dirty) {
Mike Iselyd8554972006-06-26 20:58:46 -03002761 int nvres;
Mike Isely00528d92008-06-30 03:35:52 -03002762 int gop_size;
Mike Iselyd8554972006-06-26 20:58:46 -03002763 if (hdw->std_mask_cur & V4L2_STD_525_60) {
2764 nvres = 480;
Mike Isely00528d92008-06-30 03:35:52 -03002765 gop_size = 15;
Mike Iselyd8554972006-06-26 20:58:46 -03002766 } else {
2767 nvres = 576;
Mike Isely00528d92008-06-30 03:35:52 -03002768 gop_size = 12;
Mike Iselyd8554972006-06-26 20:58:46 -03002769 }
Mike Isely00528d92008-06-30 03:35:52 -03002770 /* Rewrite the vertical resolution to be appropriate to the
2771 video standard that has been selected. */
Mike Iselyd8554972006-06-26 20:58:46 -03002772 if (nvres != hdw->res_ver_val) {
2773 hdw->res_ver_val = nvres;
2774 hdw->res_ver_dirty = !0;
2775 }
Mike Isely00528d92008-06-30 03:35:52 -03002776 /* Rewrite the GOP size to be appropriate to the video
2777 standard that has been selected. */
2778 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
2779 struct v4l2_ext_controls cs;
2780 struct v4l2_ext_control c1;
2781 memset(&cs, 0, sizeof(cs));
2782 memset(&c1, 0, sizeof(c1));
2783 cs.controls = &c1;
2784 cs.count = 1;
2785 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
2786 c1.value = gop_size;
2787 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
2788 VIDIOC_S_EXT_CTRLS);
2789 }
Mike Iselyd8554972006-06-26 20:58:46 -03002790 }
2791
Mike Isely38d9a2c2008-03-28 05:30:48 -03002792 if (hdw->input_dirty && hdw->state_pathway_ok &&
Mike Isely62433e32008-04-22 14:45:40 -03002793 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
2794 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
2795 hdw->pathway_state)) {
2796 /* Change of mode being asked for... */
2797 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03002798 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03002799 }
2800 if (!hdw->state_pathway_ok) {
2801 /* Can't commit anything until pathway is ok. */
2802 return 0;
2803 }
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03002804 /* The broadcast decoder can only scale down, so if
2805 * res_*_dirty && crop window < output format ==> enlarge crop.
2806 *
2807 * The mpeg encoder receives fields of res_hor_val dots and
2808 * res_ver_val halflines. Limits: hor<=720, ver<=576.
2809 */
2810 if (hdw->res_hor_dirty && hdw->cropw_val < hdw->res_hor_val) {
2811 hdw->cropw_val = hdw->res_hor_val;
2812 hdw->cropw_dirty = !0;
2813 } else if (hdw->cropw_dirty) {
2814 hdw->res_hor_dirty = !0; /* must rescale */
2815 hdw->res_hor_val = min(720, hdw->cropw_val);
2816 }
2817 if (hdw->res_ver_dirty && hdw->croph_val < hdw->res_ver_val) {
2818 hdw->croph_val = hdw->res_ver_val;
2819 hdw->croph_dirty = !0;
2820 } else if (hdw->croph_dirty) {
2821 int nvres = hdw->std_mask_cur & V4L2_STD_525_60 ? 480 : 576;
2822 hdw->res_ver_dirty = !0;
2823 hdw->res_ver_val = min(nvres, hdw->croph_val);
2824 }
2825
Mike Isely681c7392007-11-26 01:48:52 -03002826 /* If any of the below has changed, then we can't do the update
2827 while the pipeline is running. Pipeline must be paused first
2828 and decoder -> encoder connection be made quiescent before we
2829 can proceed. */
2830 disruptive_change =
2831 (hdw->std_dirty ||
2832 hdw->enc_unsafe_stale ||
2833 hdw->srate_dirty ||
2834 hdw->res_ver_dirty ||
2835 hdw->res_hor_dirty ||
Mike Isely755879c2008-08-31 20:50:59 -03002836 hdw->cropw_dirty ||
2837 hdw->croph_dirty ||
Mike Isely681c7392007-11-26 01:48:52 -03002838 hdw->input_dirty ||
2839 (hdw->active_stream_type != hdw->desired_stream_type));
2840 if (disruptive_change && !hdw->state_pipeline_idle) {
2841 /* Pipeline is not idle; we can't proceed. Arrange to
2842 cause pipeline to stop so that we can try this again
2843 later.... */
2844 hdw->state_pipeline_pause = !0;
2845 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002846 }
2847
Mike Iselyb30d2442006-06-25 20:05:01 -03002848 if (hdw->srate_dirty) {
2849 /* Write new sample rate into control structure since
2850 * the master copy is stale. We must track srate
2851 * separate from the mpeg control structure because
2852 * other logic also uses this value. */
2853 struct v4l2_ext_controls cs;
2854 struct v4l2_ext_control c1;
2855 memset(&cs,0,sizeof(cs));
2856 memset(&c1,0,sizeof(c1));
2857 cs.controls = &c1;
2858 cs.count = 1;
2859 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
2860 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03002861 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03002862 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002863
Mike Iselyd8554972006-06-26 20:58:46 -03002864 /* Scan i2c core at this point - before we clear all the dirty
2865 bits. Various parts of the i2c core will notice dirty bits as
2866 appropriate and arrange to broadcast or directly send updates to
2867 the client drivers in order to keep everything in sync */
2868 pvr2_i2c_core_check_stale(hdw);
2869
Mike Iselyc05c0462006-06-25 20:04:25 -03002870 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002871 cptr = hdw->controls + idx;
2872 if (!cptr->info->clear_dirty) continue;
2873 cptr->info->clear_dirty(cptr);
2874 }
2875
Mike Isely681c7392007-11-26 01:48:52 -03002876 if (hdw->active_stream_type != hdw->desired_stream_type) {
2877 /* Handle any side effects of stream config here */
2878 hdw->active_stream_type = hdw->desired_stream_type;
2879 }
2880
Mike Isely1df59f02008-04-21 03:50:39 -03002881 if (hdw->hdw_desc->signal_routing_scheme ==
2882 PVR2_ROUTING_SCHEME_GOTVIEW) {
2883 u32 b;
2884 /* Handle GOTVIEW audio switching */
2885 pvr2_hdw_gpio_get_out(hdw,&b);
2886 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2887 /* Set GPIO 11 */
2888 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
2889 } else {
2890 /* Clear GPIO 11 */
2891 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
2892 }
2893 }
2894
Mike Iselyd8554972006-06-26 20:58:46 -03002895 /* Now execute i2c core update */
2896 pvr2_i2c_core_sync(hdw);
2897
Mike Isely62433e32008-04-22 14:45:40 -03002898 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
2899 hdw->state_encoder_run) {
2900 /* If encoder isn't running or it can't be touched, then
2901 this will get worked out later when we start the
2902 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03002903 if (pvr2_encoder_adjust(hdw) < 0) return !0;
2904 }
Mike Iselyd8554972006-06-26 20:58:46 -03002905
Mike Isely681c7392007-11-26 01:48:52 -03002906 hdw->state_pipeline_config = !0;
Mike Isely432907f2008-08-31 21:02:20 -03002907 /* Hardware state may have changed in a way to cause the cropping
2908 capabilities to have changed. So mark it stale, which will
2909 cause a later re-fetch. */
Mike Isely681c7392007-11-26 01:48:52 -03002910 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2911 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002912}
2913
2914
2915int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2916{
Mike Isely681c7392007-11-26 01:48:52 -03002917 int fl;
2918 LOCK_TAKE(hdw->big_lock);
2919 fl = pvr2_hdw_commit_setup(hdw);
2920 LOCK_GIVE(hdw->big_lock);
2921 if (!fl) return 0;
2922 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03002923}
2924
2925
Mike Isely681c7392007-11-26 01:48:52 -03002926static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002927{
Mike Isely681c7392007-11-26 01:48:52 -03002928 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03002929 LOCK_TAKE(hdw->big_lock); do {
2930 pvr2_i2c_core_sync(hdw);
2931 } while (0); LOCK_GIVE(hdw->big_lock);
2932}
2933
2934
Mike Isely681c7392007-11-26 01:48:52 -03002935static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002936{
Mike Isely681c7392007-11-26 01:48:52 -03002937 int fl = 0;
2938 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03002939 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03002940 fl = pvr2_hdw_state_eval(hdw);
2941 } while (0); LOCK_GIVE(hdw->big_lock);
2942 if (fl && hdw->state_func) {
2943 hdw->state_func(hdw->state_data);
2944 }
2945}
2946
2947
Mike Isely681c7392007-11-26 01:48:52 -03002948static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03002949{
Mike Isely681c7392007-11-26 01:48:52 -03002950 return wait_event_interruptible(
2951 hdw->state_wait_data,
2952 (hdw->state_stale == 0) &&
2953 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03002954}
2955
Mike Isely681c7392007-11-26 01:48:52 -03002956
Mike Iselyd8554972006-06-26 20:58:46 -03002957/* Return name for this driver instance */
2958const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2959{
2960 return hdw->name;
2961}
2962
2963
Mike Isely78a47102007-11-26 01:58:20 -03002964const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
2965{
2966 return hdw->hdw_desc->description;
2967}
2968
2969
2970const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
2971{
2972 return hdw->hdw_desc->shortname;
2973}
2974
2975
Mike Iselyd8554972006-06-26 20:58:46 -03002976int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2977{
2978 int result;
2979 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03002980 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03002981 result = pvr2_send_request(hdw,
2982 hdw->cmd_buffer,1,
2983 hdw->cmd_buffer,1);
2984 if (result < 0) break;
2985 result = (hdw->cmd_buffer[0] != 0);
2986 } while(0); LOCK_GIVE(hdw->ctl_lock);
2987 return result;
2988}
2989
2990
Mike Isely18103c572007-01-20 00:09:47 -03002991/* Execute poll of tuner status */
2992void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002993{
Mike Iselyd8554972006-06-26 20:58:46 -03002994 LOCK_TAKE(hdw->big_lock); do {
Mike Isely18103c572007-01-20 00:09:47 -03002995 pvr2_i2c_core_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002996 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03002997}
2998
2999
Mike Isely432907f2008-08-31 21:02:20 -03003000static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw)
3001{
3002 if (!hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003003 return 0;
3004 }
3005 pvr2_i2c_core_status_poll(hdw);
3006 if (hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003007 return -EIO;
3008 }
3009 return 0;
3010}
3011
3012
3013/* Return information about cropping capabilities */
3014int pvr2_hdw_get_cropcap(struct pvr2_hdw *hdw, struct v4l2_cropcap *pp)
3015{
3016 int stat = 0;
3017 LOCK_TAKE(hdw->big_lock);
3018 stat = pvr2_hdw_check_cropcap(hdw);
3019 if (!stat) {
Mike Isely432907f2008-08-31 21:02:20 -03003020 memcpy(pp, &hdw->cropcap_info, sizeof(hdw->cropcap_info));
3021 }
3022 LOCK_GIVE(hdw->big_lock);
3023 return stat;
3024}
3025
3026
Mike Isely18103c572007-01-20 00:09:47 -03003027/* Return information about the tuner */
3028int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
3029{
3030 LOCK_TAKE(hdw->big_lock); do {
3031 if (hdw->tuner_signal_stale) {
3032 pvr2_i2c_core_status_poll(hdw);
3033 }
3034 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
3035 } while (0); LOCK_GIVE(hdw->big_lock);
3036 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003037}
3038
3039
3040/* Get handle to video output stream */
3041struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
3042{
3043 return hp->vid_stream;
3044}
3045
3046
3047void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
3048{
Mike Isely4f1a3e52006-06-25 20:04:31 -03003049 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003050 LOCK_TAKE(hdw->big_lock); do {
3051 hdw->log_requested = !0;
Mike Isely4f1a3e52006-06-25 20:04:31 -03003052 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003053 pvr2_i2c_core_check_stale(hdw);
3054 hdw->log_requested = 0;
3055 pvr2_i2c_core_sync(hdw);
Mike Iselyb30d2442006-06-25 20:05:01 -03003056 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03003057 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03003058 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03003059 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003060 } while (0); LOCK_GIVE(hdw->big_lock);
3061}
3062
Mike Isely4db666c2007-09-08 22:16:27 -03003063
3064/* Grab EEPROM contents, needed for direct method. */
3065#define EEPROM_SIZE 8192
3066#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
3067static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
3068{
3069 struct i2c_msg msg[2];
3070 u8 *eeprom;
3071 u8 iadd[2];
3072 u8 addr;
3073 u16 eepromSize;
3074 unsigned int offs;
3075 int ret;
3076 int mode16 = 0;
3077 unsigned pcnt,tcnt;
3078 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
3079 if (!eeprom) {
3080 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3081 "Failed to allocate memory"
3082 " required to read eeprom");
3083 return NULL;
3084 }
3085
3086 trace_eeprom("Value for eeprom addr from controller was 0x%x",
3087 hdw->eeprom_addr);
3088 addr = hdw->eeprom_addr;
3089 /* Seems that if the high bit is set, then the *real* eeprom
3090 address is shifted right now bit position (noticed this in
3091 newer PVR USB2 hardware) */
3092 if (addr & 0x80) addr >>= 1;
3093
3094 /* FX2 documentation states that a 16bit-addressed eeprom is
3095 expected if the I2C address is an odd number (yeah, this is
3096 strange but it's what they do) */
3097 mode16 = (addr & 1);
3098 eepromSize = (mode16 ? EEPROM_SIZE : 256);
3099 trace_eeprom("Examining %d byte eeprom at location 0x%x"
3100 " using %d bit addressing",eepromSize,addr,
3101 mode16 ? 16 : 8);
3102
3103 msg[0].addr = addr;
3104 msg[0].flags = 0;
3105 msg[0].len = mode16 ? 2 : 1;
3106 msg[0].buf = iadd;
3107 msg[1].addr = addr;
3108 msg[1].flags = I2C_M_RD;
3109
3110 /* We have to do the actual eeprom data fetch ourselves, because
3111 (1) we're only fetching part of the eeprom, and (2) if we were
3112 getting the whole thing our I2C driver can't grab it in one
3113 pass - which is what tveeprom is otherwise going to attempt */
3114 memset(eeprom,0,EEPROM_SIZE);
3115 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
3116 pcnt = 16;
3117 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
3118 offs = tcnt + (eepromSize - EEPROM_SIZE);
3119 if (mode16) {
3120 iadd[0] = offs >> 8;
3121 iadd[1] = offs;
3122 } else {
3123 iadd[0] = offs;
3124 }
3125 msg[1].len = pcnt;
3126 msg[1].buf = eeprom+tcnt;
3127 if ((ret = i2c_transfer(&hdw->i2c_adap,
3128 msg,ARRAY_SIZE(msg))) != 2) {
3129 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3130 "eeprom fetch set offs err=%d",ret);
3131 kfree(eeprom);
3132 return NULL;
3133 }
3134 }
3135 return eeprom;
3136}
3137
3138
3139void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
3140 int prom_flag,
3141 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003142{
3143 int ret;
3144 u16 address;
3145 unsigned int pipe;
3146 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00003147 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03003148
3149 if (!enable_flag) {
3150 pvr2_trace(PVR2_TRACE_FIRMWARE,
3151 "Cleaning up after CPU firmware fetch");
3152 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003153 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003154 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03003155 if (hdw->fw_cpu_flag) {
3156 /* Now release the CPU. It will disconnect
3157 and reconnect later. */
3158 pvr2_hdw_cpureset_assert(hdw,0);
3159 }
Mike Iselyd8554972006-06-26 20:58:46 -03003160 break;
3161 }
3162
Mike Isely4db666c2007-09-08 22:16:27 -03003163 hdw->fw_cpu_flag = (prom_flag == 0);
3164 if (hdw->fw_cpu_flag) {
3165 pvr2_trace(PVR2_TRACE_FIRMWARE,
3166 "Preparing to suck out CPU firmware");
3167 hdw->fw_size = 0x2000;
3168 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
3169 if (!hdw->fw_buffer) {
3170 hdw->fw_size = 0;
3171 break;
3172 }
3173
3174 /* We have to hold the CPU during firmware upload. */
3175 pvr2_hdw_cpureset_assert(hdw,1);
3176
3177 /* download the firmware from address 0000-1fff in 2048
3178 (=0x800) bytes chunk. */
3179
3180 pvr2_trace(PVR2_TRACE_FIRMWARE,
3181 "Grabbing CPU firmware");
3182 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
3183 for(address = 0; address < hdw->fw_size;
3184 address += 0x800) {
3185 ret = usb_control_msg(hdw->usb_dev,pipe,
3186 0xa0,0xc0,
3187 address,0,
3188 hdw->fw_buffer+address,
3189 0x800,HZ);
3190 if (ret < 0) break;
3191 }
3192
3193 pvr2_trace(PVR2_TRACE_FIRMWARE,
3194 "Done grabbing CPU firmware");
3195 } else {
3196 pvr2_trace(PVR2_TRACE_FIRMWARE,
3197 "Sucking down EEPROM contents");
3198 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
3199 if (!hdw->fw_buffer) {
3200 pvr2_trace(PVR2_TRACE_FIRMWARE,
3201 "EEPROM content suck failed.");
3202 break;
3203 }
3204 hdw->fw_size = EEPROM_SIZE;
3205 pvr2_trace(PVR2_TRACE_FIRMWARE,
3206 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03003207 }
3208
Mike Iselyd8554972006-06-26 20:58:46 -03003209 } while (0); LOCK_GIVE(hdw->big_lock);
3210}
3211
3212
3213/* Return true if we're in a mode for retrieval CPU firmware */
3214int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
3215{
Al Viro5fa12472008-03-29 03:07:38 +00003216 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003217}
3218
3219
3220int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
3221 char *buf,unsigned int cnt)
3222{
3223 int ret = -EINVAL;
3224 LOCK_TAKE(hdw->big_lock); do {
3225 if (!buf) break;
3226 if (!cnt) break;
3227
3228 if (!hdw->fw_buffer) {
3229 ret = -EIO;
3230 break;
3231 }
3232
3233 if (offs >= hdw->fw_size) {
3234 pvr2_trace(PVR2_TRACE_FIRMWARE,
3235 "Read firmware data offs=%d EOF",
3236 offs);
3237 ret = 0;
3238 break;
3239 }
3240
3241 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
3242
3243 memcpy(buf,hdw->fw_buffer+offs,cnt);
3244
3245 pvr2_trace(PVR2_TRACE_FIRMWARE,
3246 "Read firmware data offs=%d cnt=%d",
3247 offs,cnt);
3248 ret = cnt;
3249 } while (0); LOCK_GIVE(hdw->big_lock);
3250
3251 return ret;
3252}
3253
3254
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003255int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003256 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03003257{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003258 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003259 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
3260 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
3261 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003262 default: return -1;
3263 }
Mike Iselyd8554972006-06-26 20:58:46 -03003264}
3265
3266
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03003267/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003268void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003269 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03003270{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003271 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003272 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
3273 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
3274 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003275 default: break;
3276 }
Mike Iselyd8554972006-06-26 20:58:46 -03003277}
3278
3279
David Howells7d12e782006-10-05 14:55:46 +01003280static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003281{
3282 struct pvr2_hdw *hdw = urb->context;
3283 hdw->ctl_write_pend_flag = 0;
3284 if (hdw->ctl_read_pend_flag) return;
3285 complete(&hdw->ctl_done);
3286}
3287
3288
David Howells7d12e782006-10-05 14:55:46 +01003289static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003290{
3291 struct pvr2_hdw *hdw = urb->context;
3292 hdw->ctl_read_pend_flag = 0;
3293 if (hdw->ctl_write_pend_flag) return;
3294 complete(&hdw->ctl_done);
3295}
3296
3297
3298static void pvr2_ctl_timeout(unsigned long data)
3299{
3300 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3301 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3302 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003303 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003304 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003305 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003306 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03003307 }
3308}
3309
3310
Mike Iselye61b6fc2006-07-18 22:42:18 -03003311/* Issue a command and get a response from the device. This extended
3312 version includes a probe flag (which if set means that device errors
3313 should not be logged or treated as fatal) and a timeout in jiffies.
3314 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003315static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3316 unsigned int timeout,int probe_fl,
3317 void *write_data,unsigned int write_len,
3318 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03003319{
3320 unsigned int idx;
3321 int status = 0;
3322 struct timer_list timer;
3323 if (!hdw->ctl_lock_held) {
3324 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3325 "Attempted to execute control transfer"
3326 " without lock!!");
3327 return -EDEADLK;
3328 }
Mike Isely681c7392007-11-26 01:48:52 -03003329 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03003330 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3331 "Attempted to execute control transfer"
3332 " when device not ok");
3333 return -EIO;
3334 }
3335 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3336 if (!probe_fl) {
3337 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3338 "Attempted to execute control transfer"
3339 " when USB is disconnected");
3340 }
3341 return -ENOTTY;
3342 }
3343
3344 /* Ensure that we have sane parameters */
3345 if (!write_data) write_len = 0;
3346 if (!read_data) read_len = 0;
3347 if (write_len > PVR2_CTL_BUFFSIZE) {
3348 pvr2_trace(
3349 PVR2_TRACE_ERROR_LEGS,
3350 "Attempted to execute %d byte"
3351 " control-write transfer (limit=%d)",
3352 write_len,PVR2_CTL_BUFFSIZE);
3353 return -EINVAL;
3354 }
3355 if (read_len > PVR2_CTL_BUFFSIZE) {
3356 pvr2_trace(
3357 PVR2_TRACE_ERROR_LEGS,
3358 "Attempted to execute %d byte"
3359 " control-read transfer (limit=%d)",
3360 write_len,PVR2_CTL_BUFFSIZE);
3361 return -EINVAL;
3362 }
3363 if ((!write_len) && (!read_len)) {
3364 pvr2_trace(
3365 PVR2_TRACE_ERROR_LEGS,
3366 "Attempted to execute null control transfer?");
3367 return -EINVAL;
3368 }
3369
3370
3371 hdw->cmd_debug_state = 1;
3372 if (write_len) {
3373 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3374 } else {
3375 hdw->cmd_debug_code = 0;
3376 }
3377 hdw->cmd_debug_write_len = write_len;
3378 hdw->cmd_debug_read_len = read_len;
3379
3380 /* Initialize common stuff */
3381 init_completion(&hdw->ctl_done);
3382 hdw->ctl_timeout_flag = 0;
3383 hdw->ctl_write_pend_flag = 0;
3384 hdw->ctl_read_pend_flag = 0;
3385 init_timer(&timer);
3386 timer.expires = jiffies + timeout;
3387 timer.data = (unsigned long)hdw;
3388 timer.function = pvr2_ctl_timeout;
3389
3390 if (write_len) {
3391 hdw->cmd_debug_state = 2;
3392 /* Transfer write data to internal buffer */
3393 for (idx = 0; idx < write_len; idx++) {
3394 hdw->ctl_write_buffer[idx] =
3395 ((unsigned char *)write_data)[idx];
3396 }
3397 /* Initiate a write request */
3398 usb_fill_bulk_urb(hdw->ctl_write_urb,
3399 hdw->usb_dev,
3400 usb_sndbulkpipe(hdw->usb_dev,
3401 PVR2_CTL_WRITE_ENDPOINT),
3402 hdw->ctl_write_buffer,
3403 write_len,
3404 pvr2_ctl_write_complete,
3405 hdw);
3406 hdw->ctl_write_urb->actual_length = 0;
3407 hdw->ctl_write_pend_flag = !0;
3408 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3409 if (status < 0) {
3410 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3411 "Failed to submit write-control"
3412 " URB status=%d",status);
3413 hdw->ctl_write_pend_flag = 0;
3414 goto done;
3415 }
3416 }
3417
3418 if (read_len) {
3419 hdw->cmd_debug_state = 3;
3420 memset(hdw->ctl_read_buffer,0x43,read_len);
3421 /* Initiate a read request */
3422 usb_fill_bulk_urb(hdw->ctl_read_urb,
3423 hdw->usb_dev,
3424 usb_rcvbulkpipe(hdw->usb_dev,
3425 PVR2_CTL_READ_ENDPOINT),
3426 hdw->ctl_read_buffer,
3427 read_len,
3428 pvr2_ctl_read_complete,
3429 hdw);
3430 hdw->ctl_read_urb->actual_length = 0;
3431 hdw->ctl_read_pend_flag = !0;
3432 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3433 if (status < 0) {
3434 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3435 "Failed to submit read-control"
3436 " URB status=%d",status);
3437 hdw->ctl_read_pend_flag = 0;
3438 goto done;
3439 }
3440 }
3441
3442 /* Start timer */
3443 add_timer(&timer);
3444
3445 /* Now wait for all I/O to complete */
3446 hdw->cmd_debug_state = 4;
3447 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3448 wait_for_completion(&hdw->ctl_done);
3449 }
3450 hdw->cmd_debug_state = 5;
3451
3452 /* Stop timer */
3453 del_timer_sync(&timer);
3454
3455 hdw->cmd_debug_state = 6;
3456 status = 0;
3457
3458 if (hdw->ctl_timeout_flag) {
3459 status = -ETIMEDOUT;
3460 if (!probe_fl) {
3461 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3462 "Timed out control-write");
3463 }
3464 goto done;
3465 }
3466
3467 if (write_len) {
3468 /* Validate results of write request */
3469 if ((hdw->ctl_write_urb->status != 0) &&
3470 (hdw->ctl_write_urb->status != -ENOENT) &&
3471 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3472 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3473 /* USB subsystem is reporting some kind of failure
3474 on the write */
3475 status = hdw->ctl_write_urb->status;
3476 if (!probe_fl) {
3477 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3478 "control-write URB failure,"
3479 " status=%d",
3480 status);
3481 }
3482 goto done;
3483 }
3484 if (hdw->ctl_write_urb->actual_length < write_len) {
3485 /* Failed to write enough data */
3486 status = -EIO;
3487 if (!probe_fl) {
3488 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3489 "control-write URB short,"
3490 " expected=%d got=%d",
3491 write_len,
3492 hdw->ctl_write_urb->actual_length);
3493 }
3494 goto done;
3495 }
3496 }
3497 if (read_len) {
3498 /* Validate results of read request */
3499 if ((hdw->ctl_read_urb->status != 0) &&
3500 (hdw->ctl_read_urb->status != -ENOENT) &&
3501 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3502 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3503 /* USB subsystem is reporting some kind of failure
3504 on the read */
3505 status = hdw->ctl_read_urb->status;
3506 if (!probe_fl) {
3507 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3508 "control-read URB failure,"
3509 " status=%d",
3510 status);
3511 }
3512 goto done;
3513 }
3514 if (hdw->ctl_read_urb->actual_length < read_len) {
3515 /* Failed to read enough data */
3516 status = -EIO;
3517 if (!probe_fl) {
3518 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3519 "control-read URB short,"
3520 " expected=%d got=%d",
3521 read_len,
3522 hdw->ctl_read_urb->actual_length);
3523 }
3524 goto done;
3525 }
3526 /* Transfer retrieved data out from internal buffer */
3527 for (idx = 0; idx < read_len; idx++) {
3528 ((unsigned char *)read_data)[idx] =
3529 hdw->ctl_read_buffer[idx];
3530 }
3531 }
3532
3533 done:
3534
3535 hdw->cmd_debug_state = 0;
3536 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003537 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003538 }
3539 return status;
3540}
3541
3542
3543int pvr2_send_request(struct pvr2_hdw *hdw,
3544 void *write_data,unsigned int write_len,
3545 void *read_data,unsigned int read_len)
3546{
3547 return pvr2_send_request_ex(hdw,HZ*4,0,
3548 write_data,write_len,
3549 read_data,read_len);
3550}
3551
Mike Isely1c9d10d2008-03-28 05:38:54 -03003552
3553static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3554{
3555 int ret;
3556 unsigned int cnt = 1;
3557 unsigned int args = 0;
3558 LOCK_TAKE(hdw->ctl_lock);
3559 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3560 args = (cmdcode >> 8) & 0xffu;
3561 args = (args > 2) ? 2 : args;
3562 if (args) {
3563 cnt += args;
3564 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3565 if (args > 1) {
3566 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3567 }
3568 }
3569 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3570 unsigned int idx;
3571 unsigned int ccnt,bcnt;
3572 char tbuf[50];
3573 cmdcode &= 0xffu;
3574 bcnt = 0;
3575 ccnt = scnprintf(tbuf+bcnt,
3576 sizeof(tbuf)-bcnt,
3577 "Sending FX2 command 0x%x",cmdcode);
3578 bcnt += ccnt;
3579 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3580 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3581 ccnt = scnprintf(tbuf+bcnt,
3582 sizeof(tbuf)-bcnt,
3583 " \"%s\"",
3584 pvr2_fx2cmd_desc[idx].desc);
3585 bcnt += ccnt;
3586 break;
3587 }
3588 }
3589 if (args) {
3590 ccnt = scnprintf(tbuf+bcnt,
3591 sizeof(tbuf)-bcnt,
3592 " (%u",hdw->cmd_buffer[1]);
3593 bcnt += ccnt;
3594 if (args > 1) {
3595 ccnt = scnprintf(tbuf+bcnt,
3596 sizeof(tbuf)-bcnt,
3597 ",%u",hdw->cmd_buffer[2]);
3598 bcnt += ccnt;
3599 }
3600 ccnt = scnprintf(tbuf+bcnt,
3601 sizeof(tbuf)-bcnt,
3602 ")");
3603 bcnt += ccnt;
3604 }
3605 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
3606 }
3607 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
3608 LOCK_GIVE(hdw->ctl_lock);
3609 return ret;
3610}
3611
3612
Mike Iselyd8554972006-06-26 20:58:46 -03003613int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3614{
3615 int ret;
3616
3617 LOCK_TAKE(hdw->ctl_lock);
3618
Michael Krufky8d364362007-01-22 02:17:55 -03003619 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003620 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3621 hdw->cmd_buffer[5] = 0;
3622 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3623 hdw->cmd_buffer[7] = reg & 0xff;
3624
3625
3626 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3627
3628 LOCK_GIVE(hdw->ctl_lock);
3629
3630 return ret;
3631}
3632
3633
Adrian Bunk07e337e2006-06-30 11:30:20 -03003634static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003635{
3636 int ret = 0;
3637
3638 LOCK_TAKE(hdw->ctl_lock);
3639
Michael Krufky8d364362007-01-22 02:17:55 -03003640 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003641 hdw->cmd_buffer[1] = 0;
3642 hdw->cmd_buffer[2] = 0;
3643 hdw->cmd_buffer[3] = 0;
3644 hdw->cmd_buffer[4] = 0;
3645 hdw->cmd_buffer[5] = 0;
3646 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3647 hdw->cmd_buffer[7] = reg & 0xff;
3648
3649 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3650 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3651
3652 LOCK_GIVE(hdw->ctl_lock);
3653
3654 return ret;
3655}
3656
3657
Mike Isely681c7392007-11-26 01:48:52 -03003658void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003659{
3660 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003661 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3662 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003663 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003664 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003665 }
Mike Isely681c7392007-11-26 01:48:52 -03003666 hdw->flag_ok = 0;
3667 trace_stbit("flag_ok",hdw->flag_ok);
3668 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003669}
3670
3671
3672void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3673{
3674 int ret;
3675 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003676 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Alan Stern011b15d2008-11-04 11:29:27 -05003677 if (ret == 0) {
Mike Iselyd8554972006-06-26 20:58:46 -03003678 ret = usb_reset_device(hdw->usb_dev);
3679 usb_unlock_device(hdw->usb_dev);
3680 } else {
3681 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3682 "Failed to lock USB device ret=%d",ret);
3683 }
3684 if (init_pause_msec) {
3685 pvr2_trace(PVR2_TRACE_INFO,
3686 "Waiting %u msec for hardware to settle",
3687 init_pause_msec);
3688 msleep(init_pause_msec);
3689 }
3690
3691}
3692
3693
3694void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3695{
3696 char da[1];
3697 unsigned int pipe;
3698 int ret;
3699
3700 if (!hdw->usb_dev) return;
3701
3702 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3703
3704 da[0] = val ? 0x01 : 0x00;
3705
3706 /* Write the CPUCS register on the 8051. The lsb of the register
3707 is the reset bit; a 1 asserts reset while a 0 clears it. */
3708 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3709 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3710 if (ret < 0) {
3711 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3712 "cpureset_assert(%d) error=%d",val,ret);
3713 pvr2_hdw_render_useless(hdw);
3714 }
3715}
3716
3717
3718int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3719{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003720 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03003721}
3722
3723
Michael Krufkye1edb192008-04-22 14:45:39 -03003724int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
3725{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003726 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03003727}
3728
Mike Isely1c9d10d2008-03-28 05:38:54 -03003729
Michael Krufkye1edb192008-04-22 14:45:39 -03003730int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
3731{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003732 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03003733}
3734
Mike Iselyd8554972006-06-26 20:58:46 -03003735
3736int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
3737{
3738 if (!hdw->decoder_ctrl) {
3739 pvr2_trace(PVR2_TRACE_INIT,
3740 "Unable to reset decoder: nothing attached");
3741 return -ENOTTY;
3742 }
3743
3744 if (!hdw->decoder_ctrl->force_reset) {
3745 pvr2_trace(PVR2_TRACE_INIT,
3746 "Unable to reset decoder: not implemented");
3747 return -ENOTTY;
3748 }
3749
3750 pvr2_trace(PVR2_TRACE_INIT,
3751 "Requesting decoder reset");
3752 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
3753 return 0;
3754}
3755
3756
Mike Isely62433e32008-04-22 14:45:40 -03003757static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003758{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003759 hdw->flag_ok = !0;
3760 return pvr2_issue_simple_cmd(hdw,
3761 FX2CMD_HCW_DEMOD_RESETIN |
3762 (1 << 8) |
3763 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03003764}
3765
Mike Isely84147f32008-04-22 14:45:40 -03003766
Mike Isely62433e32008-04-22 14:45:40 -03003767static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003768{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003769 hdw->flag_ok = !0;
3770 return pvr2_issue_simple_cmd(hdw,(onoff ?
3771 FX2CMD_ONAIR_DTV_POWER_ON :
3772 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03003773}
3774
Mike Isely62433e32008-04-22 14:45:40 -03003775
3776static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
3777 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003778{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003779 return pvr2_issue_simple_cmd(hdw,(onoff ?
3780 FX2CMD_ONAIR_DTV_STREAMING_ON :
3781 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03003782}
3783
Mike Isely62433e32008-04-22 14:45:40 -03003784
3785static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
3786{
3787 int cmode;
3788 /* Compare digital/analog desired setting with current setting. If
3789 they don't match, fix it... */
3790 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
3791 if (cmode == hdw->pathway_state) {
3792 /* They match; nothing to do */
3793 return;
3794 }
3795
3796 switch (hdw->hdw_desc->digital_control_scheme) {
3797 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3798 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
3799 if (cmode == PVR2_PATHWAY_ANALOG) {
3800 /* If moving to analog mode, also force the decoder
3801 to reset. If no decoder is attached, then it's
3802 ok to ignore this because if/when the decoder
3803 attaches, it will reset itself at that time. */
3804 pvr2_hdw_cmd_decoder_reset(hdw);
3805 }
3806 break;
3807 case PVR2_DIGITAL_SCHEME_ONAIR:
3808 /* Supposedly we should always have the power on whether in
3809 digital or analog mode. But for now do what appears to
3810 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003811 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03003812 break;
3813 default: break;
3814 }
3815
Mike Isely1b9c18c2008-04-22 14:45:41 -03003816 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03003817 hdw->pathway_state = cmode;
3818}
3819
3820
Adrian Bunke9b59f62008-05-10 04:35:24 -03003821static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03003822{
3823 /* change some GPIO data
3824 *
3825 * note: bit d7 of dir appears to control the LED,
3826 * so we shut it off here.
3827 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03003828 */
Mike Isely40381cb2008-04-22 14:45:42 -03003829 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003830 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03003831 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003832 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03003833 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03003834 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03003835}
Mike Iselyc55a97d2008-04-22 14:45:41 -03003836
Mike Isely40381cb2008-04-22 14:45:42 -03003837
3838typedef void (*led_method_func)(struct pvr2_hdw *,int);
3839
3840static led_method_func led_methods[] = {
3841 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
3842};
3843
3844
3845/* Toggle LED */
3846static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
3847{
3848 unsigned int scheme_id;
3849 led_method_func fp;
3850
3851 if ((!onoff) == (!hdw->led_on)) return;
3852
3853 hdw->led_on = onoff != 0;
3854
3855 scheme_id = hdw->hdw_desc->led_scheme;
3856 if (scheme_id < ARRAY_SIZE(led_methods)) {
3857 fp = led_methods[scheme_id];
3858 } else {
3859 fp = NULL;
3860 }
3861
3862 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03003863}
3864
3865
Mike Iselye61b6fc2006-07-18 22:42:18 -03003866/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003867static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03003868{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003869 int ret;
3870
3871 /* If we're in analog mode, then just issue the usual analog
3872 command. */
3873 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3874 return pvr2_issue_simple_cmd(hdw,
3875 (runFl ?
3876 FX2CMD_STREAMING_ON :
3877 FX2CMD_STREAMING_OFF));
3878 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03003879 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003880
3881 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
3882 /* Whoops, we don't know what mode we're in... */
3883 return -EINVAL;
3884 }
3885
3886 /* To get here we have to be in digital mode. The mechanism here
3887 is unfortunately different for different vendors. So we switch
3888 on the device's digital scheme attribute in order to figure out
3889 what to do. */
3890 switch (hdw->hdw_desc->digital_control_scheme) {
3891 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3892 return pvr2_issue_simple_cmd(hdw,
3893 (runFl ?
3894 FX2CMD_HCW_DTV_STREAMING_ON :
3895 FX2CMD_HCW_DTV_STREAMING_OFF));
3896 case PVR2_DIGITAL_SCHEME_ONAIR:
3897 ret = pvr2_issue_simple_cmd(hdw,
3898 (runFl ?
3899 FX2CMD_STREAMING_ON :
3900 FX2CMD_STREAMING_OFF));
3901 if (ret) return ret;
3902 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
3903 default:
3904 return -EINVAL;
3905 }
Mike Iselyd8554972006-06-26 20:58:46 -03003906}
3907
3908
Mike Isely62433e32008-04-22 14:45:40 -03003909/* Evaluate whether or not state_pathway_ok can change */
3910static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
3911{
3912 if (hdw->state_pathway_ok) {
3913 /* Nothing to do if pathway is already ok */
3914 return 0;
3915 }
3916 if (!hdw->state_pipeline_idle) {
3917 /* Not allowed to change anything if pipeline is not idle */
3918 return 0;
3919 }
3920 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
3921 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003922 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003923 return !0;
3924}
3925
3926
Mike Isely681c7392007-11-26 01:48:52 -03003927/* Evaluate whether or not state_encoder_ok can change */
3928static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
3929{
3930 if (hdw->state_encoder_ok) return 0;
3931 if (hdw->flag_tripped) return 0;
3932 if (hdw->state_encoder_run) return 0;
3933 if (hdw->state_encoder_config) return 0;
3934 if (hdw->state_decoder_run) return 0;
3935 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03003936 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
3937 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
3938 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
3939 return 0;
3940 }
3941
Mike Isely681c7392007-11-26 01:48:52 -03003942 if (pvr2_upload_firmware2(hdw) < 0) {
3943 hdw->flag_tripped = !0;
3944 trace_stbit("flag_tripped",hdw->flag_tripped);
3945 return !0;
3946 }
3947 hdw->state_encoder_ok = !0;
3948 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
3949 return !0;
3950}
3951
3952
3953/* Evaluate whether or not state_encoder_config can change */
3954static int state_eval_encoder_config(struct pvr2_hdw *hdw)
3955{
3956 if (hdw->state_encoder_config) {
3957 if (hdw->state_encoder_ok) {
3958 if (hdw->state_pipeline_req &&
3959 !hdw->state_pipeline_pause) return 0;
3960 }
3961 hdw->state_encoder_config = 0;
3962 hdw->state_encoder_waitok = 0;
3963 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3964 /* paranoia - solve race if timer just completed */
3965 del_timer_sync(&hdw->encoder_wait_timer);
3966 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003967 if (!hdw->state_pathway_ok ||
3968 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3969 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03003970 !hdw->state_pipeline_idle ||
3971 hdw->state_pipeline_pause ||
3972 !hdw->state_pipeline_req ||
3973 !hdw->state_pipeline_config) {
3974 /* We must reset the enforced wait interval if
3975 anything has happened that might have disturbed
3976 the encoder. This should be a rare case. */
3977 if (timer_pending(&hdw->encoder_wait_timer)) {
3978 del_timer_sync(&hdw->encoder_wait_timer);
3979 }
3980 if (hdw->state_encoder_waitok) {
3981 /* Must clear the state - therefore we did
3982 something to a state bit and must also
3983 return true. */
3984 hdw->state_encoder_waitok = 0;
3985 trace_stbit("state_encoder_waitok",
3986 hdw->state_encoder_waitok);
3987 return !0;
3988 }
3989 return 0;
3990 }
3991 if (!hdw->state_encoder_waitok) {
3992 if (!timer_pending(&hdw->encoder_wait_timer)) {
3993 /* waitok flag wasn't set and timer isn't
3994 running. Check flag once more to avoid
3995 a race then start the timer. This is
3996 the point when we measure out a minimal
3997 quiet interval before doing something to
3998 the encoder. */
3999 if (!hdw->state_encoder_waitok) {
4000 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004001 jiffies +
4002 (HZ * TIME_MSEC_ENCODER_WAIT
4003 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004004 add_timer(&hdw->encoder_wait_timer);
4005 }
4006 }
4007 /* We can't continue until we know we have been
4008 quiet for the interval measured by this
4009 timer. */
4010 return 0;
4011 }
4012 pvr2_encoder_configure(hdw);
4013 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
4014 }
4015 trace_stbit("state_encoder_config",hdw->state_encoder_config);
4016 return !0;
4017}
4018
4019
Mike Iselyd913d632008-04-06 04:04:35 -03004020/* Return true if the encoder should not be running. */
4021static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
4022{
4023 if (!hdw->state_encoder_ok) {
4024 /* Encoder isn't healthy at the moment, so stop it. */
4025 return !0;
4026 }
4027 if (!hdw->state_pathway_ok) {
4028 /* Mode is not understood at the moment (i.e. it wants to
4029 change), so encoder must be stopped. */
4030 return !0;
4031 }
4032
4033 switch (hdw->pathway_state) {
4034 case PVR2_PATHWAY_ANALOG:
4035 if (!hdw->state_decoder_run) {
4036 /* We're in analog mode and the decoder is not
4037 running; thus the encoder should be stopped as
4038 well. */
4039 return !0;
4040 }
4041 break;
4042 case PVR2_PATHWAY_DIGITAL:
4043 if (hdw->state_encoder_runok) {
4044 /* This is a funny case. We're in digital mode so
4045 really the encoder should be stopped. However
4046 if it really is running, only kill it after
4047 runok has been set. This gives a chance for the
4048 onair quirk to function (encoder must run
4049 briefly first, at least once, before onair
4050 digital streaming can work). */
4051 return !0;
4052 }
4053 break;
4054 default:
4055 /* Unknown mode; so encoder should be stopped. */
4056 return !0;
4057 }
4058
4059 /* If we get here, we haven't found a reason to stop the
4060 encoder. */
4061 return 0;
4062}
4063
4064
4065/* Return true if the encoder should be running. */
4066static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
4067{
4068 if (!hdw->state_encoder_ok) {
4069 /* Don't run the encoder if it isn't healthy... */
4070 return 0;
4071 }
4072 if (!hdw->state_pathway_ok) {
4073 /* Don't run the encoder if we don't (yet) know what mode
4074 we need to be in... */
4075 return 0;
4076 }
4077
4078 switch (hdw->pathway_state) {
4079 case PVR2_PATHWAY_ANALOG:
4080 if (hdw->state_decoder_run) {
4081 /* In analog mode, if the decoder is running, then
4082 run the encoder. */
4083 return !0;
4084 }
4085 break;
4086 case PVR2_PATHWAY_DIGITAL:
4087 if ((hdw->hdw_desc->digital_control_scheme ==
4088 PVR2_DIGITAL_SCHEME_ONAIR) &&
4089 !hdw->state_encoder_runok) {
4090 /* This is a quirk. OnAir hardware won't stream
4091 digital until the encoder has been run at least
4092 once, for a minimal period of time (empiricially
4093 measured to be 1/4 second). So if we're on
4094 OnAir hardware and the encoder has never been
4095 run at all, then start the encoder. Normal
4096 state machine logic in the driver will
4097 automatically handle the remaining bits. */
4098 return !0;
4099 }
4100 break;
4101 default:
4102 /* For completeness (unknown mode; encoder won't run ever) */
4103 break;
4104 }
4105 /* If we get here, then we haven't found any reason to run the
4106 encoder, so don't run it. */
4107 return 0;
4108}
4109
4110
Mike Isely681c7392007-11-26 01:48:52 -03004111/* Evaluate whether or not state_encoder_run can change */
4112static int state_eval_encoder_run(struct pvr2_hdw *hdw)
4113{
4114 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03004115 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004116 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03004117 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03004118 if (pvr2_encoder_stop(hdw) < 0) return !0;
4119 }
4120 hdw->state_encoder_run = 0;
4121 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03004122 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004123 if (pvr2_encoder_start(hdw) < 0) return !0;
4124 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03004125 if (!hdw->state_encoder_runok) {
4126 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004127 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03004128 add_timer(&hdw->encoder_run_timer);
4129 }
Mike Isely681c7392007-11-26 01:48:52 -03004130 }
4131 trace_stbit("state_encoder_run",hdw->state_encoder_run);
4132 return !0;
4133}
4134
4135
4136/* Timeout function for quiescent timer. */
4137static void pvr2_hdw_quiescent_timeout(unsigned long data)
4138{
4139 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4140 hdw->state_decoder_quiescent = !0;
4141 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4142 hdw->state_stale = !0;
4143 queue_work(hdw->workqueue,&hdw->workpoll);
4144}
4145
4146
4147/* Timeout function for encoder wait timer. */
4148static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
4149{
4150 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4151 hdw->state_encoder_waitok = !0;
4152 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4153 hdw->state_stale = !0;
4154 queue_work(hdw->workqueue,&hdw->workpoll);
4155}
4156
4157
Mike Iselyd913d632008-04-06 04:04:35 -03004158/* Timeout function for encoder run timer. */
4159static void pvr2_hdw_encoder_run_timeout(unsigned long data)
4160{
4161 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4162 if (!hdw->state_encoder_runok) {
4163 hdw->state_encoder_runok = !0;
4164 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
4165 hdw->state_stale = !0;
4166 queue_work(hdw->workqueue,&hdw->workpoll);
4167 }
4168}
4169
4170
Mike Isely681c7392007-11-26 01:48:52 -03004171/* Evaluate whether or not state_decoder_run can change */
4172static int state_eval_decoder_run(struct pvr2_hdw *hdw)
4173{
4174 if (hdw->state_decoder_run) {
4175 if (hdw->state_encoder_ok) {
4176 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03004177 !hdw->state_pipeline_pause &&
4178 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004179 }
4180 if (!hdw->flag_decoder_missed) {
4181 pvr2_decoder_enable(hdw,0);
4182 }
4183 hdw->state_decoder_quiescent = 0;
4184 hdw->state_decoder_run = 0;
4185 /* paranoia - solve race if timer just completed */
4186 del_timer_sync(&hdw->quiescent_timer);
4187 } else {
4188 if (!hdw->state_decoder_quiescent) {
4189 if (!timer_pending(&hdw->quiescent_timer)) {
4190 /* We don't do something about the
4191 quiescent timer until right here because
4192 we also want to catch cases where the
4193 decoder was already not running (like
4194 after initialization) as opposed to
4195 knowing that we had just stopped it.
4196 The second flag check is here to cover a
4197 race - the timer could have run and set
4198 this flag just after the previous check
4199 but before we did the pending check. */
4200 if (!hdw->state_decoder_quiescent) {
4201 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004202 jiffies +
4203 (HZ * TIME_MSEC_DECODER_WAIT
4204 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004205 add_timer(&hdw->quiescent_timer);
4206 }
4207 }
4208 /* Don't allow decoder to start again until it has
4209 been quiesced first. This little detail should
4210 hopefully further stabilize the encoder. */
4211 return 0;
4212 }
Mike Isely62433e32008-04-22 14:45:40 -03004213 if (!hdw->state_pathway_ok ||
4214 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4215 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03004216 hdw->state_pipeline_pause ||
4217 !hdw->state_pipeline_config ||
4218 !hdw->state_encoder_config ||
4219 !hdw->state_encoder_ok) return 0;
4220 del_timer_sync(&hdw->quiescent_timer);
4221 if (hdw->flag_decoder_missed) return 0;
4222 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
4223 hdw->state_decoder_quiescent = 0;
4224 hdw->state_decoder_run = !0;
4225 }
4226 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4227 trace_stbit("state_decoder_run",hdw->state_decoder_run);
4228 return !0;
4229}
4230
4231
4232/* Evaluate whether or not state_usbstream_run can change */
4233static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
4234{
4235 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03004236 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03004237 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03004238 fl = (hdw->state_encoder_ok &&
4239 hdw->state_encoder_run);
4240 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4241 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4242 fl = hdw->state_encoder_ok;
4243 }
4244 if (fl &&
4245 hdw->state_pipeline_req &&
4246 !hdw->state_pipeline_pause &&
4247 hdw->state_pathway_ok) {
4248 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004249 }
4250 pvr2_hdw_cmd_usbstream(hdw,0);
4251 hdw->state_usbstream_run = 0;
4252 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004253 if (!hdw->state_pipeline_req ||
4254 hdw->state_pipeline_pause ||
4255 !hdw->state_pathway_ok) return 0;
4256 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4257 if (!hdw->state_encoder_ok ||
4258 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004259 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4260 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4261 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03004262 if (hdw->state_encoder_run) return 0;
4263 if (hdw->hdw_desc->digital_control_scheme ==
4264 PVR2_DIGITAL_SCHEME_ONAIR) {
4265 /* OnAir digital receivers won't stream
4266 unless the analog encoder has run first.
4267 Why? I have no idea. But don't even
4268 try until we know the analog side is
4269 known to have run. */
4270 if (!hdw->state_encoder_runok) return 0;
4271 }
Mike Isely62433e32008-04-22 14:45:40 -03004272 }
Mike Isely681c7392007-11-26 01:48:52 -03004273 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
4274 hdw->state_usbstream_run = !0;
4275 }
4276 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
4277 return !0;
4278}
4279
4280
4281/* Attempt to configure pipeline, if needed */
4282static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
4283{
4284 if (hdw->state_pipeline_config ||
4285 hdw->state_pipeline_pause) return 0;
4286 pvr2_hdw_commit_execute(hdw);
4287 return !0;
4288}
4289
4290
4291/* Update pipeline idle and pipeline pause tracking states based on other
4292 inputs. This must be called whenever the other relevant inputs have
4293 changed. */
4294static int state_update_pipeline_state(struct pvr2_hdw *hdw)
4295{
4296 unsigned int st;
4297 int updatedFl = 0;
4298 /* Update pipeline state */
4299 st = !(hdw->state_encoder_run ||
4300 hdw->state_decoder_run ||
4301 hdw->state_usbstream_run ||
4302 (!hdw->state_decoder_quiescent));
4303 if (!st != !hdw->state_pipeline_idle) {
4304 hdw->state_pipeline_idle = st;
4305 updatedFl = !0;
4306 }
4307 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
4308 hdw->state_pipeline_pause = 0;
4309 updatedFl = !0;
4310 }
4311 return updatedFl;
4312}
4313
4314
4315typedef int (*state_eval_func)(struct pvr2_hdw *);
4316
4317/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03004318static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03004319 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03004320 state_eval_pipeline_config,
4321 state_eval_encoder_ok,
4322 state_eval_encoder_config,
4323 state_eval_decoder_run,
4324 state_eval_encoder_run,
4325 state_eval_usbstream_run,
4326};
4327
4328
4329/* Process various states and return true if we did anything interesting. */
4330static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4331{
4332 unsigned int i;
4333 int state_updated = 0;
4334 int check_flag;
4335
4336 if (!hdw->state_stale) return 0;
4337 if ((hdw->fw1_state != FW1_STATE_OK) ||
4338 !hdw->flag_ok) {
4339 hdw->state_stale = 0;
4340 return !0;
4341 }
4342 /* This loop is the heart of the entire driver. It keeps trying to
4343 evaluate various bits of driver state until nothing changes for
4344 one full iteration. Each "bit of state" tracks some global
4345 aspect of the driver, e.g. whether decoder should run, if
4346 pipeline is configured, usb streaming is on, etc. We separately
4347 evaluate each of those questions based on other driver state to
4348 arrive at the correct running configuration. */
4349 do {
4350 check_flag = 0;
4351 state_update_pipeline_state(hdw);
4352 /* Iterate over each bit of state */
4353 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4354 if ((*eval_funcs[i])(hdw)) {
4355 check_flag = !0;
4356 state_updated = !0;
4357 state_update_pipeline_state(hdw);
4358 }
4359 }
4360 } while (check_flag && hdw->flag_ok);
4361 hdw->state_stale = 0;
4362 trace_stbit("state_stale",hdw->state_stale);
4363 return state_updated;
4364}
4365
4366
Mike Isely1cb03b72008-04-21 03:47:43 -03004367static unsigned int print_input_mask(unsigned int msk,
4368 char *buf,unsigned int acnt)
4369{
4370 unsigned int idx,ccnt;
4371 unsigned int tcnt = 0;
4372 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4373 if (!((1 << idx) & msk)) continue;
4374 ccnt = scnprintf(buf+tcnt,
4375 acnt-tcnt,
4376 "%s%s",
4377 (tcnt ? ", " : ""),
4378 control_values_input[idx]);
4379 tcnt += ccnt;
4380 }
4381 return tcnt;
4382}
4383
4384
Mike Isely62433e32008-04-22 14:45:40 -03004385static const char *pvr2_pathway_state_name(int id)
4386{
4387 switch (id) {
4388 case PVR2_PATHWAY_ANALOG: return "analog";
4389 case PVR2_PATHWAY_DIGITAL: return "digital";
4390 default: return "unknown";
4391 }
4392}
4393
4394
Mike Isely681c7392007-11-26 01:48:52 -03004395static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4396 char *buf,unsigned int acnt)
4397{
4398 switch (which) {
4399 case 0:
4400 return scnprintf(
4401 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004402 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004403 (hdw->flag_ok ? " <ok>" : " <fail>"),
4404 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4405 (hdw->flag_disconnected ? " <disconnected>" :
4406 " <connected>"),
4407 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004408 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4409 pvr2_pathway_state_name(hdw->pathway_state));
4410
Mike Isely681c7392007-11-26 01:48:52 -03004411 case 1:
4412 return scnprintf(
4413 buf,acnt,
4414 "pipeline:%s%s%s%s",
4415 (hdw->state_pipeline_idle ? " <idle>" : ""),
4416 (hdw->state_pipeline_config ?
4417 " <configok>" : " <stale>"),
4418 (hdw->state_pipeline_req ? " <req>" : ""),
4419 (hdw->state_pipeline_pause ? " <pause>" : ""));
4420 case 2:
4421 return scnprintf(
4422 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004423 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004424 (hdw->state_decoder_run ?
4425 " <decode:run>" :
4426 (hdw->state_decoder_quiescent ?
4427 "" : " <decode:stop>")),
4428 (hdw->state_decoder_quiescent ?
4429 " <decode:quiescent>" : ""),
4430 (hdw->state_encoder_ok ?
4431 "" : " <encode:init>"),
4432 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004433 (hdw->state_encoder_runok ?
4434 " <encode:run>" :
4435 " <encode:firstrun>") :
4436 (hdw->state_encoder_runok ?
4437 " <encode:stop>" :
4438 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004439 (hdw->state_encoder_config ?
4440 " <encode:configok>" :
4441 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004442 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004443 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004444 " <usb:run>" : " <usb:stop>"),
4445 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004446 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004447 case 3:
4448 return scnprintf(
4449 buf,acnt,
4450 "state: %s",
4451 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004452 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004453 unsigned int tcnt = 0;
4454 unsigned int ccnt;
4455
4456 ccnt = scnprintf(buf,
4457 acnt,
4458 "Hardware supported inputs: ");
4459 tcnt += ccnt;
4460 tcnt += print_input_mask(hdw->input_avail_mask,
4461 buf+tcnt,
4462 acnt-tcnt);
4463 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4464 ccnt = scnprintf(buf+tcnt,
4465 acnt-tcnt,
4466 "; allowed inputs: ");
4467 tcnt += ccnt;
4468 tcnt += print_input_mask(hdw->input_allowed_mask,
4469 buf+tcnt,
4470 acnt-tcnt);
4471 }
4472 return tcnt;
4473 }
4474 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004475 struct pvr2_stream_stats stats;
4476 if (!hdw->vid_stream) break;
4477 pvr2_stream_get_stats(hdw->vid_stream,
4478 &stats,
4479 0);
4480 return scnprintf(
4481 buf,acnt,
4482 "Bytes streamed=%u"
4483 " URBs: queued=%u idle=%u ready=%u"
4484 " processed=%u failed=%u",
4485 stats.bytes_processed,
4486 stats.buffers_in_queue,
4487 stats.buffers_in_idle,
4488 stats.buffers_in_ready,
4489 stats.buffers_processed,
4490 stats.buffers_failed);
4491 }
Mike Isely681c7392007-11-26 01:48:52 -03004492 default: break;
4493 }
4494 return 0;
4495}
4496
4497
4498unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4499 char *buf,unsigned int acnt)
4500{
4501 unsigned int bcnt,ccnt,idx;
4502 bcnt = 0;
4503 LOCK_TAKE(hdw->big_lock);
4504 for (idx = 0; ; idx++) {
4505 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
4506 if (!ccnt) break;
4507 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4508 if (!acnt) break;
4509 buf[0] = '\n'; ccnt = 1;
4510 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4511 }
4512 LOCK_GIVE(hdw->big_lock);
4513 return bcnt;
4514}
4515
4516
4517static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
4518{
4519 char buf[128];
4520 unsigned int idx,ccnt;
4521
4522 for (idx = 0; ; idx++) {
4523 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
4524 if (!ccnt) break;
4525 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
4526 }
4527}
4528
4529
4530/* Evaluate and update the driver's current state, taking various actions
4531 as appropriate for the update. */
4532static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
4533{
4534 unsigned int st;
4535 int state_updated = 0;
4536 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004537 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03004538
4539 pvr2_trace(PVR2_TRACE_STBITS,
4540 "Drive state check START");
4541 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4542 pvr2_hdw_state_log_state(hdw);
4543 }
4544
4545 /* Process all state and get back over disposition */
4546 state_updated = pvr2_hdw_state_update(hdw);
4547
Mike Isely1b9c18c2008-04-22 14:45:41 -03004548 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
4549
Mike Isely681c7392007-11-26 01:48:52 -03004550 /* Update master state based upon all other states. */
4551 if (!hdw->flag_ok) {
4552 st = PVR2_STATE_DEAD;
4553 } else if (hdw->fw1_state != FW1_STATE_OK) {
4554 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03004555 } else if ((analog_mode ||
4556 hdw->hdw_desc->flag_digital_requires_cx23416) &&
4557 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03004558 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004559 } else if (hdw->flag_tripped ||
4560 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03004561 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03004562 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03004563 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03004564 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03004565 st = PVR2_STATE_RUN;
4566 } else {
4567 st = PVR2_STATE_READY;
4568 }
4569 if (hdw->master_state != st) {
4570 pvr2_trace(PVR2_TRACE_STATE,
4571 "Device state change from %s to %s",
4572 pvr2_get_state_name(hdw->master_state),
4573 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03004574 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03004575 hdw->master_state = st;
4576 state_updated = !0;
4577 callback_flag = !0;
4578 }
4579 if (state_updated) {
4580 /* Trigger anyone waiting on any state changes here. */
4581 wake_up(&hdw->state_wait_data);
4582 }
4583
4584 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4585 pvr2_hdw_state_log_state(hdw);
4586 }
4587 pvr2_trace(PVR2_TRACE_STBITS,
4588 "Drive state check DONE callback=%d",callback_flag);
4589
4590 return callback_flag;
4591}
4592
4593
4594/* Cause kernel thread to check / update driver state */
4595static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
4596{
4597 if (hdw->state_stale) return;
4598 hdw->state_stale = !0;
4599 trace_stbit("state_stale",hdw->state_stale);
4600 queue_work(hdw->workqueue,&hdw->workpoll);
4601}
4602
4603
Mike Iselyd8554972006-06-26 20:58:46 -03004604int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
4605{
4606 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
4607}
4608
4609
4610int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
4611{
4612 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
4613}
4614
4615
4616int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
4617{
4618 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
4619}
4620
4621
4622int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
4623{
4624 u32 cval,nval;
4625 int ret;
4626 if (~msk) {
4627 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
4628 if (ret) return ret;
4629 nval = (cval & ~msk) | (val & msk);
4630 pvr2_trace(PVR2_TRACE_GPIO,
4631 "GPIO direction changing 0x%x:0x%x"
4632 " from 0x%x to 0x%x",
4633 msk,val,cval,nval);
4634 } else {
4635 nval = val;
4636 pvr2_trace(PVR2_TRACE_GPIO,
4637 "GPIO direction changing to 0x%x",nval);
4638 }
4639 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
4640}
4641
4642
4643int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
4644{
4645 u32 cval,nval;
4646 int ret;
4647 if (~msk) {
4648 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
4649 if (ret) return ret;
4650 nval = (cval & ~msk) | (val & msk);
4651 pvr2_trace(PVR2_TRACE_GPIO,
4652 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
4653 msk,val,cval,nval);
4654 } else {
4655 nval = val;
4656 pvr2_trace(PVR2_TRACE_GPIO,
4657 "GPIO output changing to 0x%x",nval);
4658 }
4659 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
4660}
4661
4662
Mike Isely7fb20fa2008-04-22 14:45:37 -03004663unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
4664{
4665 return hdw->input_avail_mask;
4666}
4667
4668
Mike Isely1cb03b72008-04-21 03:47:43 -03004669unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
4670{
4671 return hdw->input_allowed_mask;
4672}
4673
4674
4675static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
4676{
4677 if (hdw->input_val != v) {
4678 hdw->input_val = v;
4679 hdw->input_dirty = !0;
4680 }
4681
4682 /* Handle side effects - if we switch to a mode that needs the RF
4683 tuner, then select the right frequency choice as well and mark
4684 it dirty. */
4685 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
4686 hdw->freqSelector = 0;
4687 hdw->freqDirty = !0;
4688 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
4689 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
4690 hdw->freqSelector = 1;
4691 hdw->freqDirty = !0;
4692 }
4693 return 0;
4694}
4695
4696
4697int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
4698 unsigned int change_mask,
4699 unsigned int change_val)
4700{
4701 int ret = 0;
4702 unsigned int nv,m,idx;
4703 LOCK_TAKE(hdw->big_lock);
4704 do {
4705 nv = hdw->input_allowed_mask & ~change_mask;
4706 nv |= (change_val & change_mask);
4707 nv &= hdw->input_avail_mask;
4708 if (!nv) {
4709 /* No legal modes left; return error instead. */
4710 ret = -EPERM;
4711 break;
4712 }
4713 hdw->input_allowed_mask = nv;
4714 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
4715 /* Current mode is still in the allowed mask, so
4716 we're done. */
4717 break;
4718 }
4719 /* Select and switch to a mode that is still in the allowed
4720 mask */
4721 if (!hdw->input_allowed_mask) {
4722 /* Nothing legal; give up */
4723 break;
4724 }
4725 m = hdw->input_allowed_mask;
4726 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
4727 if (!((1 << idx) & m)) continue;
4728 pvr2_hdw_set_input(hdw,idx);
4729 break;
4730 }
4731 } while (0);
4732 LOCK_GIVE(hdw->big_lock);
4733 return ret;
4734}
4735
4736
Mike Iselye61b6fc2006-07-18 22:42:18 -03004737/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004738static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03004739{
4740 int result;
4741 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03004742 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03004743 result = pvr2_send_request(hdw,
4744 hdw->cmd_buffer,1,
4745 hdw->cmd_buffer,1);
4746 if (result < 0) break;
4747 result = hdw->cmd_buffer[0];
4748 } while(0); LOCK_GIVE(hdw->ctl_lock);
4749 return result;
4750}
4751
4752
Mike Isely32ffa9a2006-09-23 22:26:52 -03004753int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03004754 struct v4l2_dbg_match *match, u64 reg_id,
4755 int setFl, u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03004756{
4757#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03004758 struct pvr2_i2c_client *cp;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03004759 struct v4l2_dbg_register req;
Mike Isely6d988162006-09-28 17:53:49 -03004760 int stat = 0;
4761 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004762
Mike Isely201f5c92007-01-28 16:08:36 -03004763 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
4764
Hans Verkuilaecde8b2008-12-30 07:14:19 -03004765 req.match = *match;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004766 req.reg = reg_id;
4767 if (setFl) req.val = *val_ptr;
4768 mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03004769 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03004770 if (!v4l2_chip_match_i2c_client(
4771 cp->client,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03004772 &req.match)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004773 continue;
4774 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004775 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03004776 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
4777 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03004778 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03004779 okFl = !0;
4780 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004781 }
4782 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03004783 if (okFl) {
4784 return stat;
4785 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004786 return -EINVAL;
4787#else
4788 return -ENOSYS;
4789#endif
4790}
4791
4792
Mike Iselyd8554972006-06-26 20:58:46 -03004793/*
4794 Stuff for Emacs to see, in order to encourage consistent editing style:
4795 *** Local Variables: ***
4796 *** mode: c ***
4797 *** fill-column: 75 ***
4798 *** tab-width: 8 ***
4799 *** c-basic-offset: 8 ***
4800 *** End: ***
4801 */