2 * $Id: cx88-blackbird.c,v 1.17 2004/11/07 13:17:15 kraxel Exp $
4 * Support for a cx23416 mpeg encoder via cx2388x host port.
5 * "blackbird" reference design.
7 * (c) 2004 Jelle Foks <jelle@foks.8m.com>
8 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
10 * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/module.h>
28 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/firmware.h>
36 MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
37 MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
38 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
39 MODULE_LICENSE("GPL");
41 static unsigned int mpegbufs = 8;
42 module_param(mpegbufs,int,0644);
43 MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
45 static unsigned int debug = 0;
46 module_param(debug,int,0644);
47 MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
49 #define dprintk(level,fmt, arg...) if (debug >= level) \
50 printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
52 static LIST_HEAD(cx8802_devlist);
54 /* ------------------------------------------------------------------ */
56 #define BLACKBIRD_FIRM_ENC_FILENAME "blackbird-fw-enc.bin"
57 #define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
59 /* defines below are from ivtv-driver.h */
61 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
63 /*Firmware API commands*/
64 #define IVTV_API_ENC_PING_FW 0x00000080
65 #define IVTV_API_ENC_GETVER 0x000000C4
66 #define IVTV_API_ENC_HALT_FW 0x000000C3
67 #define IVTV_API_STD_TIMEOUT 0x00010000 /*units??*/
68 //#define IVTV_API_ASSIGN_PGM_INDEX_INFO 0x000000c7
69 #define IVTV_API_ASSIGN_STREAM_TYPE 0x000000b9
70 #define IVTV_API_ASSIGN_OUTPUT_PORT 0x000000bb
71 #define IVTV_API_ASSIGN_FRAMERATE 0x0000008f
72 #define IVTV_API_ASSIGN_FRAME_SIZE 0x00000091
73 #define IVTV_API_ASSIGN_ASPECT_RATIO 0x00000099
74 #define IVTV_API_ASSIGN_BITRATES 0x00000095
75 #define IVTV_API_ASSIGN_GOP_PROPERTIES 0x00000097
76 #define IVTV_API_ASSIGN_3_2_PULLDOWN 0x000000b1
77 #define IVTV_API_ASSIGN_GOP_CLOSURE 0x000000c5
78 #define IVTV_API_ASSIGN_AUDIO_PROPERTIES 0x000000bd
79 #define IVTV_API_ASSIGN_DNR_FILTER_MODE 0x0000009b
80 #define IVTV_API_ASSIGN_DNR_FILTER_PROPS 0x0000009d
81 #define IVTV_API_ASSIGN_CORING_LEVELS 0x0000009f
82 #define IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE 0x000000a1
83 #define IVTV_API_ASSIGN_FRAME_DROP_RATE 0x000000d0
84 #define IVTV_API_ASSIGN_PLACEHOLDER 0x000000d8
85 #define IVTV_API_MUTE_VIDEO 0x000000d9
86 #define IVTV_API_MUTE_AUDIO 0x000000da
87 #define IVTV_API_INITIALIZE_INPUT 0x000000cd
88 #define IVTV_API_REFRESH_INPUT 0x000000d3
89 #define IVTV_API_ASSIGN_NUM_VSYNC_LINES 0x000000d6
90 #define IVTV_API_BEGIN_CAPTURE 0x00000081
91 //#define IVTV_API_PAUSE_ENCODER 0x000000d2
92 //#define IVTV_API_EVENT_NOTIFICATION 0x000000d5
93 #define IVTV_API_END_CAPTURE 0x00000082
96 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
97 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
98 #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
99 #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
100 #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
101 #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
103 /* ------------------------------------------------------------------ */
105 static void host_setup(struct cx88_core *core)
107 /* toggle reset of the host */
108 cx_write(MO_GPHST_SOFT_RST, 1);
110 cx_write(MO_GPHST_SOFT_RST, 0);
113 /* host port setup */
114 cx_write(MO_GPHST_WSC, 0x44444444U);
115 cx_write(MO_GPHST_XFR, 0);
116 cx_write(MO_GPHST_WDTH, 15);
117 cx_write(MO_GPHST_HDSHK, 0);
118 cx_write(MO_GPHST_MUX16, 0x44448888U);
119 cx_write(MO_GPHST_MODE, 0);
122 /* ------------------------------------------------------------------ */
124 #define P1_MDATA0 0x390000
125 #define P1_MDATA1 0x390001
126 #define P1_MDATA2 0x390002
127 #define P1_MDATA3 0x390003
128 #define P1_MADDR2 0x390004
129 #define P1_MADDR1 0x390005
130 #define P1_MADDR0 0x390006
131 #define P1_RDATA0 0x390008
132 #define P1_RDATA1 0x390009
133 #define P1_RDATA2 0x39000A
134 #define P1_RDATA3 0x39000B
135 #define P1_RADDR0 0x39000C
136 #define P1_RADDR1 0x39000D
137 #define P1_RRDWR 0x39000E
139 static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
141 unsigned long timeout = jiffies + msecs_to_jiffies(1);
144 need = state ? 2 : 0;
146 gpio0 = cx_read(MO_GP0_IO) & 2;
149 if (time_after(jiffies,timeout))
155 static int memory_write(struct cx88_core *core, u32 address, u32 value)
157 /* Warning: address is dword address (4 bytes) */
158 cx_writeb(P1_MDATA0, (unsigned int)value);
159 cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
160 cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
161 cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
162 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
163 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
164 cx_writeb(P1_MADDR0, (unsigned int)address);
168 return wait_ready_gpio0_bit1(core,1);
171 static int memory_read(struct cx88_core *core, u32 address, u32 *value)
176 /* Warning: address is dword address (4 bytes) */
177 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
178 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
179 cx_writeb(P1_MADDR0, (unsigned int)address);
182 retval = wait_ready_gpio0_bit1(core,1);
184 cx_writeb(P1_MDATA3, 0);
185 val = (unsigned char)cx_read(P1_MDATA3) << 24;
186 cx_writeb(P1_MDATA2, 0);
187 val |= (unsigned char)cx_read(P1_MDATA2) << 16;
188 cx_writeb(P1_MDATA1, 0);
189 val |= (unsigned char)cx_read(P1_MDATA1) << 8;
190 cx_writeb(P1_MDATA0, 0);
191 val |= (unsigned char)cx_read(P1_MDATA0);
197 static int register_write(struct cx88_core *core, u32 address, u32 value)
199 cx_writeb(P1_RDATA0, (unsigned int)value);
200 cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
201 cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
202 cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
203 cx_writeb(P1_RADDR0, (unsigned int)address);
204 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
205 cx_writeb(P1_RRDWR, 1);
209 return wait_ready_gpio0_bit1(core,1);
211 udelay(1000); /* without this, things don't go right (subsequent memory_write()'s don't get through */
212 /* ? would this be safe here? set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(1); */
217 static int register_read(struct cx88_core *core, u32 address, u32 *value)
222 cx_writeb(P1_RADDR0, (unsigned int)address);
223 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
224 cx_writeb(P1_RRDWR, 0);
227 retval = wait_ready_gpio0_bit1(core,1);
228 val = (unsigned char)cx_read(P1_RDATA0);
229 val |= (unsigned char)cx_read(P1_RDATA1) << 8;
230 val |= (unsigned char)cx_read(P1_RDATA2) << 16;
231 val |= (unsigned char)cx_read(P1_RDATA3) << 24;
237 /* ------------------------------------------------------------------ */
239 /* We don't need to call the API often, so using just one mailbox will probably suffice */
240 static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
241 u32 inputcnt, u32 outputcnt, ...)
243 unsigned long timeout;
244 u32 value, flag, retval;
247 va_start(args, outputcnt);
249 dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
251 /* this may not be 100% safe if we can't read any memory location
252 without side effects */
253 memory_read(dev->core, dev->mailbox - 4, &value);
254 if (value != 0x12345678) {
255 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
259 memory_read(dev->core, dev->mailbox, &flag);
261 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
265 flag |= 1; /* tell 'em we're working on it */
266 memory_write(dev->core, dev->mailbox, flag);
268 /* write command + args + fill remaining with zeros */
269 memory_write(dev->core, dev->mailbox + 1, command); /* command code */
270 memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
271 for (i = 0; i < inputcnt ; i++) {
272 value = va_arg(args, int);
273 memory_write(dev->core, dev->mailbox + 4 + i, value);
274 dprintk(1, "API Input %d = %d\n", i, value);
277 memory_write(dev->core, dev->mailbox + 4 + i, 0);
279 flag |= 3; /* tell 'em we're done writing */
280 memory_write(dev->core, dev->mailbox, flag);
282 /* wait for firmware to handle the API command */
283 timeout = jiffies + msecs_to_jiffies(10);
285 memory_read(dev->core, dev->mailbox, &flag);
288 if (time_after(jiffies,timeout)) {
289 dprintk(0, "ERROR: API Mailbox timeout\n");
295 /* read output values */
296 for (i = 0; i < outputcnt ; i++) {
297 int *vptr = va_arg(args, int *);
298 memory_read(dev->core, dev->mailbox + 4 + i, vptr);
299 dprintk(1, "API Output %d = %d\n", i, *vptr);
303 memory_read(dev->core, dev->mailbox + 2, &retval);
304 dprintk(1, "API result = %d\n",retval);
307 memory_write(dev->core, dev->mailbox, flag);
312 static int blackbird_find_mailbox(struct cx8802_dev *dev)
314 u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
319 for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
320 memory_read(dev->core, i, &value);
321 if (value == signature[signaturecnt])
325 if (4 == signaturecnt) {
326 dprintk(1, "Mailbox signature found\n");
330 dprintk(0, "Mailbox signature values not found!\n");
334 static int blackbird_load_firmware(struct cx8802_dev *dev)
336 static const unsigned char magic[8] = {
337 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
339 const struct firmware *firmware;
345 retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
346 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
347 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
348 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
350 retval |= register_write(dev->core, IVTV_REG_APU, 0);
353 dprintk(0, "Error with register_write\n");
355 retval = request_firmware(&firmware, BLACKBIRD_FIRM_ENC_FILENAME,
358 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
359 BLACKBIRD_FIRM_ENC_FILENAME);
360 dprintk(0, "Please fix your hotplug setup, the board will "
361 "not work without firmware loaded!\n");
365 if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
366 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
367 firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
371 if (0 != memcmp(firmware->data, magic, 8)) {
372 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
376 /* transfer to the chip */
377 dprintk(1,"Loading firmware ...\n");
378 dataptr = (u32*)firmware->data;
379 for (i = 0; i < (firmware->size >> 2); i++) {
382 memory_write(dev->core, i, value);
386 /* read back to verify with the checksum */
387 for (i--; i >= 0; i--) {
388 memory_read(dev->core, i, &value);
392 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
395 release_firmware(firmware);
396 dprintk(0, "Firmware upload successful.\n");
398 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
399 retval |= register_read(dev->core, IVTV_REG_SPU, &value);
400 retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
403 retval |= register_read(dev->core, IVTV_REG_VPU, &value);
404 retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
407 dprintk(0, "Error with register_write\n");
411 static void blackbird_codec_settings(struct cx8802_dev *dev)
413 int bitrate_mode = 1;
414 int bitrate = 7500000;
415 int bitrate_peak = 7500000;
417 /* assign stream type */
418 blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 0); /* program stream */
419 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 2); /* MPEG1 stream */
420 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 3); /* PES A/V */
421 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 10); /* DVD stream */
423 /* assign output port */
424 blackbird_api_cmd(dev, IVTV_API_ASSIGN_OUTPUT_PORT, 1, 0, 1); /* 1 = Host */
426 /* assign framerate */
427 blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAMERATE, 1, 0, 0);
429 /* assign frame size */
430 blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_SIZE, 2, 0, 480, 720);
432 /* assign aspect ratio */
433 blackbird_api_cmd(dev, IVTV_API_ASSIGN_ASPECT_RATIO, 1, 0, 2);
435 /* assign bitrates */
436 blackbird_api_cmd(dev, IVTV_API_ASSIGN_BITRATES, 5, 0,
437 bitrate_mode, /* mode */
439 bitrate_peak / 400, /* peak/400 */
440 0, 0x70); /* encoding buffer, ckennedy */
442 /* assign gop properties */
443 blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 15, 3);
444 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 2, 1);
446 /* assign 3 2 pulldown */
447 blackbird_api_cmd(dev, IVTV_API_ASSIGN_3_2_PULLDOWN, 1, 0, 0);
449 /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
450 blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, (2<<2) | (8<<4));
452 /* assign gop closure */
453 blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_CLOSURE, 1, 0, 0);
455 /* assign audio properties */
456 blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, 0 | (2 << 2) | (14 << 4));
458 /* assign dnr filter mode */
459 blackbird_api_cmd(dev, IVTV_API_ASSIGN_DNR_FILTER_MODE, 2, 0, 0, 0);
461 /* assign dnr filter props*/
462 blackbird_api_cmd(dev, IVTV_API_ASSIGN_DNR_FILTER_PROPS, 2, 0, 0, 0);
464 /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
465 blackbird_api_cmd(dev, IVTV_API_ASSIGN_CORING_LEVELS, 4, 0, 0, 255, 0, 255);
467 /* assign spatial filter type: luma_t: 1 = horiz_only, chroma_t: 1 = horiz_only */
468 blackbird_api_cmd(dev, IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE, 2, 0, 1, 1);
470 /* assign frame drop rate */
471 blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_DROP_RATE, 1, 0, 0);
474 static int blackbird_initialize_codec(struct cx8802_dev *dev)
476 struct cx88_core *core = dev->core;
480 dprintk(1,"Initialize codec\n");
481 retval = blackbird_api_cmd(dev, IVTV_API_ENC_PING_FW, 0, 0); /* ping */
483 /* ping was not successful, reset and upload firmware */
484 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
486 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
488 retval = blackbird_load_firmware(dev);
492 dev->mailbox = blackbird_find_mailbox(dev);
493 if (dev->mailbox < 0)
496 retval = blackbird_api_cmd(dev, IVTV_API_ENC_PING_FW, 0, 0); /* ping */
498 dprintk(0, "ERROR: Firmware ping failed!\n");
502 retval = blackbird_api_cmd(dev, IVTV_API_ENC_GETVER, 0, 1, &version);
504 dprintk(0, "ERROR: Firmware get encoder version failed!\n");
507 dprintk(0, "Firmware version is 0x%08x\n", version);
511 cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
512 cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
513 cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
514 cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
517 set_scale(dev, 720, 480, V4L2_FIELD_INTERLACED);
519 blackbird_codec_settings(dev);
522 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
523 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
524 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180);
525 blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
527 blackbird_api_cmd(dev, IVTV_API_INITIALIZE_INPUT, 0, 0); /* initialize the video input */
531 blackbird_api_cmd(dev, IVTV_API_MUTE_VIDEO, 1, 0, 0);
533 blackbird_api_cmd(dev, IVTV_API_MUTE_AUDIO, 1, 0, 0);
536 blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0x13); /* start capturing to the host interface */
537 //blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0); /* start capturing to the host interface */
540 blackbird_api_cmd(dev, IVTV_API_REFRESH_INPUT, 0,0);
544 /* ------------------------------------------------------------------ */
546 static int bb_buf_setup(struct videobuf_queue *q,
547 unsigned int *count, unsigned int *size)
549 struct cx8802_fh *fh = q->priv_data;
551 fh->dev->ts_packet_size = 512;
552 fh->dev->ts_packet_count = 100;
554 *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
565 bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
566 enum v4l2_field field)
568 struct cx8802_fh *fh = q->priv_data;
569 return cx8802_buf_prepare(fh->dev, (struct cx88_buffer*)vb);
573 bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
575 struct cx8802_fh *fh = q->priv_data;
576 cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
580 bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
582 struct cx8802_fh *fh = q->priv_data;
583 cx88_free_buffer(fh->dev->pci, (struct cx88_buffer*)vb);
586 static struct videobuf_queue_ops blackbird_qops = {
587 .buf_setup = bb_buf_setup,
588 .buf_prepare = bb_buf_prepare,
589 .buf_queue = bb_buf_queue,
590 .buf_release = bb_buf_release,
593 /* ------------------------------------------------------------------ */
595 static int mpeg_do_ioctl(struct inode *inode, struct file *file,
596 unsigned int cmd, void *arg)
598 struct cx8802_fh *fh = file->private_data;
599 struct cx8802_dev *dev = fh->dev;
602 cx88_print_ioctl(dev->core->name,cmd);
606 /* --- capture ioctls ---------------------------------------- */
607 case VIDIOC_ENUM_FMT:
609 struct v4l2_fmtdesc *f = arg;
616 memset(f,0,sizeof(*f));
618 strlcpy(f->description, "MPEG TS", sizeof(f->description));
619 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
620 f->pixelformat = V4L2_PIX_FMT_MPEG;
627 /* FIXME -- quick'n'dirty for exactly one size ... */
628 struct v4l2_format *f = arg;
630 memset(f,0,sizeof(*f));
631 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
632 f->fmt.pix.width = 720;
633 f->fmt.pix.height = 576;
634 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
635 f->fmt.pix.sizeimage = 1024 * 512 /* FIXME: BUFFER_SIZE */;
638 /* --- streaming capture ------------------------------------- */
640 return videobuf_reqbufs(&fh->mpegq, arg);
642 case VIDIOC_QUERYBUF:
643 return videobuf_querybuf(&fh->mpegq, arg);
646 return videobuf_qbuf(&fh->mpegq, arg);
649 return videobuf_dqbuf(&fh->mpegq, arg,
650 file->f_flags & O_NONBLOCK);
652 case VIDIOC_STREAMON:
653 return videobuf_streamon(&fh->mpegq);
655 case VIDIOC_STREAMOFF:
656 return videobuf_streamoff(&fh->mpegq);
664 static int mpeg_ioctl(struct inode *inode, struct file *file,
665 unsigned int cmd, unsigned long arg)
667 return video_usercopy(inode, file, cmd, arg, mpeg_do_ioctl);
670 static int mpeg_open(struct inode *inode, struct file *file)
672 int minor = iminor(inode);
673 struct cx8802_dev *h,*dev = NULL;
674 struct cx8802_fh *fh;
675 struct list_head *list;
677 list_for_each(list,&cx8802_devlist) {
678 h = list_entry(list, struct cx8802_dev, devlist);
679 if (h->mpeg_dev->minor == minor)
685 if (blackbird_initialize_codec(dev) < 0)
687 dprintk(1,"open minor=%d\n",minor);
689 /* allocate + initialize per filehandle data */
690 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
693 memset(fh,0,sizeof(*fh));
694 file->private_data = fh;
697 videobuf_queue_init(&fh->mpegq, &blackbird_qops,
698 dev->pci, &dev->slock,
699 V4L2_BUF_TYPE_VIDEO_CAPTURE,
701 sizeof(struct cx88_buffer),
706 static int mpeg_release(struct inode *inode, struct file *file)
708 struct cx8802_fh *fh = file->private_data;
710 blackbird_api_cmd(fh->dev, IVTV_API_END_CAPTURE, 3, 0, 1, 0, 0x13);
712 /* stop mpeg capture */
713 if (fh->mpegq.streaming)
714 videobuf_streamoff(&fh->mpegq);
715 if (fh->mpegq.reading)
716 videobuf_read_stop(&fh->mpegq);
718 file->private_data = NULL;
724 mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
726 struct cx8802_fh *fh = file->private_data;
728 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
729 file->f_flags & O_NONBLOCK);
733 mpeg_poll(struct file *file, struct poll_table_struct *wait)
735 struct cx8802_fh *fh = file->private_data;
737 return videobuf_poll_stream(file, &fh->mpegq, wait);
741 mpeg_mmap(struct file *file, struct vm_area_struct * vma)
743 struct cx8802_fh *fh = file->private_data;
745 return videobuf_mmap_mapper(&fh->mpegq, vma);
748 static struct file_operations mpeg_fops =
750 .owner = THIS_MODULE,
752 .release = mpeg_release,
760 static struct video_device cx8802_mpeg_template =
763 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
769 /* ------------------------------------------------------------------ */
771 static void blackbird_unregister_video(struct cx8802_dev *dev)
774 if (-1 != dev->mpeg_dev->minor)
775 video_unregister_device(dev->mpeg_dev);
777 video_device_release(dev->mpeg_dev);
778 dev->mpeg_dev = NULL;
782 static int blackbird_register_video(struct cx8802_dev *dev)
786 dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
787 &cx8802_mpeg_template,"mpeg");
788 err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
790 printk(KERN_INFO "%s/2: can't register mpeg device\n",
794 printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
795 dev->core->name,dev->mpeg_dev->minor & 0x1f);
799 /* ----------------------------------------------------------- */
801 static int __devinit blackbird_probe(struct pci_dev *pci_dev,
802 const struct pci_device_id *pci_id)
804 struct cx8802_dev *dev;
805 struct cx88_core *core;
809 core = cx88_core_get(pci_dev);
814 if (!cx88_boards[core->board].blackbird)
818 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
821 memset(dev,0,sizeof(*dev));
825 err = cx8802_init_common(dev);
829 /* blackbird stuff */
830 printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
832 host_setup(dev->core);
834 list_add_tail(&dev->devlist,&cx8802_devlist);
835 blackbird_register_video(dev);
841 cx88_core_put(core,pci_dev);
845 static void __devexit blackbird_remove(struct pci_dev *pci_dev)
847 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
850 blackbird_unregister_video(dev);
851 list_del(&dev->devlist);
854 cx8802_fini_common(dev);
857 static struct pci_device_id cx8802_pci_tbl[] = {
861 .subvendor = PCI_ANY_ID,
862 .subdevice = PCI_ANY_ID,
864 /* --- end of list --- */
867 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
869 static struct pci_driver blackbird_pci_driver = {
870 .name = "cx88-blackbird",
871 .id_table = cx8802_pci_tbl,
872 .probe = blackbird_probe,
873 .remove = __devexit_p(blackbird_remove),
874 .suspend = cx8802_suspend_common,
875 .resume = cx8802_resume_common,
878 static int blackbird_init(void)
880 printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
881 (CX88_VERSION_CODE >> 16) & 0xff,
882 (CX88_VERSION_CODE >> 8) & 0xff,
883 CX88_VERSION_CODE & 0xff);
885 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
886 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
888 return pci_module_init(&blackbird_pci_driver);
891 static void blackbird_fini(void)
893 pci_unregister_driver(&blackbird_pci_driver);
896 module_init(blackbird_init);
897 module_exit(blackbird_fini);
899 /* ----------------------------------------------------------- */