1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
8 #include <linux/device.h> // for linux/firmware.h
9 #include <linux/firmware.h>
10 #include "pvrusb2-util.h"
11 #include "pvrusb2-encoder.h"
12 #include "pvrusb2-hdw-internal.h"
13 #include "pvrusb2-debug.h"
14 #include "pvrusb2-fx2-cmd.h"
18 /* Firmware mailbox flags - definitions found from ivtv */
19 #define IVTV_MBOX_FIRMWARE_DONE 0x00000004
20 #define IVTV_MBOX_DRIVER_DONE 0x00000002
21 #define IVTV_MBOX_DRIVER_BUSY 0x00000001
23 #define MBOX_BASE 0x44
26 static int pvr2_encoder_write_words(struct pvr2_hdw
*hdw
,
28 const u32
*data
, unsigned int dlen
)
30 unsigned int idx
,addr
;
33 unsigned int chunkCnt
;
37 Format: First byte must be 0x01. Remaining 32 bit words are
38 spread out into chunks of 7 bytes each, with the first 4 bytes
39 being the data word (little endian), and the next 3 bytes
40 being the address where that data word is to be written (big
41 endian). Repeat request for additional words, with offset
47 if (chunkCnt
> dlen
) chunkCnt
= dlen
;
48 memset(hdw
->cmd_buffer
,0,sizeof(hdw
->cmd_buffer
));
50 hdw
->cmd_buffer
[bAddr
++] = FX2CMD_MEM_WRITE_DWORD
;
51 for (idx
= 0; idx
< chunkCnt
; idx
++) {
53 hdw
->cmd_buffer
[bAddr
+6] = (addr
& 0xffu
);
54 hdw
->cmd_buffer
[bAddr
+5] = ((addr
>>8) & 0xffu
);
55 hdw
->cmd_buffer
[bAddr
+4] = ((addr
>>16) & 0xffu
);
56 PVR2_DECOMPOSE_LE(hdw
->cmd_buffer
, bAddr
,data
[idx
]);
59 ret
= pvr2_send_request(hdw
,
60 hdw
->cmd_buffer
,1+(chunkCnt
*7),
72 static int pvr2_encoder_read_words(struct pvr2_hdw
*hdw
,
74 u32
*data
, unsigned int dlen
)
78 unsigned int chunkCnt
;
82 Format: First byte must be 0x02 (status check) or 0x28 (read
83 back block of 32 bit words). Next 6 bytes must be zero,
84 followed by a single byte of MBOX_BASE+offset for portion to
85 be read. Returned data is packed set of 32 bits words that
92 if (chunkCnt
> dlen
) chunkCnt
= dlen
;
93 if (chunkCnt
< 16) chunkCnt
= 1;
96 FX2CMD_MEM_READ_DWORD
: FX2CMD_MEM_READ_64BYTES
);
97 hdw
->cmd_buffer
[1] = 0;
98 hdw
->cmd_buffer
[2] = 0;
99 hdw
->cmd_buffer
[3] = 0;
100 hdw
->cmd_buffer
[4] = 0;
101 hdw
->cmd_buffer
[5] = ((offs
>>16) & 0xffu
);
102 hdw
->cmd_buffer
[6] = ((offs
>>8) & 0xffu
);
103 hdw
->cmd_buffer
[7] = (offs
& 0xffu
);
104 ret
= pvr2_send_request(hdw
,
107 (chunkCnt
== 1 ? 4 : 16 * 4));
110 for (idx
= 0; idx
< chunkCnt
; idx
++) {
111 data
[idx
] = PVR2_COMPOSE_LE(hdw
->cmd_buffer
,idx
*4);
122 /* This prototype is set up to be compatible with the
123 cx2341x_mbox_func prototype in cx2341x.h, which should be in
124 kernels 2.6.18 or later. We do this so that we can enable
125 cx2341x.ko to write to our encoder (by handing it a pointer to this
126 function). For earlier kernels this doesn't really matter. */
127 static int pvr2_encoder_cmd(void *ctxt
,
133 unsigned int poll_count
;
134 unsigned int try_count
= 0;
138 /* These sizes look to be limited by the FX2 firmware implementation */
141 struct pvr2_hdw
*hdw
= (struct pvr2_hdw
*)ctxt
;
146 The encoder seems to speak entirely using blocks 32 bit words.
147 In ivtv driver terms, this is a mailbox at MBOX_BASE which we
148 populate with data and watch what the hardware does with it.
149 The first word is a set of flags used to control the
150 transaction, the second word is the command to execute, the
151 third byte is zero (ivtv driver suggests that this is some
152 kind of return value), and the fourth byte is a specified
153 timeout (windows driver always uses 0x00060000 except for one
154 case when it is zero). All successive words are the argument
155 words for the command.
157 First, write out the entire set of words, with the first word
160 Next, write out just the first word again, but set it to
161 IVTV_MBOX_DRIVER_DONE | IVTV_DRIVER_BUSY this time (which
162 probably means "go").
164 Next, read back the return count words. Check the first word,
165 which should have IVTV_MBOX_FIRMWARE_DONE set. If however
166 that bit is not set, then the command isn't done so repeat the
167 read until it is set.
169 Finally, write out just the first word again, but set it to
170 0x0 this time (which probably means "idle").
174 if (arg_cnt_send
> (ARRAY_SIZE(wrData
) - 4)) {
176 PVR2_TRACE_ERROR_LEGS
,
177 "Failed to write cx23416 command - too many input arguments (was given %u limit %lu)",
178 arg_cnt_send
, (long unsigned) ARRAY_SIZE(wrData
) - 4);
182 if (arg_cnt_recv
> (ARRAY_SIZE(rdData
) - 4)) {
184 PVR2_TRACE_ERROR_LEGS
,
185 "Failed to write cx23416 command - too many return arguments (was given %u limit %lu)",
186 arg_cnt_recv
, (long unsigned) ARRAY_SIZE(rdData
) - 4);
191 LOCK_TAKE(hdw
->ctl_lock
); while (1) {
193 if (!hdw
->state_encoder_ok
) {
204 wrData
[3] = 0x00060000;
205 for (idx
= 0; idx
< arg_cnt_send
; idx
++) {
206 wrData
[idx
+4] = argp
[idx
];
208 for (; idx
< ARRAY_SIZE(wrData
) - 4; idx
++) {
212 ret
= pvr2_encoder_write_words(hdw
,MBOX_BASE
,wrData
,idx
);
214 wrData
[0] = IVTV_MBOX_DRIVER_DONE
|IVTV_MBOX_DRIVER_BUSY
;
215 ret
= pvr2_encoder_write_words(hdw
,MBOX_BASE
,wrData
,1);
220 ret
= pvr2_encoder_read_words(hdw
,MBOX_BASE
,rdData
,
225 if (rdData
[0] & IVTV_MBOX_FIRMWARE_DONE
) {
228 if (rdData
[0] && (poll_count
< 1000)) continue;
232 PVR2_TRACE_ERROR_LEGS
,
233 "Encoder timed out waiting for us; arranging to retry");
236 PVR2_TRACE_ERROR_LEGS
,
237 "***WARNING*** device's encoder appears to be stuck (status=0x%08x)",
241 PVR2_TRACE_ERROR_LEGS
,
242 "Encoder command: 0x%02x",cmd
);
243 for (idx
= 4; idx
< arg_cnt_send
; idx
++) {
245 PVR2_TRACE_ERROR_LEGS
,
246 "Encoder arg%d: 0x%08x",
253 if (try_count
< 20) continue;
255 PVR2_TRACE_ERROR_LEGS
,
256 "Too many retries...");
260 del_timer_sync(&hdw
->encoder_run_timer
);
261 hdw
->state_encoder_ok
= 0;
262 pvr2_trace(PVR2_TRACE_STBITS
,
263 "State bit %s <-- %s",
265 (hdw
->state_encoder_ok
? "true" : "false"));
266 if (hdw
->state_encoder_runok
) {
267 hdw
->state_encoder_runok
= 0;
268 pvr2_trace(PVR2_TRACE_STBITS
,
269 "State bit %s <-- %s",
270 "state_encoder_runok",
271 (hdw
->state_encoder_runok
?
275 PVR2_TRACE_ERROR_LEGS
,
276 "Giving up on command. This is normally recovered via a firmware reload and re-initialization; concern is only warranted if this happens repeatedly and rapidly.");
280 for (idx
= 0; idx
< arg_cnt_recv
; idx
++) {
281 argp
[idx
] = rdData
[idx
+4];
285 ret
= pvr2_encoder_write_words(hdw
,MBOX_BASE
,wrData
,1);
288 LOCK_GIVE(hdw
->ctl_lock
);
294 static int pvr2_encoder_vcmd(struct pvr2_hdw
*hdw
, int cmd
,
301 if (args
> ARRAY_SIZE(data
)) {
303 PVR2_TRACE_ERROR_LEGS
,
304 "Failed to write cx23416 command - too many arguments (was given %u limit %lu)",
305 args
, (long unsigned) ARRAY_SIZE(data
));
310 for (idx
= 0; idx
< args
; idx
++) {
311 data
[idx
] = va_arg(vl
, u32
);
315 return pvr2_encoder_cmd(hdw
,cmd
,args
,0,data
);
319 /* This implements some extra setup for the encoder that seems to be
320 specific to the PVR USB2 hardware. */
321 static int pvr2_encoder_prep_config(struct pvr2_hdw
*hdw
)
327 /* This inexplicable bit happens in the Hauppauge windows
328 driver (for both 24xxx and 29xxx devices). However I
329 currently see no difference in behavior with or without
330 this stuff. Leave this here as a note of its existence,
332 LOCK_TAKE(hdw
->ctl_lock
); do {
335 pvr2_encoder_write_words(hdw
,0x01fe,dat
,1);
336 pvr2_encoder_write_words(hdw
,0x023e,dat
,1);
337 } while(0); LOCK_GIVE(hdw
->ctl_lock
);
340 /* Mike Isely <isely@pobox.com> 26-Jan-2006 The windows driver
341 sends the following list of ENC_MISC commands (for both
342 24xxx and 29xxx devices). Meanings are not entirely clear,
343 however without the ENC_MISC(3,1) command then we risk
344 random perpetual video corruption whenever the video input
345 breaks up for a moment (like when switching channels). */
349 /* This ENC_MISC(5,0) command seems to hurt 29xxx sync
350 performance on channel changes, but is not a problem on
352 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4, 5,0,0,0);
355 /* This ENC_MISC(3,encMisc3Arg) command is critical - without
356 it there will eventually be video corruption. Also, the
357 saa7115 case is strange - the Windows driver is passing 1
358 regardless of device type but if we have 1 for saa7115
359 devices the video turns sluggish. */
360 if (hdw
->hdw_desc
->flag_has_cx25840
) {
365 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4, 3,
368 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4, 8,0,0,0);
371 /* This ENC_MISC(4,1) command is poisonous, so it is commented
372 out. But I'm leaving it here anyway to document its
373 existence in the Windows driver. The effect of this
374 command is that apps displaying the stream become sluggish
375 with stuttering video. */
376 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4, 4,1,0,0);
379 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4, 0,3,0,0);
380 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
,4,15,0,0,0);
382 /* prevent the PTSs from slowly drifting away in the generated
384 ret
|= pvr2_encoder_vcmd(hdw
, CX2341X_ENC_MISC
, 2, 4, 1);
389 int pvr2_encoder_adjust(struct pvr2_hdw
*hdw
)
392 ret
= cx2341x_update(hdw
,pvr2_encoder_cmd
,
393 (hdw
->enc_cur_valid
? &hdw
->enc_cur_state
: NULL
),
394 &hdw
->enc_ctl_state
);
396 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
397 "Error from cx2341x module code=%d",ret
);
399 hdw
->enc_cur_state
= hdw
->enc_ctl_state
;
400 hdw
->enc_cur_valid
= !0;
406 int pvr2_encoder_configure(struct pvr2_hdw
*hdw
)
410 pvr2_trace(PVR2_TRACE_ENCODER
, "pvr2_encoder_configure (cx2341x module)");
411 hdw
->enc_ctl_state
.port
= CX2341X_PORT_STREAMING
;
412 hdw
->enc_ctl_state
.width
= hdw
->res_hor_val
;
413 hdw
->enc_ctl_state
.height
= hdw
->res_ver_val
;
414 hdw
->enc_ctl_state
.is_50hz
= ((hdw
->std_mask_cur
& V4L2_STD_525_60
) ?
419 ret
|= pvr2_encoder_prep_config(hdw
);
423 if (hdw
->hdw_desc
->flag_has_cx25840
) {
424 /* ivtv cx25840: 0x140 */
428 if (!ret
) ret
= pvr2_encoder_vcmd(
429 hdw
,CX2341X_ENC_SET_NUM_VSYNC_LINES
, 2,
432 /* setup firmware to notify us about some events (don't know why...) */
433 if (!ret
) ret
= pvr2_encoder_vcmd(
434 hdw
,CX2341X_ENC_SET_EVENT_NOTIFICATION
, 4,
435 0, 0, 0x10000000, 0xffffffff);
437 if (!ret
) ret
= pvr2_encoder_vcmd(
438 hdw
,CX2341X_ENC_SET_VBI_LINE
, 5,
442 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
443 "Failed to configure cx23416");
447 ret
= pvr2_encoder_adjust(hdw
);
450 ret
= pvr2_encoder_vcmd(
451 hdw
, CX2341X_ENC_INITIALIZE_INPUT
, 0);
454 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
455 "Failed to initialize cx23416 video input");
463 int pvr2_encoder_start(struct pvr2_hdw
*hdw
)
467 /* unmask some interrupts */
468 pvr2_write_register(hdw
, 0x0048, 0xbfffffff);
470 pvr2_encoder_vcmd(hdw
,CX2341X_ENC_MUTE_VIDEO
,1,
471 hdw
->input_val
== PVR2_CVAL_INPUT_RADIO
? 1 : 0);
473 switch (hdw
->active_stream_type
) {
474 case pvr2_config_vbi
:
475 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_START_CAPTURE
,2,
478 case pvr2_config_mpeg
:
479 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_START_CAPTURE
,2,
482 default: /* Unhandled cases for now */
483 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_START_CAPTURE
,2,
490 int pvr2_encoder_stop(struct pvr2_hdw
*hdw
)
494 /* mask all interrupts */
495 pvr2_write_register(hdw
, 0x0048, 0xffffffff);
497 switch (hdw
->active_stream_type
) {
498 case pvr2_config_vbi
:
499 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_STOP_CAPTURE
,3,
502 case pvr2_config_mpeg
:
503 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_STOP_CAPTURE
,3,
506 default: /* Unhandled cases for now */
507 status
= pvr2_encoder_vcmd(hdw
,CX2341X_ENC_STOP_CAPTURE
,3,