3 * Support for a cx23416 mpeg encoder via cx2388x host port.
4 * "blackbird" reference design.
6 * (c) 2004 Jelle Foks <jelle@foks.8m.com>
7 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
9 * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/firmware.h>
33 #include <media/v4l2-common.h>
34 #include <media/cx2341x.h>
38 MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
39 MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40 MODULE_LICENSE("GPL");
42 static unsigned int mpegbufs = 32;
43 module_param(mpegbufs,int,0644);
44 MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
46 static unsigned int debug = 0;
47 module_param(debug,int,0644);
48 MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
50 #define dprintk(level,fmt, arg...) if (debug >= level) \
51 printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
54 /* ------------------------------------------------------------------ */
56 #define OLD_BLACKBIRD_FIRM_IMAGE_SIZE 262144
57 #define BLACKBIRD_FIRM_IMAGE_SIZE 376836
59 /* defines below are from ivtv-driver.h */
61 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
63 /* Firmware API commands */
64 #define IVTV_API_STD_TIMEOUT 500
66 enum blackbird_capture_type {
67 BLACKBIRD_MPEG_CAPTURE,
68 BLACKBIRD_RAW_CAPTURE,
69 BLACKBIRD_RAW_PASSTHRU_CAPTURE
71 enum blackbird_capture_bits {
72 BLACKBIRD_RAW_BITS_NONE = 0x00,
73 BLACKBIRD_RAW_BITS_YUV_CAPTURE = 0x01,
74 BLACKBIRD_RAW_BITS_PCM_CAPTURE = 0x02,
75 BLACKBIRD_RAW_BITS_VBI_CAPTURE = 0x04,
76 BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
77 BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE = 0x10
79 enum blackbird_capture_end {
80 BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
81 BLACKBIRD_END_NOW, /* stop immediately, no irq */
83 enum blackbird_framerate {
84 BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
85 BLACKBIRD_FRAMERATE_PAL_25 /* PAL: 25fps */
87 enum blackbird_stream_port {
88 BLACKBIRD_OUTPUT_PORT_MEMORY,
89 BLACKBIRD_OUTPUT_PORT_STREAMING,
90 BLACKBIRD_OUTPUT_PORT_SERIAL
92 enum blackbird_data_xfer_status {
93 BLACKBIRD_MORE_BUFFERS_FOLLOW,
94 BLACKBIRD_LAST_BUFFER,
96 enum blackbird_picture_mask {
97 BLACKBIRD_PICTURE_MASK_NONE,
98 BLACKBIRD_PICTURE_MASK_I_FRAMES,
99 BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
100 BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
102 enum blackbird_vbi_mode_bits {
103 BLACKBIRD_VBI_BITS_SLICED,
104 BLACKBIRD_VBI_BITS_RAW,
106 enum blackbird_vbi_insertion_bits {
107 BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
108 BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
109 BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
110 BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
111 BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
113 enum blackbird_dma_unit {
115 BLACKBIRD_DMA_FRAMES,
117 enum blackbird_dma_transfer_status_bits {
118 BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
119 BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
120 BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
122 enum blackbird_pause {
123 BLACKBIRD_PAUSE_ENCODING,
124 BLACKBIRD_RESUME_ENCODING,
126 enum blackbird_copyright {
127 BLACKBIRD_COPYRIGHT_OFF,
128 BLACKBIRD_COPYRIGHT_ON,
130 enum blackbird_notification_type {
131 BLACKBIRD_NOTIFICATION_REFRESH,
133 enum blackbird_notification_status {
134 BLACKBIRD_NOTIFICATION_OFF,
135 BLACKBIRD_NOTIFICATION_ON,
137 enum blackbird_notification_mailbox {
138 BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
140 enum blackbird_field1_lines {
141 BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
142 BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
143 BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
145 enum blackbird_field2_lines {
146 BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
147 BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
148 BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
150 enum blackbird_custom_data_type {
151 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
152 BLACKBIRD_CUSTOM_PRIVATE_PACKET,
154 enum blackbird_mute {
158 enum blackbird_mute_video_mask {
159 BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
160 BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
161 BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
163 enum blackbird_mute_video_shift {
164 BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
165 BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
166 BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
170 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
171 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
172 #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
173 #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
174 #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
175 #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
177 /* ------------------------------------------------------------------ */
179 static void host_setup(struct cx88_core *core)
181 /* toggle reset of the host */
182 cx_write(MO_GPHST_SOFT_RST, 1);
184 cx_write(MO_GPHST_SOFT_RST, 0);
187 /* host port setup */
188 cx_write(MO_GPHST_WSC, 0x44444444U);
189 cx_write(MO_GPHST_XFR, 0);
190 cx_write(MO_GPHST_WDTH, 15);
191 cx_write(MO_GPHST_HDSHK, 0);
192 cx_write(MO_GPHST_MUX16, 0x44448888U);
193 cx_write(MO_GPHST_MODE, 0);
196 /* ------------------------------------------------------------------ */
198 #define P1_MDATA0 0x390000
199 #define P1_MDATA1 0x390001
200 #define P1_MDATA2 0x390002
201 #define P1_MDATA3 0x390003
202 #define P1_MADDR2 0x390004
203 #define P1_MADDR1 0x390005
204 #define P1_MADDR0 0x390006
205 #define P1_RDATA0 0x390008
206 #define P1_RDATA1 0x390009
207 #define P1_RDATA2 0x39000A
208 #define P1_RDATA3 0x39000B
209 #define P1_RADDR0 0x39000C
210 #define P1_RADDR1 0x39000D
211 #define P1_RRDWR 0x39000E
213 static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
215 unsigned long timeout = jiffies + msecs_to_jiffies(1);
218 need = state ? 2 : 0;
220 gpio0 = cx_read(MO_GP0_IO) & 2;
223 if (time_after(jiffies,timeout))
229 static int memory_write(struct cx88_core *core, u32 address, u32 value)
231 /* Warning: address is dword address (4 bytes) */
232 cx_writeb(P1_MDATA0, (unsigned int)value);
233 cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
234 cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
235 cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
236 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
237 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
238 cx_writeb(P1_MADDR0, (unsigned int)address);
242 return wait_ready_gpio0_bit1(core,1);
245 static int memory_read(struct cx88_core *core, u32 address, u32 *value)
250 /* Warning: address is dword address (4 bytes) */
251 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
252 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
253 cx_writeb(P1_MADDR0, (unsigned int)address);
256 retval = wait_ready_gpio0_bit1(core,1);
258 cx_writeb(P1_MDATA3, 0);
259 val = (unsigned char)cx_read(P1_MDATA3) << 24;
260 cx_writeb(P1_MDATA2, 0);
261 val |= (unsigned char)cx_read(P1_MDATA2) << 16;
262 cx_writeb(P1_MDATA1, 0);
263 val |= (unsigned char)cx_read(P1_MDATA1) << 8;
264 cx_writeb(P1_MDATA0, 0);
265 val |= (unsigned char)cx_read(P1_MDATA0);
271 static int register_write(struct cx88_core *core, u32 address, u32 value)
273 cx_writeb(P1_RDATA0, (unsigned int)value);
274 cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
275 cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
276 cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
277 cx_writeb(P1_RADDR0, (unsigned int)address);
278 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
279 cx_writeb(P1_RRDWR, 1);
283 return wait_ready_gpio0_bit1(core,1);
287 static int register_read(struct cx88_core *core, u32 address, u32 *value)
292 cx_writeb(P1_RADDR0, (unsigned int)address);
293 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
294 cx_writeb(P1_RRDWR, 0);
297 retval = wait_ready_gpio0_bit1(core,1);
298 val = (unsigned char)cx_read(P1_RDATA0);
299 val |= (unsigned char)cx_read(P1_RDATA1) << 8;
300 val |= (unsigned char)cx_read(P1_RDATA2) << 16;
301 val |= (unsigned char)cx_read(P1_RDATA3) << 24;
307 /* ------------------------------------------------------------------ */
309 static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
311 struct cx8802_dev *dev = priv;
312 unsigned long timeout;
313 u32 value, flag, retval;
316 dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
318 /* this may not be 100% safe if we can't read any memory location
319 without side effects */
320 memory_read(dev->core, dev->mailbox - 4, &value);
321 if (value != 0x12345678) {
322 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
326 memory_read(dev->core, dev->mailbox, &flag);
328 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
332 flag |= 1; /* tell 'em we're working on it */
333 memory_write(dev->core, dev->mailbox, flag);
335 /* write command + args + fill remaining with zeros */
336 memory_write(dev->core, dev->mailbox + 1, command); /* command code */
337 memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
338 for (i = 0; i < in; i++) {
339 memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
340 dprintk(1, "API Input %d = %d\n", i, data[i]);
342 for (; i < CX2341X_MBOX_MAX_DATA; i++)
343 memory_write(dev->core, dev->mailbox + 4 + i, 0);
345 flag |= 3; /* tell 'em we're done writing */
346 memory_write(dev->core, dev->mailbox, flag);
348 /* wait for firmware to handle the API command */
349 timeout = jiffies + msecs_to_jiffies(10);
351 memory_read(dev->core, dev->mailbox, &flag);
354 if (time_after(jiffies,timeout)) {
355 dprintk(0, "ERROR: API Mailbox timeout\n");
361 /* read output values */
362 for (i = 0; i < out; i++) {
363 memory_read(dev->core, dev->mailbox + 4 + i, data + i);
364 dprintk(1, "API Output %d = %d\n", i, data[i]);
367 memory_read(dev->core, dev->mailbox + 2, &retval);
368 dprintk(1, "API result = %d\n",retval);
371 memory_write(dev->core, dev->mailbox, flag);
374 /* ------------------------------------------------------------------ */
376 /* We don't need to call the API often, so using just one mailbox will probably suffice */
377 static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
378 u32 inputcnt, u32 outputcnt, ...)
380 u32 data[CX2341X_MBOX_MAX_DATA];
384 va_start(vargs, outputcnt);
386 for (i = 0; i < inputcnt; i++) {
387 data[i] = va_arg(vargs, int);
389 err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
390 for (i = 0; i < outputcnt; i++) {
391 int *vptr = va_arg(vargs, int *);
398 static int blackbird_find_mailbox(struct cx8802_dev *dev)
400 u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
405 for (i = 0; i < dev->fw_size; i++) {
406 memory_read(dev->core, i, &value);
407 if (value == signature[signaturecnt])
411 if (4 == signaturecnt) {
412 dprintk(1, "Mailbox signature found\n");
416 dprintk(0, "Mailbox signature values not found!\n");
420 static int blackbird_load_firmware(struct cx8802_dev *dev)
422 static const unsigned char magic[8] = {
423 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
425 const struct firmware *firmware;
431 retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
432 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
433 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
434 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
436 retval |= register_write(dev->core, IVTV_REG_APU, 0);
439 dprintk(0, "Error with register_write\n");
441 retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
446 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
447 CX2341X_FIRM_ENC_FILENAME);
448 dprintk(0, "Please fix your hotplug setup, the board will "
449 "not work without firmware loaded!\n");
453 if ((firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) &&
454 (firmware->size != OLD_BLACKBIRD_FIRM_IMAGE_SIZE)) {
455 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d or %d)\n",
456 firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE,
457 OLD_BLACKBIRD_FIRM_IMAGE_SIZE);
458 release_firmware(firmware);
461 dev->fw_size = firmware->size;
463 if (0 != memcmp(firmware->data, magic, 8)) {
464 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
465 release_firmware(firmware);
469 /* transfer to the chip */
470 dprintk(1,"Loading firmware ...\n");
471 dataptr = (u32*)firmware->data;
472 for (i = 0; i < (firmware->size >> 2); i++) {
475 memory_write(dev->core, i, value);
479 /* read back to verify with the checksum */
480 for (i--; i >= 0; i--) {
481 memory_read(dev->core, i, &value);
485 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
486 release_firmware(firmware);
489 release_firmware(firmware);
490 dprintk(0, "Firmware upload successful.\n");
492 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
493 retval |= register_read(dev->core, IVTV_REG_SPU, &value);
494 retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
497 retval |= register_read(dev->core, IVTV_REG_VPU, &value);
498 retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
501 dprintk(0, "Error with register_write\n");
506 Settings used by the windows tv app for PVR2000:
507 =================================================================================================================
508 Profile | Codec | Resolution | CBR/VBR | Video Qlty | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
509 -----------------------------------------------------------------------------------------------------------------
510 MPEG-1 | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 2000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
511 MPEG-2 | MPEG2 | 720x576PAL | VBR | 600 :Good | 4000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
512 VCD | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 1150 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
513 DVD | MPEG2 | 720x576PAL | VBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
514 DB* DVD | MPEG2 | 720x576PAL | CBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
515 =================================================================================================================
519 static void blackbird_codec_settings(struct cx8802_dev *dev)
521 /* assign frame size */
522 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
523 dev->height, dev->width);
525 dev->params.width = dev->width;
526 dev->params.height = dev->height;
527 dev->params.is_50hz = (dev->core->tvnorm->id & V4L2_STD_625_50) != 0;
529 cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params);
532 static struct v4l2_mpeg_compression default_mpeg_params = {
533 .st_type = V4L2_MPEG_PS_2,
535 .mode = V4L2_BITRATE_CBR,
545 .au_type = V4L2_MPEG_AU_2_II,
547 .mode = V4L2_BITRATE_CBR,
552 .au_sample_rate = 48000,
554 .vi_type = V4L2_MPEG_VI_2,
555 .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
557 .mode = V4L2_BITRATE_CBR,
563 .vi_frames_per_gop = 12,
564 .vi_bframes_count = 2,
570 static int blackbird_initialize_codec(struct cx8802_dev *dev)
572 struct cx88_core *core = dev->core;
576 dprintk(1,"Initialize codec\n");
577 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
579 /* ping was not successful, reset and upload firmware */
580 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
582 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
584 retval = blackbird_load_firmware(dev);
588 dev->mailbox = blackbird_find_mailbox(dev);
589 if (dev->mailbox < 0)
592 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
594 dprintk(0, "ERROR: Firmware ping failed!\n");
598 retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
600 dprintk(0, "ERROR: Firmware get encoder version failed!\n");
603 dprintk(0, "Firmware version is 0x%08x\n", version);
607 cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
608 cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
609 cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
610 cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
612 blackbird_codec_settings(dev);
615 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
616 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
617 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
618 blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
619 BLACKBIRD_FIELD1_SAA7115,
620 BLACKBIRD_FIELD2_SAA7115
623 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
624 blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
625 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
626 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
628 /* initialize the video input */
629 blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
633 blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
635 blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
638 /* start capturing to the host interface */
639 /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
640 blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
641 BLACKBIRD_MPEG_CAPTURE,
642 BLACKBIRD_RAW_BITS_NONE
646 blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
650 /* ------------------------------------------------------------------ */
652 static int bb_buf_setup(struct videobuf_queue *q,
653 unsigned int *count, unsigned int *size)
655 struct cx8802_fh *fh = q->priv_data;
657 fh->dev->ts_packet_size = 188 * 4; /* was: 512 */
658 fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
660 *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
661 *count = fh->dev->ts_packet_count;
666 bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
667 enum v4l2_field field)
669 struct cx8802_fh *fh = q->priv_data;
670 return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
674 bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
676 struct cx8802_fh *fh = q->priv_data;
677 cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
681 bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
683 cx88_free_buffer(q, (struct cx88_buffer*)vb);
686 static struct videobuf_queue_ops blackbird_qops = {
687 .buf_setup = bb_buf_setup,
688 .buf_prepare = bb_buf_prepare,
689 .buf_queue = bb_buf_queue,
690 .buf_release = bb_buf_release,
693 /* ------------------------------------------------------------------ */
695 static const u32 *ctrl_classes[] = {
701 static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qctrl)
703 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
707 /* Standard V4L2 controls */
708 if (cx8800_ctrl_query(qctrl) == 0)
711 /* MPEG V4L2 controls */
712 if (cx2341x_ctrl_query(&dev->params, qctrl))
713 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
717 static int blackbird_querymenu(struct cx8802_dev *dev, struct v4l2_querymenu *qmenu)
719 struct v4l2_queryctrl qctrl;
721 qctrl.id = qmenu->id;
722 blackbird_queryctrl(dev, &qctrl);
723 return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
726 /* ------------------------------------------------------------------ */
728 static int mpeg_do_ioctl(struct inode *inode, struct file *file,
729 unsigned int cmd, void *arg)
731 struct cx8802_fh *fh = file->private_data;
732 struct cx8802_dev *dev = fh->dev;
733 struct cx88_core *core = dev->core;
736 v4l_print_ioctl(core->name,cmd);
740 /* --- capabilities ------------------------------------------ */
741 case VIDIOC_QUERYCAP:
743 struct v4l2_capability *cap = arg;
745 memset(cap,0,sizeof(*cap));
746 strcpy(cap->driver, "cx88_blackbird");
747 strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card));
748 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
749 cap->version = CX88_VERSION_CODE;
751 V4L2_CAP_VIDEO_CAPTURE |
755 if (UNSET != core->tuner_type)
756 cap->capabilities |= V4L2_CAP_TUNER;
761 /* --- capture ioctls ---------------------------------------- */
762 case VIDIOC_ENUM_FMT:
764 struct v4l2_fmtdesc *f = arg;
771 memset(f,0,sizeof(*f));
773 strlcpy(f->description, "MPEG", sizeof(f->description));
774 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
775 f->pixelformat = V4L2_PIX_FMT_MPEG;
780 struct v4l2_format *f = arg;
782 memset(f,0,sizeof(*f));
783 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
784 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
785 f->fmt.pix.bytesperline = 0;
786 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
787 f->fmt.pix.colorspace = 0;
788 f->fmt.pix.width = dev->width;
789 f->fmt.pix.height = dev->height;
790 f->fmt.pix.field = fh->mpegq.field;
791 dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
792 dev->width, dev->height, fh->mpegq.field );
797 struct v4l2_format *f = arg;
799 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
800 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
801 f->fmt.pix.bytesperline = 0;
802 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
803 f->fmt.pix.colorspace = 0;
804 dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
805 dev->width, dev->height, fh->mpegq.field );
810 struct v4l2_format *f = arg;
812 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
813 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
814 f->fmt.pix.bytesperline = 0;
815 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
816 f->fmt.pix.colorspace = 0;
817 dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
818 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
822 /* --- streaming capture ------------------------------------- */
824 return videobuf_reqbufs(&fh->mpegq, arg);
826 case VIDIOC_QUERYBUF:
827 return videobuf_querybuf(&fh->mpegq, arg);
830 return videobuf_qbuf(&fh->mpegq, arg);
833 return videobuf_dqbuf(&fh->mpegq, arg,
834 file->f_flags & O_NONBLOCK);
836 case VIDIOC_STREAMON:
837 return videobuf_streamon(&fh->mpegq);
839 case VIDIOC_STREAMOFF:
840 return videobuf_streamoff(&fh->mpegq);
842 /* --- mpeg compression -------------------------------------- */
843 case VIDIOC_G_MPEGCOMP:
845 struct v4l2_mpeg_compression *f = arg;
847 printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. "
848 "Replace with VIDIOC_G_EXT_CTRLS!");
849 memcpy(f,&default_mpeg_params,sizeof(*f));
852 case VIDIOC_S_MPEGCOMP:
853 printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. "
854 "Replace with VIDIOC_S_EXT_CTRLS!");
856 case VIDIOC_G_EXT_CTRLS:
858 struct v4l2_ext_controls *f = arg;
860 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
862 return cx2341x_ext_ctrls(&dev->params, f, cmd);
864 case VIDIOC_S_EXT_CTRLS:
865 case VIDIOC_TRY_EXT_CTRLS:
867 struct v4l2_ext_controls *f = arg;
868 struct cx2341x_mpeg_params p;
871 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
874 err = cx2341x_ext_ctrls(&p, f, cmd);
875 if (err == 0 && cmd == VIDIOC_S_EXT_CTRLS) {
876 err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
881 case VIDIOC_S_FREQUENCY:
883 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
885 BLACKBIRD_MPEG_CAPTURE,
886 BLACKBIRD_RAW_BITS_NONE);
888 cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
890 blackbird_initialize_codec(dev);
891 cx88_set_scale(dev->core, dev->width, dev->height,
895 case VIDIOC_LOG_STATUS:
899 snprintf(name, sizeof(name), "%s/2", core->name);
900 printk("%s/2: ============ START LOG STATUS ============\n",
902 cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, NULL);
903 cx2341x_log_status(&dev->params, name);
904 printk("%s/2: ============= END LOG STATUS =============\n",
908 case VIDIOC_QUERYMENU:
909 return blackbird_querymenu(dev, arg);
910 case VIDIOC_QUERYCTRL:
912 struct v4l2_queryctrl *c = arg;
914 if (blackbird_queryctrl(dev, c) == 0)
916 return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, mpeg_do_ioctl);
920 return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
925 int (*cx88_ioctl_hook)(struct inode *inode, struct file *file,
926 unsigned int cmd, void *arg);
927 unsigned int (*cx88_ioctl_translator)(unsigned int cmd);
929 static unsigned int mpeg_translate_ioctl(unsigned int cmd)
934 static int mpeg_ioctl(struct inode *inode, struct file *file,
935 unsigned int cmd, unsigned long arg)
937 cmd = cx88_ioctl_translator( cmd );
938 return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
941 static int mpeg_open(struct inode *inode, struct file *file)
943 int minor = iminor(inode);
944 struct cx8802_dev *dev = NULL;
945 struct cx8802_fh *fh;
946 struct cx8802_driver *drv = NULL;
949 dev = cx8802_get_device(inode);
951 dprintk( 1, "%s\n", __FUNCTION__);
956 /* Make sure we can acquire the hardware */
957 drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
959 err = drv->request_acquire(drv);
961 dprintk(1,"%s: Unable to acquire hardware, %d\n", __FUNCTION__, err);
966 if (blackbird_initialize_codec(dev) < 0) {
968 drv->request_release(drv);
971 dprintk(1,"open minor=%d\n",minor);
973 /* allocate + initialize per filehandle data */
974 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
977 drv->request_release(drv);
980 file->private_data = fh;
983 videobuf_queue_init(&fh->mpegq, &blackbird_qops,
984 dev->pci, &dev->slock,
985 V4L2_BUF_TYPE_VIDEO_CAPTURE,
986 V4L2_FIELD_INTERLACED,
987 sizeof(struct cx88_buffer),
990 /* FIXME: locking against other video device */
991 cx88_set_scale(dev->core, dev->width, dev->height,
997 static int mpeg_release(struct inode *inode, struct file *file)
999 struct cx8802_fh *fh = file->private_data;
1000 struct cx8802_dev *dev = NULL;
1001 struct cx8802_driver *drv = NULL;
1003 /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
1004 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
1006 BLACKBIRD_MPEG_CAPTURE,
1007 BLACKBIRD_RAW_BITS_NONE
1010 cx8802_cancel_buffers(fh->dev);
1011 /* stop mpeg capture */
1012 if (fh->mpegq.streaming)
1013 videobuf_streamoff(&fh->mpegq);
1014 if (fh->mpegq.reading)
1015 videobuf_read_stop(&fh->mpegq);
1017 videobuf_mmap_free(&fh->mpegq);
1018 file->private_data = NULL;
1021 /* Make sure we release the hardware */
1022 dev = cx8802_get_device(inode);
1026 drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
1028 drv->request_release(drv);
1034 mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1036 struct cx8802_fh *fh = file->private_data;
1038 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1039 file->f_flags & O_NONBLOCK);
1043 mpeg_poll(struct file *file, struct poll_table_struct *wait)
1045 struct cx8802_fh *fh = file->private_data;
1047 return videobuf_poll_stream(file, &fh->mpegq, wait);
1051 mpeg_mmap(struct file *file, struct vm_area_struct * vma)
1053 struct cx8802_fh *fh = file->private_data;
1055 return videobuf_mmap_mapper(&fh->mpegq, vma);
1058 static struct file_operations mpeg_fops =
1060 .owner = THIS_MODULE,
1062 .release = mpeg_release,
1066 .ioctl = mpeg_ioctl,
1067 .llseek = no_llseek,
1070 static struct video_device cx8802_mpeg_template =
1073 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
1079 /* ------------------------------------------------------------------ */
1081 /* The CX8802 MPEG API will call this when we can use the hardware */
1082 static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
1084 struct cx88_core *core = drv->core;
1087 switch (core->board) {
1088 case CX88_BOARD_HAUPPAUGE_HVR1300:
1089 /* By default, core setup will leave the cx22702 out of reset, on the bus.
1090 * We left the hardware on power up with the cx22702 active.
1091 * We're being given access to re-arrange the GPIOs.
1092 * Take the bus off the cx22702 and put the cx23416 on it.
1094 cx_clear(MO_GP0_IO, 0x00000080); /* cx22702 in reset */
1095 cx_set(MO_GP0_IO, 0x00000004); /* Disable the cx22702 */
1103 /* The CX8802 MPEG API will call this when we need to release the hardware */
1104 static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
1106 struct cx88_core *core = drv->core;
1109 switch (core->board) {
1110 case CX88_BOARD_HAUPPAUGE_HVR1300:
1111 /* Exit leaving the cx23416 on the bus */
1119 static void blackbird_unregister_video(struct cx8802_dev *dev)
1121 if (dev->mpeg_dev) {
1122 if (-1 != dev->mpeg_dev->minor)
1123 video_unregister_device(dev->mpeg_dev);
1125 video_device_release(dev->mpeg_dev);
1126 dev->mpeg_dev = NULL;
1130 static int blackbird_register_video(struct cx8802_dev *dev)
1134 dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
1135 &cx8802_mpeg_template,"mpeg");
1136 err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
1138 printk(KERN_INFO "%s/2: can't register mpeg device\n",
1142 printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
1143 dev->core->name,dev->mpeg_dev->minor & 0x1f);
1147 /* ----------------------------------------------------------- */
1149 static int cx8802_blackbird_probe(struct cx8802_driver *drv)
1151 struct cx88_core *core = drv->core;
1152 struct cx8802_dev *dev = core->dvbdev;
1155 dprintk( 1, "%s\n", __FUNCTION__);
1156 dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1163 if (!(cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD))
1168 cx2341x_fill_defaults(&dev->params);
1169 dev->params.port = CX2341X_PORT_STREAMING;
1171 if (core->tvnorm->id & V4L2_STD_525_60) {
1177 /* blackbird stuff */
1178 printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1180 host_setup(dev->core);
1182 blackbird_register_video(dev);
1184 /* initial device configuration: needed ? */
1192 static int cx8802_blackbird_remove(struct cx8802_driver *drv)
1195 blackbird_unregister_video(drv->core->dvbdev);
1200 static struct cx8802_driver cx8802_blackbird_driver = {
1201 .type_id = CX88_MPEG_BLACKBIRD,
1202 .hw_access = CX8802_DRVCTL_SHARED,
1203 .probe = cx8802_blackbird_probe,
1204 .remove = cx8802_blackbird_remove,
1205 .advise_acquire = cx8802_blackbird_advise_acquire,
1206 .advise_release = cx8802_blackbird_advise_release,
1209 static int blackbird_init(void)
1211 printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
1212 (CX88_VERSION_CODE >> 16) & 0xff,
1213 (CX88_VERSION_CODE >> 8) & 0xff,
1214 CX88_VERSION_CODE & 0xff);
1216 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
1217 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1219 cx88_ioctl_hook = mpeg_do_ioctl;
1220 cx88_ioctl_translator = mpeg_translate_ioctl;
1221 return cx8802_register_driver(&cx8802_blackbird_driver);
1224 static void blackbird_fini(void)
1226 cx8802_unregister_driver(&cx8802_blackbird_driver);
1229 module_init(blackbird_init);
1230 module_exit(blackbird_fini);
1232 EXPORT_SYMBOL(cx88_ioctl_hook);
1233 EXPORT_SYMBOL(cx88_ioctl_translator);
1235 /* ----------------------------------------------------------- */
1240 * 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