5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/firmware.h>
26 #include <linux/videodev2.h>
27 #include <media/v4l2-common.h>
28 #include <asm/semaphore.h>
30 #include "pvrusb2-std.h"
31 #include "pvrusb2-util.h"
32 #include "pvrusb2-hdw.h"
33 #include "pvrusb2-i2c-core.h"
34 #include "pvrusb2-tuner.h"
35 #include "pvrusb2-eeprom.h"
36 #include "pvrusb2-hdw-internal.h"
37 #include "pvrusb2-encoder.h"
38 #include "pvrusb2-debug.h"
40 #define TV_MIN_FREQ 55250000L
41 #define TV_MAX_FREQ 850000000L
42 #define RADIO_MIN_FREQ 87000000L
43 #define RADIO_MAX_FREQ 108000000L
45 struct usb_device_id pvr2_device_table[] = {
46 [PVR2_HDW_TYPE_29XXX] = { USB_DEVICE(0x2040, 0x2900) },
47 [PVR2_HDW_TYPE_24XXX] = { USB_DEVICE(0x2040, 0x2400) },
51 MODULE_DEVICE_TABLE(usb, pvr2_device_table);
53 static const char *pvr2_device_names[] = {
54 [PVR2_HDW_TYPE_29XXX] = "WinTV PVR USB2 Model Category 29xxxx",
55 [PVR2_HDW_TYPE_24XXX] = "WinTV PVR USB2 Model Category 24xxxx",
58 struct pvr2_string_table {
63 // Names of other client modules to request for 24xxx model hardware
64 static const char *pvr2_client_24xxx[] = {
70 // Names of other client modules to request for 29xxx model hardware
71 static const char *pvr2_client_29xxx[] = {
77 static struct pvr2_string_table pvr2_client_lists[] = {
78 [PVR2_HDW_TYPE_29XXX] = {
80 sizeof(pvr2_client_29xxx)/sizeof(pvr2_client_29xxx[0]),
82 [PVR2_HDW_TYPE_24XXX] = {
84 sizeof(pvr2_client_24xxx)/sizeof(pvr2_client_24xxx[0]),
88 static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
89 static DECLARE_MUTEX(pvr2_unit_sem);
91 static int ctlchg = 0;
92 static int initusbreset = 1;
93 static int procreload = 0;
94 static int tuner[PVR_NUM] = { [0 ... PVR_NUM-1] = -1 };
95 static int tolerance[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
96 static int video_std[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
97 static int init_pause_msec = 0;
99 module_param(ctlchg, int, S_IRUGO|S_IWUSR);
100 MODULE_PARM_DESC(ctlchg, "0=optimize ctl change 1=always accept new ctl value");
101 module_param(init_pause_msec, int, S_IRUGO|S_IWUSR);
102 MODULE_PARM_DESC(init_pause_msec, "hardware initialization settling delay");
103 module_param(initusbreset, int, S_IRUGO|S_IWUSR);
104 MODULE_PARM_DESC(initusbreset, "Do USB reset device on probe");
105 module_param(procreload, int, S_IRUGO|S_IWUSR);
106 MODULE_PARM_DESC(procreload,
107 "Attempt init failure recovery with firmware reload");
108 module_param_array(tuner, int, NULL, 0444);
109 MODULE_PARM_DESC(tuner,"specify installed tuner type");
110 module_param_array(video_std, int, NULL, 0444);
111 MODULE_PARM_DESC(video_std,"specify initial video standard");
112 module_param_array(tolerance, int, NULL, 0444);
113 MODULE_PARM_DESC(tolerance,"specify stream error tolerance");
115 #define PVR2_CTL_WRITE_ENDPOINT 0x01
116 #define PVR2_CTL_READ_ENDPOINT 0x81
118 #define PVR2_GPIO_IN 0x9008
119 #define PVR2_GPIO_OUT 0x900c
120 #define PVR2_GPIO_DIR 0x9020
122 #define trace_firmware(...) pvr2_trace(PVR2_TRACE_FIRMWARE,__VA_ARGS__)
124 #define PVR2_FIRMWARE_ENDPOINT 0x02
126 /* size of a firmware chunk */
127 #define FIRMWARE_CHUNK_SIZE 0x2000
129 /* Define the list of additional controls we'll dynamically construct based
130 on query of the cx2341x module. */
131 struct pvr2_mpeg_ids {
135 static const struct pvr2_mpeg_ids mpeg_ids[] = {
137 .strid = "audio_layer",
138 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
140 .strid = "audio_bitrate",
141 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
143 /* Already using audio_mode elsewhere :-( */
144 .strid = "mpeg_audio_mode",
145 .id = V4L2_CID_MPEG_AUDIO_MODE,
147 .strid = "mpeg_audio_mode_extension",
148 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
150 .strid = "audio_emphasis",
151 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
153 .strid = "audio_crc",
154 .id = V4L2_CID_MPEG_AUDIO_CRC,
156 .strid = "video_aspect",
157 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
159 .strid = "video_b_frames",
160 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
162 .strid = "video_gop_size",
163 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
165 .strid = "video_gop_closure",
166 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
168 .strid = "video_bitrate_mode",
169 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
171 .strid = "video_bitrate",
172 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
174 .strid = "video_bitrate_peak",
175 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
177 .strid = "video_temporal_decimation",
178 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
180 .strid = "stream_type",
181 .id = V4L2_CID_MPEG_STREAM_TYPE,
183 .strid = "video_spatial_filter_mode",
184 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
186 .strid = "video_spatial_filter",
187 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
189 .strid = "video_luma_spatial_filter_type",
190 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
192 .strid = "video_chroma_spatial_filter_type",
193 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
195 .strid = "video_temporal_filter_mode",
196 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
198 .strid = "video_temporal_filter",
199 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
201 .strid = "video_median_filter_type",
202 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
204 .strid = "video_luma_median_filter_top",
205 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
207 .strid = "video_luma_median_filter_bottom",
208 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
210 .strid = "video_chroma_median_filter_top",
211 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
213 .strid = "video_chroma_median_filter_bottom",
214 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
217 #define MPEGDEF_COUNT (sizeof(mpeg_ids)/sizeof(mpeg_ids[0]))
220 static const char *control_values_srate[] = {
221 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
222 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
223 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
228 static const char *control_values_input[] = {
229 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
230 [PVR2_CVAL_INPUT_RADIO] = "radio",
231 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
232 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
236 static const char *control_values_audiomode[] = {
237 [V4L2_TUNER_MODE_MONO] = "Mono",
238 [V4L2_TUNER_MODE_STEREO] = "Stereo",
239 [V4L2_TUNER_MODE_LANG1] = "Lang1",
240 [V4L2_TUNER_MODE_LANG2] = "Lang2",
241 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
245 static const char *control_values_hsm[] = {
246 [PVR2_CVAL_HSM_FAIL] = "Fail",
247 [PVR2_CVAL_HSM_HIGH] = "High",
248 [PVR2_CVAL_HSM_FULL] = "Full",
252 static const char *control_values_subsystem[] = {
253 [PVR2_SUBSYS_B_ENC_FIRMWARE] = "enc_firmware",
254 [PVR2_SUBSYS_B_ENC_CFG] = "enc_config",
255 [PVR2_SUBSYS_B_DIGITIZER_RUN] = "digitizer_run",
256 [PVR2_SUBSYS_B_USBSTREAM_RUN] = "usbstream_run",
257 [PVR2_SUBSYS_B_ENC_RUN] = "enc_run",
260 static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
261 static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
262 static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw);
263 static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
264 static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
265 static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
266 static void pvr2_hdw_render_useless_unlocked(struct pvr2_hdw *hdw);
267 static void pvr2_hdw_subsys_bit_chg_no_lock(struct pvr2_hdw *hdw,
270 static void pvr2_hdw_subsys_stream_bit_chg_no_lock(struct pvr2_hdw *hdw,
273 static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
274 unsigned int timeout,int probe_fl,
275 void *write_data,unsigned int write_len,
276 void *read_data,unsigned int read_len);
277 static int pvr2_write_u16(struct pvr2_hdw *hdw, u16 data, int res);
278 static int pvr2_write_u8(struct pvr2_hdw *hdw, u8 data, int res);
280 static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
282 struct pvr2_hdw *hdw = cptr->hdw;
283 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
284 *vp = hdw->freqTable[hdw->freqProgSlot-1];
291 static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
293 struct pvr2_hdw *hdw = cptr->hdw;
294 unsigned int slotId = hdw->freqProgSlot;
295 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
296 hdw->freqTable[slotId-1] = v;
297 /* Handle side effects correctly - if we're tuned to this
298 slot, then forgot the slot id relation since the stored
299 frequency has been changed. */
300 if (hdw->freqSelector) {
301 if (hdw->freqSlotRadio == slotId) {
302 hdw->freqSlotRadio = 0;
305 if (hdw->freqSlotTelevision == slotId) {
306 hdw->freqSlotTelevision = 0;
313 static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
315 *vp = cptr->hdw->freqProgSlot;
319 static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
321 struct pvr2_hdw *hdw = cptr->hdw;
322 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
323 hdw->freqProgSlot = v;
328 static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
330 struct pvr2_hdw *hdw = cptr->hdw;
331 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
335 static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
338 struct pvr2_hdw *hdw = cptr->hdw;
339 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
341 freq = hdw->freqTable[slotId-1];
343 pvr2_hdw_set_cur_freq(hdw,freq);
345 if (hdw->freqSelector) {
346 hdw->freqSlotRadio = slotId;
348 hdw->freqSlotTelevision = slotId;
353 static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
355 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
359 static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
361 return cptr->hdw->freqDirty != 0;
364 static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
366 cptr->hdw->freqDirty = 0;
369 static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
371 pvr2_hdw_set_cur_freq(cptr->hdw,v);
375 static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
377 /* Actual maximum depends on the video standard in effect. */
378 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
386 static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
388 /* Actual minimum depends on device type. */
389 if (cptr->hdw->hdw_type == PVR2_HDW_TYPE_24XXX) {
397 static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
399 *vp = cptr->hdw->input_val;
403 static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
405 struct pvr2_hdw *hdw = cptr->hdw;
407 if (hdw->input_val != v) {
409 hdw->input_dirty = !0;
412 /* Handle side effects - if we switch to a mode that needs the RF
413 tuner, then select the right frequency choice as well and mark
415 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
416 hdw->freqSelector = 0;
418 } else if (hdw->input_val == PVR2_CVAL_INPUT_TV) {
419 hdw->freqSelector = 1;
425 static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
427 return cptr->hdw->input_dirty != 0;
430 static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
432 cptr->hdw->input_dirty = 0;
435 static int ctrl_freq_check(struct pvr2_ctrl *cptr,int v)
437 /* Both ranges are simultaneously considered legal, in order to
438 permit implicit mode switching, i.e. set a frequency in the
439 other range and the mode will switch */
440 return (((v >= RADIO_MIN_FREQ) && (v <= RADIO_MAX_FREQ)) ||
441 ((v >= TV_MIN_FREQ) && (v <= TV_MAX_FREQ)));
444 static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
446 /* Actual maximum depends on radio/tv mode */
447 if (cptr->hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
448 *vp = RADIO_MAX_FREQ;
455 static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
457 /* Actual minimum depends on radio/tv mode */
458 if (cptr->hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
459 *vp = RADIO_MIN_FREQ;
466 static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
468 return cptr->hdw->enc_stale != 0;
471 static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
473 cptr->hdw->enc_stale = 0;
476 static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
479 struct v4l2_ext_controls cs;
480 struct v4l2_ext_control c1;
481 memset(&cs,0,sizeof(cs));
482 memset(&c1,0,sizeof(c1));
485 c1.id = cptr->info->v4l_id;
486 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state,&cs,
493 static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
496 struct v4l2_ext_controls cs;
497 struct v4l2_ext_control c1;
498 memset(&cs,0,sizeof(cs));
499 memset(&c1,0,sizeof(c1));
502 c1.id = cptr->info->v4l_id;
504 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state,&cs,
507 cptr->hdw->enc_stale = !0;
511 static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
513 struct v4l2_queryctrl qctrl;
514 struct pvr2_ctl_info *info;
515 qctrl.id = cptr->info->v4l_id;
516 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
517 /* Strip out the const so we can adjust a function pointer. It's
518 OK to do this here because we know this is a dynamically created
519 control, so the underlying storage for the info pointer is (a)
520 private to us, and (b) not in read-only storage. Either we do
521 this or we significantly complicate the underlying control
523 info = (struct pvr2_ctl_info *)(cptr->info);
524 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
525 if (info->set_value) {
526 info->set_value = NULL;
529 if (!(info->set_value)) {
530 info->set_value = ctrl_cx2341x_set;
536 static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
538 *vp = cptr->hdw->flag_streaming_enabled;
542 static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
544 int result = pvr2_hdw_is_hsm(cptr->hdw);
545 *vp = PVR2_CVAL_HSM_FULL;
546 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
547 if (result) *vp = PVR2_CVAL_HSM_HIGH;
551 static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
553 *vp = cptr->hdw->std_mask_avail;
557 static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
559 struct pvr2_hdw *hdw = cptr->hdw;
561 ns = hdw->std_mask_avail;
562 ns = (ns & ~m) | (v & m);
563 if (ns == hdw->std_mask_avail) return 0;
564 hdw->std_mask_avail = ns;
565 pvr2_hdw_internal_set_std_avail(hdw);
566 pvr2_hdw_internal_find_stdenum(hdw);
570 static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
571 char *bufPtr,unsigned int bufSize,
574 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
578 static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
579 const char *bufPtr,unsigned int bufSize,
584 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
585 if (ret < 0) return ret;
586 if (mskp) *mskp = id;
587 if (valp) *valp = id;
591 static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
593 *vp = cptr->hdw->std_mask_cur;
597 static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
599 struct pvr2_hdw *hdw = cptr->hdw;
601 ns = hdw->std_mask_cur;
602 ns = (ns & ~m) | (v & m);
603 if (ns == hdw->std_mask_cur) return 0;
604 hdw->std_mask_cur = ns;
606 pvr2_hdw_internal_find_stdenum(hdw);
610 static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
612 return cptr->hdw->std_dirty != 0;
615 static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
617 cptr->hdw->std_dirty = 0;
620 static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
622 struct pvr2_hdw *hdw = cptr->hdw;
623 pvr2_i2c_core_status_poll(hdw);
624 *vp = hdw->tuner_signal_info.signal;
628 static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
631 unsigned int subchan;
632 struct pvr2_hdw *hdw = cptr->hdw;
633 if (hdw->tuner_signal_stale) {
634 pvr2_i2c_core_status_poll(hdw);
636 subchan = hdw->tuner_signal_info.rxsubchans;
637 if (subchan & V4L2_TUNER_SUB_MONO) {
638 val |= (1 << V4L2_TUNER_MODE_MONO);
640 if (subchan & V4L2_TUNER_SUB_STEREO) {
641 val |= (1 << V4L2_TUNER_MODE_STEREO);
643 if (subchan & V4L2_TUNER_SUB_LANG1) {
644 val |= (1 << V4L2_TUNER_MODE_LANG1);
646 if (subchan & V4L2_TUNER_SUB_LANG2) {
647 val |= (1 << V4L2_TUNER_MODE_LANG2);
653 static int ctrl_subsys_get(struct pvr2_ctrl *cptr,int *vp)
655 *vp = cptr->hdw->subsys_enabled_mask;
659 static int ctrl_subsys_set(struct pvr2_ctrl *cptr,int m,int v)
661 pvr2_hdw_subsys_bit_chg_no_lock(cptr->hdw,m,v);
665 static int ctrl_subsys_stream_get(struct pvr2_ctrl *cptr,int *vp)
667 *vp = cptr->hdw->subsys_stream_mask;
671 static int ctrl_subsys_stream_set(struct pvr2_ctrl *cptr,int m,int v)
673 pvr2_hdw_subsys_stream_bit_chg_no_lock(cptr->hdw,m,v);
677 static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
679 struct pvr2_hdw *hdw = cptr->hdw;
680 if (v < 0) return -EINVAL;
681 if (v > hdw->std_enum_cnt) return -EINVAL;
682 hdw->std_enum_cur = v;
685 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
686 hdw->std_mask_cur = hdw->std_defs[v].id;
692 static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
694 *vp = cptr->hdw->std_enum_cur;
699 static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
701 return cptr->hdw->std_dirty != 0;
705 static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
707 cptr->hdw->std_dirty = 0;
711 #define DEFINT(vmin,vmax) \
712 .type = pvr2_ctl_int, \
713 .def.type_int.min_value = vmin, \
714 .def.type_int.max_value = vmax
716 #define DEFENUM(tab) \
717 .type = pvr2_ctl_enum, \
718 .def.type_enum.count = (sizeof(tab)/sizeof((tab)[0])), \
719 .def.type_enum.value_names = tab
722 .type = pvr2_ctl_bool
724 #define DEFMASK(msk,tab) \
725 .type = pvr2_ctl_bitmask, \
726 .def.type_bitmask.valid_bits = msk, \
727 .def.type_bitmask.bit_names = tab
729 #define DEFREF(vname) \
730 .set_value = ctrl_set_##vname, \
731 .get_value = ctrl_get_##vname, \
732 .is_dirty = ctrl_isdirty_##vname, \
733 .clear_dirty = ctrl_cleardirty_##vname
736 #define VCREATE_FUNCS(vname) \
737 static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
738 {*vp = cptr->hdw->vname##_val; return 0;} \
739 static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
740 {cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
741 static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
742 {return cptr->hdw->vname##_dirty != 0;} \
743 static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
744 {cptr->hdw->vname##_dirty = 0;}
746 VCREATE_FUNCS(brightness)
747 VCREATE_FUNCS(contrast)
748 VCREATE_FUNCS(saturation)
750 VCREATE_FUNCS(volume)
751 VCREATE_FUNCS(balance)
753 VCREATE_FUNCS(treble)
755 VCREATE_FUNCS(audiomode)
756 VCREATE_FUNCS(res_hor)
757 VCREATE_FUNCS(res_ver)
760 /* Table definition of all controls which can be manipulated */
761 static const struct pvr2_ctl_info control_defs[] = {
763 .v4l_id = V4L2_CID_BRIGHTNESS,
764 .desc = "Brightness",
765 .name = "brightness",
766 .default_value = 128,
770 .v4l_id = V4L2_CID_CONTRAST,
777 .v4l_id = V4L2_CID_SATURATION,
778 .desc = "Saturation",
779 .name = "saturation",
784 .v4l_id = V4L2_CID_HUE,
791 .v4l_id = V4L2_CID_AUDIO_VOLUME,
794 .default_value = 62000,
798 .v4l_id = V4L2_CID_AUDIO_BALANCE,
803 DEFINT(-32768,32767),
805 .v4l_id = V4L2_CID_AUDIO_BASS,
810 DEFINT(-32768,32767),
812 .v4l_id = V4L2_CID_AUDIO_TREBLE,
817 DEFINT(-32768,32767),
819 .v4l_id = V4L2_CID_AUDIO_MUTE,
826 .desc = "Video Source",
828 .internal_id = PVR2_CID_INPUT,
829 .default_value = PVR2_CVAL_INPUT_TV,
831 DEFENUM(control_values_input),
833 .desc = "Audio Mode",
834 .name = "audio_mode",
835 .internal_id = PVR2_CID_AUDIOMODE,
836 .default_value = V4L2_TUNER_MODE_STEREO,
838 DEFENUM(control_values_audiomode),
840 .desc = "Horizontal capture resolution",
841 .name = "resolution_hor",
842 .internal_id = PVR2_CID_HRES,
843 .default_value = 720,
847 .desc = "Vertical capture resolution",
848 .name = "resolution_ver",
849 .internal_id = PVR2_CID_VRES,
850 .default_value = 480,
853 /* Hook in check for video standard and adjust maximum
854 depending on the standard. */
855 .get_max_value = ctrl_vres_max_get,
856 .get_min_value = ctrl_vres_min_get,
858 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
859 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
860 .desc = "Audio Sampling Frequency",
863 DEFENUM(control_values_srate),
865 .desc = "Tuner Frequency (Hz)",
867 .internal_id = PVR2_CID_FREQUENCY,
869 .set_value = ctrl_freq_set,
870 .get_value = ctrl_freq_get,
871 .is_dirty = ctrl_freq_is_dirty,
872 .clear_dirty = ctrl_freq_clear_dirty,
873 DEFINT(TV_MIN_FREQ,TV_MAX_FREQ),
874 /* Hook in check for input value (tv/radio) and adjust
875 max/min values accordingly */
876 .check_value = ctrl_freq_check,
877 .get_max_value = ctrl_freq_max_get,
878 .get_min_value = ctrl_freq_min_get,
882 .set_value = ctrl_channel_set,
883 .get_value = ctrl_channel_get,
884 DEFINT(0,FREQTABLE_SIZE),
886 .desc = "Channel Program Frequency",
887 .name = "freq_table_value",
888 .set_value = ctrl_channelfreq_set,
889 .get_value = ctrl_channelfreq_get,
890 DEFINT(TV_MIN_FREQ,TV_MAX_FREQ),
891 /* Hook in check for input value (tv/radio) and adjust
892 max/min values accordingly */
893 .check_value = ctrl_freq_check,
894 .get_max_value = ctrl_freq_max_get,
895 .get_min_value = ctrl_freq_min_get,
897 .desc = "Channel Program ID",
898 .name = "freq_table_channel",
899 .set_value = ctrl_channelprog_set,
900 .get_value = ctrl_channelprog_get,
901 DEFINT(0,FREQTABLE_SIZE),
903 .desc = "Streaming Enabled",
904 .name = "streaming_enabled",
905 .get_value = ctrl_streamingenabled_get,
910 .get_value = ctrl_hsm_get,
911 DEFENUM(control_values_hsm),
913 .desc = "Signal Present",
914 .name = "signal_present",
915 .get_value = ctrl_signal_get,
918 .desc = "Audio Modes Present",
919 .name = "audio_modes_present",
920 .get_value = ctrl_audio_modes_present_get,
921 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
922 v4l. Nothing outside of this module cares about this,
923 but I reuse it in order to also reuse the
924 control_values_audiomode string table. */
925 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
926 (1 << V4L2_TUNER_MODE_STEREO)|
927 (1 << V4L2_TUNER_MODE_LANG1)|
928 (1 << V4L2_TUNER_MODE_LANG2)),
929 control_values_audiomode),
931 .desc = "Video Standards Available Mask",
932 .name = "video_standard_mask_available",
933 .internal_id = PVR2_CID_STDAVAIL,
935 .get_value = ctrl_stdavail_get,
936 .set_value = ctrl_stdavail_set,
937 .val_to_sym = ctrl_std_val_to_sym,
938 .sym_to_val = ctrl_std_sym_to_val,
939 .type = pvr2_ctl_bitmask,
941 .desc = "Video Standards In Use Mask",
942 .name = "video_standard_mask_active",
943 .internal_id = PVR2_CID_STDCUR,
945 .get_value = ctrl_stdcur_get,
946 .set_value = ctrl_stdcur_set,
947 .is_dirty = ctrl_stdcur_is_dirty,
948 .clear_dirty = ctrl_stdcur_clear_dirty,
949 .val_to_sym = ctrl_std_val_to_sym,
950 .sym_to_val = ctrl_std_sym_to_val,
951 .type = pvr2_ctl_bitmask,
953 .desc = "Subsystem enabled mask",
954 .name = "debug_subsys_mask",
956 .get_value = ctrl_subsys_get,
957 .set_value = ctrl_subsys_set,
958 DEFMASK(PVR2_SUBSYS_ALL,control_values_subsystem),
960 .desc = "Subsystem stream mask",
961 .name = "debug_subsys_stream_mask",
963 .get_value = ctrl_subsys_stream_get,
964 .set_value = ctrl_subsys_stream_set,
965 DEFMASK(PVR2_SUBSYS_ALL,control_values_subsystem),
967 .desc = "Video Standard Name",
968 .name = "video_standard",
969 .internal_id = PVR2_CID_STDENUM,
971 .get_value = ctrl_stdenumcur_get,
972 .set_value = ctrl_stdenumcur_set,
973 .is_dirty = ctrl_stdenumcur_is_dirty,
974 .clear_dirty = ctrl_stdenumcur_clear_dirty,
975 .type = pvr2_ctl_enum,
979 #define CTRLDEF_COUNT (sizeof(control_defs)/sizeof(control_defs[0]))
982 const char *pvr2_config_get_name(enum pvr2_config cfg)
985 case pvr2_config_empty: return "empty";
986 case pvr2_config_mpeg: return "mpeg";
987 case pvr2_config_vbi: return "vbi";
988 case pvr2_config_pcm: return "pcm";
989 case pvr2_config_rawvideo: return "raw video";
995 struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1001 unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1003 return hdw->serial_number;
1006 unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1008 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1011 /* Set the currently tuned frequency and account for all possible
1012 driver-core side effects of this action. */
1013 void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
1015 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
1016 if (hdw->freqSelector) {
1017 /* Swing over to radio frequency selection */
1018 hdw->freqSelector = 0;
1019 hdw->freqDirty = !0;
1021 if (hdw->freqValRadio != val) {
1022 hdw->freqValRadio = val;
1023 hdw->freqSlotRadio = 0;
1024 hdw->freqDirty = !0;
1027 if (!(hdw->freqSelector)) {
1028 /* Swing over to television frequency selection */
1029 hdw->freqSelector = 1;
1030 hdw->freqDirty = !0;
1032 if (hdw->freqValTelevision != val) {
1033 hdw->freqValTelevision = val;
1034 hdw->freqSlotTelevision = 0;
1035 hdw->freqDirty = !0;
1040 int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1042 return hdw->unit_number;
1046 /* Attempt to locate one of the given set of files. Messages are logged
1047 appropriate to what has been found. The return value will be 0 or
1048 greater on success (it will be the index of the file name found) and
1049 fw_entry will be filled in. Otherwise a negative error is returned on
1050 failure. If the return value is -ENOENT then no viable firmware file
1051 could be located. */
1052 static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1053 const struct firmware **fw_entry,
1054 const char *fwtypename,
1055 unsigned int fwcount,
1056 const char *fwnames[])
1060 for (idx = 0; idx < fwcount; idx++) {
1061 ret = request_firmware(fw_entry,
1063 &hdw->usb_dev->dev);
1065 trace_firmware("Located %s firmware: %s;"
1071 if (ret == -ENOENT) continue;
1072 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1073 "request_firmware fatal error with code=%d",ret);
1076 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1078 " Device %s firmware"
1079 " seems to be missing.",
1081 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1082 "Did you install the pvrusb2 firmware files"
1083 " in their proper location?");
1085 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1086 "request_firmware unable to locate %s file %s",
1087 fwtypename,fwnames[0]);
1089 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1090 "request_firmware unable to locate"
1091 " one of the following %s files:",
1093 for (idx = 0; idx < fwcount; idx++) {
1094 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1095 "request_firmware: Failed to find %s",
1104 * pvr2_upload_firmware1().
1106 * Send the 8051 firmware to the device. After the upload, arrange for
1107 * device to re-enumerate.
1109 * NOTE : the pointer to the firmware data given by request_firmware()
1110 * is not suitable for an usb transaction.
1113 static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
1115 const struct firmware *fw_entry = NULL;
1120 static const char *fw_files_29xxx[] = {
1121 "v4l-pvrusb2-29xxx-01.fw",
1123 static const char *fw_files_24xxx[] = {
1124 "v4l-pvrusb2-24xxx-01.fw",
1126 static const struct pvr2_string_table fw_file_defs[] = {
1127 [PVR2_HDW_TYPE_29XXX] = {
1129 sizeof(fw_files_29xxx)/sizeof(fw_files_29xxx[0]),
1131 [PVR2_HDW_TYPE_24XXX] = {
1133 sizeof(fw_files_24xxx)/sizeof(fw_files_24xxx[0]),
1136 hdw->fw1_state = FW1_STATE_FAILED; // default result
1138 trace_firmware("pvr2_upload_firmware1");
1140 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
1141 fw_file_defs[hdw->hdw_type].cnt,
1142 fw_file_defs[hdw->hdw_type].lst);
1144 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1148 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1149 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1151 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1153 if (fw_entry->size != 0x2000){
1154 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1155 release_firmware(fw_entry);
1159 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1160 if (fw_ptr == NULL){
1161 release_firmware(fw_entry);
1165 /* We have to hold the CPU during firmware upload. */
1166 pvr2_hdw_cpureset_assert(hdw,1);
1168 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1172 for(address = 0; address < fw_entry->size; address += 0x800) {
1173 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1174 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1175 0, fw_ptr, 0x800, HZ);
1178 trace_firmware("Upload done, releasing device's CPU");
1180 /* Now release the CPU. It will disconnect and reconnect later. */
1181 pvr2_hdw_cpureset_assert(hdw,0);
1184 release_firmware(fw_entry);
1186 trace_firmware("Upload done (%d bytes sent)",ret);
1188 /* We should have written 8192 bytes */
1190 hdw->fw1_state = FW1_STATE_RELOAD;
1199 * pvr2_upload_firmware2()
1201 * This uploads encoder firmware on endpoint 2.
1205 int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1207 const struct firmware *fw_entry = NULL;
1209 unsigned int pipe, fw_len, fw_done;
1213 static const char *fw_files[] = {
1214 CX2341X_FIRM_ENC_FILENAME,
1217 trace_firmware("pvr2_upload_firmware2");
1219 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
1220 sizeof(fw_files)/sizeof(fw_files[0]),
1222 if (ret < 0) return ret;
1225 /* Since we're about to completely reinitialize the encoder,
1226 invalidate our cached copy of its configuration state. Next
1227 time we configure the encoder, then we'll fully configure it. */
1228 hdw->enc_cur_valid = 0;
1230 /* First prepare firmware loading */
1231 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1232 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1233 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1234 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1235 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1236 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1237 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1238 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1239 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1240 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1241 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1242 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1243 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1244 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1245 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1246 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
1247 ret |= pvr2_write_u8(hdw, 0x52, 0);
1248 ret |= pvr2_write_u16(hdw, 0x0600, 0);
1251 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1252 "firmware2 upload prep failed, ret=%d",ret);
1253 release_firmware(fw_entry);
1257 /* Now send firmware */
1259 fw_len = fw_entry->size;
1261 if (fw_len % FIRMWARE_CHUNK_SIZE) {
1262 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1263 "size of %s firmware"
1264 " must be a multiple of 8192B",
1266 release_firmware(fw_entry);
1270 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1271 if (fw_ptr == NULL){
1272 release_firmware(fw_entry);
1273 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1274 "failed to allocate memory for firmware2 upload");
1278 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1280 for (fw_done = 0 ; (fw_done < fw_len) && !ret ;
1281 fw_done += FIRMWARE_CHUNK_SIZE ) {
1283 memcpy(fw_ptr, fw_entry->data + fw_done, FIRMWARE_CHUNK_SIZE);
1284 /* Usbsnoop log shows that we must swap bytes... */
1285 for (i = 0; i < FIRMWARE_CHUNK_SIZE/4 ; i++)
1286 ((u32 *)fw_ptr)[i] = ___swab32(((u32 *)fw_ptr)[i]);
1288 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,
1289 FIRMWARE_CHUNK_SIZE,
1290 &actual_length, HZ);
1291 ret |= (actual_length != FIRMWARE_CHUNK_SIZE);
1294 trace_firmware("upload of %s : %i / %i ",
1295 fw_files[fwidx],fw_done,fw_len);
1298 release_firmware(fw_entry);
1301 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1302 "firmware2 upload transfer failure");
1308 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1309 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
1310 ret |= pvr2_write_u16(hdw, 0x0600, 0);
1313 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1314 "firmware2 upload post-proc failure");
1316 hdw->subsys_enabled_mask |= (1<<PVR2_SUBSYS_B_ENC_FIRMWARE);
1322 #define FIRMWARE_RECOVERY_BITS \
1323 ((1<<PVR2_SUBSYS_B_ENC_CFG) | \
1324 (1<<PVR2_SUBSYS_B_ENC_RUN) | \
1325 (1<<PVR2_SUBSYS_B_ENC_FIRMWARE) | \
1326 (1<<PVR2_SUBSYS_B_USBSTREAM_RUN))
1330 This single function is key to pretty much everything. The pvrusb2
1331 device can logically be viewed as a series of subsystems which can be
1332 stopped / started or unconfigured / configured. To get things streaming,
1333 one must configure everything and start everything, but there may be
1334 various reasons over time to deconfigure something or stop something.
1335 This function handles all of this activity. Everything EVERYWHERE that
1336 must affect a subsystem eventually comes here to do the work.
1338 The current state of all subsystems is represented by a single bit mask,
1339 known as subsys_enabled_mask. The bit positions are defined by the
1340 PVR2_SUBSYS_xxxx macros, with one subsystem per bit position. At any
1341 time the set of configured or active subsystems can be queried just by
1342 looking at that mask. To change bits in that mask, this function here
1343 must be called. The "msk" argument indicates which bit positions to
1344 change, and the "val" argument defines the new values for the positions
1347 There is a priority ordering of starting / stopping things, and for
1348 multiple requested changes, this function implements that ordering.
1349 (Thus we will act on a request to load encoder firmware before we
1350 configure the encoder.) In addition to priority ordering, there is a
1351 recovery strategy implemented here. If a particular step fails and we
1352 detect that failure, this function will clear the affected subsystem bits
1353 and restart. Thus we have a means for recovering from a dead encoder:
1354 Clear all bits that correspond to subsystems that we need to restart /
1355 reconfigure and start over.
1358 static void pvr2_hdw_subsys_bit_chg_no_lock(struct pvr2_hdw *hdw,
1365 unsigned int tryCount = 0;
1367 if (!hdw->flag_ok) return;
1369 msk &= PVR2_SUBSYS_ALL;
1370 nmsk = (hdw->subsys_enabled_mask & ~msk) | (val & msk);
1371 nmsk &= PVR2_SUBSYS_ALL;
1375 if (!((nmsk ^ hdw->subsys_enabled_mask) &
1376 PVR2_SUBSYS_ALL)) break;
1378 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1379 "Too many retries when configuring device;"
1381 pvr2_hdw_render_useless(hdw);
1385 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1386 "Retrying device reconfiguration");
1388 pvr2_trace(PVR2_TRACE_INIT,
1389 "subsys mask changing 0x%lx:0x%lx"
1390 " from 0x%lx to 0x%lx",
1391 msk,val,hdw->subsys_enabled_mask,nmsk);
1393 vmsk = (nmsk ^ hdw->subsys_enabled_mask) &
1394 hdw->subsys_enabled_mask;
1396 if (vmsk & (1<<PVR2_SUBSYS_B_ENC_RUN)) {
1397 pvr2_trace(PVR2_TRACE_CTL,
1398 "/*---TRACE_CTL----*/"
1399 " pvr2_encoder_stop");
1400 ret = pvr2_encoder_stop(hdw);
1402 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1403 "Error recovery initiated");
1404 hdw->subsys_enabled_mask &=
1405 ~FIRMWARE_RECOVERY_BITS;
1409 if (vmsk & (1<<PVR2_SUBSYS_B_USBSTREAM_RUN)) {
1410 pvr2_trace(PVR2_TRACE_CTL,
1411 "/*---TRACE_CTL----*/"
1412 " pvr2_hdw_cmd_usbstream(0)");
1413 pvr2_hdw_cmd_usbstream(hdw,0);
1415 if (vmsk & (1<<PVR2_SUBSYS_B_DIGITIZER_RUN)) {
1416 pvr2_trace(PVR2_TRACE_CTL,
1417 "/*---TRACE_CTL----*/"
1418 " decoder disable");
1419 if (hdw->decoder_ctrl) {
1420 hdw->decoder_ctrl->enable(
1421 hdw->decoder_ctrl->ctxt,0);
1423 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1425 " No decoder present");
1427 hdw->subsys_enabled_mask &=
1428 ~(1<<PVR2_SUBSYS_B_DIGITIZER_RUN);
1430 if (vmsk & PVR2_SUBSYS_CFG_ALL) {
1431 hdw->subsys_enabled_mask &=
1432 ~(vmsk & PVR2_SUBSYS_CFG_ALL);
1435 vmsk = (nmsk ^ hdw->subsys_enabled_mask) & nmsk;
1437 if (vmsk & (1<<PVR2_SUBSYS_B_ENC_FIRMWARE)) {
1438 pvr2_trace(PVR2_TRACE_CTL,
1439 "/*---TRACE_CTL----*/"
1440 " pvr2_upload_firmware2");
1441 ret = pvr2_upload_firmware2(hdw);
1443 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1444 "Failure uploading encoder"
1446 pvr2_hdw_render_useless(hdw);
1450 if (vmsk & (1<<PVR2_SUBSYS_B_ENC_CFG)) {
1451 pvr2_trace(PVR2_TRACE_CTL,
1452 "/*---TRACE_CTL----*/"
1453 " pvr2_encoder_configure");
1454 ret = pvr2_encoder_configure(hdw);
1456 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1457 "Error recovery initiated");
1458 hdw->subsys_enabled_mask &=
1459 ~FIRMWARE_RECOVERY_BITS;
1463 if (vmsk & (1<<PVR2_SUBSYS_B_DIGITIZER_RUN)) {
1464 pvr2_trace(PVR2_TRACE_CTL,
1465 "/*---TRACE_CTL----*/"
1467 if (hdw->decoder_ctrl) {
1468 hdw->decoder_ctrl->enable(
1469 hdw->decoder_ctrl->ctxt,!0);
1471 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1473 " No decoder present");
1475 hdw->subsys_enabled_mask |=
1476 (1<<PVR2_SUBSYS_B_DIGITIZER_RUN);
1478 if (vmsk & (1<<PVR2_SUBSYS_B_USBSTREAM_RUN)) {
1479 pvr2_trace(PVR2_TRACE_CTL,
1480 "/*---TRACE_CTL----*/"
1481 " pvr2_hdw_cmd_usbstream(1)");
1482 pvr2_hdw_cmd_usbstream(hdw,!0);
1484 if (vmsk & (1<<PVR2_SUBSYS_B_ENC_RUN)) {
1485 pvr2_trace(PVR2_TRACE_CTL,
1486 "/*---TRACE_CTL----*/"
1487 " pvr2_encoder_start");
1488 ret = pvr2_encoder_start(hdw);
1490 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1491 "Error recovery initiated");
1492 hdw->subsys_enabled_mask &=
1493 ~FIRMWARE_RECOVERY_BITS;
1502 void pvr2_hdw_subsys_bit_chg(struct pvr2_hdw *hdw,
1503 unsigned long msk,unsigned long val)
1505 LOCK_TAKE(hdw->big_lock); do {
1506 pvr2_hdw_subsys_bit_chg_no_lock(hdw,msk,val);
1507 } while (0); LOCK_GIVE(hdw->big_lock);
1511 unsigned long pvr2_hdw_subsys_get(struct pvr2_hdw *hdw)
1513 return hdw->subsys_enabled_mask;
1517 unsigned long pvr2_hdw_subsys_stream_get(struct pvr2_hdw *hdw)
1519 return hdw->subsys_stream_mask;
1523 static void pvr2_hdw_subsys_stream_bit_chg_no_lock(struct pvr2_hdw *hdw,
1528 msk &= PVR2_SUBSYS_ALL;
1529 val2 = ((hdw->subsys_stream_mask & ~msk) | (val & msk));
1530 pvr2_trace(PVR2_TRACE_INIT,
1531 "stream mask changing 0x%lx:0x%lx from 0x%lx to 0x%lx",
1532 msk,val,hdw->subsys_stream_mask,val2);
1533 hdw->subsys_stream_mask = val2;
1537 void pvr2_hdw_subsys_stream_bit_chg(struct pvr2_hdw *hdw,
1541 LOCK_TAKE(hdw->big_lock); do {
1542 pvr2_hdw_subsys_stream_bit_chg_no_lock(hdw,msk,val);
1543 } while (0); LOCK_GIVE(hdw->big_lock);
1547 static int pvr2_hdw_set_streaming_no_lock(struct pvr2_hdw *hdw,int enableFl)
1549 if ((!enableFl) == !(hdw->flag_streaming_enabled)) return 0;
1551 pvr2_trace(PVR2_TRACE_START_STOP,
1552 "/*--TRACE_STREAM--*/ enable");
1553 pvr2_hdw_subsys_bit_chg_no_lock(hdw,~0,~0);
1555 pvr2_trace(PVR2_TRACE_START_STOP,
1556 "/*--TRACE_STREAM--*/ disable");
1557 pvr2_hdw_subsys_bit_chg_no_lock(hdw,hdw->subsys_stream_mask,0);
1559 if (!hdw->flag_ok) return -EIO;
1560 hdw->flag_streaming_enabled = enableFl != 0;
1565 int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1567 return hdw->flag_streaming_enabled != 0;
1571 int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1574 LOCK_TAKE(hdw->big_lock); do {
1575 ret = pvr2_hdw_set_streaming_no_lock(hdw,enable_flag);
1576 } while (0); LOCK_GIVE(hdw->big_lock);
1581 static int pvr2_hdw_set_stream_type_no_lock(struct pvr2_hdw *hdw,
1582 enum pvr2_config config)
1584 unsigned long sm = hdw->subsys_enabled_mask;
1585 if (!hdw->flag_ok) return -EIO;
1586 pvr2_hdw_subsys_bit_chg_no_lock(hdw,hdw->subsys_stream_mask,0);
1587 hdw->config = config;
1588 pvr2_hdw_subsys_bit_chg_no_lock(hdw,~0,sm);
1593 int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1596 if (!hdw->flag_ok) return -EIO;
1597 LOCK_TAKE(hdw->big_lock);
1598 ret = pvr2_hdw_set_stream_type_no_lock(hdw,config);
1599 LOCK_GIVE(hdw->big_lock);
1604 static int get_default_tuner_type(struct pvr2_hdw *hdw)
1606 int unit_number = hdw->unit_number;
1608 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1609 tp = tuner[unit_number];
1611 if (tp < 0) return -EINVAL;
1612 hdw->tuner_type = tp;
1617 static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1619 int unit_number = hdw->unit_number;
1621 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1622 tp = video_std[unit_number];
1628 static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1630 int unit_number = hdw->unit_number;
1632 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1633 tp = tolerance[unit_number];
1639 static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1641 /* Try a harmless request to fetch the eeprom's address over
1642 endpoint 1. See what happens. Only the full FX2 image can
1643 respond to this. If this probe fails then likely the FX2
1644 firmware needs be loaded. */
1646 LOCK_TAKE(hdw->ctl_lock); do {
1647 hdw->cmd_buffer[0] = 0xeb;
1648 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1651 if (result < 0) break;
1652 } while(0); LOCK_GIVE(hdw->ctl_lock);
1654 pvr2_trace(PVR2_TRACE_INIT,
1655 "Probe of device endpoint 1 result status %d",
1658 pvr2_trace(PVR2_TRACE_INIT,
1659 "Probe of device endpoint 1 succeeded");
1664 static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1668 v4l2_std_id std1,std2;
1670 std1 = get_default_standard(hdw);
1672 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
1673 pvr2_trace(PVR2_TRACE_INIT,
1674 "Supported video standard(s) reported by eeprom: %.*s",
1677 hdw->std_mask_avail = hdw->std_mask_eeprom;
1679 std2 = std1 & ~hdw->std_mask_avail;
1681 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
1682 pvr2_trace(PVR2_TRACE_INIT,
1683 "Expanding supported video standards"
1684 " to include: %.*s",
1686 hdw->std_mask_avail |= std2;
1689 pvr2_hdw_internal_set_std_avail(hdw);
1692 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
1693 pvr2_trace(PVR2_TRACE_INIT,
1694 "Initial video standard forced to %.*s",
1696 hdw->std_mask_cur = std1;
1697 hdw->std_dirty = !0;
1698 pvr2_hdw_internal_find_stdenum(hdw);
1702 if (hdw->std_enum_cnt > 1) {
1703 // Autoselect the first listed standard
1704 hdw->std_enum_cur = 1;
1705 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1706 hdw->std_dirty = !0;
1707 pvr2_trace(PVR2_TRACE_INIT,
1708 "Initial video standard auto-selected to %s",
1709 hdw->std_defs[hdw->std_enum_cur-1].name);
1713 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1714 "Unable to select a viable initial video standard");
1718 static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1722 struct pvr2_ctrl *cptr;
1725 reloadFl = (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
1728 pvr2_trace(PVR2_TRACE_INIT,
1729 "USB endpoint config looks strange"
1730 "; possibly firmware needs to be loaded");
1734 reloadFl = !pvr2_hdw_check_firmware(hdw);
1736 pvr2_trace(PVR2_TRACE_INIT,
1737 "Check for FX2 firmware failed"
1738 "; possibly firmware needs to be loaded");
1742 if (pvr2_upload_firmware1(hdw) != 0) {
1743 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1744 "Failure uploading firmware1");
1748 hdw->fw1_state = FW1_STATE_OK;
1751 pvr2_hdw_device_reset(hdw);
1753 if (!pvr2_hdw_dev_ok(hdw)) return;
1755 for (idx = 0; idx < pvr2_client_lists[hdw->hdw_type].cnt; idx++) {
1756 request_module(pvr2_client_lists[hdw->hdw_type].lst[idx]);
1759 pvr2_hdw_cmd_powerup(hdw);
1760 if (!pvr2_hdw_dev_ok(hdw)) return;
1762 if (pvr2_upload_firmware2(hdw)){
1763 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"device unstable!!");
1764 pvr2_hdw_render_useless(hdw);
1768 // This step MUST happen after the earlier powerup step.
1769 pvr2_i2c_core_init(hdw);
1770 if (!pvr2_hdw_dev_ok(hdw)) return;
1772 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
1773 cptr = hdw->controls + idx;
1774 if (cptr->info->skip_init) continue;
1775 if (!cptr->info->set_value) continue;
1776 cptr->info->set_value(cptr,~0,cptr->info->default_value);
1779 /* Set up special default values for the television and radio
1780 frequencies here. It's not really important what these defaults
1781 are, but I set them to something usable in the Chicago area just
1782 to make driver testing a little easier. */
1784 /* US Broadcast channel 7 (175.25 MHz) */
1785 hdw->freqValTelevision = 175250000L;
1786 /* 104.3 MHz, a usable FM station for my area */
1787 hdw->freqValRadio = 104300000L;
1789 // Do not use pvr2_reset_ctl_endpoints() here. It is not
1790 // thread-safe against the normal pvr2_send_request() mechanism.
1791 // (We should make it thread safe).
1793 ret = pvr2_hdw_get_eeprom_addr(hdw);
1794 if (!pvr2_hdw_dev_ok(hdw)) return;
1796 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1797 "Unable to determine location of eeprom, skipping");
1799 hdw->eeprom_addr = ret;
1800 pvr2_eeprom_analyze(hdw);
1801 if (!pvr2_hdw_dev_ok(hdw)) return;
1804 pvr2_hdw_setup_std(hdw);
1806 if (!get_default_tuner_type(hdw)) {
1807 pvr2_trace(PVR2_TRACE_INIT,
1808 "pvr2_hdw_setup: Tuner type overridden to %d",
1812 hdw->tuner_updated = !0;
1813 pvr2_i2c_core_check_stale(hdw);
1814 hdw->tuner_updated = 0;
1816 if (!pvr2_hdw_dev_ok(hdw)) return;
1818 pvr2_hdw_commit_ctl_internal(hdw);
1819 if (!pvr2_hdw_dev_ok(hdw)) return;
1821 hdw->vid_stream = pvr2_stream_create();
1822 if (!pvr2_hdw_dev_ok(hdw)) return;
1823 pvr2_trace(PVR2_TRACE_INIT,
1824 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
1825 if (hdw->vid_stream) {
1826 idx = get_default_error_tolerance(hdw);
1828 pvr2_trace(PVR2_TRACE_INIT,
1829 "pvr2_hdw_setup: video stream %p"
1830 " setting tolerance %u",
1831 hdw->vid_stream,idx);
1833 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
1834 PVR2_VID_ENDPOINT,idx);
1837 if (!pvr2_hdw_dev_ok(hdw)) return;
1839 /* Make sure everything is up to date */
1840 pvr2_i2c_core_sync(hdw);
1842 if (!pvr2_hdw_dev_ok(hdw)) return;
1844 hdw->flag_init_ok = !0;
1848 int pvr2_hdw_setup(struct pvr2_hdw *hdw)
1850 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
1851 LOCK_TAKE(hdw->big_lock); do {
1852 pvr2_hdw_setup_low(hdw);
1853 pvr2_trace(PVR2_TRACE_INIT,
1854 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
1855 hdw,hdw->flag_ok,hdw->flag_init_ok);
1856 if (pvr2_hdw_dev_ok(hdw)) {
1857 if (pvr2_hdw_init_ok(hdw)) {
1860 "Device initialization"
1861 " completed successfully.");
1864 if (hdw->fw1_state == FW1_STATE_RELOAD) {
1867 "Device microcontroller firmware"
1868 " (re)loaded; it should now reset"
1873 PVR2_TRACE_ERROR_LEGS,
1874 "Device initialization was not successful.");
1875 if (hdw->fw1_state == FW1_STATE_MISSING) {
1877 PVR2_TRACE_ERROR_LEGS,
1878 "Giving up since device"
1879 " microcontroller firmware"
1880 " appears to be missing.");
1886 PVR2_TRACE_ERROR_LEGS,
1887 "Attempting pvrusb2 recovery by reloading"
1888 " primary firmware.");
1890 PVR2_TRACE_ERROR_LEGS,
1891 "If this works, device should disconnect"
1892 " and reconnect in a sane state.");
1893 hdw->fw1_state = FW1_STATE_UNKNOWN;
1894 pvr2_upload_firmware1(hdw);
1897 PVR2_TRACE_ERROR_LEGS,
1898 "***WARNING*** pvrusb2 device hardware"
1899 " appears to be jammed"
1900 " and I can't clear it.");
1902 PVR2_TRACE_ERROR_LEGS,
1903 "You might need to power cycle"
1904 " the pvrusb2 device"
1905 " in order to recover.");
1907 } while (0); LOCK_GIVE(hdw->big_lock);
1908 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
1909 return hdw->flag_init_ok;
1913 /* Create and return a structure for interacting with the underlying
1915 struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
1916 const struct usb_device_id *devid)
1918 unsigned int idx,cnt1,cnt2;
1919 struct pvr2_hdw *hdw;
1920 unsigned int hdw_type;
1922 struct pvr2_ctrl *cptr;
1924 struct v4l2_queryctrl qctrl;
1925 struct pvr2_ctl_info *ciptr;
1927 hdw_type = devid - pvr2_device_table;
1929 sizeof(pvr2_device_names)/sizeof(pvr2_device_names[0])) {
1930 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1931 "Bogus device type of %u reported",hdw_type);
1935 hdw = kmalloc(sizeof(*hdw),GFP_KERNEL);
1936 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
1937 hdw,pvr2_device_names[hdw_type]);
1938 if (!hdw) goto fail;
1939 memset(hdw,0,sizeof(*hdw));
1940 hdw->tuner_signal_stale = !0;
1941 cx2341x_fill_defaults(&hdw->enc_ctl_state);
1943 hdw->control_cnt = CTRLDEF_COUNT;
1944 hdw->control_cnt += MPEGDEF_COUNT;
1945 hdw->controls = kmalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
1947 if (!hdw->controls) goto fail;
1948 memset(hdw->controls,0,sizeof(struct pvr2_ctrl) * hdw->control_cnt);
1949 hdw->hdw_type = hdw_type;
1950 for (idx = 0; idx < hdw->control_cnt; idx++) {
1951 cptr = hdw->controls + idx;
1954 for (idx = 0; idx < 32; idx++) {
1955 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
1957 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
1958 cptr = hdw->controls + idx;
1959 cptr->info = control_defs+idx;
1961 /* Define and configure additional controls from cx2341x module. */
1962 hdw->mpeg_ctrl_info = kmalloc(
1963 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
1964 if (!hdw->mpeg_ctrl_info) goto fail;
1965 memset(hdw->mpeg_ctrl_info,0,
1966 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT);
1967 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
1968 cptr = hdw->controls + idx + CTRLDEF_COUNT;
1969 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
1970 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
1971 ciptr->name = mpeg_ids[idx].strid;
1972 ciptr->v4l_id = mpeg_ids[idx].id;
1973 ciptr->skip_init = !0;
1974 ciptr->get_value = ctrl_cx2341x_get;
1975 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
1976 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
1977 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
1978 qctrl.id = ciptr->v4l_id;
1979 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
1980 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
1981 ciptr->set_value = ctrl_cx2341x_set;
1983 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
1984 PVR2_CTLD_INFO_DESC_SIZE);
1985 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
1986 ciptr->default_value = qctrl.default_value;
1987 switch (qctrl.type) {
1989 case V4L2_CTRL_TYPE_INTEGER:
1990 ciptr->type = pvr2_ctl_int;
1991 ciptr->def.type_int.min_value = qctrl.minimum;
1992 ciptr->def.type_int.max_value = qctrl.maximum;
1994 case V4L2_CTRL_TYPE_BOOLEAN:
1995 ciptr->type = pvr2_ctl_bool;
1997 case V4L2_CTRL_TYPE_MENU:
1998 ciptr->type = pvr2_ctl_enum;
1999 ciptr->def.type_enum.value_names =
2000 cx2341x_ctrl_get_menu(ciptr->v4l_id);
2002 ciptr->def.type_enum.value_names[cnt1] != NULL;
2004 ciptr->def.type_enum.count = cnt1;
2010 // Initialize video standard enum dynamic control
2011 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2013 memcpy(&hdw->std_info_enum,cptr->info,
2014 sizeof(hdw->std_info_enum));
2015 cptr->info = &hdw->std_info_enum;
2018 // Initialize control data regarding video standard masks
2019 valid_std_mask = pvr2_std_get_usable();
2020 for (idx = 0; idx < 32; idx++) {
2021 if (!(valid_std_mask & (1 << idx))) continue;
2022 cnt1 = pvr2_std_id_to_str(
2023 hdw->std_mask_names[idx],
2024 sizeof(hdw->std_mask_names[idx])-1,
2026 hdw->std_mask_names[idx][cnt1] = 0;
2028 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2030 memcpy(&hdw->std_info_avail,cptr->info,
2031 sizeof(hdw->std_info_avail));
2032 cptr->info = &hdw->std_info_avail;
2033 hdw->std_info_avail.def.type_bitmask.bit_names =
2035 hdw->std_info_avail.def.type_bitmask.valid_bits =
2038 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2040 memcpy(&hdw->std_info_cur,cptr->info,
2041 sizeof(hdw->std_info_cur));
2042 cptr->info = &hdw->std_info_cur;
2043 hdw->std_info_cur.def.type_bitmask.bit_names =
2045 hdw->std_info_avail.def.type_bitmask.valid_bits =
2049 hdw->eeprom_addr = -1;
2050 hdw->unit_number = -1;
2051 hdw->v4l_minor_number_video = -1;
2052 hdw->v4l_minor_number_vbi = -1;
2053 hdw->v4l_minor_number_radio = -1;
2054 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2055 if (!hdw->ctl_write_buffer) goto fail;
2056 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2057 if (!hdw->ctl_read_buffer) goto fail;
2058 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2059 if (!hdw->ctl_write_urb) goto fail;
2060 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2061 if (!hdw->ctl_read_urb) goto fail;
2063 down(&pvr2_unit_sem); do {
2064 for (idx = 0; idx < PVR_NUM; idx++) {
2065 if (unit_pointers[idx]) continue;
2066 hdw->unit_number = idx;
2067 unit_pointers[idx] = hdw;
2070 } while (0); up(&pvr2_unit_sem);
2073 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2075 if (hdw->unit_number >= 0) {
2076 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2077 ('a' + hdw->unit_number));
2080 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2081 hdw->name[cnt1] = 0;
2083 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2084 hdw->unit_number,hdw->name);
2086 hdw->tuner_type = -1;
2088 /* Initialize the mask of subsystems that we will shut down when we
2090 hdw->subsys_stream_mask = PVR2_SUBSYS_RUN_ALL;
2091 hdw->subsys_stream_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG);
2093 pvr2_trace(PVR2_TRACE_INIT,"subsys_stream_mask: 0x%lx",
2094 hdw->subsys_stream_mask);
2096 hdw->usb_intf = intf;
2097 hdw->usb_dev = interface_to_usbdev(intf);
2099 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2100 usb_set_interface(hdw->usb_dev,ifnum,0);
2102 mutex_init(&hdw->ctl_lock_mutex);
2103 mutex_init(&hdw->big_lock_mutex);
2108 usb_free_urb(hdw->ctl_read_urb);
2109 usb_free_urb(hdw->ctl_write_urb);
2110 kfree(hdw->ctl_read_buffer);
2111 kfree(hdw->ctl_write_buffer);
2112 kfree(hdw->controls);
2113 kfree(hdw->mpeg_ctrl_info);
2120 /* Remove _all_ associations between this driver and the underlying USB
2122 static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
2124 if (hdw->flag_disconnected) return;
2125 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2126 if (hdw->ctl_read_urb) {
2127 usb_kill_urb(hdw->ctl_read_urb);
2128 usb_free_urb(hdw->ctl_read_urb);
2129 hdw->ctl_read_urb = NULL;
2131 if (hdw->ctl_write_urb) {
2132 usb_kill_urb(hdw->ctl_write_urb);
2133 usb_free_urb(hdw->ctl_write_urb);
2134 hdw->ctl_write_urb = NULL;
2136 if (hdw->ctl_read_buffer) {
2137 kfree(hdw->ctl_read_buffer);
2138 hdw->ctl_read_buffer = NULL;
2140 if (hdw->ctl_write_buffer) {
2141 kfree(hdw->ctl_write_buffer);
2142 hdw->ctl_write_buffer = NULL;
2144 pvr2_hdw_render_useless_unlocked(hdw);
2145 hdw->flag_disconnected = !0;
2146 hdw->usb_dev = NULL;
2147 hdw->usb_intf = NULL;
2151 /* Destroy hardware interaction structure */
2152 void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2154 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
2155 if (hdw->fw_buffer) {
2156 kfree(hdw->fw_buffer);
2157 hdw->fw_buffer = NULL;
2159 if (hdw->vid_stream) {
2160 pvr2_stream_destroy(hdw->vid_stream);
2161 hdw->vid_stream = NULL;
2163 if (hdw->decoder_ctrl) {
2164 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2166 pvr2_i2c_core_done(hdw);
2167 pvr2_hdw_remove_usb_stuff(hdw);
2168 down(&pvr2_unit_sem); do {
2169 if ((hdw->unit_number >= 0) &&
2170 (hdw->unit_number < PVR_NUM) &&
2171 (unit_pointers[hdw->unit_number] == hdw)) {
2172 unit_pointers[hdw->unit_number] = NULL;
2174 } while (0); up(&pvr2_unit_sem);
2175 kfree(hdw->controls);
2176 kfree(hdw->mpeg_ctrl_info);
2177 kfree(hdw->std_defs);
2178 kfree(hdw->std_enum_names);
2183 int pvr2_hdw_init_ok(struct pvr2_hdw *hdw)
2185 return hdw->flag_init_ok;
2189 int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2191 return (hdw && hdw->flag_ok);
2195 /* Called when hardware has been unplugged */
2196 void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2198 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2199 LOCK_TAKE(hdw->big_lock);
2200 LOCK_TAKE(hdw->ctl_lock);
2201 pvr2_hdw_remove_usb_stuff(hdw);
2202 LOCK_GIVE(hdw->ctl_lock);
2203 LOCK_GIVE(hdw->big_lock);
2207 // Attempt to autoselect an appropriate value for std_enum_cur given
2208 // whatever is currently in std_mask_cur
2209 static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
2212 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2213 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2214 hdw->std_enum_cur = idx;
2218 hdw->std_enum_cur = 0;
2222 // Calculate correct set of enumerated standards based on currently known
2223 // set of available standards bits.
2224 static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
2226 struct v4l2_standard *newstd;
2227 unsigned int std_cnt;
2230 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2232 if (hdw->std_defs) {
2233 kfree(hdw->std_defs);
2234 hdw->std_defs = NULL;
2236 hdw->std_enum_cnt = 0;
2237 if (hdw->std_enum_names) {
2238 kfree(hdw->std_enum_names);
2239 hdw->std_enum_names = NULL;
2244 PVR2_TRACE_ERROR_LEGS,
2245 "WARNING: Failed to identify any viable standards");
2247 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2248 hdw->std_enum_names[0] = "none";
2249 for (idx = 0; idx < std_cnt; idx++) {
2250 hdw->std_enum_names[idx+1] =
2253 // Set up the dynamic control for this standard
2254 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2255 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2256 hdw->std_defs = newstd;
2257 hdw->std_enum_cnt = std_cnt+1;
2258 hdw->std_enum_cur = 0;
2259 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2263 int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2264 struct v4l2_standard *std,
2268 if (!idx) return ret;
2269 LOCK_TAKE(hdw->big_lock); do {
2270 if (idx >= hdw->std_enum_cnt) break;
2272 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2274 } while (0); LOCK_GIVE(hdw->big_lock);
2279 /* Get the number of defined controls */
2280 unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2282 return hdw->control_cnt;
2286 /* Retrieve a control handle given its index (0..count-1) */
2287 struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2290 if (idx >= hdw->control_cnt) return NULL;
2291 return hdw->controls + idx;
2295 /* Retrieve a control handle given its index (0..count-1) */
2296 struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2297 unsigned int ctl_id)
2299 struct pvr2_ctrl *cptr;
2303 /* This could be made a lot more efficient, but for now... */
2304 for (idx = 0; idx < hdw->control_cnt; idx++) {
2305 cptr = hdw->controls + idx;
2306 i = cptr->info->internal_id;
2307 if (i && (i == ctl_id)) return cptr;
2313 /* Given a V4L ID, retrieve the control structure associated with it. */
2314 struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2316 struct pvr2_ctrl *cptr;
2320 /* This could be made a lot more efficient, but for now... */
2321 for (idx = 0; idx < hdw->control_cnt; idx++) {
2322 cptr = hdw->controls + idx;
2323 i = cptr->info->v4l_id;
2324 if (i && (i == ctl_id)) return cptr;
2330 /* Given a V4L ID for its immediate predecessor, retrieve the control
2331 structure associated with it. */
2332 struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2333 unsigned int ctl_id)
2335 struct pvr2_ctrl *cptr,*cp2;
2339 /* This could be made a lot more efficient, but for now... */
2341 for (idx = 0; idx < hdw->control_cnt; idx++) {
2342 cptr = hdw->controls + idx;
2343 i = cptr->info->v4l_id;
2345 if (i <= ctl_id) continue;
2346 if (cp2 && (cp2->info->v4l_id < i)) continue;
2354 static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2357 case pvr2_ctl_int: return "integer";
2358 case pvr2_ctl_enum: return "enum";
2359 case pvr2_ctl_bool: return "boolean";
2360 case pvr2_ctl_bitmask: return "bitmask";
2366 /* Commit all control changes made up to this point. Subsystems can be
2367 indirectly affected by these changes. For a given set of things being
2368 committed, we'll clear the affected subsystem bits and then once we're
2369 done committing everything we'll make a request to restore the subsystem
2370 state(s) back to their previous value before this function was called.
2371 Thus we can automatically reconfigure affected pieces of the driver as
2372 controls are changed. */
2373 static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw)
2375 unsigned long saved_subsys_mask = hdw->subsys_enabled_mask;
2376 unsigned long stale_subsys_mask = 0;
2378 struct pvr2_ctrl *cptr;
2380 int commit_flag = 0;
2382 unsigned int bcnt,ccnt;
2384 for (idx = 0; idx < hdw->control_cnt; idx++) {
2385 cptr = hdw->controls + idx;
2386 if (cptr->info->is_dirty == 0) continue;
2387 if (!cptr->info->is_dirty(cptr)) continue;
2390 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
2391 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
2394 cptr->info->get_value(cptr,&value);
2395 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
2397 sizeof(buf)-bcnt,&ccnt);
2399 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
2400 get_ctrl_typename(cptr->info->type));
2401 pvr2_trace(PVR2_TRACE_CTL,
2402 "/*--TRACE_COMMIT--*/ %.*s",
2407 /* Nothing has changed */
2411 /* When video standard changes, reset the hres and vres values -
2412 but if the user has pending changes there, then let the changes
2414 if (hdw->std_dirty) {
2415 /* Rewrite the vertical resolution to be appropriate to the
2416 video standard that has been selected. */
2418 if (hdw->std_mask_cur & V4L2_STD_525_60) {
2423 if (nvres != hdw->res_ver_val) {
2424 hdw->res_ver_val = nvres;
2425 hdw->res_ver_dirty = !0;
2429 if (hdw->std_dirty ||
2432 hdw->res_ver_dirty ||
2433 hdw->res_hor_dirty ||
2435 /* If any of this changes, then the encoder needs to be
2436 reconfigured, and we need to reset the stream. */
2437 stale_subsys_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG);
2440 if (hdw->input_dirty) {
2441 /* pk: If input changes to or from radio, then the encoder
2442 needs to be restarted (for ENC_MUTE_VIDEO to work) */
2443 stale_subsys_mask |= (1<<PVR2_SUBSYS_B_ENC_RUN);
2447 if (hdw->srate_dirty) {
2448 /* Write new sample rate into control structure since
2449 * the master copy is stale. We must track srate
2450 * separate from the mpeg control structure because
2451 * other logic also uses this value. */
2452 struct v4l2_ext_controls cs;
2453 struct v4l2_ext_control c1;
2454 memset(&cs,0,sizeof(cs));
2455 memset(&c1,0,sizeof(c1));
2458 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
2459 c1.value = hdw->srate_val;
2460 cx2341x_ext_ctrls(&hdw->enc_ctl_state,&cs,VIDIOC_S_EXT_CTRLS);
2463 /* Scan i2c core at this point - before we clear all the dirty
2464 bits. Various parts of the i2c core will notice dirty bits as
2465 appropriate and arrange to broadcast or directly send updates to
2466 the client drivers in order to keep everything in sync */
2467 pvr2_i2c_core_check_stale(hdw);
2469 for (idx = 0; idx < hdw->control_cnt; idx++) {
2470 cptr = hdw->controls + idx;
2471 if (!cptr->info->clear_dirty) continue;
2472 cptr->info->clear_dirty(cptr);
2475 /* Now execute i2c core update */
2476 pvr2_i2c_core_sync(hdw);
2478 pvr2_hdw_subsys_bit_chg_no_lock(hdw,stale_subsys_mask,0);
2479 pvr2_hdw_subsys_bit_chg_no_lock(hdw,~0,saved_subsys_mask);
2485 int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2487 LOCK_TAKE(hdw->big_lock); do {
2488 pvr2_hdw_commit_ctl_internal(hdw);
2489 } while (0); LOCK_GIVE(hdw->big_lock);
2494 void pvr2_hdw_poll(struct pvr2_hdw *hdw)
2496 LOCK_TAKE(hdw->big_lock); do {
2497 pvr2_i2c_core_sync(hdw);
2498 } while (0); LOCK_GIVE(hdw->big_lock);
2502 void pvr2_hdw_setup_poll_trigger(struct pvr2_hdw *hdw,
2503 void (*func)(void *),
2506 LOCK_TAKE(hdw->big_lock); do {
2507 hdw->poll_trigger_func = func;
2508 hdw->poll_trigger_data = data;
2509 } while (0); LOCK_GIVE(hdw->big_lock);
2513 void pvr2_hdw_poll_trigger_unlocked(struct pvr2_hdw *hdw)
2515 if (hdw->poll_trigger_func) {
2516 hdw->poll_trigger_func(hdw->poll_trigger_data);
2520 /* Return name for this driver instance */
2521 const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2527 int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2530 LOCK_TAKE(hdw->ctl_lock); do {
2531 hdw->cmd_buffer[0] = 0x0b;
2532 result = pvr2_send_request(hdw,
2535 if (result < 0) break;
2536 result = (hdw->cmd_buffer[0] != 0);
2537 } while(0); LOCK_GIVE(hdw->ctl_lock);
2542 /* Execute poll of tuner status */
2543 void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
2545 LOCK_TAKE(hdw->big_lock); do {
2546 pvr2_i2c_core_status_poll(hdw);
2547 } while (0); LOCK_GIVE(hdw->big_lock);
2551 /* Return information about the tuner */
2552 int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
2554 LOCK_TAKE(hdw->big_lock); do {
2555 if (hdw->tuner_signal_stale) {
2556 pvr2_i2c_core_status_poll(hdw);
2558 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
2559 } while (0); LOCK_GIVE(hdw->big_lock);
2564 /* Get handle to video output stream */
2565 struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
2567 return hp->vid_stream;
2571 void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
2573 int nr = pvr2_hdw_get_unit_number(hdw);
2574 LOCK_TAKE(hdw->big_lock); do {
2575 hdw->log_requested = !0;
2576 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
2577 pvr2_i2c_core_check_stale(hdw);
2578 hdw->log_requested = 0;
2579 pvr2_i2c_core_sync(hdw);
2580 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
2581 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
2582 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
2583 } while (0); LOCK_GIVE(hdw->big_lock);
2586 void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw, int enable_flag)
2591 LOCK_TAKE(hdw->big_lock); do {
2592 if ((hdw->fw_buffer == 0) == !enable_flag) break;
2595 pvr2_trace(PVR2_TRACE_FIRMWARE,
2596 "Cleaning up after CPU firmware fetch");
2597 kfree(hdw->fw_buffer);
2598 hdw->fw_buffer = NULL;
2600 /* Now release the CPU. It will disconnect and
2602 pvr2_hdw_cpureset_assert(hdw,0);
2606 pvr2_trace(PVR2_TRACE_FIRMWARE,
2607 "Preparing to suck out CPU firmware");
2608 hdw->fw_size = 0x2000;
2609 hdw->fw_buffer = kmalloc(hdw->fw_size,GFP_KERNEL);
2610 if (!hdw->fw_buffer) {
2615 memset(hdw->fw_buffer,0,hdw->fw_size);
2617 /* We have to hold the CPU during firmware upload. */
2618 pvr2_hdw_cpureset_assert(hdw,1);
2620 /* download the firmware from address 0000-1fff in 2048
2621 (=0x800) bytes chunk. */
2623 pvr2_trace(PVR2_TRACE_FIRMWARE,"Grabbing CPU firmware");
2624 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
2625 for(address = 0; address < hdw->fw_size; address += 0x800) {
2626 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0xc0,
2628 hdw->fw_buffer+address,0x800,HZ);
2632 pvr2_trace(PVR2_TRACE_FIRMWARE,"Done grabbing CPU firmware");
2634 } while (0); LOCK_GIVE(hdw->big_lock);
2638 /* Return true if we're in a mode for retrieval CPU firmware */
2639 int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
2641 return hdw->fw_buffer != 0;
2645 int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
2646 char *buf,unsigned int cnt)
2649 LOCK_TAKE(hdw->big_lock); do {
2653 if (!hdw->fw_buffer) {
2658 if (offs >= hdw->fw_size) {
2659 pvr2_trace(PVR2_TRACE_FIRMWARE,
2660 "Read firmware data offs=%d EOF",
2666 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
2668 memcpy(buf,hdw->fw_buffer+offs,cnt);
2670 pvr2_trace(PVR2_TRACE_FIRMWARE,
2671 "Read firmware data offs=%d cnt=%d",
2674 } while (0); LOCK_GIVE(hdw->big_lock);
2680 int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
2681 enum pvr2_v4l_type index)
2684 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
2685 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
2686 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
2692 /* Store a v4l minor device number */
2693 void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
2694 enum pvr2_v4l_type index,int v)
2697 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
2698 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
2699 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
2705 static void pvr2_ctl_write_complete(struct urb *urb)
2707 struct pvr2_hdw *hdw = urb->context;
2708 hdw->ctl_write_pend_flag = 0;
2709 if (hdw->ctl_read_pend_flag) return;
2710 complete(&hdw->ctl_done);
2714 static void pvr2_ctl_read_complete(struct urb *urb)
2716 struct pvr2_hdw *hdw = urb->context;
2717 hdw->ctl_read_pend_flag = 0;
2718 if (hdw->ctl_write_pend_flag) return;
2719 complete(&hdw->ctl_done);
2723 static void pvr2_ctl_timeout(unsigned long data)
2725 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
2726 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2727 hdw->ctl_timeout_flag = !0;
2728 if (hdw->ctl_write_pend_flag)
2729 usb_unlink_urb(hdw->ctl_write_urb);
2730 if (hdw->ctl_read_pend_flag)
2731 usb_unlink_urb(hdw->ctl_read_urb);
2736 /* Issue a command and get a response from the device. This extended
2737 version includes a probe flag (which if set means that device errors
2738 should not be logged or treated as fatal) and a timeout in jiffies.
2739 This can be used to non-lethally probe the health of endpoint 1. */
2740 static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
2741 unsigned int timeout,int probe_fl,
2742 void *write_data,unsigned int write_len,
2743 void *read_data,unsigned int read_len)
2747 struct timer_list timer;
2748 if (!hdw->ctl_lock_held) {
2749 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2750 "Attempted to execute control transfer"
2754 if ((!hdw->flag_ok) && !probe_fl) {
2755 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2756 "Attempted to execute control transfer"
2757 " when device not ok");
2760 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
2762 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2763 "Attempted to execute control transfer"
2764 " when USB is disconnected");
2769 /* Ensure that we have sane parameters */
2770 if (!write_data) write_len = 0;
2771 if (!read_data) read_len = 0;
2772 if (write_len > PVR2_CTL_BUFFSIZE) {
2774 PVR2_TRACE_ERROR_LEGS,
2775 "Attempted to execute %d byte"
2776 " control-write transfer (limit=%d)",
2777 write_len,PVR2_CTL_BUFFSIZE);
2780 if (read_len > PVR2_CTL_BUFFSIZE) {
2782 PVR2_TRACE_ERROR_LEGS,
2783 "Attempted to execute %d byte"
2784 " control-read transfer (limit=%d)",
2785 write_len,PVR2_CTL_BUFFSIZE);
2788 if ((!write_len) && (!read_len)) {
2790 PVR2_TRACE_ERROR_LEGS,
2791 "Attempted to execute null control transfer?");
2796 hdw->cmd_debug_state = 1;
2798 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
2800 hdw->cmd_debug_code = 0;
2802 hdw->cmd_debug_write_len = write_len;
2803 hdw->cmd_debug_read_len = read_len;
2805 /* Initialize common stuff */
2806 init_completion(&hdw->ctl_done);
2807 hdw->ctl_timeout_flag = 0;
2808 hdw->ctl_write_pend_flag = 0;
2809 hdw->ctl_read_pend_flag = 0;
2811 timer.expires = jiffies + timeout;
2812 timer.data = (unsigned long)hdw;
2813 timer.function = pvr2_ctl_timeout;
2816 hdw->cmd_debug_state = 2;
2817 /* Transfer write data to internal buffer */
2818 for (idx = 0; idx < write_len; idx++) {
2819 hdw->ctl_write_buffer[idx] =
2820 ((unsigned char *)write_data)[idx];
2822 /* Initiate a write request */
2823 usb_fill_bulk_urb(hdw->ctl_write_urb,
2825 usb_sndbulkpipe(hdw->usb_dev,
2826 PVR2_CTL_WRITE_ENDPOINT),
2827 hdw->ctl_write_buffer,
2829 pvr2_ctl_write_complete,
2831 hdw->ctl_write_urb->actual_length = 0;
2832 hdw->ctl_write_pend_flag = !0;
2833 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
2835 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2836 "Failed to submit write-control"
2837 " URB status=%d",status);
2838 hdw->ctl_write_pend_flag = 0;
2844 hdw->cmd_debug_state = 3;
2845 memset(hdw->ctl_read_buffer,0x43,read_len);
2846 /* Initiate a read request */
2847 usb_fill_bulk_urb(hdw->ctl_read_urb,
2849 usb_rcvbulkpipe(hdw->usb_dev,
2850 PVR2_CTL_READ_ENDPOINT),
2851 hdw->ctl_read_buffer,
2853 pvr2_ctl_read_complete,
2855 hdw->ctl_read_urb->actual_length = 0;
2856 hdw->ctl_read_pend_flag = !0;
2857 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
2859 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2860 "Failed to submit read-control"
2861 " URB status=%d",status);
2862 hdw->ctl_read_pend_flag = 0;
2870 /* Now wait for all I/O to complete */
2871 hdw->cmd_debug_state = 4;
2872 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2873 wait_for_completion(&hdw->ctl_done);
2875 hdw->cmd_debug_state = 5;
2878 del_timer_sync(&timer);
2880 hdw->cmd_debug_state = 6;
2883 if (hdw->ctl_timeout_flag) {
2884 status = -ETIMEDOUT;
2886 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2887 "Timed out control-write");
2893 /* Validate results of write request */
2894 if ((hdw->ctl_write_urb->status != 0) &&
2895 (hdw->ctl_write_urb->status != -ENOENT) &&
2896 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
2897 (hdw->ctl_write_urb->status != -ECONNRESET)) {
2898 /* USB subsystem is reporting some kind of failure
2900 status = hdw->ctl_write_urb->status;
2902 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2903 "control-write URB failure,"
2909 if (hdw->ctl_write_urb->actual_length < write_len) {
2910 /* Failed to write enough data */
2913 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2914 "control-write URB short,"
2915 " expected=%d got=%d",
2917 hdw->ctl_write_urb->actual_length);
2923 /* Validate results of read request */
2924 if ((hdw->ctl_read_urb->status != 0) &&
2925 (hdw->ctl_read_urb->status != -ENOENT) &&
2926 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
2927 (hdw->ctl_read_urb->status != -ECONNRESET)) {
2928 /* USB subsystem is reporting some kind of failure
2930 status = hdw->ctl_read_urb->status;
2932 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2933 "control-read URB failure,"
2939 if (hdw->ctl_read_urb->actual_length < read_len) {
2940 /* Failed to read enough data */
2943 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2944 "control-read URB short,"
2945 " expected=%d got=%d",
2947 hdw->ctl_read_urb->actual_length);
2951 /* Transfer retrieved data out from internal buffer */
2952 for (idx = 0; idx < read_len; idx++) {
2953 ((unsigned char *)read_data)[idx] =
2954 hdw->ctl_read_buffer[idx];
2960 hdw->cmd_debug_state = 0;
2961 if ((status < 0) && (!probe_fl)) {
2962 pvr2_hdw_render_useless_unlocked(hdw);
2968 int pvr2_send_request(struct pvr2_hdw *hdw,
2969 void *write_data,unsigned int write_len,
2970 void *read_data,unsigned int read_len)
2972 return pvr2_send_request_ex(hdw,HZ*4,0,
2973 write_data,write_len,
2974 read_data,read_len);
2977 int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
2981 LOCK_TAKE(hdw->ctl_lock);
2983 hdw->cmd_buffer[0] = 0x04; /* write register prefix */
2984 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
2985 hdw->cmd_buffer[5] = 0;
2986 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
2987 hdw->cmd_buffer[7] = reg & 0xff;
2990 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
2992 LOCK_GIVE(hdw->ctl_lock);
2998 static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
3002 LOCK_TAKE(hdw->ctl_lock);
3004 hdw->cmd_buffer[0] = 0x05; /* read register prefix */
3005 hdw->cmd_buffer[1] = 0;
3006 hdw->cmd_buffer[2] = 0;
3007 hdw->cmd_buffer[3] = 0;
3008 hdw->cmd_buffer[4] = 0;
3009 hdw->cmd_buffer[5] = 0;
3010 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3011 hdw->cmd_buffer[7] = reg & 0xff;
3013 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3014 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3016 LOCK_GIVE(hdw->ctl_lock);
3022 static int pvr2_write_u16(struct pvr2_hdw *hdw, u16 data, int res)
3026 LOCK_TAKE(hdw->ctl_lock);
3028 hdw->cmd_buffer[0] = (data >> 8) & 0xff;
3029 hdw->cmd_buffer[1] = data & 0xff;
3031 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 2, hdw->cmd_buffer, res);
3033 LOCK_GIVE(hdw->ctl_lock);
3039 static int pvr2_write_u8(struct pvr2_hdw *hdw, u8 data, int res)
3043 LOCK_TAKE(hdw->ctl_lock);
3045 hdw->cmd_buffer[0] = data;
3047 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 1, hdw->cmd_buffer, res);
3049 LOCK_GIVE(hdw->ctl_lock);
3055 static void pvr2_hdw_render_useless_unlocked(struct pvr2_hdw *hdw)
3057 if (!hdw->flag_ok) return;
3058 pvr2_trace(PVR2_TRACE_INIT,"render_useless");
3060 if (hdw->vid_stream) {
3061 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
3063 hdw->flag_streaming_enabled = 0;
3064 hdw->subsys_enabled_mask = 0;
3068 void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
3070 LOCK_TAKE(hdw->ctl_lock);
3071 pvr2_hdw_render_useless_unlocked(hdw);
3072 LOCK_GIVE(hdw->ctl_lock);
3076 void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3079 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
3080 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
3082 ret = usb_reset_device(hdw->usb_dev);
3083 usb_unlock_device(hdw->usb_dev);
3085 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3086 "Failed to lock USB device ret=%d",ret);
3088 if (init_pause_msec) {
3089 pvr2_trace(PVR2_TRACE_INFO,
3090 "Waiting %u msec for hardware to settle",
3092 msleep(init_pause_msec);
3098 void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3104 if (!hdw->usb_dev) return;
3106 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3108 da[0] = val ? 0x01 : 0x00;
3110 /* Write the CPUCS register on the 8051. The lsb of the register
3111 is the reset bit; a 1 asserts reset while a 0 clears it. */
3112 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3113 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3115 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3116 "cpureset_assert(%d) error=%d",val,ret);
3117 pvr2_hdw_render_useless(hdw);
3122 int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3125 LOCK_TAKE(hdw->ctl_lock); do {
3126 pvr2_trace(PVR2_TRACE_INIT,"Requesting uproc hard reset");
3128 hdw->cmd_buffer[0] = 0xdd;
3129 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
3130 } while (0); LOCK_GIVE(hdw->ctl_lock);
3135 int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
3138 LOCK_TAKE(hdw->ctl_lock); do {
3139 pvr2_trace(PVR2_TRACE_INIT,"Requesting powerup");
3140 hdw->cmd_buffer[0] = 0xde;
3141 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
3142 } while (0); LOCK_GIVE(hdw->ctl_lock);
3147 int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
3149 if (!hdw->decoder_ctrl) {
3150 pvr2_trace(PVR2_TRACE_INIT,
3151 "Unable to reset decoder: nothing attached");
3155 if (!hdw->decoder_ctrl->force_reset) {
3156 pvr2_trace(PVR2_TRACE_INIT,
3157 "Unable to reset decoder: not implemented");
3161 pvr2_trace(PVR2_TRACE_INIT,
3162 "Requesting decoder reset");
3163 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
3168 /* Stop / start video stream transport */
3169 static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
3172 LOCK_TAKE(hdw->ctl_lock); do {
3173 hdw->cmd_buffer[0] = (runFl ? 0x36 : 0x37);
3174 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
3175 } while (0); LOCK_GIVE(hdw->ctl_lock);
3177 hdw->subsys_enabled_mask =
3178 ((hdw->subsys_enabled_mask &
3179 ~(1<<PVR2_SUBSYS_B_USBSTREAM_RUN)) |
3180 (runFl ? (1<<PVR2_SUBSYS_B_USBSTREAM_RUN) : 0));
3186 void pvr2_hdw_get_debug_info(const struct pvr2_hdw *hdw,
3187 struct pvr2_hdw_debug_info *ptr)
3189 ptr->big_lock_held = hdw->big_lock_held;
3190 ptr->ctl_lock_held = hdw->ctl_lock_held;
3191 ptr->flag_ok = hdw->flag_ok;
3192 ptr->flag_disconnected = hdw->flag_disconnected;
3193 ptr->flag_init_ok = hdw->flag_init_ok;
3194 ptr->flag_streaming_enabled = hdw->flag_streaming_enabled;
3195 ptr->subsys_flags = hdw->subsys_enabled_mask;
3196 ptr->cmd_debug_state = hdw->cmd_debug_state;
3197 ptr->cmd_code = hdw->cmd_debug_code;
3198 ptr->cmd_debug_write_len = hdw->cmd_debug_write_len;
3199 ptr->cmd_debug_read_len = hdw->cmd_debug_read_len;
3200 ptr->cmd_debug_timeout = hdw->ctl_timeout_flag;
3201 ptr->cmd_debug_write_pend = hdw->ctl_write_pend_flag;
3202 ptr->cmd_debug_read_pend = hdw->ctl_read_pend_flag;
3203 ptr->cmd_debug_rstatus = hdw->ctl_read_urb->status;
3204 ptr->cmd_debug_wstatus = hdw->ctl_read_urb->status;
3208 int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
3210 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
3214 int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
3216 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
3220 int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
3222 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
3226 int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
3231 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
3232 if (ret) return ret;
3233 nval = (cval & ~msk) | (val & msk);
3234 pvr2_trace(PVR2_TRACE_GPIO,
3235 "GPIO direction changing 0x%x:0x%x"
3236 " from 0x%x to 0x%x",
3240 pvr2_trace(PVR2_TRACE_GPIO,
3241 "GPIO direction changing to 0x%x",nval);
3243 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
3247 int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
3252 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
3253 if (ret) return ret;
3254 nval = (cval & ~msk) | (val & msk);
3255 pvr2_trace(PVR2_TRACE_GPIO,
3256 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
3260 pvr2_trace(PVR2_TRACE_GPIO,
3261 "GPIO output changing to 0x%x",nval);
3263 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
3267 /* Find I2C address of eeprom */
3268 static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
3271 LOCK_TAKE(hdw->ctl_lock); do {
3272 hdw->cmd_buffer[0] = 0xeb;
3273 result = pvr2_send_request(hdw,
3276 if (result < 0) break;
3277 result = hdw->cmd_buffer[0];
3278 } while(0); LOCK_GIVE(hdw->ctl_lock);
3283 int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
3284 u32 chip_id,unsigned long reg_id,
3285 int setFl,u32 *val_ptr)
3287 #ifdef CONFIG_VIDEO_ADV_DEBUG
3288 struct list_head *item;
3289 struct pvr2_i2c_client *cp;
3290 struct v4l2_register req;
3294 req.i2c_id = chip_id;
3296 if (setFl) req.val = *val_ptr;
3297 mutex_lock(&hdw->i2c_list_lock); do {
3298 list_for_each(item,&hdw->i2c_clients) {
3299 cp = list_entry(item,struct pvr2_i2c_client,list);
3300 if (cp->client->driver->id != chip_id) continue;
3301 stat = pvr2_i2c_client_cmd(
3302 cp,(setFl ? VIDIOC_INT_S_REGISTER :
3303 VIDIOC_INT_G_REGISTER),&req);
3304 if (!setFl) *val_ptr = req.val;
3308 } while (0); mutex_unlock(&hdw->i2c_list_lock);
3320 Stuff for Emacs to see, in order to encourage consistent editing style:
3321 *** Local Variables: ***
3323 *** fill-column: 75 ***
3324 *** tab-width: 8 ***
3325 *** c-basic-offset: 8 ***