1/*
2 *
3 *  Support for a cx23416 mpeg encoder via cx2388x host port.
4 *  "blackbird" reference design.
5 *
6 *    (c) 2004 Jelle Foks <jelle@foks.8m.com>
7 *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
8 *
9 *    (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10 *        - video_ioctl2 conversion
11 *
12 *  Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
13 *
14 *  This program is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License as published by
16 *  the Free Software Foundation; either version 2 of the License, or
17 *  (at your option) any later version.
18 *
19 *  This program is distributed in the hope that it will be useful,
20 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 *  GNU General Public License for more details.
23 *
24 *  You should have received a copy of the GNU General Public License
25 *  along with this program; if not, write to the Free Software
26 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/init.h>
32#include <linux/fs.h>
33#include <linux/delay.h>
34#include <linux/device.h>
35#include <linux/firmware.h>
36#include <media/v4l2-common.h>
37#include <media/cx2341x.h>
38
39#include "cx88.h"
40
41MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
42MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
43MODULE_LICENSE("GPL");
44
45static unsigned int mpegbufs = 32;
46module_param(mpegbufs,int,0644);
47MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
48
49static unsigned int debug = 0;
50module_param(debug,int,0644);
51MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
52
53#define dprintk(level,fmt, arg...)	if (debug >= level) \
54	printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
55
56
57/* ------------------------------------------------------------------ */
58
59#define OLD_BLACKBIRD_FIRM_IMAGE_SIZE 262144
60#define     BLACKBIRD_FIRM_IMAGE_SIZE 376836
61
62/* defines below are from ivtv-driver.h */
63
64#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
65
66/* Firmware API commands */
67#define IVTV_API_STD_TIMEOUT 500
68
69enum blackbird_capture_type {
70	BLACKBIRD_MPEG_CAPTURE,
71	BLACKBIRD_RAW_CAPTURE,
72	BLACKBIRD_RAW_PASSTHRU_CAPTURE
73};
74enum blackbird_capture_bits {
75	BLACKBIRD_RAW_BITS_NONE             = 0x00,
76	BLACKBIRD_RAW_BITS_YUV_CAPTURE      = 0x01,
77	BLACKBIRD_RAW_BITS_PCM_CAPTURE      = 0x02,
78	BLACKBIRD_RAW_BITS_VBI_CAPTURE      = 0x04,
79	BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
80	BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE  = 0x10
81};
82enum blackbird_capture_end {
83	BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
84	BLACKBIRD_END_NOW, /* stop immediately, no irq */
85};
86enum blackbird_framerate {
87	BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
88	BLACKBIRD_FRAMERATE_PAL_25   /* PAL: 25fps */
89};
90enum blackbird_stream_port {
91	BLACKBIRD_OUTPUT_PORT_MEMORY,
92	BLACKBIRD_OUTPUT_PORT_STREAMING,
93	BLACKBIRD_OUTPUT_PORT_SERIAL
94};
95enum blackbird_data_xfer_status {
96	BLACKBIRD_MORE_BUFFERS_FOLLOW,
97	BLACKBIRD_LAST_BUFFER,
98};
99enum blackbird_picture_mask {
100	BLACKBIRD_PICTURE_MASK_NONE,
101	BLACKBIRD_PICTURE_MASK_I_FRAMES,
102	BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
103	BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
104};
105enum blackbird_vbi_mode_bits {
106	BLACKBIRD_VBI_BITS_SLICED,
107	BLACKBIRD_VBI_BITS_RAW,
108};
109enum blackbird_vbi_insertion_bits {
110	BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
111	BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
112	BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
113	BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
114	BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
115};
116enum blackbird_dma_unit {
117	BLACKBIRD_DMA_BYTES,
118	BLACKBIRD_DMA_FRAMES,
119};
120enum blackbird_dma_transfer_status_bits {
121	BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
122	BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
123	BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
124};
125enum blackbird_pause {
126	BLACKBIRD_PAUSE_ENCODING,
127	BLACKBIRD_RESUME_ENCODING,
128};
129enum blackbird_copyright {
130	BLACKBIRD_COPYRIGHT_OFF,
131	BLACKBIRD_COPYRIGHT_ON,
132};
133enum blackbird_notification_type {
134	BLACKBIRD_NOTIFICATION_REFRESH,
135};
136enum blackbird_notification_status {
137	BLACKBIRD_NOTIFICATION_OFF,
138	BLACKBIRD_NOTIFICATION_ON,
139};
140enum blackbird_notification_mailbox {
141	BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
142};
143enum blackbird_field1_lines {
144	BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
145	BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
146	BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
147};
148enum blackbird_field2_lines {
149	BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
150	BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
151	BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
152};
153enum blackbird_custom_data_type {
154	BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
155	BLACKBIRD_CUSTOM_PRIVATE_PACKET,
156};
157enum blackbird_mute {
158	BLACKBIRD_UNMUTE,
159	BLACKBIRD_MUTE,
160};
161enum blackbird_mute_video_mask {
162	BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
163	BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
164	BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
165};
166enum blackbird_mute_video_shift {
167	BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
168	BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
169	BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
170};
171
172/* Registers */
173#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
174#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
175#define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
176#define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
177#define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
178#define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
179
180/* ------------------------------------------------------------------ */
181
182static void host_setup(struct cx88_core *core)
183{
184	/* toggle reset of the host */
185	cx_write(MO_GPHST_SOFT_RST, 1);
186	udelay(100);
187	cx_write(MO_GPHST_SOFT_RST, 0);
188	udelay(100);
189
190	/* host port setup */
191	cx_write(MO_GPHST_WSC, 0x44444444U);
192	cx_write(MO_GPHST_XFR, 0);
193	cx_write(MO_GPHST_WDTH, 15);
194	cx_write(MO_GPHST_HDSHK, 0);
195	cx_write(MO_GPHST_MUX16, 0x44448888U);
196	cx_write(MO_GPHST_MODE, 0);
197}
198
199/* ------------------------------------------------------------------ */
200
201#define P1_MDATA0 0x390000
202#define P1_MDATA1 0x390001
203#define P1_MDATA2 0x390002
204#define P1_MDATA3 0x390003
205#define P1_MADDR2 0x390004
206#define P1_MADDR1 0x390005
207#define P1_MADDR0 0x390006
208#define P1_RDATA0 0x390008
209#define P1_RDATA1 0x390009
210#define P1_RDATA2 0x39000A
211#define P1_RDATA3 0x39000B
212#define P1_RADDR0 0x39000C
213#define P1_RADDR1 0x39000D
214#define P1_RRDWR  0x39000E
215
216static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
217{
218	unsigned long timeout = jiffies + msecs_to_jiffies(1);
219	u32 gpio0,need;
220
221	need = state ? 2 : 0;
222	for (;;) {
223		gpio0 = cx_read(MO_GP0_IO) & 2;
224		if (need == gpio0)
225			return 0;
226		if (time_after(jiffies,timeout))
227			return -1;
228		udelay(1);
229	}
230}
231
232static int memory_write(struct cx88_core *core, u32 address, u32 value)
233{
234	/* Warning: address is dword address (4 bytes) */
235	cx_writeb(P1_MDATA0, (unsigned int)value);
236	cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
237	cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
238	cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
239	cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
240	cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
241	cx_writeb(P1_MADDR0, (unsigned int)address);
242	cx_read(P1_MDATA0);
243	cx_read(P1_MADDR0);
244
245	return wait_ready_gpio0_bit1(core,1);
246}
247
248static int memory_read(struct cx88_core *core, u32 address, u32 *value)
249{
250	int retval;
251	u32 val;
252
253	/* Warning: address is dword address (4 bytes) */
254	cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
255	cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
256	cx_writeb(P1_MADDR0, (unsigned int)address);
257	cx_read(P1_MADDR0);
258
259	retval = wait_ready_gpio0_bit1(core,1);
260
261	cx_writeb(P1_MDATA3, 0);
262	val     = (unsigned char)cx_read(P1_MDATA3) << 24;
263	cx_writeb(P1_MDATA2, 0);
264	val    |= (unsigned char)cx_read(P1_MDATA2) << 16;
265	cx_writeb(P1_MDATA1, 0);
266	val    |= (unsigned char)cx_read(P1_MDATA1) << 8;
267	cx_writeb(P1_MDATA0, 0);
268	val    |= (unsigned char)cx_read(P1_MDATA0);
269
270	*value  = val;
271	return retval;
272}
273
274static int register_write(struct cx88_core *core, u32 address, u32 value)
275{
276	cx_writeb(P1_RDATA0, (unsigned int)value);
277	cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
278	cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
279	cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
280	cx_writeb(P1_RADDR0, (unsigned int)address);
281	cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
282	cx_writeb(P1_RRDWR, 1);
283	cx_read(P1_RDATA0);
284	cx_read(P1_RADDR0);
285
286	return wait_ready_gpio0_bit1(core,1);
287}
288
289
290static int register_read(struct cx88_core *core, u32 address, u32 *value)
291{
292	int retval;
293	u32 val;
294
295	cx_writeb(P1_RADDR0, (unsigned int)address);
296	cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
297	cx_writeb(P1_RRDWR, 0);
298	cx_read(P1_RADDR0);
299
300	retval  = wait_ready_gpio0_bit1(core,1);
301	val     = (unsigned char)cx_read(P1_RDATA0);
302	val    |= (unsigned char)cx_read(P1_RDATA1) << 8;
303	val    |= (unsigned char)cx_read(P1_RDATA2) << 16;
304	val    |= (unsigned char)cx_read(P1_RDATA3) << 24;
305
306	*value  = val;
307	return retval;
308}
309
310/* ------------------------------------------------------------------ */
311
312static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
313{
314	struct cx8802_dev *dev = priv;
315	unsigned long timeout;
316	u32 value, flag, retval;
317	int i;
318
319	dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
320
321	/* this may not be 100% safe if we can't read any memory location
322	   without side effects */
323	memory_read(dev->core, dev->mailbox - 4, &value);
324	if (value != 0x12345678) {
325		dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
326		return -1;
327	}
328
329	memory_read(dev->core, dev->mailbox, &flag);
330	if (flag) {
331		dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
332		return -1;
333	}
334
335	flag |= 1; /* tell 'em we're working on it */
336	memory_write(dev->core, dev->mailbox, flag);
337
338	/* write command + args + fill remaining with zeros */
339	memory_write(dev->core, dev->mailbox + 1, command); /* command code */
340	memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
341	for (i = 0; i < in; i++) {
342		memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
343		dprintk(1, "API Input %d = %d\n", i, data[i]);
344	}
345	for (; i < CX2341X_MBOX_MAX_DATA; i++)
346		memory_write(dev->core, dev->mailbox + 4 + i, 0);
347
348	flag |= 3; /* tell 'em we're done writing */
349	memory_write(dev->core, dev->mailbox, flag);
350
351	/* wait for firmware to handle the API command */
352	timeout = jiffies + msecs_to_jiffies(10);
353	for (;;) {
354		memory_read(dev->core, dev->mailbox, &flag);
355		if (0 != (flag & 4))
356			break;
357		if (time_after(jiffies,timeout)) {
358			dprintk(0, "ERROR: API Mailbox timeout\n");
359			return -1;
360		}
361		udelay(10);
362	}
363
364	/* read output values */
365	for (i = 0; i < out; i++) {
366		memory_read(dev->core, dev->mailbox + 4 + i, data + i);
367		dprintk(1, "API Output %d = %d\n", i, data[i]);
368	}
369
370	memory_read(dev->core, dev->mailbox + 2, &retval);
371	dprintk(1, "API result = %d\n",retval);
372
373	flag = 0;
374	memory_write(dev->core, dev->mailbox, flag);
375	return retval;
376}
377/* ------------------------------------------------------------------ */
378
379/* We don't need to call the API often, so using just one mailbox will probably suffice */
380static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
381			     u32 inputcnt, u32 outputcnt, ...)
382{
383	u32 data[CX2341X_MBOX_MAX_DATA];
384	va_list vargs;
385	int i, err;
386
387	va_start(vargs, outputcnt);
388
389	for (i = 0; i < inputcnt; i++) {
390		data[i] = va_arg(vargs, int);
391	}
392	err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
393	for (i = 0; i < outputcnt; i++) {
394		int *vptr = va_arg(vargs, int *);
395		*vptr = data[i];
396	}
397	va_end(vargs);
398	return err;
399}
400
401static int blackbird_find_mailbox(struct cx8802_dev *dev)
402{
403	u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
404	int signaturecnt=0;
405	u32 value;
406	int i;
407
408	for (i = 0; i < dev->fw_size; i++) {
409		memory_read(dev->core, i, &value);
410		if (value == signature[signaturecnt])
411			signaturecnt++;
412		else
413			signaturecnt = 0;
414		if (4 == signaturecnt) {
415			dprintk(1, "Mailbox signature found\n");
416			return i+1;
417		}
418	}
419	dprintk(0, "Mailbox signature values not found!\n");
420	return -1;
421}
422
423static int blackbird_load_firmware(struct cx8802_dev *dev)
424{
425	static const unsigned char magic[8] = {
426		0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
427	};
428	const struct firmware *firmware;
429	int i, retval = 0;
430	u32 value = 0;
431	u32 checksum = 0;
432	u32 *dataptr;
433
434	retval  = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
435	retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
436	retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
437	retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
438	msleep(1);
439	retval |= register_write(dev->core, IVTV_REG_APU, 0);
440
441	if (retval < 0)
442		dprintk(0, "Error with register_write\n");
443
444	retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
445				  &dev->pci->dev);
446
447
448	if (retval != 0) {
449		dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
450			CX2341X_FIRM_ENC_FILENAME);
451		dprintk(0, "Please fix your hotplug setup, the board will "
452			"not work without firmware loaded!\n");
453		return -1;
454	}
455
456	if ((firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) &&
457	    (firmware->size != OLD_BLACKBIRD_FIRM_IMAGE_SIZE)) {
458		dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d or %d)\n",
459			firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE,
460			OLD_BLACKBIRD_FIRM_IMAGE_SIZE);
461		release_firmware(firmware);
462		return -1;
463	}
464	dev->fw_size = firmware->size;
465
466	if (0 != memcmp(firmware->data, magic, 8)) {
467		dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
468		release_firmware(firmware);
469		return -1;
470	}
471
472	/* transfer to the chip */
473	dprintk(1,"Loading firmware ...\n");
474	dataptr = (u32*)firmware->data;
475	for (i = 0; i < (firmware->size >> 2); i++) {
476		value = *dataptr;
477		checksum += ~value;
478		memory_write(dev->core, i, value);
479		dataptr++;
480	}
481
482	/* read back to verify with the checksum */
483	for (i--; i >= 0; i--) {
484		memory_read(dev->core, i, &value);
485		checksum -= ~value;
486	}
487	if (checksum) {
488		dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
489		release_firmware(firmware);
490		return -1;
491	}
492	release_firmware(firmware);
493	dprintk(0, "Firmware upload successful.\n");
494
495	retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
496	retval |= register_read(dev->core, IVTV_REG_SPU, &value);
497	retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
498	msleep(1);
499
500	retval |= register_read(dev->core, IVTV_REG_VPU, &value);
501	retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
502
503	if (retval < 0)
504		dprintk(0, "Error with register_write\n");
505	return 0;
506}
507
508/**
509 Settings used by the windows tv app for PVR2000:
510=================================================================================================================
511Profile | Codec | Resolution | CBR/VBR | Video Qlty   | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
512-----------------------------------------------------------------------------------------------------------------
513MPEG-1  | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 2000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
514MPEG-2  | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 4000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
515VCD     | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 1150 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
516DVD     | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
517DB* DVD | MPEG2 | 720x576PAL | CBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
518=================================================================================================================
519*DB: "DirectBurn"
520*/
521
522static void blackbird_codec_settings(struct cx8802_dev *dev)
523{
524	/* assign frame size */
525	blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
526				dev->height, dev->width);
527
528	dev->params.width = dev->width;
529	dev->params.height = dev->height;
530	dev->params.is_50hz = (dev->core->tvnorm & V4L2_STD_625_50) != 0;
531
532	cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params);
533}
534
535static struct v4l2_mpeg_compression default_mpeg_params = {
536	.st_type          = V4L2_MPEG_PS_2,
537	.st_bitrate       = {
538		.mode     = V4L2_BITRATE_CBR,
539		.min      = 0,
540		.target   = 0,
541		.max      = 0
542	},
543	.ts_pid_pmt       = 16,
544	.ts_pid_audio     = 260,
545	.ts_pid_video     = 256,
546	.ts_pid_pcr       = 259,
547	.ps_size          = 0,
548	.au_type          = V4L2_MPEG_AU_2_II,
549	.au_bitrate       = {
550		.mode     = V4L2_BITRATE_CBR,
551		.min      = 224,
552		.target   = 224,
553		.max      = 224
554	},
555	.au_sample_rate    = 48000,
556	.au_pesid          = 0,
557	.vi_type           = V4L2_MPEG_VI_2,
558	.vi_aspect_ratio   = V4L2_MPEG_ASPECT_4_3,
559	.vi_bitrate        = {
560		.mode      = V4L2_BITRATE_CBR,
561		.min       = 4000,
562		.target    = 4500,
563		.max       = 6000
564	},
565	.vi_frame_rate     = 25,
566	.vi_frames_per_gop = 12,
567	.vi_bframes_count  = 2,
568	.vi_pesid          = 0,
569	.closed_gops       = 1,
570	.pulldown          = 0
571};
572
573static int blackbird_initialize_codec(struct cx8802_dev *dev)
574{
575	struct cx88_core *core = dev->core;
576	int version;
577	int retval;
578
579	dprintk(1,"Initialize codec\n");
580	retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
581	if (retval < 0) {
582		/* ping was not successful, reset and upload firmware */
583		cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
584		msleep(1);
585		cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
586		msleep(1);
587		retval = blackbird_load_firmware(dev);
588		if (retval < 0)
589			return retval;
590
591		dev->mailbox = blackbird_find_mailbox(dev);
592		if (dev->mailbox < 0)
593			return -1;
594
595		retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
596		if (retval < 0) {
597			dprintk(0, "ERROR: Firmware ping failed!\n");
598			return -1;
599		}
600
601		retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
602		if (retval < 0) {
603			dprintk(0, "ERROR: Firmware get encoder version failed!\n");
604			return -1;
605		}
606		dprintk(0, "Firmware version is 0x%08x\n", version);
607	}
608	msleep(1);
609
610	cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
611	cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
612	cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
613	cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
614
615	blackbird_codec_settings(dev);
616	msleep(1);
617
618	/* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
619	   blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
620	   blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
621	blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
622			BLACKBIRD_FIELD1_SAA7115,
623			BLACKBIRD_FIELD2_SAA7115
624		);
625
626	/* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
627	blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
628			BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
629			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
630
631	/* initialize the video input */
632	blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
633
634	msleep(1);
635
636	blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
637	msleep(1);
638	blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
639	msleep(1);
640
641	/* start capturing to the host interface */
642	/* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
643	blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
644			BLACKBIRD_MPEG_CAPTURE,
645			BLACKBIRD_RAW_BITS_NONE
646		);
647	msleep(10);
648
649	blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
650	return 0;
651}
652
653/* ------------------------------------------------------------------ */
654
655static int bb_buf_setup(struct videobuf_queue *q,
656			unsigned int *count, unsigned int *size)
657{
658	struct cx8802_fh *fh = q->priv_data;
659
660	fh->dev->ts_packet_size  = 188 * 4; /* was: 512 */
661	fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
662
663	*size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
664	*count = fh->dev->ts_packet_count;
665	return 0;
666}
667
668static int
669bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
670	       enum v4l2_field field)
671{
672	struct cx8802_fh *fh = q->priv_data;
673	return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
674}
675
676static void
677bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
678{
679	struct cx8802_fh *fh = q->priv_data;
680	cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
681}
682
683static void
684bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
685{
686	cx88_free_buffer(q, (struct cx88_buffer*)vb);
687}
688
689static struct videobuf_queue_ops blackbird_qops = {
690	.buf_setup    = bb_buf_setup,
691	.buf_prepare  = bb_buf_prepare,
692	.buf_queue    = bb_buf_queue,
693	.buf_release  = bb_buf_release,
694};
695
696/* ------------------------------------------------------------------ */
697
698static const u32 *ctrl_classes[] = {
699	cx88_user_ctrls,
700	cx2341x_mpeg_ctrls,
701	NULL
702};
703
704static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qctrl)
705{
706	qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
707	if (qctrl->id == 0)
708		return -EINVAL;
709
710	/* Standard V4L2 controls */
711	if (cx8800_ctrl_query(qctrl) == 0)
712		return 0;
713
714	/* MPEG V4L2 controls */
715	if (cx2341x_ctrl_query(&dev->params, qctrl))
716		qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
717	return 0;
718}
719
720/* ------------------------------------------------------------------ */
721/* IOCTL Handlers                                                     */
722
723static int vidioc_querymenu (struct file *file, void *priv,
724				struct v4l2_querymenu *qmenu)
725{
726	struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
727	struct v4l2_queryctrl qctrl;
728
729	qctrl.id = qmenu->id;
730	blackbird_queryctrl(dev, &qctrl);
731	return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
732}
733
734static int vidioc_querycap (struct file *file, void  *priv,
735					struct v4l2_capability *cap)
736{
737	struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
738	struct cx88_core  *core = dev->core;
739
740	strcpy(cap->driver, "cx88_blackbird");
741	strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card));
742	sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
743	cap->version = CX88_VERSION_CODE;
744	cap->capabilities =
745		V4L2_CAP_VIDEO_CAPTURE |
746		V4L2_CAP_READWRITE     |
747		V4L2_CAP_STREAMING;
748	if (UNSET != core->tuner_type)
749		cap->capabilities |= V4L2_CAP_TUNER;
750	return 0;
751}
752
753static int vidioc_enum_fmt_cap (struct file *file, void  *priv,
754					struct v4l2_fmtdesc *f)
755{
756	if (f->index != 0)
757		return -EINVAL;
758
759	strlcpy(f->description, "MPEG", sizeof(f->description));
760	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
761	f->pixelformat = V4L2_PIX_FMT_MPEG;
762	return 0;
763}
764
765static int vidioc_g_fmt_cap (struct file *file, void *priv,
766					struct v4l2_format *f)
767{
768	struct cx8802_fh  *fh   = priv;
769	struct cx8802_dev *dev  = fh->dev;
770
771	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
772	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
773	f->fmt.pix.bytesperline = 0;
774	f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
775	f->fmt.pix.colorspace   = 0;
776	f->fmt.pix.width        = dev->width;
777	f->fmt.pix.height       = dev->height;
778	f->fmt.pix.field        = fh->mpegq.field;
779	dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
780		dev->width, dev->height, fh->mpegq.field );
781	return 0;
782}
783
784static int vidioc_try_fmt_cap (struct file *file, void *priv,
785			struct v4l2_format *f)
786{
787	struct cx8802_fh  *fh   = priv;
788	struct cx8802_dev *dev  = fh->dev;
789
790	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
791	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
792	f->fmt.pix.bytesperline = 0;
793	f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
794	f->fmt.pix.colorspace   = 0;
795	dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
796		dev->width, dev->height, fh->mpegq.field );
797	return 0;
798}
799
800static int vidioc_s_fmt_cap (struct file *file, void *priv,
801					struct v4l2_format *f)
802{
803	struct cx8802_fh  *fh   = priv;
804	struct cx8802_dev *dev  = fh->dev;
805	struct cx88_core  *core = dev->core;
806
807	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
808	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
809	f->fmt.pix.bytesperline = 0;
810	f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
811	f->fmt.pix.colorspace   = 0;
812	dev->width              = f->fmt.pix.width;
813	dev->height             = f->fmt.pix.height;
814	fh->mpegq.field         = f->fmt.pix.field;
815	cx88_set_scale(core, f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
816	blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
817				f->fmt.pix.height, f->fmt.pix.width);
818	dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
819		f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
820	return 0;
821}
822
823static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
824{
825	struct cx8802_fh  *fh   = priv;
826	return (videobuf_reqbufs(&fh->mpegq, p));
827}
828
829static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
830{
831	struct cx8802_fh  *fh   = priv;
832	return (videobuf_querybuf(&fh->mpegq, p));
833}
834
835static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
836{
837	struct cx8802_fh  *fh   = priv;
838	return (videobuf_qbuf(&fh->mpegq, p));
839}
840
841static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
842{
843	struct cx8802_fh  *fh   = priv;
844	return (videobuf_dqbuf(&fh->mpegq, p,
845				file->f_flags & O_NONBLOCK));
846}
847
848static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
849{
850	struct cx8802_fh  *fh   = priv;
851	return videobuf_streamon(&fh->mpegq);
852}
853
854static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
855{
856	struct cx8802_fh  *fh   = priv;
857	return videobuf_streamoff(&fh->mpegq);
858}
859
860static int vidioc_g_mpegcomp (struct file *file, void *fh,
861			      struct v4l2_mpeg_compression *f)
862{
863	printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. "
864				"Replace with VIDIOC_G_EXT_CTRLS!");
865	memcpy(f,&default_mpeg_params,sizeof(*f));
866	return 0;
867}
868
869static int vidioc_s_mpegcomp (struct file *file, void *fh,
870			      struct v4l2_mpeg_compression *f)
871{
872	printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. "
873				"Replace with VIDIOC_S_EXT_CTRLS!");
874	return 0;
875}
876
877static int vidioc_g_ext_ctrls (struct file *file, void *priv,
878			       struct v4l2_ext_controls *f)
879{
880	struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
881
882	if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
883		return -EINVAL;
884	return cx2341x_ext_ctrls(&dev->params, f, VIDIOC_G_EXT_CTRLS);
885}
886
887static int vidioc_s_ext_ctrls (struct file *file, void *priv,
888			       struct v4l2_ext_controls *f)
889{
890	struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
891	struct cx2341x_mpeg_params p;
892	int err;
893
894	if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
895		return -EINVAL;
896	p = dev->params;
897	err = cx2341x_ext_ctrls(&p, f, VIDIOC_S_EXT_CTRLS);
898	if (!err) {
899		err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
900		dev->params = p;
901	}
902	return err;
903}
904
905static int vidioc_try_ext_ctrls (struct file *file, void *priv,
906			       struct v4l2_ext_controls *f)
907{
908	struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
909	struct cx2341x_mpeg_params p;
910	int err;
911
912	if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
913		return -EINVAL;
914	p = dev->params;
915	err = cx2341x_ext_ctrls(&p, f, VIDIOC_TRY_EXT_CTRLS);
916
917	return err;
918}
919
920static int vidioc_s_frequency (struct file *file, void *priv,
921				struct v4l2_frequency *f)
922{
923	struct cx8802_fh  *fh   = priv;
924	struct cx8802_dev *dev  = fh->dev;
925	struct cx88_core  *core = dev->core;
926
927	blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
928				BLACKBIRD_END_NOW,
929				BLACKBIRD_MPEG_CAPTURE,
930				BLACKBIRD_RAW_BITS_NONE);
931	cx88_set_freq (core,f);
932	blackbird_initialize_codec(dev);
933	cx88_set_scale(dev->core, dev->width, dev->height,
934			fh->mpegq.field);
935	return 0;
936}
937
938static int vidioc_log_status (struct file *file, void *priv)
939{
940	struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
941	struct cx88_core  *core = dev->core;
942	char name[32 + 2];
943
944	snprintf(name, sizeof(name), "%s/2", core->name);
945	printk("%s/2: ============  START LOG STATUS  ============\n",
946		core->name);
947	cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, NULL);
948	cx2341x_log_status(&dev->params, name);
949	printk("%s/2: =============  END LOG STATUS  =============\n",
950		core->name);
951	return 0;
952}
953
954static int vidioc_queryctrl (struct file *file, void *priv,
955				struct v4l2_queryctrl *qctrl)
956{
957	struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
958
959	if (blackbird_queryctrl(dev, qctrl) == 0)
960		return 0;
961
962	qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
963	if (unlikely(qctrl->id == 0))
964		return -EINVAL;
965	return cx8800_ctrl_query(qctrl);
966}
967
968static int vidioc_enum_input (struct file *file, void *priv,
969				struct v4l2_input *i)
970{
971	struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
972	return cx88_enum_input (core,i);
973}
974
975static int vidioc_g_ctrl (struct file *file, void *priv,
976				struct v4l2_control *ctl)
977{
978	struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
979	return
980		cx88_get_control(core,ctl);
981}
982
983static int vidioc_s_ctrl (struct file *file, void *priv,
984				struct v4l2_control *ctl)
985{
986	struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
987	return
988		cx88_set_control(core,ctl);
989}
990
991static int vidioc_g_frequency (struct file *file, void *priv,
992				struct v4l2_frequency *f)
993{
994	struct cx8802_fh  *fh   = priv;
995	struct cx88_core  *core = fh->dev->core;
996
997	if (unlikely(UNSET == core->tuner_type))
998		return -EINVAL;
999
1000	f->type = V4L2_TUNER_ANALOG_TV;
1001	f->frequency = core->freq;
1002	cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1003
1004	return 0;
1005}
1006
1007static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1008{
1009	struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
1010
1011	*i = core->input;
1012	return 0;
1013}
1014
1015static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1016{
1017	struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
1018
1019	if (i >= 4)
1020		return -EINVAL;
1021
1022	mutex_lock(&core->lock);
1023	cx88_newstation(core);
1024	cx88_video_mux(core,i);
1025	mutex_unlock(&core->lock);
1026	return 0;
1027}
1028
1029static int vidioc_g_tuner (struct file *file, void *priv,
1030				struct v4l2_tuner *t)
1031{
1032	struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
1033	u32 reg;
1034
1035	if (unlikely(UNSET == core->tuner_type))
1036		return -EINVAL;
1037	if (0 != t->index)
1038		return -EINVAL;
1039
1040	strcpy(t->name, "Television");
1041	t->type       = V4L2_TUNER_ANALOG_TV;
1042	t->capability = V4L2_TUNER_CAP_NORM;
1043	t->rangehigh  = 0xffffffffUL;
1044
1045	cx88_get_stereo(core ,t);
1046	reg = cx_read(MO_DEVICE_STATUS);
1047	t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1048	return 0;
1049}
1050
1051static int vidioc_s_tuner (struct file *file, void *priv,
1052				struct v4l2_tuner *t)
1053{
1054	struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
1055
1056	if (UNSET == core->tuner_type)
1057		return -EINVAL;
1058	if (0 != t->index)
1059		return -EINVAL;
1060
1061	cx88_set_stereo(core, t->audmode, 1);
1062	return 0;
1063}
1064
1065static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
1066{
1067	struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
1068
1069	mutex_lock(&core->lock);
1070	cx88_set_tvnorm(core,*id);
1071	mutex_unlock(&core->lock);
1072	return 0;
1073}
1074
1075
1076static int mpeg_open(struct inode *inode, struct file *file)
1077{
1078	int minor = iminor(inode);
1079	struct cx8802_dev *dev = NULL;
1080	struct cx8802_fh *fh;
1081	struct cx8802_driver *drv = NULL;
1082	int err;
1083
1084       dev = cx8802_get_device(inode);
1085
1086	dprintk( 1, "%s\n", __FUNCTION__);
1087
1088	if (dev == NULL)
1089		return -ENODEV;
1090
1091	/* Make sure we can acquire the hardware */
1092	drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
1093	if (drv) {
1094		err = drv->request_acquire(drv);
1095		if(err != 0) {
1096			dprintk(1,"%s: Unable to acquire hardware, %d\n", __FUNCTION__, err);
1097			return err;
1098		}
1099	}
1100
1101	if (blackbird_initialize_codec(dev) < 0) {
1102		if (drv)
1103			drv->request_release(drv);
1104		return -EINVAL;
1105	}
1106	dprintk(1,"open minor=%d\n",minor);
1107
1108	/* allocate + initialize per filehandle data */
1109	fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1110	if (NULL == fh) {
1111		if (drv)
1112			drv->request_release(drv);
1113		return -ENOMEM;
1114	}
1115	file->private_data = fh;
1116	fh->dev      = dev;
1117
1118	videobuf_queue_init(&fh->mpegq, &blackbird_qops,
1119			    dev->pci, &dev->slock,
1120			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
1121			    V4L2_FIELD_INTERLACED,
1122			    sizeof(struct cx88_buffer),
1123			    fh);
1124
1125	cx88_set_scale(dev->core, dev->width, dev->height,
1126			fh->mpegq.field);
1127
1128	return 0;
1129}
1130
1131static int mpeg_release(struct inode *inode, struct file *file)
1132{
1133	struct cx8802_fh  *fh  = file->private_data;
1134	struct cx8802_dev *dev = NULL;
1135	struct cx8802_driver *drv = NULL;
1136
1137	/* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
1138	blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
1139			BLACKBIRD_END_NOW,
1140			BLACKBIRD_MPEG_CAPTURE,
1141			BLACKBIRD_RAW_BITS_NONE
1142		);
1143
1144	cx8802_cancel_buffers(fh->dev);
1145	/* stop mpeg capture */
1146	if (fh->mpegq.streaming)
1147		videobuf_streamoff(&fh->mpegq);
1148	if (fh->mpegq.reading)
1149		videobuf_read_stop(&fh->mpegq);
1150
1151	videobuf_mmap_free(&fh->mpegq);
1152	file->private_data = NULL;
1153	kfree(fh);
1154
1155	/* Make sure we release the hardware */
1156	dev = cx8802_get_device(inode);
1157	if (dev == NULL)
1158		return -ENODEV;
1159
1160	drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
1161	if (drv)
1162		drv->request_release(drv);
1163
1164	return 0;
1165}
1166
1167static ssize_t
1168mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1169{
1170	struct cx8802_fh *fh = file->private_data;
1171
1172	return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1173				    file->f_flags & O_NONBLOCK);
1174}
1175
1176static unsigned int
1177mpeg_poll(struct file *file, struct poll_table_struct *wait)
1178{
1179	struct cx8802_fh *fh = file->private_data;
1180
1181	return videobuf_poll_stream(file, &fh->mpegq, wait);
1182}
1183
1184static int
1185mpeg_mmap(struct file *file, struct vm_area_struct * vma)
1186{
1187	struct cx8802_fh *fh = file->private_data;
1188
1189	return videobuf_mmap_mapper(&fh->mpegq, vma);
1190}
1191
1192static const struct file_operations mpeg_fops =
1193{
1194	.owner	       = THIS_MODULE,
1195	.open	       = mpeg_open,
1196	.release       = mpeg_release,
1197	.read	       = mpeg_read,
1198	.poll          = mpeg_poll,
1199	.mmap	       = mpeg_mmap,
1200	.ioctl	       = video_ioctl2,
1201	.llseek        = no_llseek,
1202};
1203
1204static struct video_device cx8802_mpeg_template =
1205{
1206	.name                 = "cx8802",
1207	.type                 = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
1208	.fops                 = &mpeg_fops,
1209	.minor                = -1,
1210	.vidioc_querymenu     = vidioc_querymenu,
1211	.vidioc_querycap      = vidioc_querycap,
1212	.vidioc_enum_fmt_cap  = vidioc_enum_fmt_cap,
1213	.vidioc_g_fmt_cap     = vidioc_g_fmt_cap,
1214	.vidioc_try_fmt_cap   = vidioc_try_fmt_cap,
1215	.vidioc_s_fmt_cap     = vidioc_s_fmt_cap,
1216	.vidioc_reqbufs       = vidioc_reqbufs,
1217	.vidioc_querybuf      = vidioc_querybuf,
1218	.vidioc_qbuf          = vidioc_qbuf,
1219	.vidioc_dqbuf         = vidioc_dqbuf,
1220	.vidioc_streamon      = vidioc_streamon,
1221	.vidioc_streamoff     = vidioc_streamoff,
1222	.vidioc_g_mpegcomp    = vidioc_g_mpegcomp,
1223	.vidioc_s_mpegcomp    = vidioc_s_mpegcomp,
1224	.vidioc_g_ext_ctrls   = vidioc_g_ext_ctrls,
1225	.vidioc_s_ext_ctrls   = vidioc_s_ext_ctrls,
1226	.vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1227	.vidioc_s_frequency   = vidioc_s_frequency,
1228	.vidioc_log_status    = vidioc_log_status,
1229	.vidioc_queryctrl     = vidioc_queryctrl,
1230	.vidioc_enum_input    = vidioc_enum_input,
1231	.vidioc_g_ctrl        = vidioc_g_ctrl,
1232	.vidioc_s_ctrl        = vidioc_s_ctrl,
1233	.vidioc_g_frequency   = vidioc_g_frequency,
1234	.vidioc_g_input       = vidioc_g_input,
1235	.vidioc_s_input       = vidioc_s_input,
1236	.vidioc_g_tuner       = vidioc_g_tuner,
1237	.vidioc_s_tuner       = vidioc_s_tuner,
1238	.vidioc_s_std         = vidioc_s_std,
1239	.tvnorms              = CX88_NORMS,
1240       .current_norm         = V4L2_STD_NTSC_M,
1241};
1242
1243/* ------------------------------------------------------------------ */
1244
1245/* The CX8802 MPEG API will call this when we can use the hardware */
1246static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
1247{
1248	struct cx88_core *core = drv->core;
1249	int err = 0;
1250
1251	switch (core->board) {
1252	case CX88_BOARD_HAUPPAUGE_HVR1300:
1253		/* By default, core setup will leave the cx22702 out of reset, on the bus.
1254		 * We left the hardware on power up with the cx22702 active.
1255		 * We're being given access to re-arrange the GPIOs.
1256		 * Take the bus off the cx22702 and put the cx23416 on it.
1257		 */
1258		cx_clear(MO_GP0_IO, 0x00000080); /* cx22702 in reset */
1259		cx_set(MO_GP0_IO,   0x00000004); /* Disable the cx22702 */
1260		break;
1261	default:
1262		err = -ENODEV;
1263	}
1264	return err;
1265}
1266
1267/* The CX8802 MPEG API will call this when we need to release the hardware */
1268static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
1269{
1270	struct cx88_core *core = drv->core;
1271	int err = 0;
1272
1273	switch (core->board) {
1274	case CX88_BOARD_HAUPPAUGE_HVR1300:
1275		/* Exit leaving the cx23416 on the bus */
1276		break;
1277	default:
1278		err = -ENODEV;
1279	}
1280	return err;
1281}
1282
1283static void blackbird_unregister_video(struct cx8802_dev *dev)
1284{
1285	if (dev->mpeg_dev) {
1286		if (-1 != dev->mpeg_dev->minor)
1287			video_unregister_device(dev->mpeg_dev);
1288		else
1289			video_device_release(dev->mpeg_dev);
1290		dev->mpeg_dev = NULL;
1291	}
1292}
1293
1294static int blackbird_register_video(struct cx8802_dev *dev)
1295{
1296	int err;
1297
1298	dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
1299				       &cx8802_mpeg_template,"mpeg");
1300	err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
1301	if (err < 0) {
1302		printk(KERN_INFO "%s/2: can't register mpeg device\n",
1303		       dev->core->name);
1304		return err;
1305	}
1306	printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
1307	       dev->core->name,dev->mpeg_dev->minor & 0x1f);
1308	return 0;
1309}
1310
1311/* ----------------------------------------------------------- */
1312
1313static int cx8802_blackbird_probe(struct cx8802_driver *drv)
1314{
1315	struct cx88_core *core = drv->core;
1316	struct cx8802_dev *dev = core->dvbdev;
1317	int err;
1318
1319	dprintk( 1, "%s\n", __FUNCTION__);
1320	dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1321		core->board,
1322		core->name,
1323		core->pci_bus,
1324		core->pci_slot);
1325
1326	err = -ENODEV;
1327	if (!(cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD))
1328		goto fail_core;
1329
1330	dev->width = 720;
1331	dev->height = 576;
1332	cx2341x_fill_defaults(&dev->params);
1333	dev->params.port = CX2341X_PORT_STREAMING;
1334
1335	cx8802_mpeg_template.current_norm = core->tvnorm;
1336
1337	if (core->tvnorm & V4L2_STD_525_60) {
1338		dev->height = 480;
1339	} else {
1340		dev->height = 576;
1341	}
1342
1343	/* blackbird stuff */
1344	printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1345	       core->name);
1346	host_setup(dev->core);
1347
1348	blackbird_register_video(dev);
1349
1350	/* initial device configuration: needed ? */
1351	mutex_lock(&dev->core->lock);
1352//	init_controls(core);
1353	cx88_set_tvnorm(core,core->tvnorm);
1354	cx88_video_mux(core,0);
1355	mutex_unlock(&dev->core->lock);
1356
1357	return 0;
1358
1359 fail_core:
1360	return err;
1361}
1362
1363static int cx8802_blackbird_remove(struct cx8802_driver *drv)
1364{
1365	/* blackbird */
1366	blackbird_unregister_video(drv->core->dvbdev);
1367
1368	return 0;
1369}
1370
1371static struct cx8802_driver cx8802_blackbird_driver = {
1372	.type_id	= CX88_MPEG_BLACKBIRD,
1373	.hw_access	= CX8802_DRVCTL_SHARED,
1374	.probe		= cx8802_blackbird_probe,
1375	.remove		= cx8802_blackbird_remove,
1376	.advise_acquire	= cx8802_blackbird_advise_acquire,
1377	.advise_release	= cx8802_blackbird_advise_release,
1378};
1379
1380static int blackbird_init(void)
1381{
1382	printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
1383	       (CX88_VERSION_CODE >> 16) & 0xff,
1384	       (CX88_VERSION_CODE >>  8) & 0xff,
1385	       CX88_VERSION_CODE & 0xff);
1386#ifdef SNAPSHOT
1387	printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
1388	       SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1389#endif
1390	return cx8802_register_driver(&cx8802_blackbird_driver);
1391}
1392
1393static void blackbird_fini(void)
1394{
1395	cx8802_unregister_driver(&cx8802_blackbird_driver);
1396}
1397
1398module_init(blackbird_init);
1399module_exit(blackbird_fini);
1400
1401module_param_named(video_debug,cx8802_mpeg_template.debug, int, 0644);
1402MODULE_PARM_DESC(debug,"enable debug messages [video]");
1403
1404/* ----------------------------------------------------------- */
1405/*
1406 * Local variables:
1407 * c-basic-offset: 8
1408 * End:
1409 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
1410 */
1411