Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / video / pvrusb2 / pvrusb2-encoder.c
blob64062879981e4297db9f8386c68306c15e61c616
1 /*
3 * $Id$
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/device.h> // for linux/firmware.h
24 #include <linux/firmware.h>
25 #include "pvrusb2-util.h"
26 #include "pvrusb2-encoder.h"
27 #include "pvrusb2-hdw-internal.h"
28 #include "pvrusb2-debug.h"
29 #include "pvrusb2-fx2-cmd.h"
33 /* Firmware mailbox flags - definitions found from ivtv */
34 #define IVTV_MBOX_FIRMWARE_DONE 0x00000004
35 #define IVTV_MBOX_DRIVER_DONE 0x00000002
36 #define IVTV_MBOX_DRIVER_BUSY 0x00000001
38 #define MBOX_BASE 0x44
41 static int pvr2_encoder_write_words(struct pvr2_hdw *hdw,
42 unsigned int offs,
43 const u32 *data, unsigned int dlen)
45 unsigned int idx,addr;
46 unsigned int bAddr;
47 int ret;
48 unsigned int chunkCnt;
52 Format: First byte must be 0x01. Remaining 32 bit words are
53 spread out into chunks of 7 bytes each, with the first 4 bytes
54 being the data word (little endian), and the next 3 bytes
55 being the address where that data word is to be written (big
56 endian). Repeat request for additional words, with offset
57 adjusted accordingly.
60 while (dlen) {
61 chunkCnt = 8;
62 if (chunkCnt > dlen) chunkCnt = dlen;
63 memset(hdw->cmd_buffer,0,sizeof(hdw->cmd_buffer));
64 bAddr = 0;
65 hdw->cmd_buffer[bAddr++] = FX2CMD_MEM_WRITE_DWORD;
66 for (idx = 0; idx < chunkCnt; idx++) {
67 addr = idx + offs;
68 hdw->cmd_buffer[bAddr+6] = (addr & 0xffu);
69 hdw->cmd_buffer[bAddr+5] = ((addr>>8) & 0xffu);
70 hdw->cmd_buffer[bAddr+4] = ((addr>>16) & 0xffu);
71 PVR2_DECOMPOSE_LE(hdw->cmd_buffer, bAddr,data[idx]);
72 bAddr += 7;
74 ret = pvr2_send_request(hdw,
75 hdw->cmd_buffer,1+(chunkCnt*7),
76 NULL,0);
77 if (ret) return ret;
78 data += chunkCnt;
79 dlen -= chunkCnt;
80 offs += chunkCnt;
83 return 0;
87 static int pvr2_encoder_read_words(struct pvr2_hdw *hdw,
88 unsigned int offs,
89 u32 *data, unsigned int dlen)
91 unsigned int idx;
92 int ret;
93 unsigned int chunkCnt;
97 Format: First byte must be 0x02 (status check) or 0x28 (read
98 back block of 32 bit words). Next 6 bytes must be zero,
99 followed by a single byte of MBOX_BASE+offset for portion to
100 be read. Returned data is packed set of 32 bits words that
101 were read.
105 while (dlen) {
106 chunkCnt = 16;
107 if (chunkCnt > dlen) chunkCnt = dlen;
108 if (chunkCnt < 16) chunkCnt = 1;
109 hdw->cmd_buffer[0] =
110 ((chunkCnt == 1) ?
111 FX2CMD_MEM_READ_DWORD : FX2CMD_MEM_READ_64BYTES);
112 hdw->cmd_buffer[1] = 0;
113 hdw->cmd_buffer[2] = 0;
114 hdw->cmd_buffer[3] = 0;
115 hdw->cmd_buffer[4] = 0;
116 hdw->cmd_buffer[5] = ((offs>>16) & 0xffu);
117 hdw->cmd_buffer[6] = ((offs>>8) & 0xffu);
118 hdw->cmd_buffer[7] = (offs & 0xffu);
119 ret = pvr2_send_request(hdw,
120 hdw->cmd_buffer,8,
121 hdw->cmd_buffer,
122 (chunkCnt == 1 ? 4 : 16 * 4));
123 if (ret) return ret;
125 for (idx = 0; idx < chunkCnt; idx++) {
126 data[idx] = PVR2_COMPOSE_LE(hdw->cmd_buffer,idx*4);
128 data += chunkCnt;
129 dlen -= chunkCnt;
130 offs += chunkCnt;
133 return 0;
137 /* This prototype is set up to be compatible with the
138 cx2341x_mbox_func prototype in cx2341x.h, which should be in
139 kernels 2.6.18 or later. We do this so that we can enable
140 cx2341x.ko to write to our encoder (by handing it a pointer to this
141 function). For earlier kernels this doesn't really matter. */
142 static int pvr2_encoder_cmd(void *ctxt,
143 u32 cmd,
144 int arg_cnt_send,
145 int arg_cnt_recv,
146 u32 *argp)
148 unsigned int poll_count;
149 unsigned int try_count = 0;
150 int retry_flag;
151 int ret = 0;
152 unsigned int idx;
153 /* These sizes look to be limited by the FX2 firmware implementation */
154 u32 wrData[16];
155 u32 rdData[16];
156 struct pvr2_hdw *hdw = (struct pvr2_hdw *)ctxt;
161 The encoder seems to speak entirely using blocks 32 bit words.
162 In ivtv driver terms, this is a mailbox at MBOX_BASE which we
163 populate with data and watch what the hardware does with it.
164 The first word is a set of flags used to control the
165 transaction, the second word is the command to execute, the
166 third byte is zero (ivtv driver suggests that this is some
167 kind of return value), and the fourth byte is a specified
168 timeout (windows driver always uses 0x00060000 except for one
169 case when it is zero). All successive words are the argument
170 words for the command.
172 First, write out the entire set of words, with the first word
173 being zero.
175 Next, write out just the first word again, but set it to
176 IVTV_MBOX_DRIVER_DONE | IVTV_DRIVER_BUSY this time (which
177 probably means "go").
179 Next, read back the return count words. Check the first word,
180 which should have IVTV_MBOX_FIRMWARE_DONE set. If however
181 that bit is not set, then the command isn't done so repeat the
182 read until it is set.
184 Finally, write out just the first word again, but set it to
185 0x0 this time (which probably means "idle").
189 if (arg_cnt_send > (ARRAY_SIZE(wrData) - 4)) {
190 pvr2_trace(
191 PVR2_TRACE_ERROR_LEGS,
192 "Failed to write cx23416 command"
193 " - too many input arguments"
194 " (was given %u limit %lu)",
195 arg_cnt_send, (long unsigned) ARRAY_SIZE(wrData) - 4);
196 return -EINVAL;
199 if (arg_cnt_recv > (ARRAY_SIZE(rdData) - 4)) {
200 pvr2_trace(
201 PVR2_TRACE_ERROR_LEGS,
202 "Failed to write cx23416 command"
203 " - too many return arguments"
204 " (was given %u limit %lu)",
205 arg_cnt_recv, (long unsigned) ARRAY_SIZE(rdData) - 4);
206 return -EINVAL;
210 LOCK_TAKE(hdw->ctl_lock); do {
212 if (!hdw->state_encoder_ok) {
213 ret = -EIO;
214 break;
217 retry_flag = 0;
218 try_count++;
219 ret = 0;
220 wrData[0] = 0;
221 wrData[1] = cmd;
222 wrData[2] = 0;
223 wrData[3] = 0x00060000;
224 for (idx = 0; idx < arg_cnt_send; idx++) {
225 wrData[idx+4] = argp[idx];
227 for (; idx < ARRAY_SIZE(wrData) - 4; idx++) {
228 wrData[idx+4] = 0;
231 ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,idx);
232 if (ret) break;
233 wrData[0] = IVTV_MBOX_DRIVER_DONE|IVTV_MBOX_DRIVER_BUSY;
234 ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1);
235 if (ret) break;
236 poll_count = 0;
237 while (1) {
238 poll_count++;
239 ret = pvr2_encoder_read_words(hdw,MBOX_BASE,rdData,
240 arg_cnt_recv+4);
241 if (ret) {
242 break;
244 if (rdData[0] & IVTV_MBOX_FIRMWARE_DONE) {
245 break;
247 if (rdData[0] && (poll_count < 1000)) continue;
248 if (!rdData[0]) {
249 retry_flag = !0;
250 pvr2_trace(
251 PVR2_TRACE_ERROR_LEGS,
252 "Encoder timed out waiting for us"
253 "; arranging to retry");
254 } else {
255 pvr2_trace(
256 PVR2_TRACE_ERROR_LEGS,
257 "***WARNING*** device's encoder"
258 " appears to be stuck"
259 " (status=0x%08x)",rdData[0]);
261 pvr2_trace(
262 PVR2_TRACE_ERROR_LEGS,
263 "Encoder command: 0x%02x",cmd);
264 for (idx = 4; idx < arg_cnt_send; idx++) {
265 pvr2_trace(
266 PVR2_TRACE_ERROR_LEGS,
267 "Encoder arg%d: 0x%08x",
268 idx-3,wrData[idx]);
270 ret = -EBUSY;
271 break;
273 if (retry_flag) {
274 if (try_count < 20) continue;
275 pvr2_trace(
276 PVR2_TRACE_ERROR_LEGS,
277 "Too many retries...");
278 ret = -EBUSY;
280 if (ret) {
281 hdw->state_encoder_ok = 0;
282 pvr2_trace(PVR2_TRACE_STBITS,
283 "State bit %s <-- %s",
284 "state_encoder_ok",
285 (hdw->state_encoder_ok ? "true" : "false"));
286 pvr2_trace(
287 PVR2_TRACE_ERROR_LEGS,
288 "Giving up on command."
289 " This is normally recovered by the driver.");
290 break;
292 wrData[0] = 0x7;
293 for (idx = 0; idx < arg_cnt_recv; idx++) {
294 argp[idx] = rdData[idx+4];
297 wrData[0] = 0x0;
298 ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1);
299 if (ret) break;
301 } while(0); LOCK_GIVE(hdw->ctl_lock);
303 return ret;
307 static int pvr2_encoder_vcmd(struct pvr2_hdw *hdw, int cmd,
308 int args, ...)
310 va_list vl;
311 unsigned int idx;
312 u32 data[12];
314 if (args > ARRAY_SIZE(data)) {
315 pvr2_trace(
316 PVR2_TRACE_ERROR_LEGS,
317 "Failed to write cx23416 command"
318 " - too many arguments"
319 " (was given %u limit %lu)",
320 args, (long unsigned) ARRAY_SIZE(data));
321 return -EINVAL;
324 va_start(vl, args);
325 for (idx = 0; idx < args; idx++) {
326 data[idx] = va_arg(vl, u32);
328 va_end(vl);
330 return pvr2_encoder_cmd(hdw,cmd,args,0,data);
334 /* This implements some extra setup for the encoder that seems to be
335 specific to the PVR USB2 hardware. */
336 static int pvr2_encoder_prep_config(struct pvr2_hdw *hdw)
338 int ret = 0;
339 int encMisc3Arg = 0;
341 #if 0
342 /* This inexplicable bit happens in the Hauppage windows
343 driver (for both 24xxx and 29xxx devices). However I
344 currently see no difference in behavior with or without
345 this stuff. Leave this here as a note of its existence,
346 but don't use it. */
347 LOCK_TAKE(hdw->ctl_lock); do {
348 u32 dat[1];
349 dat[0] = 0x80000640;
350 pvr2_encoder_write_words(hdw,0x01fe,dat,1);
351 pvr2_encoder_write_words(hdw,0x023e,dat,1);
352 } while(0); LOCK_GIVE(hdw->ctl_lock);
353 #endif
355 /* Mike Isely <isely@pobox.com> 26-Jan-2006 The windows driver
356 sends the following list of ENC_MISC commands (for both
357 24xxx and 29xxx devices). Meanings are not entirely clear,
358 however without the ENC_MISC(3,1) command then we risk
359 random perpetual video corruption whenever the video input
360 breaks up for a moment (like when switching channels). */
363 #if 0
364 /* This ENC_MISC(5,0) command seems to hurt 29xxx sync
365 performance on channel changes, but is not a problem on
366 24xxx devices. */
367 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 5,0,0,0);
368 #endif
370 /* This ENC_MISC(3,encMisc3Arg) command is critical - without
371 it there will eventually be video corruption. Also, the
372 saa7115 case is strange - the Windows driver is passing 1
373 regardless of device type but if we have 1 for saa7115
374 devices the video turns sluggish. */
375 if (hdw->hdw_desc->flag_has_cx25840) {
376 encMisc3Arg = 1;
377 } else {
378 encMisc3Arg = 0;
380 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 3,
381 encMisc3Arg,0,0);
383 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 8,0,0,0);
385 #if 0
386 /* This ENC_MISC(4,1) command is poisonous, so it is commented
387 out. But I'm leaving it here anyway to document its
388 existence in the Windows driver. The effect of this
389 command is that apps displaying the stream become sluggish
390 with stuttering video. */
391 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 4,1,0,0);
392 #endif
394 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 0,3,0,0);
395 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4,15,0,0,0);
397 return ret;
400 int pvr2_encoder_adjust(struct pvr2_hdw *hdw)
402 int ret;
403 ret = cx2341x_update(hdw,pvr2_encoder_cmd,
404 (hdw->enc_cur_valid ? &hdw->enc_cur_state : NULL),
405 &hdw->enc_ctl_state);
406 if (ret) {
407 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
408 "Error from cx2341x module code=%d",ret);
409 } else {
410 memcpy(&hdw->enc_cur_state,&hdw->enc_ctl_state,
411 sizeof(struct cx2341x_mpeg_params));
412 hdw->enc_cur_valid = !0;
414 return ret;
418 int pvr2_encoder_configure(struct pvr2_hdw *hdw)
420 int ret;
421 int val;
422 pvr2_trace(PVR2_TRACE_ENCODER,"pvr2_encoder_configure"
423 " (cx2341x module)");
424 hdw->enc_ctl_state.port = CX2341X_PORT_STREAMING;
425 hdw->enc_ctl_state.width = hdw->res_hor_val;
426 hdw->enc_ctl_state.height = hdw->res_ver_val;
427 hdw->enc_ctl_state.is_50hz = ((hdw->std_mask_cur & V4L2_STD_525_60) ?
428 0 : 1);
430 ret = 0;
432 ret |= pvr2_encoder_prep_config(hdw);
434 /* saa7115: 0xf0 */
435 val = 0xf0;
436 if (hdw->hdw_desc->flag_has_cx25840) {
437 /* ivtv cx25840: 0x140 */
438 val = 0x140;
441 if (!ret) ret = pvr2_encoder_vcmd(
442 hdw,CX2341X_ENC_SET_NUM_VSYNC_LINES, 2,
443 val, val);
445 /* setup firmware to notify us about some events (don't know why...) */
446 if (!ret) ret = pvr2_encoder_vcmd(
447 hdw,CX2341X_ENC_SET_EVENT_NOTIFICATION, 4,
448 0, 0, 0x10000000, 0xffffffff);
450 if (!ret) ret = pvr2_encoder_vcmd(
451 hdw,CX2341X_ENC_SET_VBI_LINE, 5,
452 0xffffffff,0,0,0,0);
454 if (ret) {
455 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
456 "Failed to configure cx23416");
457 return ret;
460 ret = pvr2_encoder_adjust(hdw);
461 if (ret) return ret;
463 ret = pvr2_encoder_vcmd(
464 hdw, CX2341X_ENC_INITIALIZE_INPUT, 0);
466 if (ret) {
467 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
468 "Failed to initialize cx23416 video input");
469 return ret;
472 return 0;
476 int pvr2_encoder_start(struct pvr2_hdw *hdw)
478 int status;
480 /* unmask some interrupts */
481 pvr2_write_register(hdw, 0x0048, 0xbfffffff);
483 /* change some GPIO data */
484 pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000481);
485 pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000);
487 pvr2_encoder_vcmd(hdw,CX2341X_ENC_MUTE_VIDEO,1,
488 hdw->input_val == PVR2_CVAL_INPUT_RADIO ? 1 : 0);
490 switch (hdw->active_stream_type) {
491 case pvr2_config_vbi:
492 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
493 0x01,0x14);
494 break;
495 case pvr2_config_mpeg:
496 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
497 0,0x13);
498 break;
499 default: /* Unhandled cases for now */
500 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
501 0,0x13);
502 break;
504 return status;
507 int pvr2_encoder_stop(struct pvr2_hdw *hdw)
509 int status;
511 /* mask all interrupts */
512 pvr2_write_register(hdw, 0x0048, 0xffffffff);
514 switch (hdw->active_stream_type) {
515 case pvr2_config_vbi:
516 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
517 0x01,0x01,0x14);
518 break;
519 case pvr2_config_mpeg:
520 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
521 0x01,0,0x13);
522 break;
523 default: /* Unhandled cases for now */
524 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
525 0x01,0,0x13);
526 break;
529 /* change some GPIO data */
530 /* Note: Bit d7 of dir appears to control the LED. So we shut it
531 off here. */
532 pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000401);
533 pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000);
535 return status;
540 Stuff for Emacs to see, in order to encourage consistent editing style:
541 *** Local Variables: ***
542 *** mode: c ***
543 *** fill-column: 70 ***
544 *** tab-width: 8 ***
545 *** c-basic-offset: 8 ***
546 *** End: ***