ivtvfb.c 33.2 KB
Newer Older
1 2 3 4 5
/*
    On Screen Display cx23415 Framebuffer driver

    This module presents the cx23415 OSD (onscreen display) framebuffer memory
    as a standard Linux /dev/fb style framebuffer device. The framebuffer has
H
Hans Verkuil 已提交
6
    support for 8, 16 & 32 bpp packed pixel formats with alpha channel. In 16bpp
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    mode, there is a choice of a three color depths (12, 15 or 16 bits), but no
    local alpha. The colorspace is selectable between rgb & yuv.
    Depending on the TV standard configured in the ivtv module at load time,
    the initial resolution is either 640x400 (NTSC) or 640x480 (PAL) at 8bpp.
    Video timings are locked to ensure a vertical refresh rate of 50Hz (PAL)
    or 59.94 (NTSC)

    Copyright (c) 2003 Matt T. Yourst <yourst@yourst.com>

    Derived from drivers/video/vesafb.c
    Portions (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>

    2.6 kernel port:
    Copyright (C) 2004 Matthias Badaire

    Copyright (C) 2004  Chris Kennedy <c@groovy.org>

    Copyright (C) 2006  Ian Armstrong <ian@iarmst.demon.co.uk>

    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, or
    (at your option) any later version.

    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/module.h>
#include <linux/kernel.h>
#include <linux/fb.h>
44
#include <linux/ivtvfb.h>
45 46 47 48 49 50 51 52 53 54

#ifdef CONFIG_MTRR
#include <asm/mtrr.h>
#endif

#include "ivtv-driver.h"
#include "ivtv-udma.h"
#include "ivtv-mailbox.h"

/* card parameters */
55 56
static int ivtvfb_card_id = -1;
static int ivtvfb_debug = 0;
57 58 59 60 61 62 63 64
static int osd_laced;
static int osd_compat;
static int osd_depth;
static int osd_upper;
static int osd_left;
static int osd_yres;
static int osd_xres;

65 66
module_param(ivtvfb_card_id, int, 0444);
module_param_named(debug,ivtvfb_debug, int, 0644);
67 68 69 70 71 72 73 74
module_param(osd_laced, bool, 0444);
module_param(osd_compat, bool, 0444);
module_param(osd_depth, int, 0444);
module_param(osd_upper, int, 0444);
module_param(osd_left, int, 0444);
module_param(osd_yres, int, 0444);
module_param(osd_xres, int, 0444);

75
MODULE_PARM_DESC(ivtvfb_card_id,
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		 "Only use framebuffer of the specified ivtv card (0-31)\n"
		 "\t\t\tdefault -1: initialize all available framebuffers");

MODULE_PARM_DESC(debug,
		 "Debug level (bitmask). Default: errors only\n"
		 "\t\t\t(debug = 3 gives full debugging)");

MODULE_PARM_DESC(osd_compat,
		 "Compatibility mode - Display size is locked (use for old X drivers)\n"
		 "\t\t\t0=off\n"
		 "\t\t\t1=on\n"
		 "\t\t\tdefault off");

/* Why upper, left, xres, yres, depth, laced ? To match terminology used
   by fbset.
   Why start at 1 for left & upper coordinate ? Because X doesn't allow 0 */

MODULE_PARM_DESC(osd_laced,
		 "Interlaced mode\n"
		 "\t\t\t0=off\n"
		 "\t\t\t1=on\n"
		 "\t\t\tdefault off");

MODULE_PARM_DESC(osd_depth,
H
Hans Verkuil 已提交
100
		 "Bits per pixel - 8, 16, 32\n"
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		 "\t\t\tdefault 8");

MODULE_PARM_DESC(osd_upper,
		 "Vertical start position\n"
		 "\t\t\tdefault 0 (Centered)");

MODULE_PARM_DESC(osd_left,
		 "Horizontal start position\n"
		 "\t\t\tdefault 0 (Centered)");

MODULE_PARM_DESC(osd_yres,
		 "Display height\n"
		 "\t\t\tdefault 480 (PAL)\n"
		 "\t\t\t        400 (NTSC)");

MODULE_PARM_DESC(osd_xres,
		 "Display width\n"
		 "\t\t\tdefault 640");

MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil, John Harvey, Ian Armstrong");
MODULE_LICENSE("GPL");

/* --------------------------------------------------------------------- */

125 126
#define IVTVFB_DBGFLG_WARN  (1 << 0)
#define IVTVFB_DBGFLG_INFO  (1 << 1)
127

128
#define IVTVFB_DEBUG(x, type, fmt, args...) \
129
	do { \
130
		if ((x) & ivtvfb_debug) \
131
			printk(KERN_INFO "ivtvfb%d " type ": " fmt, itv->num , ## args); \
132
	} while (0)
133 134
#define IVTVFB_DEBUG_WARN(fmt, args...)  IVTVFB_DEBUG(IVTVFB_DBGFLG_WARN, "warning", fmt , ## args)
#define IVTVFB_DEBUG_INFO(fmt, args...)  IVTVFB_DEBUG(IVTVFB_DBGFLG_INFO, "info", fmt , ## args)
135 136

/* Standard kernel messages */
137 138 139
#define IVTVFB_ERR(fmt, args...)   printk(KERN_ERR  "ivtvfb%d: " fmt, itv->num , ## args)
#define IVTVFB_WARN(fmt, args...)  printk(KERN_WARNING  "ivtvfb%d: " fmt, itv->num , ## args)
#define IVTVFB_INFO(fmt, args...)  printk(KERN_INFO "ivtvfb%d: " fmt, itv->num , ## args)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

/* --------------------------------------------------------------------- */

#define IVTV_OSD_MAX_WIDTH  720
#define IVTV_OSD_MAX_HEIGHT 576

#define IVTV_OSD_BPP_8      0x00
#define IVTV_OSD_BPP_16_444 0x03
#define IVTV_OSD_BPP_16_555 0x02
#define IVTV_OSD_BPP_16_565 0x01
#define IVTV_OSD_BPP_32     0x04

struct osd_info {
	/* Physical base address */
	unsigned long video_pbase;
	/* Relative base address (relative to start of decoder memory) */
	u32 video_rbase;
	/* Mapped base address */
	volatile char __iomem *video_vbase;
	/* Buffer size */
	u32 video_buffer_size;

#ifdef CONFIG_MTRR
	/* video_base rounded down as required by hardware MTRRs */
	unsigned long fb_start_aligned_physaddr;
	/* video_base rounded up as required by hardware MTRRs */
	unsigned long fb_end_aligned_physaddr;
#endif

169 170 171
	/* Current osd mode */
	int osd_mode;

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	/* Store the buffer offset */
	int set_osd_coords_x;
	int set_osd_coords_y;

	/* Current dimensions (NOT VISIBLE SIZE!) */
	int display_width;
	int display_height;
	int display_byte_stride;

	/* Current bits per pixel */
	int bits_per_pixel;
	int bytes_per_pixel;

	/* Frame buffer stuff */
	struct fb_info ivtvfb_info;
	struct fb_var_screeninfo ivtvfb_defined;
	struct fb_fix_screeninfo ivtvfb_fix;
};

struct ivtv_osd_coords {
	unsigned long offset;
	unsigned long max_offset;
	int pixel_stride;
	int lines;
	int x;
	int y;
};

/* --------------------------------------------------------------------- */

/* ivtv API calls for framebuffer related support */

204
static int ivtvfb_get_framebuffer(struct ivtv *itv, u32 *fbbase,
205 206 207 208 209 210 211 212 213 214 215
				       u32 *fblength)
{
	u32 data[CX2341X_MBOX_MAX_DATA];
	int rc;

	rc = ivtv_vapi_result(itv, data, CX2341X_OSD_GET_FRAMEBUFFER, 0);
	*fbbase = data[0];
	*fblength = data[1];
	return rc;
}

216
static int ivtvfb_get_osd_coords(struct ivtv *itv,
217 218
				      struct ivtv_osd_coords *osd)
{
H
Hans Verkuil 已提交
219
	struct osd_info *oi = itv->osd_info;
220 221 222 223
	u32 data[CX2341X_MBOX_MAX_DATA];

	ivtv_vapi_result(itv, data, CX2341X_OSD_GET_OSD_COORDS, 0);

H
Hans Verkuil 已提交
224 225
	osd->offset = data[0] - oi->video_rbase;
	osd->max_offset = oi->display_width * oi->display_height * 4;
226 227 228 229 230 231 232
	osd->pixel_stride = data[1];
	osd->lines = data[2];
	osd->x = data[3];
	osd->y = data[4];
	return 0;
}

233
static int ivtvfb_set_osd_coords(struct ivtv *itv, const struct ivtv_osd_coords *osd)
234
{
H
Hans Verkuil 已提交
235 236 237 238 239 240
	struct osd_info *oi = itv->osd_info;

	oi->display_width = osd->pixel_stride;
	oi->display_byte_stride = osd->pixel_stride * oi->bytes_per_pixel;
	oi->set_osd_coords_x += osd->x;
	oi->set_osd_coords_y = osd->y;
241 242

	return ivtv_vapi(itv, CX2341X_OSD_SET_OSD_COORDS, 5,
H
Hans Verkuil 已提交
243
			osd->offset + oi->video_rbase,
244 245 246 247
			osd->pixel_stride,
			osd->lines, osd->x, osd->y);
}

248
static int ivtvfb_set_display_window(struct ivtv *itv, struct v4l2_rect *ivtv_window)
249 250 251 252 253 254 255 256 257
{
	int osd_height_limit = itv->is_50hz ? 576 : 480;

	/* Only fail if resolution too high, otherwise fudge the start coords. */
	if ((ivtv_window->height > osd_height_limit) || (ivtv_window->width > IVTV_OSD_MAX_WIDTH))
		return -EINVAL;

	/* Ensure we don't exceed display limits */
	if (ivtv_window->top + ivtv_window->height > osd_height_limit) {
258
		IVTVFB_DEBUG_WARN("ivtv_ioctl_fb_set_display_window - Invalid height setting (%d, %d)\n",
259 260 261 262 263
			ivtv_window->top, ivtv_window->height);
		ivtv_window->top = osd_height_limit - ivtv_window->height;
	}

	if (ivtv_window->left + ivtv_window->width > IVTV_OSD_MAX_WIDTH) {
264
		IVTVFB_DEBUG_WARN("ivtv_ioctl_fb_set_display_window - Invalid width setting (%d, %d)\n",
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
			ivtv_window->left, ivtv_window->width);
		ivtv_window->left = IVTV_OSD_MAX_WIDTH - ivtv_window->width;
	}

	/* Set the OSD origin */
	write_reg((ivtv_window->top << 16) | ivtv_window->left, 0x02a04);

	/* How much to display */
	write_reg(((ivtv_window->top+ivtv_window->height) << 16) | (ivtv_window->left+ivtv_window->width), 0x02a08);

	/* Pass this info back the yuv handler */
	itv->yuv_info.osd_vis_w = ivtv_window->width;
	itv->yuv_info.osd_vis_h = ivtv_window->height;
	itv->yuv_info.osd_x_offset = ivtv_window->left;
	itv->yuv_info.osd_y_offset = ivtv_window->top;

	return 0;
}

284
static int ivtvfb_prep_dec_dma_to_device(struct ivtv *itv,
285 286 287 288 289 290 291 292 293 294 295
				  unsigned long ivtv_dest_addr, void __user *userbuf,
				  int size_in_bytes)
{
	DEFINE_WAIT(wait);
	int ret = 0;
	int got_sig = 0;

	mutex_lock(&itv->udma.lock);
	/* Map User DMA */
	if (ivtv_udma_setup(itv, ivtv_dest_addr, userbuf, size_in_bytes) <= 0) {
		mutex_unlock(&itv->udma.lock);
296
		IVTVFB_WARN("ivtvfb_prep_dec_dma_to_device, "
297 298 299 300 301 302 303
			       "Error with get_user_pages: %d bytes, %d pages returned\n",
			       size_in_bytes, itv->udma.page_count);

		/* get_user_pages must have failed completely */
		return -EIO;
	}

304
	IVTVFB_DEBUG_INFO("ivtvfb_prep_dec_dma_to_device, %d bytes, %d pages\n",
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
		       size_in_bytes, itv->udma.page_count);

	ivtv_udma_prepare(itv);
	prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
	/* if no UDMA is pending and no UDMA is in progress, then the DMA
	   is finished */
	while (itv->i_flags & (IVTV_F_I_UDMA_PENDING | IVTV_F_I_UDMA)) {
		/* don't interrupt if the DMA is in progress but break off
		   a still pending DMA. */
		got_sig = signal_pending(current);
		if (got_sig && test_and_clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
			break;
		got_sig = 0;
		schedule();
	}
	finish_wait(&itv->dma_waitq, &wait);

	/* Unmap Last DMA Xfer */
	ivtv_udma_unmap(itv);
	mutex_unlock(&itv->udma.lock);
	if (got_sig) {
		IVTV_DEBUG_INFO("User stopped OSD\n");
		return -EINTR;
	}

	return ret;
}

333
static int ivtvfb_prep_frame(struct ivtv *itv, int cmd, void __user *source,
H
Hans Verkuil 已提交
334
			      unsigned long dest_offset, int count)
335 336
{
	DEFINE_WAIT(wait);
337
	struct osd_info *oi = itv->osd_info;
338 339 340

	/* Nothing to do */
	if (count == 0) {
341
		IVTVFB_DEBUG_WARN("ivtvfb_prep_frame: Nothing to do. count = 0\n");
342 343 344 345
		return -EINVAL;
	}

	/* Check Total FB Size */
346
	if ((dest_offset + count) > oi->video_buffer_size) {
347
		IVTVFB_WARN("ivtvfb_prep_frame: Overflowing the framebuffer %ld, only %d available\n",
348
			dest_offset + count, oi->video_buffer_size);
349 350 351 352 353
		return -E2BIG;
	}

	/* Not fatal, but will have undesirable results */
	if ((unsigned long)source & 3)
354
		IVTVFB_WARN("ivtvfb_prep_frame: Source address not 32 bit aligned (0x%08lx)\n",
H
Hans Verkuil 已提交
355
			(unsigned long)source);
356 357

	if (dest_offset & 3)
358
		IVTVFB_WARN("ivtvfb_prep_frame: Dest offset not 32 bit aligned (%ld)\n", dest_offset);
359 360

	if (count & 3)
361
		IVTVFB_WARN("ivtvfb_prep_frame: Count not a multiple of 4 (%d)\n", count);
362 363 364

	/* Check Source */
	if (!access_ok(VERIFY_READ, source + dest_offset, count)) {
365
		IVTVFB_WARN("Invalid userspace pointer 0x%08lx\n",
366 367
			(unsigned long)source);

368
		IVTVFB_DEBUG_WARN("access_ok() failed for offset 0x%08lx source 0x%08lx count %d\n",
369 370 371 372 373 374
			dest_offset, (unsigned long)source,
			count);
		return -EINVAL;
	}

	/* OSD Address to send DMA to */
375
	dest_offset += IVTV_DECODER_OFFSET + oi->video_rbase;
376 377

	/* Fill Buffers */
378
	return ivtvfb_prep_dec_dma_to_device(itv, dest_offset, source, count);
379 380 381 382 383 384
}

static int ivtvfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
{
	DEFINE_WAIT(wait);
	struct ivtv *itv = (struct ivtv *)info->par;
H
Hans Verkuil 已提交
385
	int rc = 0;
386 387 388 389 390 391 392 393

	switch (cmd) {
		case FBIOGET_VBLANK: {
			struct fb_vblank vblank;
			u32 trace;

			vblank.flags = FB_VBLANK_HAVE_COUNT |FB_VBLANK_HAVE_VCOUNT |
					FB_VBLANK_HAVE_VSYNC;
H
Hans Verkuil 已提交
394
			trace = read_reg(0x028c0) >> 16;
395 396 397
			if (itv->is_50hz && trace > 312) trace -= 312;
			else if (itv->is_60hz && trace > 262) trace -= 262;
			if (trace == 1) vblank.flags |= FB_VBLANK_VSYNCING;
398
			vblank.count = itv->last_vsync_field;
399 400 401 402 403 404 405
			vblank.vcount = trace;
			vblank.hcount = 0;
			if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
				return -EFAULT;
			return 0;
		}

H
Hans Verkuil 已提交
406
		case FBIO_WAITFORVSYNC:
407
			prepare_to_wait(&itv->vsync_waitq, &wait, TASK_INTERRUPTIBLE);
408
			if (!schedule_timeout(msecs_to_jiffies(50))) rc = -ETIMEDOUT;
H
Hans Verkuil 已提交
409
			finish_wait(&itv->vsync_waitq, &wait);
410 411
			return rc;

412 413
		case IVTVFB_IOC_DMA_FRAME: {
			struct ivtvfb_dma_frame args;
414

415
			IVTVFB_DEBUG_INFO("IVTVFB_IOC_DMA_FRAME\n");
416 417 418
			if (copy_from_user(&args, (void __user *)arg, sizeof(args)))
				return -EFAULT;

419
			return ivtvfb_prep_frame(itv, cmd, args.source, args.dest_offset, args.count);
420 421 422
		}

		default:
423
			IVTVFB_DEBUG_INFO("Unknown ioctl %08x\n", cmd);
424 425 426 427 428 429 430 431 432
			return -EINVAL;
	}
	return 0;
}

/* Framebuffer device handling */

static int ivtvfb_set_var(struct ivtv *itv, struct fb_var_screeninfo *var)
{
433
	struct osd_info *oi = itv->osd_info;
434 435
	struct ivtv_osd_coords ivtv_osd;
	struct v4l2_rect ivtv_window;
436
	int osd_mode = -1;
437

438
	IVTVFB_DEBUG_INFO("ivtvfb_set_var\n");
439 440 441

	/* Select color space */
	if (var->nonstd) /* YUV */
H
Hans Verkuil 已提交
442
		write_reg(read_reg(0x02a00) | 0x0002000, 0x02a00);
443
	else /* RGB  */
H
Hans Verkuil 已提交
444
		write_reg(read_reg(0x02a00) & ~0x0002000, 0x02a00);
445

446
	/* Set the color mode */
447 448
	switch (var->bits_per_pixel) {
		case 8:
449
			osd_mode = IVTV_OSD_BPP_8;
450 451
			break;
		case 32:
452
			osd_mode = IVTV_OSD_BPP_32;
453 454 455 456
			break;
		case 16:
			switch (var->green.length) {
			case 4:
457
				osd_mode = IVTV_OSD_BPP_16_444;
458 459
				break;
			case 5:
460
				osd_mode = IVTV_OSD_BPP_16_555;
461 462
				break;
			case 6:
463
				osd_mode = IVTV_OSD_BPP_16_565;
464 465
				break;
			default:
466
				IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid bpp\n");
467 468 469
			}
			break;
		default:
470
			IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid bpp\n");
471 472
	}

473 474 475 476 477 478 479 480 481 482 483
	/* Change osd mode if needed.
	   Although rare, things can go wrong. The extra mode
	   change seems to help... */
	if (osd_mode != -1 && osd_mode != oi->osd_mode) {
		ivtv_vapi(itv, CX2341X_OSD_SET_PIXEL_FORMAT, 1, 0);
		ivtv_vapi(itv, CX2341X_OSD_SET_PIXEL_FORMAT, 1, osd_mode);
		oi->osd_mode = osd_mode;
	}

	oi->bits_per_pixel = var->bits_per_pixel;
	oi->bytes_per_pixel = var->bits_per_pixel / 8;
484 485 486 487 488 489 490 491 492 493

	/* Set the flicker filter */
	switch (var->vmode & FB_VMODE_MASK) {
		case FB_VMODE_NONINTERLACED: /* Filter on */
			ivtv_vapi(itv, CX2341X_OSD_SET_FLICKER_STATE, 1, 1);
			break;
		case FB_VMODE_INTERLACED: /* Filter off */
			ivtv_vapi(itv, CX2341X_OSD_SET_FLICKER_STATE, 1, 0);
			break;
		default:
494
			IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid video mode\n");
495 496 497
	}

	/* Read the current osd info */
498
	ivtvfb_get_osd_coords(itv, &ivtv_osd);
499 500 501 502 503 504

	/* Now set the OSD to the size we want */
	ivtv_osd.pixel_stride = var->xres_virtual;
	ivtv_osd.lines = var->yres_virtual;
	ivtv_osd.x = 0;
	ivtv_osd.y = 0;
505
	ivtvfb_set_osd_coords(itv, &ivtv_osd);
506 507 508 509 510 511 512

	/* Can't seem to find the right API combo for this.
	   Use another function which does what we need through direct register access. */
	ivtv_window.width = var->xres;
	ivtv_window.height = var->yres;

	/* Minimum margin cannot be 0, as X won't allow such a mode */
H
Hans Verkuil 已提交
513 514
	if (!var->upper_margin) var->upper_margin++;
	if (!var->left_margin) var->left_margin++;
515 516 517
	ivtv_window.top = var->upper_margin - 1;
	ivtv_window.left = var->left_margin - 1;

518
	ivtvfb_set_display_window(itv, &ivtv_window);
519 520 521 522

	/* Force update of yuv registers */
	itv->yuv_info.yuv_forced_update = 1;

523
	IVTVFB_DEBUG_INFO("Display size: %dx%d (virtual %dx%d) @ %dbpp\n",
H
Hans Verkuil 已提交
524 525 526
		      var->xres, var->yres,
		      var->xres_virtual, var->yres_virtual,
		      var->bits_per_pixel);
527

528
	IVTVFB_DEBUG_INFO("Display position: %d, %d\n",
H
Hans Verkuil 已提交
529
		      var->left_margin, var->upper_margin);
530

531
	IVTVFB_DEBUG_INFO("Display filter: %s\n",
H
Hans Verkuil 已提交
532
			(var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED ? "on" : "off");
533
	IVTVFB_DEBUG_INFO("Color space: %s\n", var->nonstd ? "YUV" : "RGB");
534 535 536 537 538 539

	return 0;
}

static int ivtvfb_get_fix(struct ivtv *itv, struct fb_fix_screeninfo *fix)
{
H
Hans Verkuil 已提交
540 541
	struct osd_info *oi = itv->osd_info;

542
	IVTVFB_DEBUG_INFO("ivtvfb_get_fix\n");
543 544
	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
	strcpy(fix->id, "cx23415 TV out");
H
Hans Verkuil 已提交
545 546
	fix->smem_start = oi->video_pbase;
	fix->smem_len = oi->video_buffer_size;
547
	fix->type = FB_TYPE_PACKED_PIXELS;
H
Hans Verkuil 已提交
548
	fix->visual = (oi->bits_per_pixel == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
549 550 551
	fix->xpanstep = 1;
	fix->ypanstep = 1;
	fix->ywrapstep = 0;
H
Hans Verkuil 已提交
552
	fix->line_length = oi->display_byte_stride;
553 554 555 556 557 558 559 560 561
	fix->accel = FB_ACCEL_NONE;
	return 0;
}

/* Check the requested display mode, returning -EINVAL if we can't
   handle it. */

static int _ivtvfb_check_var(struct fb_var_screeninfo *var, struct ivtv *itv)
{
H
Hans Verkuil 已提交
562
	struct osd_info *oi = itv->osd_info;
563 564
	int osd_height_limit;
	u32 pixclock, hlimit, vlimit;
565

566
	IVTVFB_DEBUG_INFO("ivtvfb_check_var\n");
567

568 569 570 571 572 573 574 575 576 577 578 579 580 581
	/* Set base references for mode calcs. */
	if (itv->is_50hz) {
		pixclock = 84316;
		hlimit = 776;
		vlimit = 591;
		osd_height_limit = 576;
	}
	else {
		pixclock = 83926;
		hlimit = 776;
		vlimit = 495;
		osd_height_limit = 480;
	}

582 583 584
	/* Check the bits per pixel */
	if (osd_compat) {
		if (var->bits_per_pixel != 32) {
585
			IVTVFB_DEBUG_WARN("Invalid colour mode: %d\n", var->bits_per_pixel);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
			return -EINVAL;
		}
	}

	if (var->bits_per_pixel == 8 || var->bits_per_pixel == 32) {
		var->transp.offset = 24;
		var->transp.length = 8;
		var->red.offset = 16;
		var->red.length = 8;
		var->green.offset = 8;
		var->green.length = 8;
		var->blue.offset = 0;
		var->blue.length = 8;
	}
	else if (var->bits_per_pixel == 16) {
		/* To find out the true mode, check green length */
		switch (var->green.length) {
			case 4:
				var->red.offset = 8;
				var->red.length = 4;
				var->green.offset = 4;
				var->green.length = 4;
				var->blue.offset = 0;
				var->blue.length = 4;
610 611
				var->transp.offset = 12;
				var->transp.length = 1;
612 613 614 615 616 617 618 619
				break;
			case 5:
				var->red.offset = 10;
				var->red.length = 5;
				var->green.offset = 5;
				var->green.length = 5;
				var->blue.offset = 0;
				var->blue.length = 5;
620 621
				var->transp.offset = 15;
				var->transp.length = 1;
622 623 624 625 626 627 628 629
				break;
			default:
				var->red.offset = 11;
				var->red.length = 5;
				var->green.offset = 5;
				var->green.length = 6;
				var->blue.offset = 0;
				var->blue.length = 5;
630 631
				var->transp.offset = 0;
				var->transp.length = 0;
632 633 634 635
				break;
		}
	}
	else {
636
		IVTVFB_DEBUG_WARN("Invalid colour mode: %d\n", var->bits_per_pixel);
637 638 639 640 641
		return -EINVAL;
	}

	/* Check the resolution */
	if (osd_compat) {
H
Hans Verkuil 已提交
642 643 644 645
		if (var->xres != oi->ivtvfb_defined.xres ||
		    var->yres != oi->ivtvfb_defined.yres ||
		    var->xres_virtual != oi->ivtvfb_defined.xres_virtual ||
		    var->yres_virtual != oi->ivtvfb_defined.yres_virtual) {
646
			IVTVFB_DEBUG_WARN("Invalid resolution: %dx%d (virtual %dx%d)\n",
H
Hans Verkuil 已提交
647
				var->xres, var->yres, var->xres_virtual, var->yres_virtual);
648 649 650 651
			return -EINVAL;
		}
	}
	else {
H
Hans Verkuil 已提交
652
		if (var->xres > IVTV_OSD_MAX_WIDTH || var->yres > osd_height_limit) {
653
			IVTVFB_DEBUG_WARN("Invalid resolution: %dx%d\n",
H
Hans Verkuil 已提交
654
					var->xres, var->yres);
655 656 657 658 659
			return -EINVAL;
		}

		/* Max horizontal size is 1023 @ 32bpp, 2046 & 16bpp, 4092 @ 8bpp */
		if (var->xres_virtual > 4095 / (var->bits_per_pixel / 8) ||
H
Hans Verkuil 已提交
660
		    var->xres_virtual * var->yres_virtual * (var->bits_per_pixel / 8) > oi->video_buffer_size ||
661 662
		    var->xres_virtual < var->xres ||
		    var->yres_virtual < var->yres) {
663
			IVTVFB_DEBUG_WARN("Invalid virtual resolution: %dx%d\n",
664 665 666 667 668 669 670 671 672
				var->xres_virtual, var->yres_virtual);
			return -EINVAL;
		}
	}

	/* Some extra checks if in 8 bit mode */
	if (var->bits_per_pixel == 8) {
		/* Width must be a multiple of 4 */
		if (var->xres & 3) {
673
			IVTVFB_DEBUG_WARN("Invalid resolution for 8bpp: %d\n", var->xres);
674 675 676
			return -EINVAL;
		}
		if (var->xres_virtual & 3) {
677
			IVTVFB_DEBUG_WARN("Invalid virtual resolution for 8bpp: %d)\n", var->xres_virtual);
678 679 680 681 682 683
			return -EINVAL;
		}
	}
	else if (var->bits_per_pixel == 16) {
		/* Width must be a multiple of 2 */
		if (var->xres & 1) {
684
			IVTVFB_DEBUG_WARN("Invalid resolution for 16bpp: %d\n", var->xres);
685 686 687
			return -EINVAL;
		}
		if (var->xres_virtual & 1) {
688
			IVTVFB_DEBUG_WARN("Invalid virtual resolution for 16bpp: %d)\n", var->xres_virtual);
689 690 691 692 693 694
			return -EINVAL;
		}
	}

	/* Now check the offsets */
	if (var->xoffset >= var->xres_virtual || var->yoffset >= var->yres_virtual) {
695
		IVTVFB_DEBUG_WARN("Invalid offset: %d (%d) %d (%d)\n",
H
Hans Verkuil 已提交
696
			var->xoffset, var->xres_virtual, var->yoffset, var->yres_virtual);
697 698 699 700 701
		return -EINVAL;
	}

	/* Check pixel format */
	if (var->nonstd > 1) {
702
		IVTVFB_DEBUG_WARN("Invalid nonstd % d\n", var->nonstd);
703 704 705 706 707 708
		return -EINVAL;
	}

	/* Check video mode */
	if (((var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED) &&
		((var->vmode & FB_VMODE_MASK) != FB_VMODE_INTERLACED)) {
709
		IVTVFB_DEBUG_WARN("Invalid video mode: %d\n", var->vmode & FB_VMODE_MASK);
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
		return -EINVAL;
	}

	/* Check the left & upper margins
	   If the margins are too large, just center the screen
	   (enforcing margins causes too many problems) */

	if (var->left_margin + var->xres > IVTV_OSD_MAX_WIDTH + 1) {
		var->left_margin = 1 + ((IVTV_OSD_MAX_WIDTH - var->xres) / 2);
	}
	if (var->upper_margin + var->yres > (itv->is_50hz ? 577 : 481)) {
		var->upper_margin = 1 + (((itv->is_50hz ? 576 : 480) - var->yres) / 2);
	}

	/* Maintain overall 'size' for a constant refresh rate */
725 726
	var->right_margin = hlimit - var->left_margin - var->xres;
	var->lower_margin = vlimit - var->upper_margin - var->yres;
727 728 729 730 731 732 733 734 735

	/* Fixed sync times */
	var->hsync_len = 24;
	var->vsync_len = 2;

	/* Non-interlaced / interlaced mode is used to switch the OSD filter
	   on or off. Adjust the clock timings to maintain a constant
	   vertical refresh rate. */
	if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED)
736 737 738
		var->pixclock = pixclock / 2;
	else
		var->pixclock = pixclock;
739

740
	IVTVFB_DEBUG_INFO("Display size: %dx%d (virtual %dx%d) @ %dbpp\n",
H
Hans Verkuil 已提交
741 742
		      var->xres, var->yres,
		      var->xres_virtual, var->yres_virtual,
743 744
		      var->bits_per_pixel);

745
	IVTVFB_DEBUG_INFO("Display position: %d, %d\n",
H
Hans Verkuil 已提交
746
		      var->left_margin, var->upper_margin);
747

748
	IVTVFB_DEBUG_INFO("Display filter: %s\n",
H
Hans Verkuil 已提交
749
			(var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED ? "on" : "off");
750
	IVTVFB_DEBUG_INFO("Color space: %s\n", var->nonstd ? "YUV" : "RGB");
751 752 753 754 755 756
	return 0;
}

static int ivtvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
{
	struct ivtv *itv = (struct ivtv *) info->par;
757
	IVTVFB_DEBUG_INFO("ivtvfb_check_var\n");
H
Hans Verkuil 已提交
758
	return _ivtvfb_check_var(var, itv);
759 760 761 762 763 764 765 766
}

static int ivtvfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
{
	u32 osd_pan_index;
	struct ivtv *itv = (struct ivtv *) info->par;

	osd_pan_index = (var->xoffset + (var->yoffset * var->xres_virtual))*var->bits_per_pixel/8;
H
Hans Verkuil 已提交
767
	write_reg(osd_pan_index, 0x02A0C);
768 769 770 771 772 773 774 775 776 777 778 779 780 781

	/* Pass this info back the yuv handler */
	itv->yuv_info.osd_x_pan = var->xoffset;
	itv->yuv_info.osd_y_pan = var->yoffset;
	/* Force update of yuv registers */
	itv->yuv_info.yuv_forced_update = 1;
	return 0;
}

static int ivtvfb_set_par(struct fb_info *info)
{
	int rc = 0;
	struct ivtv *itv = (struct ivtv *) info->par;

782
	IVTVFB_DEBUG_INFO("ivtvfb_set_par\n");
783 784 785

	rc = ivtvfb_set_var(itv, &info->var);
	ivtvfb_pan_display(&info->var, info);
H
Hans Verkuil 已提交
786
	ivtvfb_get_fix(itv, &info->fix);
787 788 789 790 791 792 793 794
	return rc;
}

static int ivtvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
				unsigned blue, unsigned transp,
				struct fb_info *info)
{
	u32 color, *palette;
H
Hans Verkuil 已提交
795
	struct ivtv *itv = (struct ivtv *)info->par;
796 797 798 799 800 801 802 803

	if (regno >= info->cmap.len)
		return -EINVAL;

	color = ((transp & 0xFF00) << 16) |((red & 0xFF00) << 8) | (green & 0xFF00) | ((blue & 0xFF00) >> 8);
	if (info->var.bits_per_pixel <= 8) {
		write_reg(regno, 0x02a30);
		write_reg(color, 0x02a34);
H
Hans Verkuil 已提交
804
		return 0;
805
	}
H
Hans Verkuil 已提交
806 807
	if (regno >= 16)
		return -EINVAL;
808

H
Hans Verkuil 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	palette = info->pseudo_palette;
	if (info->var.bits_per_pixel == 16) {
		switch (info->var.green.length) {
			case 4:
				color = ((red & 0xf000) >> 4) |
					((green & 0xf000) >> 8) |
					((blue & 0xf000) >> 12);
				break;
			case 5:
				color = ((red & 0xf800) >> 1) |
					((green & 0xf800) >> 6) |
					((blue & 0xf800) >> 11);
				break;
			case 6:
				color = (red & 0xf800 ) |
					((green & 0xfc00) >> 5) |
					((blue & 0xf800) >> 11);
				break;
827 828
		}
	}
H
Hans Verkuil 已提交
829
	palette[regno] = color;
830 831 832 833 834 835 836 837 838
	return 0;
}

/* We don't really support blanking. All this does is enable or
   disable the OSD. */
static int ivtvfb_blank(int blank_mode, struct fb_info *info)
{
	struct ivtv *itv = (struct ivtv *)info->par;

839
	IVTVFB_DEBUG_INFO("Set blanking mode : %d\n", blank_mode);
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
	switch (blank_mode) {
	case FB_BLANK_UNBLANK:
		ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, 1);
		break;
	case FB_BLANK_NORMAL:
	case FB_BLANK_HSYNC_SUSPEND:
	case FB_BLANK_VSYNC_SUSPEND:
	case FB_BLANK_POWERDOWN:
		ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, 0);
		break;
	}
	return 0;
}

static struct fb_ops ivtvfb_ops = {
	.owner = THIS_MODULE,
	.fb_check_var   = ivtvfb_check_var,
	.fb_set_par     = ivtvfb_set_par,
	.fb_setcolreg   = ivtvfb_setcolreg,
	.fb_fillrect    = cfb_fillrect,
	.fb_copyarea    = cfb_copyarea,
	.fb_imageblit   = cfb_imageblit,
	.fb_cursor      = NULL,
	.fb_ioctl       = ivtvfb_ioctl,
	.fb_pan_display = ivtvfb_pan_display,
	.fb_blank       = ivtvfb_blank,
};

/* Initialization */


/* Setup our initial video mode */
static int ivtvfb_init_vidmode(struct ivtv *itv)
{
H
Hans Verkuil 已提交
874
	struct osd_info *oi = itv->osd_info;
875
	struct v4l2_rect start_window;
H
Hans Verkuil 已提交
876
	int max_height;
877 878 879 880 881

	/* Color mode */

	if (osd_compat) osd_depth = 32;
	if (osd_depth != 8 && osd_depth != 16 && osd_depth != 32) osd_depth = 8;
H
Hans Verkuil 已提交
882 883
	oi->bits_per_pixel = osd_depth;
	oi->bytes_per_pixel = oi->bits_per_pixel / 8;
884

885 886 887
	/* Invalidate current osd mode to force a mode switch later */
	oi->osd_mode = -1;

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
	/* Horizontal size & position */

	if (osd_xres > 720) osd_xres = 720;

	/* Must be a multiple of 4 for 8bpp & 2 for 16bpp */
	if (osd_depth == 8)
		osd_xres &= ~3;
	else if (osd_depth == 16)
		osd_xres &= ~1;

	if (osd_xres)
		start_window.width = osd_xres;
	else
		start_window.width = osd_compat ? 720: 640;

	/* Check horizontal start (osd_left). */
	if (osd_left && osd_left + start_window.width > 721) {
905
		IVTVFB_ERR("Invalid osd_left - assuming default\n");
906 907 908 909
		osd_left = 0;
	}

	/* Hardware coords start at 0, user coords start at 1. */
H
Hans Verkuil 已提交
910
	osd_left--;
911

H
Hans Verkuil 已提交
912
	start_window.left = osd_left >= 0 ? osd_left : ((IVTV_OSD_MAX_WIDTH - start_window.width) / 2);
913

H
Hans Verkuil 已提交
914 915
	oi->display_byte_stride =
			start_window.width * oi->bytes_per_pixel;
916 917 918 919 920

	/* Vertical size & position */

	max_height = itv->is_50hz ? 576 : 480;

H
Hans Verkuil 已提交
921 922
	if (osd_yres > max_height)
		osd_yres = max_height;
923 924 925

	if (osd_yres)
		start_window.height = osd_yres;
H
Hans Verkuil 已提交
926 927
	else
		start_window.height = osd_compat ? max_height : (itv->is_50hz ? 480 : 400);
928 929 930

	/* Check vertical start (osd_upper). */
	if (osd_upper + start_window.height > max_height + 1) {
931
		IVTVFB_ERR("Invalid osd_upper - assuming default\n");
932 933 934 935
		osd_upper = 0;
	}

	/* Hardware coords start at 0, user coords start at 1. */
H
Hans Verkuil 已提交
936
	osd_upper--;
937 938 939

	start_window.top = osd_upper >= 0 ? osd_upper : ((max_height - start_window.height) / 2);

H
Hans Verkuil 已提交
940 941
	oi->display_width = start_window.width;
	oi->display_height = start_window.height;
942 943 944

	/* Generate a valid fb_var_screeninfo */

H
Hans Verkuil 已提交
945 946 947 948 949 950 951 952 953 954
	oi->ivtvfb_defined.xres = oi->display_width;
	oi->ivtvfb_defined.yres = oi->display_height;
	oi->ivtvfb_defined.xres_virtual = oi->display_width;
	oi->ivtvfb_defined.yres_virtual = oi->display_height;
	oi->ivtvfb_defined.bits_per_pixel = oi->bits_per_pixel;
	oi->ivtvfb_defined.vmode = (osd_laced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED);
	oi->ivtvfb_defined.left_margin = start_window.left + 1;
	oi->ivtvfb_defined.upper_margin = start_window.top + 1;
	oi->ivtvfb_defined.accel_flags = FB_ACCEL_NONE;
	oi->ivtvfb_defined.nonstd = 0;
955 956 957

	/* We've filled in the most data, let the usual mode check
	   routine fill in the rest. */
H
Hans Verkuil 已提交
958
	_ivtvfb_check_var(&oi->ivtvfb_defined, itv);
959 960 961

	/* Generate valid fb_fix_screeninfo */

H
Hans Verkuil 已提交
962
	ivtvfb_get_fix(itv, &oi->ivtvfb_fix);
963 964 965

	/* Generate valid fb_info */

H
Hans Verkuil 已提交
966 967 968 969 970 971 972 973
	oi->ivtvfb_info.node = -1;
	oi->ivtvfb_info.flags = FBINFO_FLAG_DEFAULT;
	oi->ivtvfb_info.fbops = &ivtvfb_ops;
	oi->ivtvfb_info.par = itv;
	oi->ivtvfb_info.var = oi->ivtvfb_defined;
	oi->ivtvfb_info.fix = oi->ivtvfb_fix;
	oi->ivtvfb_info.screen_base = (u8 __iomem *)oi->video_vbase;
	oi->ivtvfb_info.fbops = &ivtvfb_ops;
974 975

	/* Supply some monitor specs. Bogus values will do for now */
H
Hans Verkuil 已提交
976 977 978 979
	oi->ivtvfb_info.monspecs.hfmin = 8000;
	oi->ivtvfb_info.monspecs.hfmax = 70000;
	oi->ivtvfb_info.monspecs.vfmin = 10;
	oi->ivtvfb_info.monspecs.vfmax = 100;
980 981

	/* Allocate color map */
H
Hans Verkuil 已提交
982
	if (fb_alloc_cmap(&oi->ivtvfb_info.cmap, 256, 1)) {
983
		IVTVFB_ERR("abort, unable to alloc cmap\n");
984 985 986 987
		return -ENOMEM;
	}

	/* Allocate the pseudo palette */
H
Hans Verkuil 已提交
988
	oi->ivtvfb_info.pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
989

H
Hans Verkuil 已提交
990
	if (!oi->ivtvfb_info.pseudo_palette) {
991
		IVTVFB_ERR("abort, unable to alloc pseudo pallete\n");
992 993 994 995 996 997 998 999 1000 1001
		return -ENOMEM;
	}

	return 0;
}

/* Find OSD buffer base & size. Add to mtrr. Zero osd buffer. */

static int ivtvfb_init_io(struct ivtv *itv)
{
H
Hans Verkuil 已提交
1002 1003
	struct osd_info *oi = itv->osd_info;

1004
	mutex_lock(&itv->serialize_lock);
1005
	if (ivtv_init_on_first_open(itv)) {
1006
		mutex_unlock(&itv->serialize_lock);
1007
		IVTVFB_ERR("Failed to initialize ivtv\n");
1008 1009
		return -ENXIO;
	}
1010
	mutex_unlock(&itv->serialize_lock);
1011

1012
	ivtvfb_get_framebuffer(itv, &oi->video_rbase, &oi->video_buffer_size);
1013 1014 1015 1016

	/* The osd buffer size depends on the number of video buffers allocated
	   on the PVR350 itself. For now we'll hardcode the smallest osd buffer
	   size to prevent any overlap. */
H
Hans Verkuil 已提交
1017
	oi->video_buffer_size = 1704960;
1018

H
Hans Verkuil 已提交
1019 1020
	oi->video_pbase = itv->base_addr + IVTV_DECODER_OFFSET + oi->video_rbase;
	oi->video_vbase = itv->dec_mem + oi->video_rbase;
1021

H
Hans Verkuil 已提交
1022
	if (!oi->video_vbase) {
1023
		IVTVFB_ERR("abort, video memory 0x%x @ 0x%lx isn't mapped!\n",
H
Hans Verkuil 已提交
1024
		     oi->video_buffer_size, oi->video_pbase);
1025 1026 1027
		return -EIO;
	}

1028
	IVTVFB_INFO("Framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
H
Hans Verkuil 已提交
1029 1030
			oi->video_pbase, oi->video_vbase,
			oi->video_buffer_size / 1024);
1031 1032 1033 1034 1035 1036

#ifdef CONFIG_MTRR
	{
		/* Find the largest power of two that maps the whole buffer */
		int size_shift = 31;

H
Hans Verkuil 已提交
1037
		while (!(oi->video_buffer_size & (1 << size_shift))) {
1038 1039 1040
			size_shift--;
		}
		size_shift++;
H
Hans Verkuil 已提交
1041 1042 1043 1044 1045 1046
		oi->fb_start_aligned_physaddr = oi->video_pbase & ~((1 << size_shift) - 1);
		oi->fb_end_aligned_physaddr = oi->video_pbase + oi->video_buffer_size;
		oi->fb_end_aligned_physaddr += (1 << size_shift) - 1;
		oi->fb_end_aligned_physaddr &= ~((1 << size_shift) - 1);
		if (mtrr_add(oi->fb_start_aligned_physaddr,
			oi->fb_end_aligned_physaddr - oi->fb_start_aligned_physaddr,
1047
			     MTRR_TYPE_WRCOMB, 1) < 0) {
1048
			IVTVFB_INFO("disabled mttr\n");
H
Hans Verkuil 已提交
1049 1050
			oi->fb_start_aligned_physaddr = 0;
			oi->fb_end_aligned_physaddr = 0;
1051 1052
		}
	}
H
Hans Verkuil 已提交
1053
#endif
1054 1055

	/* Blank the entire osd. */
H
Hans Verkuil 已提交
1056
	memset_io(oi->video_vbase, 0, oi->video_buffer_size);
1057 1058 1059 1060 1061 1062 1063

	return 0;
}

/* Release any memory we've grabbed & remove mtrr entry */
static void ivtvfb_release_buffers (struct ivtv *itv)
{
H
Hans Verkuil 已提交
1064 1065
	struct osd_info *oi = itv->osd_info;

1066
	/* Release cmap */
1067 1068
	if (oi->ivtvfb_info.cmap.len)
		fb_dealloc_cmap(&oi->ivtvfb_info.cmap);
1069 1070

	/* Release pseudo palette */
H
Hans Verkuil 已提交
1071 1072
	if (oi->ivtvfb_info.pseudo_palette)
		kfree(oi->ivtvfb_info.pseudo_palette);
1073 1074

#ifdef CONFIG_MTRR
H
Hans Verkuil 已提交
1075 1076 1077 1078 1079
	if (oi->fb_end_aligned_physaddr) {
		mtrr_del(-1, oi->fb_start_aligned_physaddr,
			oi->fb_end_aligned_physaddr - oi->fb_start_aligned_physaddr);
	}
#endif
1080

H
Hans Verkuil 已提交
1081
	kfree(oi);
1082 1083 1084 1085 1086
	itv->osd_info = NULL;
}

/* Initialize the specified card */

H
Hans Verkuil 已提交
1087
static int ivtvfb_init_card(struct ivtv *itv)
1088 1089 1090 1091
{
	int rc;

	if (itv->osd_info) {
1092
		IVTVFB_ERR("Card %d already initialised\n", ivtvfb_card_id);
1093 1094 1095 1096 1097
		return -EBUSY;
	}

	itv->osd_info = kzalloc(sizeof(struct osd_info), GFP_ATOMIC);
	if (itv->osd_info == 0) {
1098
		IVTVFB_ERR("Failed to allocate memory for osd_info\n");
1099 1100 1101 1102
		return -ENOMEM;
	}

	/* Find & setup the OSD buffer */
H
Hans Verkuil 已提交
1103
	if ((rc = ivtvfb_init_io(itv)))
1104 1105 1106
		return rc;

	/* Set the startup video mode information */
H
Hans Verkuil 已提交
1107
	if ((rc = ivtvfb_init_vidmode(itv))) {
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
		ivtvfb_release_buffers(itv);
		return rc;
	}

	/* Register the framebuffer */
	if (register_framebuffer(&itv->osd_info->ivtvfb_info) < 0) {
		ivtvfb_release_buffers(itv);
		return -EINVAL;
	}

	itv->osd_video_pbase = itv->osd_info->video_pbase;

	/* Set the card to the requested mode */
	ivtvfb_set_par(&itv->osd_info->ivtvfb_info);

	/* Set color 0 to black */
	write_reg(0, 0x02a30);
	write_reg(0, 0x02a34);

	/* Enable the osd */
	ivtvfb_blank(FB_BLANK_UNBLANK, &itv->osd_info->ivtvfb_info);

	/* Note if we're running in compatibility mode */
	if (osd_compat)
1132
		IVTVFB_INFO("Running in compatibility mode. Display resize & mode change disabled\n");
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

	/* Allocate DMA */
	ivtv_udma_alloc(itv);
	return 0;

}

static int __init ivtvfb_init(void)
{
	struct ivtv *itv;
	int i, registered = 0;

1145 1146
	if (ivtvfb_card_id < -1 || ivtvfb_card_id >= IVTV_MAX_CARDS) {
		printk(KERN_ERR "ivtvfb:  ivtvfb_card_id parameter is out of range (valid range: -1 - %d)\n",
1147 1148 1149 1150 1151 1152
		     IVTV_MAX_CARDS - 1);
		return -EINVAL;
	}

	/* Locate & initialise all cards supporting an OSD. */
	for (i = 0; i < ivtv_cards_active; i++) {
1153
		if (ivtvfb_card_id != -1 && i != ivtvfb_card_id)
1154 1155 1156 1157
			continue;
		itv = ivtv_cards[i];
		if (itv && (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
			if (ivtvfb_init_card(itv) == 0) {
1158
				IVTVFB_INFO("Framebuffer registered on ivtv card id %d\n", i);
1159 1160 1161 1162 1163
				registered++;
			}
		}
	}
	if (!registered) {
1164
		printk(KERN_ERR "ivtvfb:  no cards found");
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
		return -ENODEV;
	}
	return 0;
}

static void ivtvfb_cleanup(void)
{
	struct ivtv *itv;
	int i;

1175
	printk(KERN_INFO "ivtvfb:  Unloading framebuffer module\n");
1176 1177 1178 1179

	for (i = 0; i < ivtv_cards_active; i++) {
		itv = ivtv_cards[i];
		if (itv && (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) && itv->osd_info) {
1180
			IVTVFB_DEBUG_INFO("Unregister framebuffer %d\n", i);
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
			ivtvfb_blank(FB_BLANK_POWERDOWN, &itv->osd_info->ivtvfb_info);
			unregister_framebuffer(&itv->osd_info->ivtvfb_info);
			ivtvfb_release_buffers(itv);
			itv->osd_video_pbase = 0;
		}
	}
}

module_init(ivtvfb_init);
module_exit(ivtvfb_cleanup);