2 * $Id: cx88-blackbird.c,v 1.26 2005/03/07 15:58:05 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/moduleparam.h>
29 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/device.h>
33 #include <linux/firmware.h>
37 MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
38 MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
39 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40 MODULE_LICENSE("GPL");
42 static unsigned int mpegbufs
= 8;
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)
53 static LIST_HEAD(cx8802_devlist
);
55 /* ------------------------------------------------------------------ */
57 #define BLACKBIRD_FIRM_ENC_FILENAME "blackbird-fw-enc.bin"
58 #define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
60 /* defines below are from ivtv-driver.h */
62 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
64 /*Firmware API commands*/
65 #define IVTV_API_ENC_PING_FW 0x00000080
66 #define IVTV_API_ENC_GETVER 0x000000C4
67 #define IVTV_API_ENC_HALT_FW 0x000000C3
68 #define IVTV_API_STD_TIMEOUT 0x00010000 /*units??*/
69 //#define IVTV_API_ASSIGN_PGM_INDEX_INFO 0x000000c7
70 #define IVTV_API_ASSIGN_STREAM_TYPE 0x000000b9
71 #define IVTV_API_ASSIGN_OUTPUT_PORT 0x000000bb
72 #define IVTV_API_ASSIGN_FRAMERATE 0x0000008f
73 #define IVTV_API_ASSIGN_FRAME_SIZE 0x00000091
74 #define IVTV_API_ASSIGN_ASPECT_RATIO 0x00000099
75 #define IVTV_API_ASSIGN_BITRATES 0x00000095
76 #define IVTV_API_ASSIGN_GOP_PROPERTIES 0x00000097
77 #define IVTV_API_ASSIGN_3_2_PULLDOWN 0x000000b1
78 #define IVTV_API_ASSIGN_GOP_CLOSURE 0x000000c5
79 #define IVTV_API_ASSIGN_AUDIO_PROPERTIES 0x000000bd
80 #define IVTV_API_ASSIGN_DNR_FILTER_MODE 0x0000009b
81 #define IVTV_API_ASSIGN_DNR_FILTER_PROPS 0x0000009d
82 #define IVTV_API_ASSIGN_CORING_LEVELS 0x0000009f
83 #define IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE 0x000000a1
84 #define IVTV_API_ASSIGN_FRAME_DROP_RATE 0x000000d0
85 #define IVTV_API_ASSIGN_PLACEHOLDER 0x000000d8
86 #define IVTV_API_MUTE_VIDEO 0x000000d9
87 #define IVTV_API_MUTE_AUDIO 0x000000da
88 #define IVTV_API_INITIALIZE_INPUT 0x000000cd
89 #define IVTV_API_REFRESH_INPUT 0x000000d3
90 #define IVTV_API_ASSIGN_NUM_VSYNC_LINES 0x000000d6
91 #define IVTV_API_BEGIN_CAPTURE 0x00000081
92 //#define IVTV_API_PAUSE_ENCODER 0x000000d2
93 //#define IVTV_API_EVENT_NOTIFICATION 0x000000d5
94 #define IVTV_API_END_CAPTURE 0x00000082
97 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
98 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
99 #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
100 #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
101 #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
102 #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
104 /* ------------------------------------------------------------------ */
106 static void host_setup(struct cx88_core
*core
)
108 /* toggle reset of the host */
109 cx_write(MO_GPHST_SOFT_RST
, 1);
111 cx_write(MO_GPHST_SOFT_RST
, 0);
114 /* host port setup */
115 cx_write(MO_GPHST_WSC
, 0x44444444U
);
116 cx_write(MO_GPHST_XFR
, 0);
117 cx_write(MO_GPHST_WDTH
, 15);
118 cx_write(MO_GPHST_HDSHK
, 0);
119 cx_write(MO_GPHST_MUX16
, 0x44448888U
);
120 cx_write(MO_GPHST_MODE
, 0);
123 /* ------------------------------------------------------------------ */
125 #define P1_MDATA0 0x390000
126 #define P1_MDATA1 0x390001
127 #define P1_MDATA2 0x390002
128 #define P1_MDATA3 0x390003
129 #define P1_MADDR2 0x390004
130 #define P1_MADDR1 0x390005
131 #define P1_MADDR0 0x390006
132 #define P1_RDATA0 0x390008
133 #define P1_RDATA1 0x390009
134 #define P1_RDATA2 0x39000A
135 #define P1_RDATA3 0x39000B
136 #define P1_RADDR0 0x39000C
137 #define P1_RADDR1 0x39000D
138 #define P1_RRDWR 0x39000E
140 static int wait_ready_gpio0_bit1(struct cx88_core
*core
, u32 state
)
142 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1);
145 need
= state
? 2 : 0;
147 gpio0
= cx_read(MO_GP0_IO
) & 2;
150 if (time_after(jiffies
,timeout
))
156 static int memory_write(struct cx88_core
*core
, u32 address
, u32 value
)
158 /* Warning: address is dword address (4 bytes) */
159 cx_writeb(P1_MDATA0
, (unsigned int)value
);
160 cx_writeb(P1_MDATA1
, (unsigned int)(value
>> 8));
161 cx_writeb(P1_MDATA2
, (unsigned int)(value
>> 16));
162 cx_writeb(P1_MDATA3
, (unsigned int)(value
>> 24));
163 cx_writeb(P1_MADDR2
, (unsigned int)(address
>> 16) | 0x40);
164 cx_writeb(P1_MADDR1
, (unsigned int)(address
>> 8));
165 cx_writeb(P1_MADDR0
, (unsigned int)address
);
169 return wait_ready_gpio0_bit1(core
,1);
172 static int memory_read(struct cx88_core
*core
, u32 address
, u32
*value
)
177 /* Warning: address is dword address (4 bytes) */
178 cx_writeb(P1_MADDR2
, (unsigned int)(address
>> 16) & ~0xC0);
179 cx_writeb(P1_MADDR1
, (unsigned int)(address
>> 8));
180 cx_writeb(P1_MADDR0
, (unsigned int)address
);
183 retval
= wait_ready_gpio0_bit1(core
,1);
185 cx_writeb(P1_MDATA3
, 0);
186 val
= (unsigned char)cx_read(P1_MDATA3
) << 24;
187 cx_writeb(P1_MDATA2
, 0);
188 val
|= (unsigned char)cx_read(P1_MDATA2
) << 16;
189 cx_writeb(P1_MDATA1
, 0);
190 val
|= (unsigned char)cx_read(P1_MDATA1
) << 8;
191 cx_writeb(P1_MDATA0
, 0);
192 val
|= (unsigned char)cx_read(P1_MDATA0
);
198 static int register_write(struct cx88_core
*core
, u32 address
, u32 value
)
200 cx_writeb(P1_RDATA0
, (unsigned int)value
);
201 cx_writeb(P1_RDATA1
, (unsigned int)(value
>> 8));
202 cx_writeb(P1_RDATA2
, (unsigned int)(value
>> 16));
203 cx_writeb(P1_RDATA3
, (unsigned int)(value
>> 24));
204 cx_writeb(P1_RADDR0
, (unsigned int)address
);
205 cx_writeb(P1_RADDR1
, (unsigned int)(address
>> 8));
206 cx_writeb(P1_RRDWR
, 1);
210 return wait_ready_gpio0_bit1(core
,1);
214 static int register_read(struct cx88_core
*core
, u32 address
, u32
*value
)
219 cx_writeb(P1_RADDR0
, (unsigned int)address
);
220 cx_writeb(P1_RADDR1
, (unsigned int)(address
>> 8));
221 cx_writeb(P1_RRDWR
, 0);
224 retval
= wait_ready_gpio0_bit1(core
,1);
225 val
= (unsigned char)cx_read(P1_RDATA0
);
226 val
|= (unsigned char)cx_read(P1_RDATA1
) << 8;
227 val
|= (unsigned char)cx_read(P1_RDATA2
) << 16;
228 val
|= (unsigned char)cx_read(P1_RDATA3
) << 24;
234 /* ------------------------------------------------------------------ */
236 /* We don't need to call the API often, so using just one mailbox will probably suffice */
237 static int blackbird_api_cmd(struct cx8802_dev
*dev
, u32 command
,
238 u32 inputcnt
, u32 outputcnt
, ...)
240 unsigned long timeout
;
241 u32 value
, flag
, retval
;
244 va_start(args
, outputcnt
);
246 dprintk(1,"%s: 0x%X\n", __FUNCTION__
, command
);
248 /* this may not be 100% safe if we can't read any memory location
249 without side effects */
250 memory_read(dev
->core
, dev
->mailbox
- 4, &value
);
251 if (value
!= 0x12345678) {
252 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
256 memory_read(dev
->core
, dev
->mailbox
, &flag
);
258 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag
);
262 flag
|= 1; /* tell 'em we're working on it */
263 memory_write(dev
->core
, dev
->mailbox
, flag
);
265 /* write command + args + fill remaining with zeros */
266 memory_write(dev
->core
, dev
->mailbox
+ 1, command
); /* command code */
267 memory_write(dev
->core
, dev
->mailbox
+ 3, IVTV_API_STD_TIMEOUT
); /* timeout */
268 for (i
= 0; i
< inputcnt
; i
++) {
269 value
= va_arg(args
, int);
270 memory_write(dev
->core
, dev
->mailbox
+ 4 + i
, value
);
271 dprintk(1, "API Input %d = %d\n", i
, value
);
274 memory_write(dev
->core
, dev
->mailbox
+ 4 + i
, 0);
276 flag
|= 3; /* tell 'em we're done writing */
277 memory_write(dev
->core
, dev
->mailbox
, flag
);
279 /* wait for firmware to handle the API command */
280 timeout
= jiffies
+ msecs_to_jiffies(10);
282 memory_read(dev
->core
, dev
->mailbox
, &flag
);
285 if (time_after(jiffies
,timeout
)) {
286 dprintk(0, "ERROR: API Mailbox timeout\n");
292 /* read output values */
293 for (i
= 0; i
< outputcnt
; i
++) {
294 int *vptr
= va_arg(args
, int *);
295 memory_read(dev
->core
, dev
->mailbox
+ 4 + i
, vptr
);
296 dprintk(1, "API Output %d = %d\n", i
, *vptr
);
300 memory_read(dev
->core
, dev
->mailbox
+ 2, &retval
);
301 dprintk(1, "API result = %d\n",retval
);
304 memory_write(dev
->core
, dev
->mailbox
, flag
);
309 static int blackbird_find_mailbox(struct cx8802_dev
*dev
)
311 u32 signature
[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
316 for (i
= 0; i
< BLACKBIRD_FIRM_IMAGE_SIZE
; i
++) {
317 memory_read(dev
->core
, i
, &value
);
318 if (value
== signature
[signaturecnt
])
322 if (4 == signaturecnt
) {
323 dprintk(1, "Mailbox signature found\n");
327 dprintk(0, "Mailbox signature values not found!\n");
331 static int blackbird_load_firmware(struct cx8802_dev
*dev
)
333 static const unsigned char magic
[8] = {
334 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
336 const struct firmware
*firmware
;
342 retval
= register_write(dev
->core
, IVTV_REG_VPU
, 0xFFFFFFED);
343 retval
|= register_write(dev
->core
, IVTV_REG_HW_BLOCKS
, IVTV_CMD_HW_BLOCKS_RST
);
344 retval
|= register_write(dev
->core
, IVTV_REG_ENC_SDRAM_REFRESH
, 0x80000640);
345 retval
|= register_write(dev
->core
, IVTV_REG_ENC_SDRAM_PRECHARGE
, 0x1A);
347 retval
|= register_write(dev
->core
, IVTV_REG_APU
, 0);
350 dprintk(0, "Error with register_write\n");
352 retval
= request_firmware(&firmware
, BLACKBIRD_FIRM_ENC_FILENAME
,
355 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
356 BLACKBIRD_FIRM_ENC_FILENAME
);
357 dprintk(0, "Please fix your hotplug setup, the board will "
358 "not work without firmware loaded!\n");
362 if (firmware
->size
!= BLACKBIRD_FIRM_IMAGE_SIZE
) {
363 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
364 firmware
->size
, BLACKBIRD_FIRM_IMAGE_SIZE
);
368 if (0 != memcmp(firmware
->data
, magic
, 8)) {
369 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
373 /* transfer to the chip */
374 dprintk(1,"Loading firmware ...\n");
375 dataptr
= (u32
*)firmware
->data
;
376 for (i
= 0; i
< (firmware
->size
>> 2); i
++) {
379 memory_write(dev
->core
, i
, value
);
383 /* read back to verify with the checksum */
384 for (i
--; i
>= 0; i
--) {
385 memory_read(dev
->core
, i
, &value
);
389 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
392 release_firmware(firmware
);
393 dprintk(0, "Firmware upload successful.\n");
395 retval
|= register_write(dev
->core
, IVTV_REG_HW_BLOCKS
, IVTV_CMD_HW_BLOCKS_RST
);
396 retval
|= register_read(dev
->core
, IVTV_REG_SPU
, &value
);
397 retval
|= register_write(dev
->core
, IVTV_REG_SPU
, value
& 0xFFFFFFFE);
400 retval
|= register_read(dev
->core
, IVTV_REG_VPU
, &value
);
401 retval
|= register_write(dev
->core
, IVTV_REG_VPU
, value
& 0xFFFFFFE8);
404 dprintk(0, "Error with register_write\n");
408 static void blackbird_codec_settings(struct cx8802_dev
*dev
)
410 int bitrate_mode
= 1;
411 int bitrate
= 7500000;
412 int bitrate_peak
= 7500000;
414 /* assign stream type */
415 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_STREAM_TYPE
, 1, 0, 0); /* program stream */
416 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 2); /* MPEG1 stream */
417 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 3); /* PES A/V */
418 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 10); /* DVD stream */
420 /* assign output port */
421 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_OUTPUT_PORT
, 1, 0, 1); /* 1 = Host */
423 /* assign framerate */
424 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_FRAMERATE
, 1, 0, 0);
426 /* assign frame size */
427 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_FRAME_SIZE
, 2, 0,
428 dev
->height
, dev
->width
);
430 /* assign aspect ratio */
431 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_ASPECT_RATIO
, 1, 0, 2);
433 /* assign bitrates */
434 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_BITRATES
, 5, 0,
435 bitrate_mode
, /* mode */
437 bitrate_peak
/ 400, /* peak/400 */
438 0, 0x70); /* encoding buffer, ckennedy */
440 /* assign gop properties */
441 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_GOP_PROPERTIES
, 2, 0, 15, 3);
442 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 2, 1);
444 /* assign 3 2 pulldown */
445 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_3_2_PULLDOWN
, 1, 0, 0);
447 /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
448 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_AUDIO_PROPERTIES
, 1, 0, (2<<2) | (8<<4));
450 /* assign gop closure */
451 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_GOP_CLOSURE
, 1, 0, 0);
453 /* assign audio properties */
454 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_AUDIO_PROPERTIES
, 1, 0, 0 | (2 << 2) | (14 << 4));
456 /* assign dnr filter mode */
457 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_DNR_FILTER_MODE
, 2, 0, 0, 0);
459 /* assign dnr filter props*/
460 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_DNR_FILTER_PROPS
, 2, 0, 0, 0);
462 /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
463 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_CORING_LEVELS
, 4, 0, 0, 255, 0, 255);
465 /* assign spatial filter type: luma_t: 1 = horiz_only, chroma_t: 1 = horiz_only */
466 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE
, 2, 0, 1, 1);
468 /* assign frame drop rate */
469 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_FRAME_DROP_RATE
, 1, 0, 0);
472 static int blackbird_initialize_codec(struct cx8802_dev
*dev
)
474 struct cx88_core
*core
= dev
->core
;
478 dprintk(1,"Initialize codec\n");
479 retval
= blackbird_api_cmd(dev
, IVTV_API_ENC_PING_FW
, 0, 0); /* ping */
481 /* ping was not successful, reset and upload firmware */
482 cx_write(MO_SRST_IO
, 0); /* SYS_RSTO=0 */
484 cx_write(MO_SRST_IO
, 1); /* SYS_RSTO=1 */
486 retval
= blackbird_load_firmware(dev
);
490 dev
->mailbox
= blackbird_find_mailbox(dev
);
491 if (dev
->mailbox
< 0)
494 retval
= blackbird_api_cmd(dev
, IVTV_API_ENC_PING_FW
, 0, 0); /* ping */
496 dprintk(0, "ERROR: Firmware ping failed!\n");
500 retval
= blackbird_api_cmd(dev
, IVTV_API_ENC_GETVER
, 0, 1, &version
);
502 dprintk(0, "ERROR: Firmware get encoder version failed!\n");
505 dprintk(0, "Firmware version is 0x%08x\n", version
);
509 cx_write(MO_PINMUX_IO
, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
510 cx_clear(MO_INPUT_FORMAT
, 0x100); /* chroma subcarrier lock to normal? */
511 cx_write(MO_VBOS_CONTROL
, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
512 cx_clear(MO_OUTPUT_FORMAT
, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
515 set_scale(dev
, 720, 480, V4L2_FIELD_INTERLACED
);
517 blackbird_codec_settings(dev
);
520 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
521 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_NUM_VSYNC_LINES
, 4, 0, 0xf0, 0xf0);
522 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180);
523 blackbird_api_cmd(dev
, IVTV_API_ASSIGN_PLACEHOLDER
, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
525 blackbird_api_cmd(dev
, IVTV_API_INITIALIZE_INPUT
, 0, 0); /* initialize the video input */
529 blackbird_api_cmd(dev
, IVTV_API_MUTE_VIDEO
, 1, 0, 0);
531 blackbird_api_cmd(dev
, IVTV_API_MUTE_AUDIO
, 1, 0, 0);
534 blackbird_api_cmd(dev
, IVTV_API_BEGIN_CAPTURE
, 2, 0, 0, 0x13); /* start capturing to the host interface */
535 //blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0); /* start capturing to the host interface */
538 blackbird_api_cmd(dev
, IVTV_API_REFRESH_INPUT
, 0,0);
542 /* ------------------------------------------------------------------ */
544 static int bb_buf_setup(struct videobuf_queue
*q
,
545 unsigned int *count
, unsigned int *size
)
547 struct cx8802_fh
*fh
= q
->priv_data
;
549 fh
->dev
->ts_packet_size
= 512;
550 fh
->dev
->ts_packet_count
= 100;
552 *size
= fh
->dev
->ts_packet_size
* fh
->dev
->ts_packet_count
;
563 bb_buf_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
564 enum v4l2_field field
)
566 struct cx8802_fh
*fh
= q
->priv_data
;
567 return cx8802_buf_prepare(fh
->dev
, (struct cx88_buffer
*)vb
);
571 bb_buf_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
573 struct cx8802_fh
*fh
= q
->priv_data
;
574 cx8802_buf_queue(fh
->dev
, (struct cx88_buffer
*)vb
);
578 bb_buf_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
580 struct cx8802_fh
*fh
= q
->priv_data
;
581 cx88_free_buffer(fh
->dev
->pci
, (struct cx88_buffer
*)vb
);
584 static struct videobuf_queue_ops blackbird_qops
= {
585 .buf_setup
= bb_buf_setup
,
586 .buf_prepare
= bb_buf_prepare
,
587 .buf_queue
= bb_buf_queue
,
588 .buf_release
= bb_buf_release
,
591 /* ------------------------------------------------------------------ */
593 static int mpeg_do_ioctl(struct inode
*inode
, struct file
*file
,
594 unsigned int cmd
, void *arg
)
596 struct cx8802_fh
*fh
= file
->private_data
;
597 struct cx8802_dev
*dev
= fh
->dev
;
600 cx88_print_ioctl(dev
->core
->name
,cmd
);
604 /* --- capture ioctls ---------------------------------------- */
605 case VIDIOC_ENUM_FMT
:
607 struct v4l2_fmtdesc
*f
= arg
;
614 memset(f
,0,sizeof(*f
));
616 strlcpy(f
->description
, "MPEG TS", sizeof(f
->description
));
617 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
618 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
625 /* FIXME -- quick'n'dirty for exactly one size ... */
626 struct v4l2_format
*f
= arg
;
628 memset(f
,0,sizeof(*f
));
629 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
630 f
->fmt
.pix
.width
= dev
->width
;
631 f
->fmt
.pix
.height
= dev
->height
;
632 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
633 f
->fmt
.pix
.sizeimage
= 1024 * 512 /* FIXME: BUFFER_SIZE */;
636 /* --- streaming capture ------------------------------------- */
638 return videobuf_reqbufs(&fh
->mpegq
, arg
);
640 case VIDIOC_QUERYBUF
:
641 return videobuf_querybuf(&fh
->mpegq
, arg
);
644 return videobuf_qbuf(&fh
->mpegq
, arg
);
647 return videobuf_dqbuf(&fh
->mpegq
, arg
,
648 file
->f_flags
& O_NONBLOCK
);
650 case VIDIOC_STREAMON
:
651 return videobuf_streamon(&fh
->mpegq
);
653 case VIDIOC_STREAMOFF
:
654 return videobuf_streamoff(&fh
->mpegq
);
662 static int mpeg_ioctl(struct inode
*inode
, struct file
*file
,
663 unsigned int cmd
, unsigned long arg
)
665 return video_usercopy(inode
, file
, cmd
, arg
, mpeg_do_ioctl
);
668 static int mpeg_open(struct inode
*inode
, struct file
*file
)
670 int minor
= iminor(inode
);
671 struct cx8802_dev
*h
,*dev
= NULL
;
672 struct cx8802_fh
*fh
;
673 struct list_head
*list
;
675 list_for_each(list
,&cx8802_devlist
) {
676 h
= list_entry(list
, struct cx8802_dev
, devlist
);
677 if (h
->mpeg_dev
->minor
== minor
)
683 if (blackbird_initialize_codec(dev
) < 0)
685 dprintk(1,"open minor=%d\n",minor
);
687 /* allocate + initialize per filehandle data */
688 fh
= kmalloc(sizeof(*fh
),GFP_KERNEL
);
691 memset(fh
,0,sizeof(*fh
));
692 file
->private_data
= fh
;
695 /* FIXME: locking against other video device */
696 cx88_set_scale(dev
->core
, dev
->width
, dev
->height
,
697 V4L2_FIELD_INTERLACED
);
699 videobuf_queue_init(&fh
->mpegq
, &blackbird_qops
,
700 dev
->pci
, &dev
->slock
,
701 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
703 sizeof(struct cx88_buffer
),
708 static int mpeg_release(struct inode
*inode
, struct file
*file
)
710 struct cx8802_fh
*fh
= file
->private_data
;
712 blackbird_api_cmd(fh
->dev
, IVTV_API_END_CAPTURE
, 3, 0, 1, 0, 0x13);
714 /* stop mpeg capture */
715 if (fh
->mpegq
.streaming
)
716 videobuf_streamoff(&fh
->mpegq
);
717 if (fh
->mpegq
.reading
)
718 videobuf_read_stop(&fh
->mpegq
);
720 videobuf_mmap_free(&fh
->mpegq
);
721 file
->private_data
= NULL
;
727 mpeg_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
729 struct cx8802_fh
*fh
= file
->private_data
;
731 return videobuf_read_stream(&fh
->mpegq
, data
, count
, ppos
, 0,
732 file
->f_flags
& O_NONBLOCK
);
736 mpeg_poll(struct file
*file
, struct poll_table_struct
*wait
)
738 struct cx8802_fh
*fh
= file
->private_data
;
740 return videobuf_poll_stream(file
, &fh
->mpegq
, wait
);
744 mpeg_mmap(struct file
*file
, struct vm_area_struct
* vma
)
746 struct cx8802_fh
*fh
= file
->private_data
;
748 return videobuf_mmap_mapper(&fh
->mpegq
, vma
);
751 static struct file_operations mpeg_fops
=
753 .owner
= THIS_MODULE
,
755 .release
= mpeg_release
,
763 static struct video_device cx8802_mpeg_template
=
766 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|VID_TYPE_SCALES
|VID_TYPE_MPEG_ENCODER
,
772 /* ------------------------------------------------------------------ */
774 static void blackbird_unregister_video(struct cx8802_dev
*dev
)
777 if (-1 != dev
->mpeg_dev
->minor
)
778 video_unregister_device(dev
->mpeg_dev
);
780 video_device_release(dev
->mpeg_dev
);
781 dev
->mpeg_dev
= NULL
;
785 static int blackbird_register_video(struct cx8802_dev
*dev
)
789 dev
->mpeg_dev
= cx88_vdev_init(dev
->core
,dev
->pci
,
790 &cx8802_mpeg_template
,"mpeg");
791 err
= video_register_device(dev
->mpeg_dev
,VFL_TYPE_GRABBER
, -1);
793 printk(KERN_INFO
"%s/2: can't register mpeg device\n",
797 printk(KERN_INFO
"%s/2: registered device video%d [mpeg]\n",
798 dev
->core
->name
,dev
->mpeg_dev
->minor
& 0x1f);
802 /* ----------------------------------------------------------- */
804 static int __devinit
blackbird_probe(struct pci_dev
*pci_dev
,
805 const struct pci_device_id
*pci_id
)
807 struct cx8802_dev
*dev
;
808 struct cx88_core
*core
;
812 core
= cx88_core_get(pci_dev
);
817 if (!cx88_boards
[core
->board
].blackbird
)
821 dev
= kmalloc(sizeof(*dev
),GFP_KERNEL
);
824 memset(dev
,0,sizeof(*dev
));
830 err
= cx8802_init_common(dev
);
834 /* blackbird stuff */
835 printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
837 host_setup(dev
->core
);
839 list_add_tail(&dev
->devlist
,&cx8802_devlist
);
840 blackbird_register_video(dev
);
846 cx88_core_put(core
,pci_dev
);
850 static void __devexit
blackbird_remove(struct pci_dev
*pci_dev
)
852 struct cx8802_dev
*dev
= pci_get_drvdata(pci_dev
);
855 blackbird_unregister_video(dev
);
856 list_del(&dev
->devlist
);
859 cx8802_fini_common(dev
);
860 cx88_core_put(dev
->core
,dev
->pci
);
864 static struct pci_device_id cx8802_pci_tbl
[] = {
868 .subvendor
= PCI_ANY_ID
,
869 .subdevice
= PCI_ANY_ID
,
871 /* --- end of list --- */
874 MODULE_DEVICE_TABLE(pci
, cx8802_pci_tbl
);
876 static struct pci_driver blackbird_pci_driver
= {
877 .name
= "cx88-blackbird",
878 .id_table
= cx8802_pci_tbl
,
879 .probe
= blackbird_probe
,
880 .remove
= __devexit_p(blackbird_remove
),
881 .suspend
= cx8802_suspend_common
,
882 .resume
= cx8802_resume_common
,
885 static int blackbird_init(void)
887 printk(KERN_INFO
"cx2388x blackbird driver version %d.%d.%d loaded\n",
888 (CX88_VERSION_CODE
>> 16) & 0xff,
889 (CX88_VERSION_CODE
>> 8) & 0xff,
890 CX88_VERSION_CODE
& 0xff);
892 printk(KERN_INFO
"cx2388x: snapshot date %04d-%02d-%02d\n",
893 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
895 return pci_register_driver(&blackbird_pci_driver
);
898 static void blackbird_fini(void)
900 pci_unregister_driver(&blackbird_pci_driver
);
903 module_init(blackbird_init
);
904 module_exit(blackbird_fini
);
906 /* ----------------------------------------------------------- */