4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
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/device.h> // for linux/firmware.h
23 #include <linux/firmware.h>
24 #include "pvrusb2-util.h"
25 #include "pvrusb2-encoder.h"
26 #include "pvrusb2-hdw-internal.h"
27 #include "pvrusb2-debug.h"
28 #include "pvrusb2-fx2-cmd.h"
32 /* Firmware mailbox flags - definitions found from ivtv */
33 #define IVTV_MBOX_FIRMWARE_DONE 0x00000004
34 #define IVTV_MBOX_DRIVER_DONE 0x00000002
35 #define IVTV_MBOX_DRIVER_BUSY 0x00000001
37 #define MBOX_BASE 0x44
40 static int pvr2_encoder_write_words(struct pvr2_hdw
*hdw
,
42 const u32
*data
, unsigned int dlen
)
44 unsigned int idx
,addr
;
47 unsigned int chunkCnt
;
51 Format: First byte must be 0x01. Remaining 32 bit words are
52 spread out into chunks of 7 bytes each, with the first 4 bytes
53 being the data word (little endian), and the next 3 bytes
54 being the address where that data word is to be written (big
55 endian). Repeat request for additional words, with offset
61 if (chunkCnt
> dlen
) chunkCnt
= dlen
;
62 memset(hdw
->cmd_buffer
,0,sizeof(hdw
->cmd_buffer
));
64 hdw
->cmd_buffer
[bAddr
++] = FX2CMD_MEM_WRITE_DWORD
;
65 for (idx
= 0; idx
< chunkCnt
; idx
++) {
67 hdw
->cmd_buffer
[bAddr
+6] = (addr
& 0xffu
);
68 hdw
->cmd_buffer
[bAddr
+5] = ((addr
>>8) & 0xffu
);
69 hdw
->cmd_buffer
[bAddr
+4] = ((addr
>>16) & 0xffu
);
70 PVR2_DECOMPOSE_LE(hdw
->cmd_buffer
, bAddr
,data
[idx
]);
73 ret
= pvr2_send_request(hdw
,
74 hdw
->cmd_buffer
,1+(chunkCnt
*7),
86 static int pvr2_encoder_read_words(struct pvr2_hdw
*hdw
,
88 u32
*data
, unsigned int dlen
)
92 unsigned int chunkCnt
;
96 Format: First byte must be 0x02 (status check) or 0x28 (read
97 back block of 32 bit words). Next 6 bytes must be zero,
98 followed by a single byte of MBOX_BASE+offset for portion to
99 be read. Returned data is packed set of 32 bits words that
106 if (chunkCnt
> dlen
) chunkCnt
= dlen
;
107 if (chunkCnt
< 16) chunkCnt
= 1;
110 FX2CMD_MEM_READ_DWORD
: FX2CMD_MEM_READ_64BYTES
);
111 hdw
->cmd_buffer
[1] = 0;
112 hdw
->cmd_buffer
[2] = 0;
113 hdw
->cmd_buffer
[3] = 0;
114 hdw
->cmd_buffer
[4] = 0;
115 hdw
->cmd_buffer
[5] = ((offs
>>16) & 0xffu
);
116 hdw
->cmd_buffer
[6] = ((offs
>>8) & 0xffu
);
117 hdw
->cmd_buffer
[7] = (offs
& 0xffu
);
118 ret
= pvr2_send_request(hdw
,
121 (chunkCnt
== 1 ? 4 : 16 * 4));
124 for (idx
= 0; idx
< chunkCnt
; idx
++) {
125 data
[idx
] = PVR2_COMPOSE_LE(hdw
->cmd_buffer
,idx
*4);
136 /* This prototype is set up to be compatible with the
137 cx2341x_mbox_func prototype in cx2341x.h, which should be in
138 kernels 2.6.18 or later. We do this so that we can enable
139 cx2341x.ko to write to our encoder (by handing it a pointer to this
140 function). For earlier kernels this doesn't really matter. */
141 static int pvr2_encoder_cmd(void *ctxt
,
147 unsigned int poll_count
;
148 unsigned int try_count
= 0;
152 /* These sizes look to be limited by the FX2 firmware implementation */
155 struct pvr2_hdw
*hdw
= (struct pvr2_hdw
*)ctxt
;
160 The encoder seems to speak entirely using blocks 32 bit words.
161 In ivtv driver terms, this is a mailbox at MBOX_BASE which we
162 populate with data and watch what the hardware does with it.
163 The first word is a set of flags used to control the
164 transaction, the second word is the command to execute, the
165 third byte is zero (ivtv driver suggests that this is some
166 kind of return value), and the fourth byte is a specified
167 timeout (windows driver always uses 0x00060000 except for one
168 case when it is zero). All successive words are the argument
169 words for the command.
171 First, write out the entire set of words, with the first word
174 Next, write out just the first word again, but set it to
175 IVTV_MBOX_DRIVER_DONE | IVTV_DRIVER_BUSY this time (which
176 probably means "go").
178 Next, read back the return count words. Check the first word,
179 which should have IVTV_MBOX_FIRMWARE_DONE set. If however
180 that bit is not set, then the command isn't done so repeat the
181 read until it is set.
183 Finally, write out just the first word again, but set it to
184 0x0 this time (which probably means "idle").
188 if (arg_cnt_send
> (ARRAY_SIZE(wrData
) - 4)) {
190 PVR2_TRACE_ERROR_LEGS
,
191 "Failed to write cx23416 command"
192 " - too many input arguments"
193 " (was given %u limit %lu)",
194 arg_cnt_send
, (long unsigned) ARRAY_SIZE(wrData
) - 4);
198 if (arg_cnt_recv
> (ARRAY_SIZE(rdData
) - 4)) {
200 PVR2_TRACE_ERROR_LEGS
,
201 "Failed to write cx23416 command"
202 " - too many return arguments"
203 " (was given %u limit %lu)",
204 arg_cnt_recv
, (long unsigned) ARRAY_SIZE(rdData
) - 4);
209 LOCK_TAKE(hdw
->ctl_lock
); do {
211 if (!hdw
->state_encoder_ok
) {
222 wrData
[3] = 0x00060000;
223 for (idx
= 0; idx
< arg_cnt_send
; idx
++) {
224 wrData
[idx
+4] = argp
[idx
];
226 for (; idx
< ARRAY_SIZE(wrData
) - 4; idx
++) {
230 ret
= pvr2_encoder_write_words(hdw
,MBOX_BASE
,wrData
,idx
);
232 wrData
[0] = IVTV_MBOX_DRIVER_DONE
|IVTV_MBOX_DRIVER_BUSY
;
233 ret
= pvr2_encoder_write_words(hdw
,MBOX_BASE
,wrData
,1);
238 ret
= pvr2_encoder_read_words(hdw
,MBOX_BASE
,rdData
,
243 if (rdData
[0] & IVTV_MBOX_FIRMWARE_DONE
) {
246 if (rdData
[0] && (poll_count
< 1000)) continue;
250 PVR2_TRACE_ERROR_LEGS
,
251 "Encoder timed out waiting for us"
252 "; arranging to retry");
255 PVR2_TRACE_ERROR_LEGS
,
256 "***WARNING*** device's encoder"
257 " appears to be stuck"
258 " (status=0x%08x)",rdData
[0]);
261 PVR2_TRACE_ERROR_LEGS
,
262 "Encoder command: 0x%02x",cmd
);
263 for (idx
= 4; idx
< arg_cnt_send
; idx
++) {
265 PVR2_TRACE_ERROR_LEGS
,
266 "Encoder arg%d: 0x%08x",
273 if (try_count
< 20) continue;
275 PVR2_TRACE_ERROR_LEGS
,
276 "Too many retries...");
280 del_timer_sync(&hdw
->encoder_run_timer
);
281 hdw
->state_encoder_ok
= 0;
282 pvr2_trace(PVR2_TRACE_STBITS
,
283 "State bit %s <-- %s",
285 (hdw
->state_encoder_ok
? "true" : "false"));
286 if (hdw
->state_encoder_runok
) {
287 hdw
->state_encoder_runok
= 0;
288 pvr2_trace(PVR2_TRACE_STBITS
,
289 "State bit %s <-- %s",
290 "state_encoder_runok",
291 (hdw
->state_encoder_runok
?
295 PVR2_TRACE_ERROR_LEGS
,
296 "Giving up on command."
297 " This is normally recovered via a firmware"
298 " reload and re-initialization; concern"
299 " is only warranted if this happens repeatedly"
304 for (idx
= 0; idx
< arg_cnt_recv
; idx
++) {
305 argp
[idx
] = rdData
[idx
+4];
309 ret
= pvr2_encoder_write_words(hdw
,MBOX_BASE
,wrData
,1);
312 } while(0); LOCK_GIVE(hdw
->ctl_lock
);
318 static int pvr2_encoder_vcmd(struct pvr2_hdw
*hdw
, int cmd
,
325 if (args
> ARRAY_SIZE(data
)) {
327 PVR2_TRACE_ERROR_LEGS
,
328 "Failed to write cx23416 command"
329 " - too many arguments"
330 " (was given %u limit %lu)",
331 args
, (long unsigned) ARRAY_SIZE(data
));
336 for (idx
= 0; idx
< args
; idx
++) {
337 data
[idx
] = va_arg(vl
, u32
);
341 return pvr2_encoder_cmd(hdw
,cmd
,args
,0,data
);
345 /* This implements some extra setup for the encoder that seems to be
346 specific to the PVR USB2 hardware. */
347 static int pvr2_encoder_prep_config(struct pvr2_hdw
*hdw
)
353 /* Mike Isely <isely@pobox.com> 26-Jan-2006 The windows driver
354 sends the following list of ENC_MISC commands (for both
355 24xxx and 29xxx devices). Meanings are not entirely clear,
356 however without the ENC_MISC(3,1) command then we risk
357 random perpetual video corruption whenever the video input
358 breaks up for a moment (like when switching channels). */
362 /* This ENC_MISC(3,encMisc3Arg) command is critical - without
363 it there will eventually be video corruption. Also, the
364 saa7115 case is strange - the Windows driver is passing 1
365 regardless of device type but if we have 1 for saa7115
366 devices the video turns sluggish. */
367 if (hdw
->hdw_desc
->flag_has_cx25840
) {
372 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4, 3,
375 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4, 8,0,0,0);
378 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4, 0,3,0,0);
379 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4,15,0,0,0);
381 /* prevent the PTSs from slowly drifting away in the generated
383 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
, 2, 4, 1);
388 int pvr2_encoder_adjust(struct pvr2_hdw
*hdw
)
391 ret
= cx2341x_update(hdw
,pvr2_encoder_cmd
,
392 (hdw
->enc_cur_valid
? &hdw
->enc_cur_state
: NULL
),
393 &hdw
->enc_ctl_state
);
395 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
396 "Error from cx2341x module code=%d",ret
);
398 memcpy(&hdw
->enc_cur_state
,&hdw
->enc_ctl_state
,
399 sizeof(struct cx2341x_mpeg_params
));
400 hdw
->enc_cur_valid
= !0;
406 int pvr2_encoder_configure(struct pvr2_hdw
*hdw
)
410 pvr2_trace(PVR2_TRACE_ENCODER
,"pvr2_encoder_configure"
411 " (cx2341x module)");
412 hdw
->enc_ctl_state
.port
= CX2341X_PORT_STREAMING
;
413 hdw
->enc_ctl_state
.width
= hdw
->res_hor_val
;
414 hdw
->enc_ctl_state
.height
= hdw
->res_ver_val
;
415 hdw
->enc_ctl_state
.is_50hz
= ((hdw
->std_mask_cur
& V4L2_STD_525_60
) ?
420 ret
|= pvr2_encoder_prep_config(hdw
);
424 if (hdw
->hdw_desc
->flag_has_cx25840
) {
425 /* ivtv cx25840: 0x140 */
429 if (!ret
) ret
= pvr2_encoder_vcmd(
430 hdw
,CX2341X_ENC_SET_NUM_VSYNC_LINES
, 2,
433 /* setup firmware to notify us about some events (don't know why...) */
434 if (!ret
) ret
= pvr2_encoder_vcmd(
435 hdw
,CX2341X_ENC_SET_EVENT_NOTIFICATION
, 4,
436 0, 0, 0x10000000, 0xffffffff);
438 if (!ret
) ret
= pvr2_encoder_vcmd(
439 hdw
,CX2341X_ENC_SET_VBI_LINE
, 5,
443 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
444 "Failed to configure cx23416");
448 ret
= pvr2_encoder_adjust(hdw
);
451 ret
= pvr2_encoder_vcmd(
452 hdw
, CX2341X_ENC_INITIALIZE_INPUT
, 0);
455 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
456 "Failed to initialize cx23416 video input");
464 int pvr2_encoder_start(struct pvr2_hdw
*hdw
)
468 /* unmask some interrupts */
469 pvr2_write_register(hdw
, 0x0048, 0xbfffffff);
471 pvr2_encoder_vcmd(hdw
,CX2341X_ENC_MUTE_VIDEO
,1,
472 hdw
->input_val
== PVR2_CVAL_INPUT_RADIO
? 1 : 0);
474 switch (hdw
->active_stream_type
) {
475 case pvr2_config_vbi
:
476 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_START_CAPTURE
,2,
479 case pvr2_config_mpeg
:
480 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_START_CAPTURE
,2,
483 default: /* Unhandled cases for now */
484 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_START_CAPTURE
,2,
491 int pvr2_encoder_stop(struct pvr2_hdw
*hdw
)
495 /* mask all interrupts */
496 pvr2_write_register(hdw
, 0x0048, 0xffffffff);
498 switch (hdw
->active_stream_type
) {
499 case pvr2_config_vbi
:
500 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_STOP_CAPTURE
,3,
503 case pvr2_config_mpeg
:
504 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_STOP_CAPTURE
,3,
507 default: /* Unhandled cases for now */
508 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_STOP_CAPTURE
,3,
518 Stuff for Emacs to see, in order to encourage consistent editing style:
519 *** Local Variables: ***
521 *** fill-column: 70 ***
523 *** c-basic-offset: 8 ***