V4L/DVB (3973a): Return value is ssize_t
[deliverable/linux.git] / drivers / media / video / cx88 / cx88-blackbird.c
CommitLineData
1da177e4 1/*
1da177e4
LT
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 * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
10 *
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.
15 *
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.
20 *
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.
24 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
29#include <linux/fs.h>
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/firmware.h>
855dbada
MK
33#include <media/v4l2-common.h>
34#include <media/cx2341x.h>
1da177e4
LT
35
36#include "cx88.h"
37
38MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
d21838dd 39MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
1da177e4
LT
40MODULE_LICENSE("GPL");
41
31629424 42static unsigned int mpegbufs = 32;
1da177e4
LT
43module_param(mpegbufs,int,0644);
44MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
45
46static unsigned int debug = 0;
47module_param(debug,int,0644);
48MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
49
50#define dprintk(level,fmt, arg...) if (debug >= level) \
51 printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
52
53static LIST_HEAD(cx8802_devlist);
54
55/* ------------------------------------------------------------------ */
56
861018a6 57#define BLACKBIRD_FIRM_ENC_FILENAME "v4l-cx2341x-enc.fw"
1da177e4
LT
58#define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
59
60/* defines below are from ivtv-driver.h */
61
62#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
63
b45009b0 64/* Firmware API commands */
b45009b0
MCC
65#define IVTV_API_STD_TIMEOUT 500
66
b45009b0
MCC
67enum blackbird_capture_type {
68 BLACKBIRD_MPEG_CAPTURE,
69 BLACKBIRD_RAW_CAPTURE,
70 BLACKBIRD_RAW_PASSTHRU_CAPTURE
71};
72enum blackbird_capture_bits {
73 BLACKBIRD_RAW_BITS_NONE = 0x00,
74 BLACKBIRD_RAW_BITS_YUV_CAPTURE = 0x01,
75 BLACKBIRD_RAW_BITS_PCM_CAPTURE = 0x02,
76 BLACKBIRD_RAW_BITS_VBI_CAPTURE = 0x04,
77 BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
78 BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE = 0x10
79};
b45009b0
MCC
80enum blackbird_capture_end {
81 BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
82 BLACKBIRD_END_NOW, /* stop immediately, no irq */
83};
b45009b0
MCC
84enum blackbird_framerate {
85 BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
86 BLACKBIRD_FRAMERATE_PAL_25 /* PAL: 25fps */
87};
b45009b0
MCC
88enum blackbird_video_bitrate_type {
89 BLACKBIRD_VIDEO_VBR,
90 BLACKBIRD_VIDEO_CBR
91};
92#define BLACKBIRD_PEAK_RATE_DIVISOR 400
93enum blackbird_mux_rate {
94 BLACKBIRD_MUX_RATE_DEFAULT,
95 /* dvd mux rate: multiply by 400 to get the actual rate */
96 BLACKBIRD_MUX_RATE_DVD = 25200
97};
b45009b0
MCC
98enum blackbird_aspect_ratio {
99 BLACKBIRD_ASPECT_RATIO_FORBIDDEN,
100 BLACKBIRD_ASPECT_RATIO_1_1_SQUARE,
101 BLACKBIRD_ASPECT_RATIO_4_3,
102 BLACKBIRD_ASPECT_RATIO_16_9,
103 BLACKBIRD_ASPECT_RATIO_221_100,
104 BLACKBIRD_ASPECT_RATIO_RESERVED
105};
b45009b0
MCC
106enum blackbird_dnr_bits {
107 BLACKBIRD_DNR_BITS_MANUAL,
108 BLACKBIRD_DNR_BITS_AUTO_SPATIAL,
109 BLACKBIRD_DNR_BITS_AUTO_TEMPORAL,
110 BLACKBIRD_DNR_BITS_AUTO
111};
112enum blackbird_median_filter {
113 BLACKBIRD_MEDIAN_FILTER_DISABLED,
114 BLACKBIRD_MEDIAN_FILTER_HORIZONTAL,
115 BLACKBIRD_MEDIAN_FILTER_VERTICAL,
116 BLACKBIRD_MEDIAN_FILTER_HV,
117 BLACKBIRD_MEDIAN_FILTER_DIAGONAL
118};
b45009b0
MCC
119enum blackbird_spatial_filter_luma {
120 BLACKBIRD_SPATIAL_FILTER_LUMA_DISABLED,
121 BLACKBIRD_SPATIAL_FILTER_LUMA_1D_HORIZ,
122 BLACKBIRD_SPATIAL_FILTER_LUMA_1D_VERT,
123 BLACKBIRD_SPATIAL_FILTER_LUMA_2D_HV, /* separable, default */
124 BLACKBIRD_SPATIAL_FILTER_LUMA_2D_SYMM /* symmetric non-separable */
125};
126enum blackbird_spatial_filter_chroma {
127 BLACKBIRD_SPATIAL_FILTER_CHROMA_DISABLED,
128 BLACKBIRD_SPATIAL_FILTER_CHROMA_1D_HORIZ /* default */
129};
b45009b0
MCC
130enum blackbird_pulldown {
131 BLACKBIRD_3_2_PULLDOWN_DISABLED,
132 BLACKBIRD_3_2_PULLDOWN_ENABLED
133};
b45009b0
MCC
134enum blackbird_vbi_line_bits {
135 BLACKBIRD_VBI_LINE_BITS_TOP_FIELD,
136 BLACKBIRD_VBI_LINE_BITS_BOT_FIELD = (1 << 31),
137 BLACKBIRD_VBI_LINE_BITS_ALL_LINES = 0xFFFFFFFF
138};
139enum blackbird_vbi_line {
140 BLACKBIRD_VBI_LINE_DISABLED,
141 BLACKBIRD_VBI_LINE_ENABLED
142};
143enum blackbird_vbi_slicing {
144 BLACKBIRD_VBI_SLICING_NONE,
145 BLACKBIRD_VBI_SLICING_CLOSED_CAPTION
146};
b45009b0
MCC
147enum blackbird_stream_type {
148 BLACKBIRD_STREAM_PROGRAM,
149 BLACKBIRD_STREAM_TRANSPORT,
150 BLACKBIRD_STREAM_MPEG1,
151 BLACKBIRD_STREAM_PES_AV,
152 BLACKBIRD_STREAM_UNKNOWN4,
153 BLACKBIRD_STREAM_PES_VIDEO,
154 BLACKBIRD_STREAM_UNKNOWN6,
155 BLACKBIRD_STREAM_PES_AUDIO,
156 BLACKBIRD_STREAM_UNKNOWN8,
157 BLACKBIRD_STREAM_UNKNOWN9, /* audio/pcm ? */
158 BLACKBIRD_STREAM_DVD,
159 BLACKBIRD_STREAM_VCD,
160 BLACKBIRD_STREAM_UNKNOWN12 /* svcd/xvcd ? */
161};
b45009b0
MCC
162enum blackbird_stream_port {
163 BLACKBIRD_OUTPUT_PORT_MEMORY,
164 BLACKBIRD_OUTPUT_PORT_STREAMING,
165 BLACKBIRD_OUTPUT_PORT_SERIAL
166};
b45009b0
MCC
167enum blackbird_audio_bits_sample_rate {
168 BLACKBIRD_AUDIO_BITS_44100HZ,
169 BLACKBIRD_AUDIO_BITS_48000HZ,
170 BLACKBIRD_AUDIO_BITS_32000HZ,
171 BLACKBIRD_AUDIO_BITS_RESERVED_HZ,
172};
173enum blackbird_audio_bits_encoding {
174 BLACKBIRD_AUDIO_BITS_LAYER_1 = 0x1 << 2,
175 BLACKBIRD_AUDIO_BITS_LAYER_2 = 0x2 << 2,
176};
177enum blackbird_audio_bits_bitrate_layer_1 {
178 BLACKBIRD_AUDIO_BITS_LAYER_1_FREE_FORMAT,
179 BLACKBIRD_AUDIO_BITS_LAYER_1_32 = 0x01 << 4,
180 BLACKBIRD_AUDIO_BITS_LAYER_1_64 = 0x02 << 4,
181 BLACKBIRD_AUDIO_BITS_LAYER_1_96 = 0x03 << 4,
182 BLACKBIRD_AUDIO_BITS_LAYER_1_128 = 0x04 << 4,
183 BLACKBIRD_AUDIO_BITS_LAYER_1_160 = 0x05 << 4,
184 BLACKBIRD_AUDIO_BITS_LAYER_1_192 = 0x06 << 4,
185 BLACKBIRD_AUDIO_BITS_LAYER_1_224 = 0x07 << 4,
186 BLACKBIRD_AUDIO_BITS_LAYER_1_256 = 0x08 << 4,
187 BLACKBIRD_AUDIO_BITS_LAYER_1_288 = 0x09 << 4,
188 BLACKBIRD_AUDIO_BITS_LAYER_1_320 = 0x0A << 4,
189 BLACKBIRD_AUDIO_BITS_LAYER_1_352 = 0x0B << 4,
190 BLACKBIRD_AUDIO_BITS_LAYER_1_384 = 0x0C << 4,
191 BLACKBIRD_AUDIO_BITS_LAYER_1_416 = 0x0D << 4,
192 BLACKBIRD_AUDIO_BITS_LAYER_1_448 = 0x0E << 4,
193};
194enum blackbird_audio_bits_bitrate_layer_2 {
195 BLACKBIRD_AUDIO_BITS_LAYER_2_FREE_FORMAT,
196 BLACKBIRD_AUDIO_BITS_LAYER_2_32 = 0x01 << 4,
197 BLACKBIRD_AUDIO_BITS_LAYER_2_48 = 0x02 << 4,
198 BLACKBIRD_AUDIO_BITS_LAYER_2_56 = 0x03 << 4,
199 BLACKBIRD_AUDIO_BITS_LAYER_2_64 = 0x04 << 4,
200 BLACKBIRD_AUDIO_BITS_LAYER_2_80 = 0x05 << 4,
201 BLACKBIRD_AUDIO_BITS_LAYER_2_96 = 0x06 << 4,
202 BLACKBIRD_AUDIO_BITS_LAYER_2_112 = 0x07 << 4,
203 BLACKBIRD_AUDIO_BITS_LAYER_2_128 = 0x08 << 4,
204 BLACKBIRD_AUDIO_BITS_LAYER_2_160 = 0x09 << 4,
205 BLACKBIRD_AUDIO_BITS_LAYER_2_192 = 0x0A << 4,
206 BLACKBIRD_AUDIO_BITS_LAYER_2_224 = 0x0B << 4,
207 BLACKBIRD_AUDIO_BITS_LAYER_2_256 = 0x0C << 4,
208 BLACKBIRD_AUDIO_BITS_LAYER_2_320 = 0x0D << 4,
209 BLACKBIRD_AUDIO_BITS_LAYER_2_384 = 0x0E << 4,
210};
211enum blackbird_audio_bits_mode {
212 BLACKBIRD_AUDIO_BITS_STEREO,
213 BLACKBIRD_AUDIO_BITS_JOINT_STEREO = 0x1 << 8,
214 BLACKBIRD_AUDIO_BITS_DUAL = 0x2 << 8,
215 BLACKBIRD_AUDIO_BITS_MONO = 0x3 << 8,
216};
217enum blackbird_audio_bits_mode_extension {
218 BLACKBIRD_AUDIO_BITS_BOUND_4,
219 BLACKBIRD_AUDIO_BITS_BOUND_8 = 0x1 << 10,
220 BLACKBIRD_AUDIO_BITS_BOUND_12 = 0x2 << 10,
221 BLACKBIRD_AUDIO_BITS_BOUND_16 = 0x3 << 10,
222};
223enum blackbird_audio_bits_emphasis {
224 BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE,
225 BLACKBIRD_AUDIO_BITS_EMPHASIS_50_15 = 0x1 << 12,
226 BLACKBIRD_AUDIO_BITS_EMPHASIS_RESERVED = 0x2 << 12,
227 BLACKBIRD_AUDIO_BITS_EMPHASIS_CCITT_J17 = 0x3 << 12,
228};
229enum blackbird_audio_bits_crc {
230 BLACKBIRD_AUDIO_BITS_CRC_OFF,
231 BLACKBIRD_AUDIO_BITS_CRC_ON = 0x1 << 14,
232};
233enum blackbird_audio_bits_copyright {
234 BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF,
235 BLACKBIRD_AUDIO_BITS_COPYRIGHT_ON = 0x1 << 15,
236};
237enum blackbird_audio_bits_original {
238 BLACKBIRD_AUDIO_BITS_COPY,
239 BLACKBIRD_AUDIO_BITS_ORIGINAL = 0x1 << 16,
240};
b45009b0
MCC
241enum blackbird_gop_closure {
242 BLACKBIRD_GOP_CLOSURE_OFF,
243 BLACKBIRD_GOP_CLOSURE_ON,
244};
b45009b0
MCC
245enum blackbird_data_xfer_status {
246 BLACKBIRD_MORE_BUFFERS_FOLLOW,
247 BLACKBIRD_LAST_BUFFER,
248};
b45009b0
MCC
249enum blackbird_picture_mask {
250 BLACKBIRD_PICTURE_MASK_NONE,
251 BLACKBIRD_PICTURE_MASK_I_FRAMES,
252 BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
253 BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
254};
b45009b0
MCC
255enum blackbird_vbi_mode_bits {
256 BLACKBIRD_VBI_BITS_SLICED,
257 BLACKBIRD_VBI_BITS_RAW,
258};
259enum blackbird_vbi_insertion_bits {
260 BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
261 BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
262 BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
263 BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
264 BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
265};
b45009b0
MCC
266enum blackbird_dma_unit {
267 BLACKBIRD_DMA_BYTES,
268 BLACKBIRD_DMA_FRAMES,
269};
b45009b0
MCC
270enum blackbird_dma_transfer_status_bits {
271 BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
272 BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
273 BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
274};
b45009b0
MCC
275enum blackbird_pause {
276 BLACKBIRD_PAUSE_ENCODING,
277 BLACKBIRD_RESUME_ENCODING,
278};
b45009b0
MCC
279enum blackbird_copyright {
280 BLACKBIRD_COPYRIGHT_OFF,
281 BLACKBIRD_COPYRIGHT_ON,
282};
b45009b0
MCC
283enum blackbird_notification_type {
284 BLACKBIRD_NOTIFICATION_REFRESH,
285};
286enum blackbird_notification_status {
287 BLACKBIRD_NOTIFICATION_OFF,
288 BLACKBIRD_NOTIFICATION_ON,
289};
290enum blackbird_notification_mailbox {
291 BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
292};
b45009b0
MCC
293enum blackbird_field1_lines {
294 BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
295 BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
296 BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
297};
298enum blackbird_field2_lines {
299 BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
300 BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
301 BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
302};
b45009b0
MCC
303enum blackbird_custom_data_type {
304 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
305 BLACKBIRD_CUSTOM_PRIVATE_PACKET,
306};
b45009b0
MCC
307enum blackbird_mute {
308 BLACKBIRD_UNMUTE,
309 BLACKBIRD_MUTE,
310};
311enum blackbird_mute_video_mask {
312 BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
313 BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
314 BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
315};
316enum blackbird_mute_video_shift {
317 BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
318 BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
319 BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
320};
1da177e4
LT
321
322/* Registers */
323#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
324#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
325#define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
326#define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
327#define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
328#define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
329
330/* ------------------------------------------------------------------ */
331
332static void host_setup(struct cx88_core *core)
333{
334 /* toggle reset of the host */
335 cx_write(MO_GPHST_SOFT_RST, 1);
336 udelay(100);
337 cx_write(MO_GPHST_SOFT_RST, 0);
338 udelay(100);
339
340 /* host port setup */
341 cx_write(MO_GPHST_WSC, 0x44444444U);
342 cx_write(MO_GPHST_XFR, 0);
343 cx_write(MO_GPHST_WDTH, 15);
344 cx_write(MO_GPHST_HDSHK, 0);
345 cx_write(MO_GPHST_MUX16, 0x44448888U);
346 cx_write(MO_GPHST_MODE, 0);
347}
348
349/* ------------------------------------------------------------------ */
350
351#define P1_MDATA0 0x390000
352#define P1_MDATA1 0x390001
353#define P1_MDATA2 0x390002
354#define P1_MDATA3 0x390003
355#define P1_MADDR2 0x390004
356#define P1_MADDR1 0x390005
357#define P1_MADDR0 0x390006
358#define P1_RDATA0 0x390008
359#define P1_RDATA1 0x390009
360#define P1_RDATA2 0x39000A
361#define P1_RDATA3 0x39000B
362#define P1_RADDR0 0x39000C
363#define P1_RADDR1 0x39000D
364#define P1_RRDWR 0x39000E
365
366static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
367{
368 unsigned long timeout = jiffies + msecs_to_jiffies(1);
369 u32 gpio0,need;
370
371 need = state ? 2 : 0;
372 for (;;) {
373 gpio0 = cx_read(MO_GP0_IO) & 2;
374 if (need == gpio0)
375 return 0;
376 if (time_after(jiffies,timeout))
377 return -1;
378 udelay(1);
379 }
380}
381
382static int memory_write(struct cx88_core *core, u32 address, u32 value)
383{
384 /* Warning: address is dword address (4 bytes) */
385 cx_writeb(P1_MDATA0, (unsigned int)value);
386 cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
387 cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
388 cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
389 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
390 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
391 cx_writeb(P1_MADDR0, (unsigned int)address);
392 cx_read(P1_MDATA0);
393 cx_read(P1_MADDR0);
394
395 return wait_ready_gpio0_bit1(core,1);
396}
397
398static int memory_read(struct cx88_core *core, u32 address, u32 *value)
399{
4ac97914 400 int retval;
1da177e4
LT
401 u32 val;
402
403 /* Warning: address is dword address (4 bytes) */
404 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
405 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
406 cx_writeb(P1_MADDR0, (unsigned int)address);
407 cx_read(P1_MADDR0);
408
409 retval = wait_ready_gpio0_bit1(core,1);
410
411 cx_writeb(P1_MDATA3, 0);
412 val = (unsigned char)cx_read(P1_MDATA3) << 24;
413 cx_writeb(P1_MDATA2, 0);
414 val |= (unsigned char)cx_read(P1_MDATA2) << 16;
415 cx_writeb(P1_MDATA1, 0);
416 val |= (unsigned char)cx_read(P1_MDATA1) << 8;
417 cx_writeb(P1_MDATA0, 0);
418 val |= (unsigned char)cx_read(P1_MDATA0);
419
420 *value = val;
421 return retval;
422}
423
424static int register_write(struct cx88_core *core, u32 address, u32 value)
425{
426 cx_writeb(P1_RDATA0, (unsigned int)value);
427 cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
428 cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
429 cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
430 cx_writeb(P1_RADDR0, (unsigned int)address);
431 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
432 cx_writeb(P1_RRDWR, 1);
433 cx_read(P1_RDATA0);
434 cx_read(P1_RADDR0);
435
436 return wait_ready_gpio0_bit1(core,1);
437}
438
439
440static int register_read(struct cx88_core *core, u32 address, u32 *value)
441{
442 int retval;
443 u32 val;
444
445 cx_writeb(P1_RADDR0, (unsigned int)address);
446 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
447 cx_writeb(P1_RRDWR, 0);
448 cx_read(P1_RADDR0);
449
450 retval = wait_ready_gpio0_bit1(core,1);
451 val = (unsigned char)cx_read(P1_RDATA0);
452 val |= (unsigned char)cx_read(P1_RDATA1) << 8;
453 val |= (unsigned char)cx_read(P1_RDATA2) << 16;
454 val |= (unsigned char)cx_read(P1_RDATA3) << 24;
455
456 *value = val;
457 return retval;
458}
459
460/* ------------------------------------------------------------------ */
461
462/* We don't need to call the API often, so using just one mailbox will probably suffice */
463static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
464 u32 inputcnt, u32 outputcnt, ...)
465{
466 unsigned long timeout;
467 u32 value, flag, retval;
468 int i;
469 va_list args;
470 va_start(args, outputcnt);
471
472 dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
473
474 /* this may not be 100% safe if we can't read any memory location
475 without side effects */
476 memory_read(dev->core, dev->mailbox - 4, &value);
477 if (value != 0x12345678) {
478 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
479 return -1;
480 }
481
482 memory_read(dev->core, dev->mailbox, &flag);
483 if (flag) {
484 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
485 return -1;
486 }
487
488 flag |= 1; /* tell 'em we're working on it */
489 memory_write(dev->core, dev->mailbox, flag);
490
491 /* write command + args + fill remaining with zeros */
492 memory_write(dev->core, dev->mailbox + 1, command); /* command code */
493 memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
494 for (i = 0; i < inputcnt ; i++) {
495 value = va_arg(args, int);
496 memory_write(dev->core, dev->mailbox + 4 + i, value);
497 dprintk(1, "API Input %d = %d\n", i, value);
498 }
499 for (; i < 16 ; i++)
500 memory_write(dev->core, dev->mailbox + 4 + i, 0);
501
502 flag |= 3; /* tell 'em we're done writing */
503 memory_write(dev->core, dev->mailbox, flag);
504
505 /* wait for firmware to handle the API command */
506 timeout = jiffies + msecs_to_jiffies(10);
507 for (;;) {
508 memory_read(dev->core, dev->mailbox, &flag);
509 if (0 != (flag & 4))
510 break;
511 if (time_after(jiffies,timeout)) {
512 dprintk(0, "ERROR: API Mailbox timeout\n");
513 return -1;
514 }
515 udelay(10);
516 }
517
518 /* read output values */
519 for (i = 0; i < outputcnt ; i++) {
520 int *vptr = va_arg(args, int *);
521 memory_read(dev->core, dev->mailbox + 4 + i, vptr);
522 dprintk(1, "API Output %d = %d\n", i, *vptr);
523 }
524 va_end(args);
525
526 memory_read(dev->core, dev->mailbox + 2, &retval);
527 dprintk(1, "API result = %d\n",retval);
528
529 flag = 0;
530 memory_write(dev->core, dev->mailbox, flag);
531 return retval;
532}
533
534
535static int blackbird_find_mailbox(struct cx8802_dev *dev)
536{
537 u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
538 int signaturecnt=0;
539 u32 value;
540 int i;
541
542 for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
543 memory_read(dev->core, i, &value);
544 if (value == signature[signaturecnt])
545 signaturecnt++;
546 else
547 signaturecnt = 0;
548 if (4 == signaturecnt) {
549 dprintk(1, "Mailbox signature found\n");
550 return i+1;
551 }
552 }
553 dprintk(0, "Mailbox signature values not found!\n");
554 return -1;
555}
556
557static int blackbird_load_firmware(struct cx8802_dev *dev)
558{
559 static const unsigned char magic[8] = {
560 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
561 };
562 const struct firmware *firmware;
563 int i, retval = 0;
564 u32 value = 0;
565 u32 checksum = 0;
566 u32 *dataptr;
567
568 retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
4ac97914
MCC
569 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
570 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
571 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
1da177e4 572 msleep(1);
4ac97914 573 retval |= register_write(dev->core, IVTV_REG_APU, 0);
1da177e4
LT
574
575 if (retval < 0)
576 dprintk(0, "Error with register_write\n");
577
578 retval = request_firmware(&firmware, BLACKBIRD_FIRM_ENC_FILENAME,
579 &dev->pci->dev);
674434c6
MCC
580
581
1da177e4
LT
582 if (retval != 0) {
583 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
584 BLACKBIRD_FIRM_ENC_FILENAME);
585 dprintk(0, "Please fix your hotplug setup, the board will "
586 "not work without firmware loaded!\n");
587 return -1;
588 }
589
590 if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
591 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
592 firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
593 return -1;
594 }
595
596 if (0 != memcmp(firmware->data, magic, 8)) {
597 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
598 return -1;
599 }
600
601 /* transfer to the chip */
602 dprintk(1,"Loading firmware ...\n");
603 dataptr = (u32*)firmware->data;
604 for (i = 0; i < (firmware->size >> 2); i++) {
605 value = *dataptr;
606 checksum += ~value;
607 memory_write(dev->core, i, value);
608 dataptr++;
609 }
610
611 /* read back to verify with the checksum */
612 for (i--; i >= 0; i--) {
613 memory_read(dev->core, i, &value);
614 checksum -= ~value;
615 }
616 if (checksum) {
617 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
618 return -1;
619 }
620 release_firmware(firmware);
621 dprintk(0, "Firmware upload successful.\n");
622
4ac97914
MCC
623 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
624 retval |= register_read(dev->core, IVTV_REG_SPU, &value);
625 retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
1da177e4
LT
626 msleep(1);
627
628 retval |= register_read(dev->core, IVTV_REG_VPU, &value);
4ac97914 629 retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
1da177e4
LT
630
631 if (retval < 0)
632 dprintk(0, "Error with register_write\n");
633 return 0;
634}
635
b45009b0
MCC
636/**
637 Settings used by the windows tv app for PVR2000:
638=================================================================================================================
639Profile | Codec | Resolution | CBR/VBR | Video Qlty | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
640-----------------------------------------------------------------------------------------------------------------
641MPEG-1 | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 2000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
642MPEG-2 | MPEG2 | 720x576PAL | VBR | 600 :Good | 4000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
643VCD | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 1150 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
644DVD | MPEG2 | 720x576PAL | VBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
645DB* DVD | MPEG2 | 720x576PAL | CBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
646=================================================================================================================
647*DB: "DirectBurn"
648*/
31629424
CC
649
650static struct blackbird_dnr default_dnr_params = {
651 .mode = BLACKBIRD_DNR_BITS_MANUAL,
652 .type = BLACKBIRD_MEDIAN_FILTER_DISABLED,
653 .spatial = 0,
654 .temporal = 0
655};
656static struct v4l2_mpeg_compression default_mpeg_params = {
657 .st_type = V4L2_MPEG_PS_2,
658 .st_bitrate = {
659 .mode = V4L2_BITRATE_CBR,
660 .min = 0,
661 .target = 0,
662 .max = 0
663 },
664 .ts_pid_pmt = 16,
665 .ts_pid_audio = 260,
666 .ts_pid_video = 256,
667 .ts_pid_pcr = 259,
668 .ps_size = 0,
669 .au_type = V4L2_MPEG_AU_2_II,
670 .au_bitrate = {
671 .mode = V4L2_BITRATE_CBR,
672 .min = 224,
673 .target = 224,
674 .max = 224
675 },
676 .au_sample_rate = 44100,
677 .au_pesid = 0,
678 .vi_type = V4L2_MPEG_VI_2,
679 .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
680 .vi_bitrate = {
681 .mode = V4L2_BITRATE_CBR,
682 .min = 4000,
683 .target = 4500,
684 .max = 6000
685 },
686 .vi_frame_rate = 25,
687 .vi_frames_per_gop = 15,
688 .vi_bframes_count = 2,
689 .vi_pesid = 0,
690 .closed_gops = 0,
691 .pulldown = 0
692};
693
694static enum blackbird_stream_type mpeg_stream_types[] = {
695 [V4L2_MPEG_SS_1] = BLACKBIRD_STREAM_MPEG1,
696 [V4L2_MPEG_PS_2] = BLACKBIRD_STREAM_PROGRAM,
697 [V4L2_MPEG_TS_2] = BLACKBIRD_STREAM_TRANSPORT,
698 [V4L2_MPEG_PS_DVD] = BLACKBIRD_STREAM_DVD,
699};
700static enum blackbird_aspect_ratio mpeg_stream_ratios[] = {
701 [V4L2_MPEG_ASPECT_SQUARE] = BLACKBIRD_ASPECT_RATIO_1_1_SQUARE,
702 [V4L2_MPEG_ASPECT_4_3] = BLACKBIRD_ASPECT_RATIO_4_3,
703 [V4L2_MPEG_ASPECT_16_9] = BLACKBIRD_ASPECT_RATIO_16_9,
704 [V4L2_MPEG_ASPECT_1_221] = BLACKBIRD_ASPECT_RATIO_221_100,
705};
706static enum blackbird_video_bitrate_type mpeg_video_bitrates[] = {
707 [V4L2_BITRATE_NONE] = BLACKBIRD_VIDEO_CBR,
708 [V4L2_BITRATE_CBR] = BLACKBIRD_VIDEO_CBR,
709 [V4L2_BITRATE_VBR] = BLACKBIRD_VIDEO_VBR,
710};
711/* find the best layer I/II bitrate to fit a given numeric value */
712struct bitrate_bits {
713 u32 bits; /* layer bits for the best fit */
714 u32 rate; /* actual numeric value for the layer best fit */
715};
716struct bitrate_approximation {
717 u32 target; /* numeric value of the rate we want */
718 struct bitrate_bits layer[2];
719};
720static struct bitrate_approximation mpeg_audio_bitrates[] = {
721 /* target layer[0].bits layer[0].rate layer[1].bits layer[1].rate */
722 { 0, { { 0, 0, }, { 0, 0, }, }, },
723 { 32, { { BLACKBIRD_AUDIO_BITS_LAYER_1_32 , 32, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_32 , 32, }, }, },
724 { 48, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 , 64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_48 , 48, }, }, },
725 { 56, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 , 64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_56 , 56, }, }, },
726 { 64, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 , 64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_64 , 64, }, }, },
727 { 80, { { BLACKBIRD_AUDIO_BITS_LAYER_1_96 , 96, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_80 , 80, }, }, },
728 { 96, { { BLACKBIRD_AUDIO_BITS_LAYER_1_96 , 96, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_96 , 96, }, }, },
729 { 112, { { BLACKBIRD_AUDIO_BITS_LAYER_1_128, 128, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_112, 112, }, }, },
730 { 128, { { BLACKBIRD_AUDIO_BITS_LAYER_1_128, 128, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_128, 128, }, }, },
731 { 160, { { BLACKBIRD_AUDIO_BITS_LAYER_1_160, 160, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_160, 160, }, }, },
732 { 192, { { BLACKBIRD_AUDIO_BITS_LAYER_1_192, 192, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_192, 192, }, }, },
733 { 224, { { BLACKBIRD_AUDIO_BITS_LAYER_1_224, 224, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_224, 224, }, }, },
734 { 256, { { BLACKBIRD_AUDIO_BITS_LAYER_1_256, 256, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_256, 256, }, }, },
735 { 288, { { BLACKBIRD_AUDIO_BITS_LAYER_1_288, 288, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_320, 320, }, }, },
736 { 320, { { BLACKBIRD_AUDIO_BITS_LAYER_1_320, 320, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_320, 320, }, }, },
737 { 352, { { BLACKBIRD_AUDIO_BITS_LAYER_1_352, 352, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
738 { 384, { { BLACKBIRD_AUDIO_BITS_LAYER_1_384, 384, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
739 { 416, { { BLACKBIRD_AUDIO_BITS_LAYER_1_416, 416, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
740 { 448, { { BLACKBIRD_AUDIO_BITS_LAYER_1_448, 448, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
741};
742static const int BITRATES_SIZE = ARRAY_SIZE(mpeg_audio_bitrates);
743
744static void blackbird_set_default_params(struct cx8802_dev *dev)
1da177e4 745{
31629424 746 struct v4l2_mpeg_compression *params = &dev->params;
c5df599b 747 u32 au_params;
1da177e4
LT
748
749 /* assign stream type */
31629424
CC
750 if( params->st_type >= ARRAY_SIZE(mpeg_stream_types) )
751 params->st_type = V4L2_MPEG_PS_2;
752 if( params->st_type == V4L2_MPEG_SS_1 )
753 params->vi_type = V4L2_MPEG_VI_1;
754 else
755 params->vi_type = V4L2_MPEG_VI_2;
855dbada 756 blackbird_api_cmd(dev, CX2341X_ENC_SET_STREAM_TYPE, 1, 0, mpeg_stream_types[params->st_type]);
1da177e4 757
b45009b0 758 /* assign framerate */
31629424
CC
759 if( params->vi_frame_rate <= 25 )
760 {
761 params->vi_frame_rate = 25;
855dbada 762 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_RATE, 1, 0, BLACKBIRD_FRAMERATE_PAL_25);
31629424
CC
763 }
764 else
765 {
766 params->vi_frame_rate = 30;
855dbada 767 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_RATE, 1, 0, BLACKBIRD_FRAMERATE_NTSC_30);
31629424 768 }
1da177e4 769
b45009b0 770 /* assign aspect ratio */
31629424
CC
771 if( params->vi_aspect_ratio >= ARRAY_SIZE(mpeg_stream_ratios) )
772 params->vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3;
855dbada 773 blackbird_api_cmd(dev, CX2341X_ENC_SET_ASPECT_RATIO, 1, 0, mpeg_stream_ratios[params->vi_aspect_ratio]);
b45009b0
MCC
774
775 /* assign gop properties */
855dbada 776 blackbird_api_cmd(dev, CX2341X_ENC_SET_GOP_PROPERTIES, 2, 0, params->vi_frames_per_gop, params->vi_bframes_count+1);
31629424
CC
777
778 /* assign gop closure */
855dbada 779 blackbird_api_cmd(dev, CX2341X_ENC_SET_GOP_CLOSURE, 1, 0, params->closed_gops);
b45009b0
MCC
780
781 /* assign 3 2 pulldown */
855dbada 782 blackbird_api_cmd(dev, CX2341X_ENC_SET_3_2_PULLDOWN, 1, 0, params->pulldown);
31629424
CC
783
784 /* make sure the params are within bounds */
785 if( params->st_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
786 params->vi_bitrate.mode = V4L2_BITRATE_NONE;
787 if( params->vi_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
788 params->vi_bitrate.mode = V4L2_BITRATE_NONE;
789 if( params->au_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
790 params->au_bitrate.mode = V4L2_BITRATE_NONE;
b45009b0
MCC
791
792 /* assign audio properties */
793 /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
c5df599b 794 au_params = BLACKBIRD_AUDIO_BITS_STEREO |
b45009b0
MCC
795 /* BLACKBIRD_AUDIO_BITS_BOUND_4 | */
796 BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE |
797 BLACKBIRD_AUDIO_BITS_CRC_OFF |
798 BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF |
31629424
CC
799 BLACKBIRD_AUDIO_BITS_COPY |
800 0;
801 if( params->au_sample_rate <= 32000 )
802 {
803 params->au_sample_rate = 32000;
804 au_params |= BLACKBIRD_AUDIO_BITS_32000HZ;
805 }
806 else if( params->au_sample_rate <= 44100 )
807 {
808 params->au_sample_rate = 44100;
809 au_params |= BLACKBIRD_AUDIO_BITS_44100HZ;
810 }
811 else
812 {
813 params->au_sample_rate = 48000;
814 au_params |= BLACKBIRD_AUDIO_BITS_48000HZ;
815 }
816 if( params->au_type == V4L2_MPEG_AU_2_I )
817 {
818 au_params |= BLACKBIRD_AUDIO_BITS_LAYER_1;
819 }
820 else
821 {
822 /* TODO: try to handle the other formats more gracefully */
823 params->au_type = V4L2_MPEG_AU_2_II;
824 au_params |= BLACKBIRD_AUDIO_BITS_LAYER_2;
825 }
826 if( params->au_bitrate.mode )
827 {
c5df599b
CC
828 int layer;
829
31629424
CC
830 if( params->au_bitrate.mode == V4L2_BITRATE_CBR )
831 params->au_bitrate.max = params->vi_bitrate.target;
832 else
833 params->au_bitrate.target = params->vi_bitrate.max;
834
c5df599b 835 layer = params->au_type;
31629424
CC
836 if( params->au_bitrate.target == 0 )
837 {
838 /* TODO: use the minimum possible bitrate instead of 0 ? */
839 au_params |= 0;
840 }
841 else if( params->au_bitrate.target >=
4ac97914 842 mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate )
31629424
CC
843 {
844 /* clamp the bitrate to the max supported by the standard */
845 params->au_bitrate.target = mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate;
846 params->au_bitrate.max = params->au_bitrate.target;
847 au_params |= mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].bits;
848 }
849 else
850 {
851 /* round up to the nearest supported bitrate */
852 int i;
853 for(i = 1; i < BITRATES_SIZE; i++)
854 {
855 if( params->au_bitrate.target > mpeg_audio_bitrates[i-1].layer[layer].rate &&
856 params->au_bitrate.target <= mpeg_audio_bitrates[i].layer[layer].rate )
857 {
858 params->au_bitrate.target = mpeg_audio_bitrates[i].layer[layer].rate;
859 params->au_bitrate.max = params->au_bitrate.target;
860 au_params |= mpeg_audio_bitrates[i].layer[layer].bits;
861 break;
862 }
863 }
864 }
865 }
866 else
867 {
868 /* TODO: ??? */
869 params->au_bitrate.target = params->au_bitrate.max = 0;
870 au_params |= 0;
871 }
855dbada 872 blackbird_api_cmd(dev, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, 0, au_params );
31629424
CC
873
874 /* assign bitrates */
875 if( params->vi_bitrate.mode )
876 {
877 /* bitrate is set, let's figure out the cbr/vbr mess */
878 if( params->vi_bitrate.max < params->vi_bitrate.target )
879 {
880 if( params->vi_bitrate.mode == V4L2_BITRATE_CBR )
881 params->vi_bitrate.max = params->vi_bitrate.target;
882 else
883 params->vi_bitrate.target = params->vi_bitrate.max;
884 }
885 }
886 else
887 {
888 if( params->st_bitrate.max < params->st_bitrate.target )
889 {
890 if( params->st_bitrate.mode == V4L2_BITRATE_VBR )
891 params->st_bitrate.target = params->st_bitrate.max;
892 else
893 params->st_bitrate.max = params->st_bitrate.target;
894 }
895 /* calculate vi_bitrate = st_bitrate - au_bitrate */
896 params->vi_bitrate.max = params->st_bitrate.max - params->au_bitrate.max;
897 params->vi_bitrate.target = params->st_bitrate.target - params->au_bitrate.target;
898 }
855dbada 899 blackbird_api_cmd(dev, CX2341X_ENC_SET_BIT_RATE, 4, 0,
31629424
CC
900 mpeg_video_bitrates[params->vi_bitrate.mode],
901 params->vi_bitrate.target * 1000, /* kbps -> bps */
902 params->vi_bitrate.max * 1000 / BLACKBIRD_PEAK_RATE_DIVISOR, /* peak/400 */
903 BLACKBIRD_MUX_RATE_DEFAULT /*, 0x70*/); /* encoding buffer, ckennedy */
904
905 /* TODO: implement the stream ID stuff:
906 ts_pid_pmt, ts_pid_audio, ts_pid_video, ts_pid_pcr,
907 ps_size, au_pesid, vi_pesid
4ac97914 908 */
31629424
CC
909}
910#define CHECK_PARAM( name ) ( dev->params.name != params->name )
911#define IF_PARAM( name ) if( CHECK_PARAM( name ) )
912#define UPDATE_PARAM( name ) dev->params.name = params->name
913void blackbird_set_params(struct cx8802_dev *dev, struct v4l2_mpeg_compression *params)
914{
c5df599b
CC
915 u32 au_params;
916
31629424
CC
917 /* assign stream type */
918 if( params->st_type >= ARRAY_SIZE(mpeg_stream_types) )
919 params->st_type = V4L2_MPEG_PS_2;
920 if( params->st_type == V4L2_MPEG_SS_1 )
921 params->vi_type = V4L2_MPEG_VI_1;
922 else
923 params->vi_type = V4L2_MPEG_VI_2;
924 if( CHECK_PARAM( st_type ) || CHECK_PARAM( vi_type ) )
925 {
926 UPDATE_PARAM( st_type );
927 UPDATE_PARAM( vi_type );
855dbada 928 blackbird_api_cmd(dev, CX2341X_ENC_SET_STREAM_TYPE, 1, 0, mpeg_stream_types[params->st_type]);
31629424
CC
929 }
930
931 /* assign framerate */
932 if( params->vi_frame_rate <= 25 )
933 params->vi_frame_rate = 25;
934 else
935 params->vi_frame_rate = 30;
936 IF_PARAM( vi_frame_rate )
937 {
938 UPDATE_PARAM( vi_frame_rate );
939 if( params->vi_frame_rate == 25 )
855dbada 940 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_RATE, 1, 0, BLACKBIRD_FRAMERATE_PAL_25);
31629424 941 else
855dbada 942 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_RATE, 1, 0, BLACKBIRD_FRAMERATE_NTSC_30);
31629424
CC
943 }
944
945 /* assign aspect ratio */
946 if( params->vi_aspect_ratio >= ARRAY_SIZE(mpeg_stream_ratios) )
947 params->vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3;
948 IF_PARAM( vi_aspect_ratio )
949 {
950 UPDATE_PARAM( vi_aspect_ratio );
855dbada 951 blackbird_api_cmd(dev, CX2341X_ENC_SET_ASPECT_RATIO, 1, 0, mpeg_stream_ratios[params->vi_aspect_ratio]);
31629424
CC
952 }
953
954 /* assign gop properties */
955 if( CHECK_PARAM( vi_frames_per_gop ) || CHECK_PARAM( vi_bframes_count ) )
956 {
957 UPDATE_PARAM( vi_frames_per_gop );
958 UPDATE_PARAM( vi_bframes_count );
855dbada 959 blackbird_api_cmd(dev, CX2341X_ENC_SET_GOP_PROPERTIES, 2, 0, params->vi_frames_per_gop, params->vi_bframes_count+1);
31629424 960 }
1da177e4
LT
961
962 /* assign gop closure */
31629424
CC
963 IF_PARAM( closed_gops )
964 {
965 UPDATE_PARAM( closed_gops );
855dbada 966 blackbird_api_cmd(dev, CX2341X_ENC_SET_GOP_CLOSURE, 1, 0, params->closed_gops);
31629424
CC
967 }
968
969 /* assign 3 2 pulldown */
970 IF_PARAM( pulldown )
971 {
972 UPDATE_PARAM( pulldown );
855dbada 973 blackbird_api_cmd(dev, CX2341X_ENC_SET_3_2_PULLDOWN, 1, 0, params->pulldown);
31629424
CC
974 }
975
976 /* make sure the params are within bounds */
977 if( params->st_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
978 params->vi_bitrate.mode = V4L2_BITRATE_NONE;
979 if( params->vi_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
980 params->vi_bitrate.mode = V4L2_BITRATE_NONE;
981 if( params->au_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
982 params->au_bitrate.mode = V4L2_BITRATE_NONE;
983
984 /* assign audio properties */
985 /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
c5df599b 986 au_params = BLACKBIRD_AUDIO_BITS_STEREO |
31629424
CC
987 /* BLACKBIRD_AUDIO_BITS_BOUND_4 | */
988 BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE |
989 BLACKBIRD_AUDIO_BITS_CRC_OFF |
990 BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF |
991 BLACKBIRD_AUDIO_BITS_COPY |
992 0;
993 if( params->au_sample_rate < 32000 )
994 {
995 params->au_sample_rate = 32000;
996 au_params |= BLACKBIRD_AUDIO_BITS_32000HZ;
997 }
998 else if( params->au_sample_rate < 44100 )
999 {
1000 params->au_sample_rate = 44100;
1001 au_params |= BLACKBIRD_AUDIO_BITS_44100HZ;
1002 }
1003 else
1004 {
1005 params->au_sample_rate = 48000;
1006 au_params |= BLACKBIRD_AUDIO_BITS_48000HZ;
1007 }
1008 if( params->au_type == V4L2_MPEG_AU_2_I )
1009 {
1010 au_params |= BLACKBIRD_AUDIO_BITS_LAYER_1;
1011 }
1012 else
1013 {
1014 /* TODO: try to handle the other formats more gracefully */
1015 params->au_type = V4L2_MPEG_AU_2_II;
1016 au_params |= BLACKBIRD_AUDIO_BITS_LAYER_2;
1017 }
1018 if( params->au_bitrate.mode )
1019 {
c5df599b
CC
1020 int layer;
1021
31629424
CC
1022 if( params->au_bitrate.mode == V4L2_BITRATE_CBR )
1023 params->au_bitrate.max = params->vi_bitrate.target;
1024 else
1025 params->au_bitrate.target = params->vi_bitrate.max;
1da177e4 1026
c5df599b 1027 layer = params->au_type;
31629424
CC
1028 if( params->au_bitrate.target == 0 )
1029 {
1030 /* TODO: use the minimum possible bitrate instead of 0 ? */
1031 au_params |= 0;
1032 }
1033 else if( params->au_bitrate.target >=
4ac97914 1034 mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate )
31629424
CC
1035 {
1036 /* clamp the bitrate to the max supported by the standard */
1037 params->au_bitrate.target = mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate;
1038 params->au_bitrate.max = params->au_bitrate.target;
1039 au_params |= mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].bits;
1040 }
1041 else
1042 {
1043 /* round up to the nearest supported bitrate */
1044 int i;
1045 for(i = 1; i < BITRATES_SIZE; i++)
1046 {
1047 if( params->au_bitrate.target > mpeg_audio_bitrates[i-1].layer[layer].rate &&
1048 params->au_bitrate.target <= mpeg_audio_bitrates[i].layer[layer].rate )
1049 {
1050 params->au_bitrate.target = mpeg_audio_bitrates[i].layer[layer].rate;
1051 params->au_bitrate.max = params->au_bitrate.target;
1052 au_params |= mpeg_audio_bitrates[i].layer[layer].bits;
1053 break;
1054 }
1055 }
1056 }
1057 }
1058 else
1059 {
1060 /* TODO: ??? */
1061 params->au_bitrate.target = params->au_bitrate.max = 0;
1062 au_params |= 0;
1063 }
1064 if( CHECK_PARAM( au_type ) || CHECK_PARAM( au_sample_rate )
1065 || CHECK_PARAM( au_bitrate.mode ) || CHECK_PARAM( au_bitrate.max )
1066 || CHECK_PARAM( au_bitrate.target )
1067 )
1068 {
1069 UPDATE_PARAM( au_type );
1070 UPDATE_PARAM( au_sample_rate );
1071 UPDATE_PARAM( au_bitrate );
855dbada 1072 blackbird_api_cmd(dev, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, 0, au_params );
31629424 1073 }
1da177e4 1074
31629424
CC
1075 /* assign bitrates */
1076 if( params->vi_bitrate.mode )
1077 {
1078 /* bitrate is set, let's figure out the cbr/vbr mess */
1079 if( params->vi_bitrate.max < params->vi_bitrate.target )
1080 {
1081 if( params->vi_bitrate.mode == V4L2_BITRATE_CBR )
1082 params->vi_bitrate.max = params->vi_bitrate.target;
1083 else
1084 params->vi_bitrate.target = params->vi_bitrate.max;
1085 }
1086 }
1087 else
1088 {
1089 if( params->st_bitrate.max < params->st_bitrate.target )
1090 {
1091 if( params->st_bitrate.mode == V4L2_BITRATE_VBR )
1092 params->st_bitrate.target = params->st_bitrate.max;
1093 else
1094 params->st_bitrate.max = params->st_bitrate.target;
1095 }
1096 /* calculate vi_bitrate = st_bitrate - au_bitrate */
1097 params->vi_bitrate.max = params->st_bitrate.max - params->au_bitrate.max;
1098 params->vi_bitrate.target = params->st_bitrate.target - params->au_bitrate.target;
1099 }
1100 UPDATE_PARAM( st_bitrate );
1101 if( CHECK_PARAM( vi_bitrate.mode ) || CHECK_PARAM( vi_bitrate.max )
1102 || CHECK_PARAM( vi_bitrate.target )
1103 )
1104 {
1105 UPDATE_PARAM( vi_bitrate );
855dbada 1106 blackbird_api_cmd(dev, CX2341X_ENC_SET_BIT_RATE, 4, 0,
31629424
CC
1107 mpeg_video_bitrates[params->vi_bitrate.mode],
1108 params->vi_bitrate.target * 1000, /* kbps -> bps */
1109 params->vi_bitrate.max * 1000 / BLACKBIRD_PEAK_RATE_DIVISOR, /* peak/400 */
1110 BLACKBIRD_MUX_RATE_DEFAULT /*, 0x70*/); /* encoding buffer, ckennedy */
1111 }
1112
1113 /* TODO: implement the stream ID stuff:
1114 ts_pid_pmt, ts_pid_audio, ts_pid_video, ts_pid_pcr,
1115 ps_size, au_pesid, vi_pesid
4ac97914 1116 */
31629424
CC
1117 UPDATE_PARAM( ts_pid_pmt );
1118 UPDATE_PARAM( ts_pid_audio );
1119 UPDATE_PARAM( ts_pid_video );
1120 UPDATE_PARAM( ts_pid_pcr );
1121 UPDATE_PARAM( ps_size );
1122 UPDATE_PARAM( au_pesid );
1123 UPDATE_PARAM( vi_pesid );
1124}
1125
1126static void blackbird_set_default_dnr_params(struct cx8802_dev *dev)
1127{
b45009b0 1128 /* assign dnr filter mode */
31629424
CC
1129 if( dev->dnr_params.mode > BLACKBIRD_DNR_BITS_AUTO )
1130 dev->dnr_params.mode = BLACKBIRD_DNR_BITS_MANUAL;
1131 if( dev->dnr_params.type > BLACKBIRD_MEDIAN_FILTER_DIAGONAL )
1132 dev->dnr_params.type = BLACKBIRD_MEDIAN_FILTER_DISABLED;
855dbada 1133 blackbird_api_cmd(dev, CX2341X_ENC_SET_DNR_FILTER_MODE, 2, 0,
31629424
CC
1134 dev->dnr_params.mode,
1135 dev->dnr_params.type
1136 );
1137
1138 /* assign dnr filter props*/
1139 if( dev->dnr_params.spatial > 15 )
1140 dev->dnr_params.spatial = 15;
1141 if( dev->dnr_params.temporal > 31 )
1142 dev->dnr_params.temporal = 31;
855dbada 1143 blackbird_api_cmd(dev, CX2341X_ENC_SET_DNR_FILTER_PROPS, 2, 0,
31629424
CC
1144 dev->dnr_params.spatial,
1145 dev->dnr_params.temporal
1146 );
1147}
1148#define CHECK_DNR_PARAM( name ) ( dev->dnr_params.name != dnr_params->name )
1149#define UPDATE_DNR_PARAM( name ) dev->dnr_params.name = dnr_params->name
1150void blackbird_set_dnr_params(struct cx8802_dev *dev, struct blackbird_dnr* dnr_params)
1151{
1152 /* assign dnr filter mode */
1153 /* clamp values */
1154 if( dnr_params->mode > BLACKBIRD_DNR_BITS_AUTO )
1155 dnr_params->mode = BLACKBIRD_DNR_BITS_MANUAL;
1156 if( dnr_params->type > BLACKBIRD_MEDIAN_FILTER_DIAGONAL )
1157 dnr_params->type = BLACKBIRD_MEDIAN_FILTER_DISABLED;
1158 /* check if the params actually changed */
1159 if( CHECK_DNR_PARAM( mode ) || CHECK_DNR_PARAM( type ) )
1160 {
1161 UPDATE_DNR_PARAM( mode );
1162 UPDATE_DNR_PARAM( type );
855dbada 1163 blackbird_api_cmd(dev, CX2341X_ENC_SET_DNR_FILTER_MODE, 2, 0, dnr_params->mode, dnr_params->type);
31629424 1164 }
1da177e4 1165
b45009b0 1166 /* assign dnr filter props*/
31629424
CC
1167 if( dnr_params->spatial > 15 )
1168 dnr_params->spatial = 15;
1169 if( dnr_params->temporal > 31 )
1170 dnr_params->temporal = 31;
1171 if( CHECK_DNR_PARAM( spatial ) || CHECK_DNR_PARAM( temporal ) )
1172 {
1173 UPDATE_DNR_PARAM( spatial );
1174 UPDATE_DNR_PARAM( temporal );
855dbada 1175 blackbird_api_cmd(dev, CX2341X_ENC_SET_DNR_FILTER_PROPS, 2, 0, dnr_params->spatial, dnr_params->temporal);
31629424
CC
1176 }
1177}
1178
1179static void blackbird_codec_settings(struct cx8802_dev *dev)
1180{
1181
1182 /* assign output port */
855dbada 1183 blackbird_api_cmd(dev, CX2341X_ENC_SET_OUTPUT_PORT, 1, 0, BLACKBIRD_OUTPUT_PORT_STREAMING); /* Host */
31629424
CC
1184
1185 /* assign frame size */
855dbada 1186 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
31629424 1187 dev->height, dev->width);
1da177e4 1188
b45009b0 1189 /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
855dbada 1190 blackbird_api_cmd(dev, CX2341X_ENC_SET_CORING_LEVELS, 4, 0, 0, 255, 0, 255);
1da177e4 1191
b45009b0 1192 /* assign spatial filter type: luma_t: horiz_only, chroma_t: horiz_only */
855dbada 1193 blackbird_api_cmd(dev, CX2341X_ENC_SET_SPATIAL_FILTER_TYPE, 2, 0,
31629424
CC
1194 BLACKBIRD_SPATIAL_FILTER_LUMA_1D_HORIZ,
1195 BLACKBIRD_SPATIAL_FILTER_CHROMA_1D_HORIZ
1196 );
1da177e4 1197
b45009b0
MCC
1198 /* assign frame drop rate */
1199 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_DROP_RATE, 1, 0, 0); */
31629424
CC
1200
1201 blackbird_set_default_params(dev);
1202 blackbird_set_default_dnr_params(dev);
1da177e4
LT
1203}
1204
1205static int blackbird_initialize_codec(struct cx8802_dev *dev)
1206{
1207 struct cx88_core *core = dev->core;
1208 int version;
1209 int retval;
1210
1211 dprintk(1,"Initialize codec\n");
855dbada 1212 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
1da177e4
LT
1213 if (retval < 0) {
1214 /* ping was not successful, reset and upload firmware */
1215 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
1216 msleep(1);
1217 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
1218 msleep(1);
1219 retval = blackbird_load_firmware(dev);
1220 if (retval < 0)
1221 return retval;
1222
1223 dev->mailbox = blackbird_find_mailbox(dev);
1224 if (dev->mailbox < 0)
1225 return -1;
1226
855dbada 1227 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
1da177e4
LT
1228 if (retval < 0) {
1229 dprintk(0, "ERROR: Firmware ping failed!\n");
1230 return -1;
1231 }
1232
855dbada 1233 retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
1da177e4
LT
1234 if (retval < 0) {
1235 dprintk(0, "ERROR: Firmware get encoder version failed!\n");
1236 return -1;
1237 }
1238 dprintk(0, "Firmware version is 0x%08x\n", version);
1239 }
1240 msleep(1);
1241
1242 cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
1243 cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
1244 cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
1245 cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
1246
1da177e4
LT
1247 blackbird_codec_settings(dev);
1248 msleep(1);
1249
b45009b0
MCC
1250 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
1251 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
1252 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
855dbada 1253 blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
b45009b0 1254 BLACKBIRD_FIELD1_SAA7115,
8a17ef97 1255 BLACKBIRD_FIELD2_SAA7115
b45009b0
MCC
1256 );
1257
1258 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
855dbada 1259 blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
b45009b0
MCC
1260 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
1261 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1da177e4 1262
e52e98a7 1263 /* initialize the video input */
855dbada 1264 blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
1da177e4
LT
1265
1266 msleep(1);
1267
855dbada 1268 blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
1da177e4 1269 msleep(1);
855dbada 1270 blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
1da177e4
LT
1271 msleep(1);
1272
e52e98a7 1273 /* start capturing to the host interface */
855dbada
MK
1274 /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
1275 blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
b45009b0
MCC
1276 BLACKBIRD_MPEG_CAPTURE,
1277 BLACKBIRD_RAW_BITS_NONE
e52e98a7 1278 );
b45009b0 1279 msleep(10);
1da177e4 1280
855dbada 1281 blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
1da177e4
LT
1282 return 0;
1283}
1284
1285/* ------------------------------------------------------------------ */
1286
1287static int bb_buf_setup(struct videobuf_queue *q,
1288 unsigned int *count, unsigned int *size)
1289{
1290 struct cx8802_fh *fh = q->priv_data;
1291
e52e98a7 1292 fh->dev->ts_packet_size = 188 * 4; /* was: 512 */
31629424 1293 fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
1da177e4
LT
1294
1295 *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
31629424 1296 *count = fh->dev->ts_packet_count;
1da177e4
LT
1297 return 0;
1298}
1299
1300static int
1301bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1302 enum v4l2_field field)
1303{
1304 struct cx8802_fh *fh = q->priv_data;
c7b0ac05 1305 return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
1da177e4
LT
1306}
1307
1308static void
1309bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1310{
1311 struct cx8802_fh *fh = q->priv_data;
1312 cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
1313}
1314
1315static void
1316bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1317{
c7b0ac05 1318 cx88_free_buffer(q, (struct cx88_buffer*)vb);
1da177e4
LT
1319}
1320
1321static struct videobuf_queue_ops blackbird_qops = {
1322 .buf_setup = bb_buf_setup,
1323 .buf_prepare = bb_buf_prepare,
1324 .buf_queue = bb_buf_queue,
1325 .buf_release = bb_buf_release,
1326};
1327
1328/* ------------------------------------------------------------------ */
1329
1330static int mpeg_do_ioctl(struct inode *inode, struct file *file,
1331 unsigned int cmd, void *arg)
1332{
1333 struct cx8802_fh *fh = file->private_data;
1334 struct cx8802_dev *dev = fh->dev;
e52e98a7 1335 struct cx88_core *core = dev->core;
1da177e4
LT
1336
1337 if (debug > 1)
5e453dc7 1338 v4l_print_ioctl(core->name,cmd);
1da177e4
LT
1339
1340 switch (cmd) {
1341
e52e98a7
MCC
1342 /* --- capabilities ------------------------------------------ */
1343 case VIDIOC_QUERYCAP:
1344 {
1345 struct v4l2_capability *cap = arg;
1346
1347 memset(cap,0,sizeof(*cap));
1348 strcpy(cap->driver, "cx88_blackbird");
1349 strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card));
1350 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1351 cap->version = CX88_VERSION_CODE;
1352 cap->capabilities =
1353 V4L2_CAP_VIDEO_CAPTURE |
1354 V4L2_CAP_READWRITE |
1355 V4L2_CAP_STREAMING |
e52e98a7
MCC
1356 0;
1357 if (UNSET != core->tuner_type)
1358 cap->capabilities |= V4L2_CAP_TUNER;
1359
1360 return 0;
1361 }
1362
1da177e4
LT
1363 /* --- capture ioctls ---------------------------------------- */
1364 case VIDIOC_ENUM_FMT:
1365 {
1366 struct v4l2_fmtdesc *f = arg;
1367 int index;
1368
1369 index = f->index;
1370 if (index != 0)
1371 return -EINVAL;
1372
1373 memset(f,0,sizeof(*f));
1374 f->index = index;
31629424 1375 strlcpy(f->description, "MPEG", sizeof(f->description));
1da177e4
LT
1376 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1377 f->pixelformat = V4L2_PIX_FMT_MPEG;
1378 return 0;
1379 }
1380 case VIDIOC_G_FMT:
1da177e4 1381 {
1da177e4
LT
1382 struct v4l2_format *f = arg;
1383
1384 memset(f,0,sizeof(*f));
1385 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
31629424
CC
1386 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1387 f->fmt.pix.bytesperline = 0;
1388 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
1389 f->fmt.pix.colorspace = 0;
1da177e4
LT
1390 f->fmt.pix.width = dev->width;
1391 f->fmt.pix.height = dev->height;
31629424
CC
1392 f->fmt.pix.field = fh->mpegq.field;
1393 dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
1394 dev->width, dev->height, fh->mpegq.field );
1395 return 0;
1396 }
1397 case VIDIOC_TRY_FMT:
1398 {
1399 struct v4l2_format *f = arg;
1400
1401 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1da177e4 1402 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
e52e98a7 1403 f->fmt.pix.bytesperline = 0;
31629424 1404 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
e52e98a7 1405 f->fmt.pix.colorspace = 0;
31629424
CC
1406 dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
1407 dev->width, dev->height, fh->mpegq.field );
1408 return 0;
1409 }
1410 case VIDIOC_S_FMT:
1411 {
1412 struct v4l2_format *f = arg;
1413
1414 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1415 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1416 f->fmt.pix.bytesperline = 0;
1417 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
1418 f->fmt.pix.colorspace = 0;
1419 dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
1420 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
e52e98a7 1421 return 0;
1da177e4
LT
1422 }
1423
1424 /* --- streaming capture ------------------------------------- */
1425 case VIDIOC_REQBUFS:
1426 return videobuf_reqbufs(&fh->mpegq, arg);
1427
1428 case VIDIOC_QUERYBUF:
1429 return videobuf_querybuf(&fh->mpegq, arg);
1430
1431 case VIDIOC_QBUF:
1432 return videobuf_qbuf(&fh->mpegq, arg);
1433
1434 case VIDIOC_DQBUF:
1435 return videobuf_dqbuf(&fh->mpegq, arg,
1436 file->f_flags & O_NONBLOCK);
1437
1438 case VIDIOC_STREAMON:
1439 return videobuf_streamon(&fh->mpegq);
1440
1441 case VIDIOC_STREAMOFF:
1442 return videobuf_streamoff(&fh->mpegq);
1443
31629424
CC
1444 /* --- mpeg compression -------------------------------------- */
1445 case VIDIOC_G_MPEGCOMP:
1446 {
1447 struct v4l2_mpeg_compression *f = arg;
1448
1449 memcpy(f,&dev->params,sizeof(*f));
1450 return 0;
1451 }
1452 case VIDIOC_S_MPEGCOMP:
1453 {
1454 struct v4l2_mpeg_compression *f = arg;
1455
1456 blackbird_set_params(dev, f);
1457 return 0;
1458 }
1459
1da177e4 1460 default:
e52e98a7 1461 return cx88_do_ioctl( inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook );
1da177e4
LT
1462 }
1463 return 0;
1464}
1465
e52e98a7
MCC
1466int (*cx88_ioctl_hook)(struct inode *inode, struct file *file,
1467 unsigned int cmd, void *arg);
1468unsigned int (*cx88_ioctl_translator)(unsigned int cmd);
1469
1470static unsigned int mpeg_translate_ioctl(unsigned int cmd)
1471{
1472 return cmd;
1473}
1474
1da177e4 1475static int mpeg_ioctl(struct inode *inode, struct file *file,
e52e98a7 1476 unsigned int cmd, unsigned long arg)
1da177e4 1477{
e52e98a7
MCC
1478 cmd = cx88_ioctl_translator( cmd );
1479 return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
1da177e4
LT
1480}
1481
1482static int mpeg_open(struct inode *inode, struct file *file)
1483{
1484 int minor = iminor(inode);
1485 struct cx8802_dev *h,*dev = NULL;
1486 struct cx8802_fh *fh;
1487 struct list_head *list;
1488
1489 list_for_each(list,&cx8802_devlist) {
1490 h = list_entry(list, struct cx8802_dev, devlist);
1491 if (h->mpeg_dev->minor == minor)
1492 dev = h;
1493 }
1494 if (NULL == dev)
1495 return -ENODEV;
1496
1497 if (blackbird_initialize_codec(dev) < 0)
1498 return -EINVAL;
1499 dprintk(1,"open minor=%d\n",minor);
1500
1501 /* allocate + initialize per filehandle data */
7408187d 1502 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1da177e4
LT
1503 if (NULL == fh)
1504 return -ENOMEM;
1da177e4
LT
1505 file->private_data = fh;
1506 fh->dev = dev;
1507
1da177e4
LT
1508 videobuf_queue_init(&fh->mpegq, &blackbird_qops,
1509 dev->pci, &dev->slock,
1510 V4L2_BUF_TYPE_VIDEO_CAPTURE,
31629424 1511 V4L2_FIELD_INTERLACED,
1da177e4
LT
1512 sizeof(struct cx88_buffer),
1513 fh);
31629424
CC
1514
1515 /* FIXME: locking against other video device */
1516 cx88_set_scale(dev->core, dev->width, dev->height,
1517 fh->mpegq.field);
1518
1da177e4
LT
1519 return 0;
1520}
1521
1522static int mpeg_release(struct inode *inode, struct file *file)
1523{
1524 struct cx8802_fh *fh = file->private_data;
1525
855dbada
MK
1526 /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
1527 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
b45009b0
MCC
1528 BLACKBIRD_END_NOW,
1529 BLACKBIRD_MPEG_CAPTURE,
1530 BLACKBIRD_RAW_BITS_NONE
1531 );
1da177e4 1532
0b5f56d6 1533 cx8802_cancel_buffers(fh->dev);
1da177e4
LT
1534 /* stop mpeg capture */
1535 if (fh->mpegq.streaming)
1536 videobuf_streamoff(&fh->mpegq);
1537 if (fh->mpegq.reading)
1538 videobuf_read_stop(&fh->mpegq);
1539
1540 videobuf_mmap_free(&fh->mpegq);
1541 file->private_data = NULL;
1542 kfree(fh);
1543 return 0;
1544}
1545
1546static ssize_t
1547mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1548{
1549 struct cx8802_fh *fh = file->private_data;
1550
1551 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1552 file->f_flags & O_NONBLOCK);
1553}
1554
1555static unsigned int
1556mpeg_poll(struct file *file, struct poll_table_struct *wait)
1557{
1558 struct cx8802_fh *fh = file->private_data;
1559
1560 return videobuf_poll_stream(file, &fh->mpegq, wait);
1561}
1562
1563static int
1564mpeg_mmap(struct file *file, struct vm_area_struct * vma)
1565{
1566 struct cx8802_fh *fh = file->private_data;
1567
1568 return videobuf_mmap_mapper(&fh->mpegq, vma);
1569}
1570
1571static struct file_operations mpeg_fops =
1572{
1573 .owner = THIS_MODULE,
1574 .open = mpeg_open,
1575 .release = mpeg_release,
1576 .read = mpeg_read,
1577 .poll = mpeg_poll,
1578 .mmap = mpeg_mmap,
1579 .ioctl = mpeg_ioctl,
1580 .llseek = no_llseek,
1581};
1582
1583static struct video_device cx8802_mpeg_template =
1584{
1585 .name = "cx8802",
1586 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
1587 .hardware = 0,
1588 .fops = &mpeg_fops,
1589 .minor = -1,
1590};
1591
1592/* ------------------------------------------------------------------ */
1593
1594static void blackbird_unregister_video(struct cx8802_dev *dev)
1595{
1596 if (dev->mpeg_dev) {
1597 if (-1 != dev->mpeg_dev->minor)
1598 video_unregister_device(dev->mpeg_dev);
1599 else
1600 video_device_release(dev->mpeg_dev);
1601 dev->mpeg_dev = NULL;
1602 }
1603}
1604
1605static int blackbird_register_video(struct cx8802_dev *dev)
1606{
1607 int err;
1608
1609 dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
1610 &cx8802_mpeg_template,"mpeg");
1611 err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
1612 if (err < 0) {
1613 printk(KERN_INFO "%s/2: can't register mpeg device\n",
1614 dev->core->name);
1615 return err;
1616 }
1617 printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
1618 dev->core->name,dev->mpeg_dev->minor & 0x1f);
1619 return 0;
1620}
1621
1622/* ----------------------------------------------------------- */
1623
1624static int __devinit blackbird_probe(struct pci_dev *pci_dev,
1625 const struct pci_device_id *pci_id)
1626{
1627 struct cx8802_dev *dev;
1628 struct cx88_core *core;
1629 int err;
1630
1631 /* general setup */
1632 core = cx88_core_get(pci_dev);
1633 if (NULL == core)
1634 return -EINVAL;
1635
1636 err = -ENODEV;
1637 if (!cx88_boards[core->board].blackbird)
1638 goto fail_core;
1639
1640 err = -ENOMEM;
7408187d 1641 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1da177e4
LT
1642 if (NULL == dev)
1643 goto fail_core;
1da177e4
LT
1644 dev->pci = pci_dev;
1645 dev->core = core;
1646 dev->width = 720;
e52e98a7 1647 dev->height = 576;
31629424
CC
1648 memcpy(&dev->params,&default_mpeg_params,sizeof(default_mpeg_params));
1649 memcpy(&dev->dnr_params,&default_dnr_params,sizeof(default_dnr_params));
1da177e4 1650
01a9cd99
MK
1651 switch (core->board) {
1652 case CX88_BOARD_HAUPPAUGE_ROSLYN:
3febc04d
MK
1653 if (core->tuner_formats & V4L2_STD_525_60) {
1654 dev->height = 480;
1655 dev->params.vi_frame_rate = 30;
1656 } else {
1657 dev->height = 576;
1658 dev->params.vi_frame_rate = 25;
1659 }
01a9cd99
MK
1660 break;
1661 case CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT:
1662 if (core->tvnorm->id & V4L2_STD_525_60) {
1663 dev->height = 480;
1664 dev->params.vi_frame_rate = 30;
1665 } else {
1666 dev->height = 576;
1667 dev->params.vi_frame_rate = 25;
1668 }
1669 break;
0345c387
ST
1670 }
1671
1da177e4
LT
1672 err = cx8802_init_common(dev);
1673 if (0 != err)
1674 goto fail_free;
1675
1676 /* blackbird stuff */
1677 printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1678 core->name);
1679 host_setup(dev->core);
1680
1681 list_add_tail(&dev->devlist,&cx8802_devlist);
1682 blackbird_register_video(dev);
e52e98a7
MCC
1683
1684 /* initial device configuration: needed ? */
1685
1da177e4
LT
1686 return 0;
1687
1688 fail_free:
1689 kfree(dev);
1690 fail_core:
1691 cx88_core_put(core,pci_dev);
1692 return err;
1693}
1694
1695static void __devexit blackbird_remove(struct pci_dev *pci_dev)
1696{
4ac97914 1697 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
1da177e4
LT
1698
1699 /* blackbird */
1700 blackbird_unregister_video(dev);
1701 list_del(&dev->devlist);
1702
1703 /* common */
1704 cx8802_fini_common(dev);
1705 cx88_core_put(dev->core,dev->pci);
1706 kfree(dev);
1707}
1708
1709static struct pci_device_id cx8802_pci_tbl[] = {
1710 {
1711 .vendor = 0x14f1,
1712 .device = 0x8802,
4ac97914
MCC
1713 .subvendor = PCI_ANY_ID,
1714 .subdevice = PCI_ANY_ID,
1da177e4
LT
1715 },{
1716 /* --- end of list --- */
1717 }
1718};
1719MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
1720
1721static struct pci_driver blackbird_pci_driver = {
4ac97914
MCC
1722 .name = "cx88-blackbird",
1723 .id_table = cx8802_pci_tbl,
1724 .probe = blackbird_probe,
1725 .remove = __devexit_p(blackbird_remove),
1da177e4
LT
1726 .suspend = cx8802_suspend_common,
1727 .resume = cx8802_resume_common,
1728};
1729
1730static int blackbird_init(void)
1731{
1732 printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
1733 (CX88_VERSION_CODE >> 16) & 0xff,
1734 (CX88_VERSION_CODE >> 8) & 0xff,
1735 CX88_VERSION_CODE & 0xff);
1736#ifdef SNAPSHOT
1737 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
1738 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1739#endif
e52e98a7
MCC
1740 cx88_ioctl_hook = mpeg_do_ioctl;
1741 cx88_ioctl_translator = mpeg_translate_ioctl;
1da177e4
LT
1742 return pci_register_driver(&blackbird_pci_driver);
1743}
1744
1745static void blackbird_fini(void)
1746{
1747 pci_unregister_driver(&blackbird_pci_driver);
1748}
1749
1750module_init(blackbird_init);
1751module_exit(blackbird_fini);
1752
e52e98a7
MCC
1753EXPORT_SYMBOL(cx88_ioctl_hook);
1754EXPORT_SYMBOL(cx88_ioctl_translator);
31629424
CC
1755EXPORT_SYMBOL(blackbird_set_params);
1756EXPORT_SYMBOL(blackbird_set_dnr_params);
e52e98a7 1757
1da177e4
LT
1758/* ----------------------------------------------------------- */
1759/*
1760 * Local variables:
1761 * c-basic-offset: 8
1762 * End:
b45009b0 1763 * 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
1da177e4 1764 */
This page took 0.222903 seconds and 5 git commands to generate.