pvrusb2-encoder.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 *
 *  $Id$
 *
 *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
 *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/device.h>   // for linux/firmware.h
#include <linux/firmware.h>
#include "pvrusb2-util.h"
#include "pvrusb2-encoder.h"
#include "pvrusb2-hdw-internal.h"
#include "pvrusb2-debug.h"
29
#include "pvrusb2-fx2-cmd.h"
30 31 32 33 34 35 36 37



/* Firmware mailbox flags - definitions found from ivtv */
#define IVTV_MBOX_FIRMWARE_DONE 0x00000004
#define IVTV_MBOX_DRIVER_DONE 0x00000002
#define IVTV_MBOX_DRIVER_BUSY 0x00000001

38 39
#define MBOX_BASE 0x44

40

41
static int pvr2_encoder_write_words(struct pvr2_hdw *hdw,
42
				    unsigned int offs,
43 44
				    const u32 *data, unsigned int dlen)
{
45 46
	unsigned int idx,addr;
	unsigned int bAddr;
47 48 49 50 51 52
	int ret;
	unsigned int chunkCnt;

	/*

	Format: First byte must be 0x01.  Remaining 32 bit words are
53 54 55 56 57
	spread out into chunks of 7 bytes each, with the first 4 bytes
	being the data word (little endian), and the next 3 bytes
	being the address where that data word is to be written (big
	endian).  Repeat request for additional words, with offset
	adjusted accordingly.
58 59 60 61 62 63

	*/
	while (dlen) {
		chunkCnt = 8;
		if (chunkCnt > dlen) chunkCnt = dlen;
		memset(hdw->cmd_buffer,0,sizeof(hdw->cmd_buffer));
64 65
		bAddr = 0;
		hdw->cmd_buffer[bAddr++] = FX2CMD_MEM_WRITE_DWORD;
66
		for (idx = 0; idx < chunkCnt; idx++) {
67 68 69 70 71 72
			addr = idx + offs;
			hdw->cmd_buffer[bAddr+6] = (addr & 0xffu);
			hdw->cmd_buffer[bAddr+5] = ((addr>>8) & 0xffu);
			hdw->cmd_buffer[bAddr+4] = ((addr>>16) & 0xffu);
			PVR2_DECOMPOSE_LE(hdw->cmd_buffer, bAddr,data[idx]);
			bAddr += 7;
73 74 75
		}
		ret = pvr2_send_request(hdw,
					hdw->cmd_buffer,1+(chunkCnt*7),
76
					NULL,0);
77 78 79 80 81 82 83 84 85 86
		if (ret) return ret;
		data += chunkCnt;
		dlen -= chunkCnt;
		offs += chunkCnt;
	}

	return 0;
}


87 88
static int pvr2_encoder_read_words(struct pvr2_hdw *hdw,
				   unsigned int offs,
89 90 91 92 93 94 95 96 97 98
				   u32 *data, unsigned int dlen)
{
	unsigned int idx;
	int ret;
	unsigned int chunkCnt;

	/*

	Format: First byte must be 0x02 (status check) or 0x28 (read
	back block of 32 bit words).  Next 6 bytes must be zero,
99 100 101
	followed by a single byte of MBOX_BASE+offset for portion to
	be read.  Returned data is packed set of 32 bits words that
	were read.
102 103 104 105 106 107

	*/

	while (dlen) {
		chunkCnt = 16;
		if (chunkCnt > dlen) chunkCnt = dlen;
108
		if (chunkCnt < 16) chunkCnt = 1;
109
		hdw->cmd_buffer[0] =
110 111 112 113 114 115 116 117 118
			((chunkCnt == 1) ?
			 FX2CMD_MEM_READ_DWORD : FX2CMD_MEM_READ_64BYTES);
		hdw->cmd_buffer[1] = 0;
		hdw->cmd_buffer[2] = 0;
		hdw->cmd_buffer[3] = 0;
		hdw->cmd_buffer[4] = 0;
		hdw->cmd_buffer[5] = ((offs>>16) & 0xffu);
		hdw->cmd_buffer[6] = ((offs>>8) & 0xffu);
		hdw->cmd_buffer[7] = (offs & 0xffu);
119 120
		ret = pvr2_send_request(hdw,
					hdw->cmd_buffer,8,
121 122
					hdw->cmd_buffer,
					(chunkCnt == 1 ? 4 : 16 * 4));
123 124 125 126 127 128 129 130 131 132 133 134 135 136
		if (ret) return ret;

		for (idx = 0; idx < chunkCnt; idx++) {
			data[idx] = PVR2_COMPOSE_LE(hdw->cmd_buffer,idx*4);
		}
		data += chunkCnt;
		dlen -= chunkCnt;
		offs += chunkCnt;
	}

	return 0;
}


137 138 139 140 141 142
/* This prototype is set up to be compatible with the
   cx2341x_mbox_func prototype in cx2341x.h, which should be in
   kernels 2.6.18 or later.  We do this so that we can enable
   cx2341x.ko to write to our encoder (by handing it a pointer to this
   function).  For earlier kernels this doesn't really matter. */
static int pvr2_encoder_cmd(void *ctxt,
143
			    u32 cmd,
144 145 146
			    int arg_cnt_send,
			    int arg_cnt_recv,
			    u32 *argp)
147 148
{
	unsigned int poll_count;
149 150
	unsigned int try_count = 0;
	int retry_flag;
151 152
	int ret = 0;
	unsigned int idx;
153
	/* These sizes look to be limited by the FX2 firmware implementation */
154
	u32 wrData[16];
155 156
	u32 rdData[16];
	struct pvr2_hdw *hdw = (struct pvr2_hdw *)ctxt;
157

158

159 160 161
	/*

	The encoder seems to speak entirely using blocks 32 bit words.
162 163 164 165 166 167 168 169 170
	In ivtv driver terms, this is a mailbox at MBOX_BASE which we
	populate with data and watch what the hardware does with it.
	The first word is a set of flags used to control the
	transaction, the second word is the command to execute, the
	third byte is zero (ivtv driver suggests that this is some
	kind of return value), and the fourth byte is a specified
	timeout (windows driver always uses 0x00060000 except for one
	case when it is zero).  All successive words are the argument
	words for the command.
171 172 173 174 175 176 177 178

	First, write out the entire set of words, with the first word
	being zero.

	Next, write out just the first word again, but set it to
	IVTV_MBOX_DRIVER_DONE | IVTV_DRIVER_BUSY this time (which
	probably means "go").

179
	Next, read back the return count words.  Check the first word,
180 181
	which should have IVTV_MBOX_FIRMWARE_DONE set.  If however
	that bit is not set, then the command isn't done so repeat the
182
	read until it is set.
183 184 185 186 187 188

	Finally, write out just the first word again, but set it to
	0x0 this time (which probably means "idle").

	*/

189
	if (arg_cnt_send > (ARRAY_SIZE(wrData) - 4)) {
190 191 192 193
		pvr2_trace(
			PVR2_TRACE_ERROR_LEGS,
			"Failed to write cx23416 command"
			" - too many input arguments"
194 195
			" (was given %u limit %lu)",
			arg_cnt_send, (long unsigned) ARRAY_SIZE(wrData) - 4);
196 197 198
		return -EINVAL;
	}

199
	if (arg_cnt_recv > (ARRAY_SIZE(rdData) - 4)) {
200 201 202 203
		pvr2_trace(
			PVR2_TRACE_ERROR_LEGS,
			"Failed to write cx23416 command"
			" - too many return arguments"
204 205
			" (was given %u limit %lu)",
			arg_cnt_recv, (long unsigned) ARRAY_SIZE(rdData) - 4);
206 207 208
		return -EINVAL;
	}

209 210 211

	LOCK_TAKE(hdw->ctl_lock); do {

212
		if (!hdw->state_encoder_ok) {
213 214 215 216
			ret = -EIO;
			break;
		}

217 218 219
		retry_flag = 0;
		try_count++;
		ret = 0;
220 221 222 223
		wrData[0] = 0;
		wrData[1] = cmd;
		wrData[2] = 0;
		wrData[3] = 0x00060000;
224 225
		for (idx = 0; idx < arg_cnt_send; idx++) {
			wrData[idx+4] = argp[idx];
226
		}
227
		for (; idx < ARRAY_SIZE(wrData) - 4; idx++) {
228
			wrData[idx+4] = 0;
229 230
		}

231
		ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,idx);
232 233
		if (ret) break;
		wrData[0] = IVTV_MBOX_DRIVER_DONE|IVTV_MBOX_DRIVER_BUSY;
234
		ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1);
235 236 237
		if (ret) break;
		poll_count = 0;
		while (1) {
238 239 240 241 242 243
			poll_count++;
			ret = pvr2_encoder_read_words(hdw,MBOX_BASE,rdData,
						      arg_cnt_recv+4);
			if (ret) {
				break;
			}
244 245 246
			if (rdData[0] & IVTV_MBOX_FIRMWARE_DONE) {
				break;
			}
247 248 249 250 251 252 253 254
			if (rdData[0] && (poll_count < 1000)) continue;
			if (!rdData[0]) {
				retry_flag = !0;
				pvr2_trace(
					PVR2_TRACE_ERROR_LEGS,
					"Encoder timed out waiting for us"
					"; arranging to retry");
			} else {
255 256 257 258
				pvr2_trace(
					PVR2_TRACE_ERROR_LEGS,
					"***WARNING*** device's encoder"
					" appears to be stuck"
259
					" (status=0x%08x)",rdData[0]);
260 261 262 263 264
			}
			pvr2_trace(
				PVR2_TRACE_ERROR_LEGS,
				"Encoder command: 0x%02x",cmd);
			for (idx = 4; idx < arg_cnt_send; idx++) {
265 266
				pvr2_trace(
					PVR2_TRACE_ERROR_LEGS,
267 268
					"Encoder arg%d: 0x%08x",
					idx-3,wrData[idx]);
269
			}
270 271 272 273 274 275 276 277 278 279 280
			ret = -EBUSY;
			break;
		}
		if (retry_flag) {
			if (try_count < 20) continue;
			pvr2_trace(
				PVR2_TRACE_ERROR_LEGS,
				"Too many retries...");
			ret = -EBUSY;
		}
		if (ret) {
281 282 283 284 285
			hdw->state_encoder_ok = 0;
			pvr2_trace(PVR2_TRACE_STBITS,
				   "State bit %s <-- %s",
				   "state_encoder_ok",
				   (hdw->state_encoder_ok ? "true" : "false"));
286 287 288
			pvr2_trace(
				PVR2_TRACE_ERROR_LEGS,
				"Giving up on command."
289
				"  This is normally recovered by the driver.");
290
			break;
291 292
		}
		wrData[0] = 0x7;
293 294
		for (idx = 0; idx < arg_cnt_recv; idx++) {
			argp[idx] = rdData[idx+4];
295 296 297
		}

		wrData[0] = 0x0;
298
		ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1);
299 300 301 302 303 304 305
		if (ret) break;

	} while(0); LOCK_GIVE(hdw->ctl_lock);

	return ret;
}

306 307 308 309 310 311 312 313

static int pvr2_encoder_vcmd(struct pvr2_hdw *hdw, int cmd,
			     int args, ...)
{
	va_list vl;
	unsigned int idx;
	u32 data[12];

314
	if (args > ARRAY_SIZE(data)) {
315 316 317 318
		pvr2_trace(
			PVR2_TRACE_ERROR_LEGS,
			"Failed to write cx23416 command"
			" - too many arguments"
319 320
			" (was given %u limit %lu)",
			args, (long unsigned) ARRAY_SIZE(data));
321 322 323 324 325 326 327 328 329 330 331 332
		return -EINVAL;
	}

	va_start(vl, args);
	for (idx = 0; idx < args; idx++) {
		data[idx] = va_arg(vl, u32);
	}
	va_end(vl);

	return pvr2_encoder_cmd(hdw,cmd,args,0,data);
}

333 334 335

/* This implements some extra setup for the encoder that seems to be
   specific to the PVR USB2 hardware. */
336
static int pvr2_encoder_prep_config(struct pvr2_hdw *hdw)
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
{
	int ret = 0;
	int encMisc3Arg = 0;

#if 0
	/* This inexplicable bit happens in the Hauppage windows
	   driver (for both 24xxx and 29xxx devices).  However I
	   currently see no difference in behavior with or without
	   this stuff.  Leave this here as a note of its existence,
	   but don't use it. */
	LOCK_TAKE(hdw->ctl_lock); do {
		u32 dat[1];
		dat[0] = 0x80000640;
		pvr2_encoder_write_words(hdw,0x01fe,dat,1);
		pvr2_encoder_write_words(hdw,0x023e,dat,1);
	} while(0); LOCK_GIVE(hdw->ctl_lock);
#endif

	/* Mike Isely <isely@pobox.com> 26-Jan-2006 The windows driver
	   sends the following list of ENC_MISC commands (for both
	   24xxx and 29xxx devices).  Meanings are not entirely clear,
	   however without the ENC_MISC(3,1) command then we risk
	   random perpetual video corruption whenever the video input
	   breaks up for a moment (like when switching channels). */


#if 0
	/* This ENC_MISC(5,0) command seems to hurt 29xxx sync
	   performance on channel changes, but is not a problem on
	   24xxx devices. */
	ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 5,0,0,0);
#endif

	/* This ENC_MISC(3,encMisc3Arg) command is critical - without
	   it there will eventually be video corruption.  Also, the
372 373 374 375 376 377 378
	   saa7115 case is strange - the Windows driver is passing 1
	   regardless of device type but if we have 1 for saa7115
	   devices the video turns sluggish.  */
	if (hdw->hdw_desc->flag_has_cx25840) {
		encMisc3Arg = 1;
	} else {
		encMisc3Arg = 0;
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	}
	ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 3,
				 encMisc3Arg,0,0);

	ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 8,0,0,0);

#if 0
	/* This ENC_MISC(4,1) command is poisonous, so it is commented
	   out.  But I'm leaving it here anyway to document its
	   existence in the Windows driver.  The effect of this
	   command is that apps displaying the stream become sluggish
	   with stuttering video. */
	ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 4,1,0,0);
#endif

	ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 0,3,0,0);
	ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4,15,0,0,0);

	return ret;
}

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
int pvr2_encoder_adjust(struct pvr2_hdw *hdw)
{
	int ret;
	ret = cx2341x_update(hdw,pvr2_encoder_cmd,
			     (hdw->enc_cur_valid ? &hdw->enc_cur_state : NULL),
			     &hdw->enc_ctl_state);
	if (ret) {
		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
			   "Error from cx2341x module code=%d",ret);
	} else {
		memcpy(&hdw->enc_cur_state,&hdw->enc_ctl_state,
		       sizeof(struct cx2341x_mpeg_params));
		hdw->enc_cur_valid = !0;
	}
	return ret;
}


418 419
int pvr2_encoder_configure(struct pvr2_hdw *hdw)
{
420
	int ret;
421
	int val;
422 423 424 425 426
	pvr2_trace(PVR2_TRACE_ENCODER,"pvr2_encoder_configure"
		   " (cx2341x module)");
	hdw->enc_ctl_state.port = CX2341X_PORT_STREAMING;
	hdw->enc_ctl_state.width = hdw->res_hor_val;
	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);
429

430 431
	ret = 0;

432 433
	ret |= pvr2_encoder_prep_config(hdw);

434 435
	/* saa7115: 0xf0 */
	val = 0xf0;
436
	if (hdw->hdw_desc->flag_has_cx25840) {
437 438 439 440
		/* ivtv cx25840: 0x140 */
		val = 0x140;
	}

441 442
	if (!ret) ret = pvr2_encoder_vcmd(
		hdw,CX2341X_ENC_SET_NUM_VSYNC_LINES, 2,
443
		val, val);
444 445

	/* setup firmware to notify us about some events (don't know why...) */
446 447 448 449 450 451 452 453 454 455
	if (!ret) ret = pvr2_encoder_vcmd(
		hdw,CX2341X_ENC_SET_EVENT_NOTIFICATION, 4,
		0, 0, 0x10000000, 0xffffffff);

	if (!ret) ret = pvr2_encoder_vcmd(
		hdw,CX2341X_ENC_SET_VBI_LINE, 5,
		0xffffffff,0,0,0,0);

	if (ret) {
		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
456
			   "Failed to configure cx23416");
457 458
		return ret;
	}
459

460 461
	ret = pvr2_encoder_adjust(hdw);
	if (ret) return ret;
462

463
	ret = pvr2_encoder_vcmd(
464
		hdw, CX2341X_ENC_INITIALIZE_INPUT, 0);
465

466 467
	if (ret) {
		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
468
			   "Failed to initialize cx23416 video input");
469
		return ret;
470 471
	}

472
	return 0;
473 474
}

475

476 477 478 479 480 481 482 483 484 485 486
int pvr2_encoder_start(struct pvr2_hdw *hdw)
{
	int status;

	/* unmask some interrupts */
	pvr2_write_register(hdw, 0x0048, 0xbfffffff);

	/* change some GPIO data */
	pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000481);
	pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000);

487 488 489
	pvr2_encoder_vcmd(hdw,CX2341X_ENC_MUTE_VIDEO,1,
			  hdw->input_val == PVR2_CVAL_INPUT_RADIO ? 1 : 0);

490
	switch (hdw->active_stream_type) {
491
	case pvr2_config_vbi:
492 493
		status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
					   0x01,0x14);
494 495
		break;
	case pvr2_config_mpeg:
496 497
		status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
					   0,0x13);
498 499
		break;
	default: /* Unhandled cases for now */
500 501
		status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
					   0,0x13);
502
		break;
503 504 505 506 507 508 509 510 511 512 513
	}
	return status;
}

int pvr2_encoder_stop(struct pvr2_hdw *hdw)
{
	int status;

	/* mask all interrupts */
	pvr2_write_register(hdw, 0x0048, 0xffffffff);

514
	switch (hdw->active_stream_type) {
515
	case pvr2_config_vbi:
516 517
		status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
					   0x01,0x01,0x14);
518 519
		break;
	case pvr2_config_mpeg:
520 521
		status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
					   0x01,0,0x13);
522 523
		break;
	default: /* Unhandled cases for now */
524 525
		status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
					   0x01,0,0x13);
526
		break;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	}

	/* change some GPIO data */
	/* Note: Bit d7 of dir appears to control the LED.  So we shut it
	   off here. */
	pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000401);
	pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000);

	return status;
}


/*
  Stuff for Emacs to see, in order to encourage consistent editing style:
  *** Local Variables: ***
  *** mode: c ***
  *** fill-column: 70 ***
  *** tab-width: 8 ***
  *** c-basic-offset: 8 ***
  *** End: ***
  */