Merge ../to-linus-stable/
[linux-2.6] / drivers / media / video / cx88 / cx88-blackbird.c
1 /*
2  * $Id: cx88-blackbird.c,v 1.27 2005/06/03 13:31:50 mchehab Exp $
3  *
4  *  Support for a cx23416 mpeg encoder via cx2388x host port.
5  *  "blackbird" reference design.
6  *
7  *    (c) 2004 Jelle Foks <jelle@foks.8m.com>
8  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
9  *
10  *  Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/fs.h>
31 #include <linux/delay.h>
32 #include <linux/device.h>
33 #include <linux/firmware.h>
34
35 #include "cx88.h"
36
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");
41
42 static unsigned int mpegbufs = 8;
43 module_param(mpegbufs,int,0644);
44 MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
45
46 static unsigned int debug = 0;
47 module_param(debug,int,0644);
48 MODULE_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
53 static LIST_HEAD(cx8802_devlist);
54
55 /* ------------------------------------------------------------------ */
56
57 #define BLACKBIRD_FIRM_ENC_FILENAME "blackbird-fw-enc.bin"
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
64 /* Firmware API commands */
65 /* #define IVTV_API_STD_TIMEOUT 0x00010000 // 65536, units?? */
66 #define IVTV_API_STD_TIMEOUT 500
67
68 #define BLACKBIRD_API_PING               0x80
69 #define BLACKBIRD_API_BEGIN_CAPTURE      0x81
70 enum blackbird_capture_type {
71         BLACKBIRD_MPEG_CAPTURE,
72         BLACKBIRD_RAW_CAPTURE,
73         BLACKBIRD_RAW_PASSTHRU_CAPTURE
74 };
75 enum blackbird_capture_bits {
76         BLACKBIRD_RAW_BITS_NONE             = 0x00,
77         BLACKBIRD_RAW_BITS_YUV_CAPTURE      = 0x01,
78         BLACKBIRD_RAW_BITS_PCM_CAPTURE      = 0x02,
79         BLACKBIRD_RAW_BITS_VBI_CAPTURE      = 0x04,
80         BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
81         BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE  = 0x10
82 };
83 #define BLACKBIRD_API_END_CAPTURE        0x82
84 enum blackbird_capture_end {
85         BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
86         BLACKBIRD_END_NOW, /* stop immediately, no irq */
87 };
88 #define BLACKBIRD_API_SET_AUDIO_ID       0x89
89 #define BLACKBIRD_API_SET_VIDEO_ID       0x8B
90 #define BLACKBIRD_API_SET_PCR_ID         0x8D
91 #define BLACKBIRD_API_SET_FRAMERATE      0x8F
92 enum blackbird_framerate {
93         BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
94         BLACKBIRD_FRAMERATE_PAL_25   /* PAL: 25fps */
95 };
96 #define BLACKBIRD_API_SET_RESOLUTION     0x91
97 #define BLACKBIRD_API_SET_VIDEO_BITRATE  0x95
98 enum blackbird_video_bitrate_type {
99         BLACKBIRD_VIDEO_VBR,
100         BLACKBIRD_VIDEO_CBR
101 };
102 #define BLACKBIRD_PEAK_RATE_DIVISOR 400
103 enum blackbird_mux_rate {
104         BLACKBIRD_MUX_RATE_DEFAULT,
105          /* dvd mux rate: multiply by 400 to get the actual rate */
106         BLACKBIRD_MUX_RATE_DVD = 25200
107 };
108 #define BLACKBIRD_API_SET_GOP_STRUCTURE  0x97
109 #define BLACKBIRD_API_SET_ASPECT_RATIO   0x99
110 enum blackbird_aspect_ratio {
111         BLACKBIRD_ASPECT_RATIO_FORBIDDEN,
112         BLACKBIRD_ASPECT_RATIO_1_1_SQUARE,
113         BLACKBIRD_ASPECT_RATIO_4_3,
114         BLACKBIRD_ASPECT_RATIO_16_9,
115         BLACKBIRD_ASPECT_RATIO_221_100,
116         BLACKBIRD_ASPECT_RATIO_RESERVED
117 };
118 #define BLACKBIRD_API_SET_DNR_MODE       0x9B
119 enum blackbird_dnr_bits {
120         BLACKBIRD_DNR_BITS_MANUAL,
121         BLACKBIRD_DNR_BITS_AUTO_SPATIAL,
122         BLACKBIRD_DNR_BITS_AUTO_TEMPORAL,
123         BLACKBIRD_DNR_BITS_AUTO
124 };
125 enum blackbird_median_filter {
126         BLACKBIRD_MEDIAN_FILTER_DISABLED,
127         BLACKBIRD_MEDIAN_FILTER_HORIZONTAL,
128         BLACKBIRD_MEDIAN_FILTER_VERTICAL,
129         BLACKBIRD_MEDIAN_FILTER_HV,
130         BLACKBIRD_MEDIAN_FILTER_DIAGONAL
131 };
132 #define BLACKBIRD_API_SET_MANUAL_DNR     0x9D
133 #define BLACKBIRD_API_SET_DNR_MEDIAN     0x9F
134 #define BLACKBIRD_API_SET_SPATIAL_FILTER 0xA1
135 enum blackbird_spatial_filter_luma {
136         BLACKBIRD_SPATIAL_FILTER_LUMA_DISABLED,
137         BLACKBIRD_SPATIAL_FILTER_LUMA_1D_HORIZ,
138         BLACKBIRD_SPATIAL_FILTER_LUMA_1D_VERT,
139         BLACKBIRD_SPATIAL_FILTER_LUMA_2D_HV, /* separable, default */
140         BLACKBIRD_SPATIAL_FILTER_LUMA_2D_SYMM /* symmetric non-separable */
141 };
142 enum blackbird_spatial_filter_chroma {
143         BLACKBIRD_SPATIAL_FILTER_CHROMA_DISABLED,
144         BLACKBIRD_SPATIAL_FILTER_CHROMA_1D_HORIZ /* default */
145 };
146 #define BLACKBIRD_API_SET_3_2_PULLDOWN   0xB1
147 enum blackbird_pulldown {
148         BLACKBIRD_3_2_PULLDOWN_DISABLED,
149         BLACKBIRD_3_2_PULLDOWN_ENABLED
150 };
151 #define BLACKBIRD_API_SET_VBI_LINE_NO    0xB7
152 enum blackbird_vbi_line_bits {
153         BLACKBIRD_VBI_LINE_BITS_TOP_FIELD,
154         BLACKBIRD_VBI_LINE_BITS_BOT_FIELD = (1 << 31),
155         BLACKBIRD_VBI_LINE_BITS_ALL_LINES = 0xFFFFFFFF
156 };
157 enum blackbird_vbi_line {
158         BLACKBIRD_VBI_LINE_DISABLED,
159         BLACKBIRD_VBI_LINE_ENABLED
160 };
161 enum blackbird_vbi_slicing {
162         BLACKBIRD_VBI_SLICING_NONE,
163         BLACKBIRD_VBI_SLICING_CLOSED_CAPTION
164 };
165 #define BLACKBIRD_API_SET_STREAM_TYPE    0xB9
166 enum blackbird_stream_type {
167         BLACKBIRD_STREAM_PROGRAM,
168         BLACKBIRD_STREAM_TRANSPORT,
169         BLACKBIRD_STREAM_MPEG1,
170         BLACKBIRD_STREAM_PES_AV,
171         BLACKBIRD_STREAM_UNKNOWN4,
172         BLACKBIRD_STREAM_PES_VIDEO,
173         BLACKBIRD_STREAM_UNKNOWN6,
174         BLACKBIRD_STREAM_PES_AUDIO,
175         BLACKBIRD_STREAM_UNKNOWN8,
176         BLACKBIRD_STREAM_UNKNOWN9, /* audio/pcm ? */
177         BLACKBIRD_STREAM_DVD,
178         BLACKBIRD_STREAM_VCD,
179         BLACKBIRD_STREAM_UNKNOWN12 /* svcd/xvcd ? */
180 };
181 #define BLACKBIRD_API_SET_OUTPUT_PORT    0xBB
182 enum blackbird_stream_port {
183         BLACKBIRD_OUTPUT_PORT_MEMORY,
184         BLACKBIRD_OUTPUT_PORT_STREAMING,
185         BLACKBIRD_OUTPUT_PORT_SERIAL
186 };
187 #define BLACKBIRD_API_SET_AUDIO_PARAMS   0xBD
188 enum blackbird_audio_bits_sample_rate {
189         BLACKBIRD_AUDIO_BITS_44100HZ,
190         BLACKBIRD_AUDIO_BITS_48000HZ,
191         BLACKBIRD_AUDIO_BITS_32000HZ,
192         BLACKBIRD_AUDIO_BITS_RESERVED_HZ,
193 };
194 enum blackbird_audio_bits_encoding {
195         BLACKBIRD_AUDIO_BITS_LAYER_1 = 0x1 << 2,
196         BLACKBIRD_AUDIO_BITS_LAYER_2 = 0x2 << 2,
197 };
198 enum blackbird_audio_bits_bitrate_layer_1 {
199         BLACKBIRD_AUDIO_BITS_LAYER_1_FREE_FORMAT,
200         BLACKBIRD_AUDIO_BITS_LAYER_1_32  = 0x01 << 4,
201         BLACKBIRD_AUDIO_BITS_LAYER_1_64  = 0x02 << 4,
202         BLACKBIRD_AUDIO_BITS_LAYER_1_96  = 0x03 << 4,
203         BLACKBIRD_AUDIO_BITS_LAYER_1_128 = 0x04 << 4,
204         BLACKBIRD_AUDIO_BITS_LAYER_1_160 = 0x05 << 4,
205         BLACKBIRD_AUDIO_BITS_LAYER_1_192 = 0x06 << 4,
206         BLACKBIRD_AUDIO_BITS_LAYER_1_224 = 0x07 << 4,
207         BLACKBIRD_AUDIO_BITS_LAYER_1_256 = 0x08 << 4,
208         BLACKBIRD_AUDIO_BITS_LAYER_1_288 = 0x09 << 4,
209         BLACKBIRD_AUDIO_BITS_LAYER_1_320 = 0x0A << 4,
210         BLACKBIRD_AUDIO_BITS_LAYER_1_352 = 0x0B << 4,
211         BLACKBIRD_AUDIO_BITS_LAYER_1_384 = 0x0C << 4,
212         BLACKBIRD_AUDIO_BITS_LAYER_1_416 = 0x0D << 4,
213         BLACKBIRD_AUDIO_BITS_LAYER_1_448 = 0x0E << 4,
214 };
215 enum blackbird_audio_bits_bitrate_layer_2 {
216         BLACKBIRD_AUDIO_BITS_LAYER_2_FREE_FORMAT,
217         BLACKBIRD_AUDIO_BITS_LAYER_2_32  = 0x01 << 4,
218         BLACKBIRD_AUDIO_BITS_LAYER_2_48  = 0x02 << 4,
219         BLACKBIRD_AUDIO_BITS_LAYER_2_56  = 0x03 << 4,
220         BLACKBIRD_AUDIO_BITS_LAYER_2_64  = 0x04 << 4,
221         BLACKBIRD_AUDIO_BITS_LAYER_2_80  = 0x05 << 4,
222         BLACKBIRD_AUDIO_BITS_LAYER_2_96  = 0x06 << 4,
223         BLACKBIRD_AUDIO_BITS_LAYER_2_112 = 0x07 << 4,
224         BLACKBIRD_AUDIO_BITS_LAYER_2_128 = 0x08 << 4,
225         BLACKBIRD_AUDIO_BITS_LAYER_2_160 = 0x09 << 4,
226         BLACKBIRD_AUDIO_BITS_LAYER_2_192 = 0x0A << 4,
227         BLACKBIRD_AUDIO_BITS_LAYER_2_224 = 0x0B << 4,
228         BLACKBIRD_AUDIO_BITS_LAYER_2_256 = 0x0C << 4,
229         BLACKBIRD_AUDIO_BITS_LAYER_2_320 = 0x0D << 4,
230         BLACKBIRD_AUDIO_BITS_LAYER_2_384 = 0x0E << 4,
231 };
232 enum blackbird_audio_bits_mode {
233         BLACKBIRD_AUDIO_BITS_STEREO,
234         BLACKBIRD_AUDIO_BITS_JOINT_STEREO = 0x1 << 8,
235         BLACKBIRD_AUDIO_BITS_DUAL         = 0x2 << 8,
236         BLACKBIRD_AUDIO_BITS_MONO         = 0x3 << 8,
237 };
238 enum blackbird_audio_bits_mode_extension {
239         BLACKBIRD_AUDIO_BITS_BOUND_4,
240         BLACKBIRD_AUDIO_BITS_BOUND_8  = 0x1 << 10,
241         BLACKBIRD_AUDIO_BITS_BOUND_12 = 0x2 << 10,
242         BLACKBIRD_AUDIO_BITS_BOUND_16 = 0x3 << 10,
243 };
244 enum blackbird_audio_bits_emphasis {
245         BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE,
246         BLACKBIRD_AUDIO_BITS_EMPHASIS_50_15     = 0x1 << 12,
247         BLACKBIRD_AUDIO_BITS_EMPHASIS_RESERVED  = 0x2 << 12,
248         BLACKBIRD_AUDIO_BITS_EMPHASIS_CCITT_J17 = 0x3 << 12,
249 };
250 enum blackbird_audio_bits_crc {
251         BLACKBIRD_AUDIO_BITS_CRC_OFF,
252         BLACKBIRD_AUDIO_BITS_CRC_ON = 0x1 << 14,
253 };
254 enum blackbird_audio_bits_copyright {
255         BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF,
256         BLACKBIRD_AUDIO_BITS_COPYRIGHT_ON = 0x1 << 15,
257 };
258 enum blackbird_audio_bits_original {
259         BLACKBIRD_AUDIO_BITS_COPY,
260         BLACKBIRD_AUDIO_BITS_ORIGINAL = 0x1 << 16,
261 };
262 #define BLACKBIRD_API_HALT               0xC3
263 #define BLACKBIRD_API_GET_VERSION        0xC4
264 #define BLACKBIRD_API_SET_GOP_CLOSURE    0xC5
265 enum blackbird_gop_closure {
266         BLACKBIRD_GOP_CLOSURE_OFF,
267         BLACKBIRD_GOP_CLOSURE_ON,
268 };
269 #define BLACKBIRD_API_DATA_XFER_STATUS   0xC6
270 enum blackbird_data_xfer_status {
271         BLACKBIRD_MORE_BUFFERS_FOLLOW,
272         BLACKBIRD_LAST_BUFFER,
273 };
274 #define BLACKBIRD_API_PROGRAM_INDEX_INFO 0xC7
275 enum blackbird_picture_mask {
276         BLACKBIRD_PICTURE_MASK_NONE,
277         BLACKBIRD_PICTURE_MASK_I_FRAMES,
278         BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
279         BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
280 };
281 #define BLACKBIRD_API_SET_VBI_PARAMS     0xC8
282 enum blackbird_vbi_mode_bits {
283         BLACKBIRD_VBI_BITS_SLICED,
284         BLACKBIRD_VBI_BITS_RAW,
285 };
286 enum blackbird_vbi_insertion_bits {
287         BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
288         BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
289         BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
290         BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
291         BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
292 };
293 #define BLACKBIRD_API_SET_DMA_BLOCK_SIZE 0xC9
294 enum blackbird_dma_unit {
295         BLACKBIRD_DMA_BYTES,
296         BLACKBIRD_DMA_FRAMES,
297 };
298 #define BLACKBIRD_API_DMA_TRANSFER_INFO  0xCA
299 #define BLACKBIRD_API_DMA_TRANSFER_STAT  0xCB
300 enum blackbird_dma_transfer_status_bits {
301         BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
302         BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
303         BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
304 };
305 #define BLACKBIRD_API_SET_DMA2HOST_ADDR  0xCC
306 #define BLACKBIRD_API_INIT_VIDEO_INPUT   0xCD
307 #define BLACKBIRD_API_SET_FRAMESKIP      0xD0
308 #define BLACKBIRD_API_PAUSE              0xD2
309 enum blackbird_pause {
310         BLACKBIRD_PAUSE_ENCODING,
311         BLACKBIRD_RESUME_ENCODING,
312 };
313 #define BLACKBIRD_API_REFRESH_INPUT      0xD3
314 #define BLACKBIRD_API_SET_COPYRIGHT      0xD4
315 enum blackbird_copyright {
316         BLACKBIRD_COPYRIGHT_OFF,
317         BLACKBIRD_COPYRIGHT_ON,
318 };
319 #define BLACKBIRD_API_SET_NOTIFICATION   0xD5
320 enum blackbird_notification_type {
321         BLACKBIRD_NOTIFICATION_REFRESH,
322 };
323 enum blackbird_notification_status {
324         BLACKBIRD_NOTIFICATION_OFF,
325         BLACKBIRD_NOTIFICATION_ON,
326 };
327 enum blackbird_notification_mailbox {
328         BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
329 };
330 #define BLACKBIRD_API_SET_CAPTURE_LINES  0xD6
331 enum blackbird_field1_lines {
332         BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
333         BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
334         BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
335 };
336 enum blackbird_field2_lines {
337         BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
338         BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
339         BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
340 };
341 #define BLACKBIRD_API_SET_CUSTOM_DATA    0xD7
342 enum blackbird_custom_data_type {
343         BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
344         BLACKBIRD_CUSTOM_PRIVATE_PACKET,
345 };
346 #define BLACKBIRD_API_MUTE_VIDEO         0xD9
347 enum blackbird_mute {
348         BLACKBIRD_UNMUTE,
349         BLACKBIRD_MUTE,
350 };
351 enum blackbird_mute_video_mask {
352         BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
353         BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
354         BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
355 };
356 enum blackbird_mute_video_shift {
357         BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
358         BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
359         BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
360 };
361 #define BLACKBIRD_API_MUTE_AUDIO         0xDA
362
363 /* Registers */
364 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
365 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
366 #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
367 #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
368 #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
369 #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
370
371 /* ------------------------------------------------------------------ */
372
373 static void host_setup(struct cx88_core *core)
374 {
375         /* toggle reset of the host */
376         cx_write(MO_GPHST_SOFT_RST, 1);
377         udelay(100);
378         cx_write(MO_GPHST_SOFT_RST, 0);
379         udelay(100);
380
381         /* host port setup */
382         cx_write(MO_GPHST_WSC, 0x44444444U);
383         cx_write(MO_GPHST_XFR, 0);
384         cx_write(MO_GPHST_WDTH, 15);
385         cx_write(MO_GPHST_HDSHK, 0);
386         cx_write(MO_GPHST_MUX16, 0x44448888U);
387         cx_write(MO_GPHST_MODE, 0);
388 }
389
390 /* ------------------------------------------------------------------ */
391
392 #define P1_MDATA0 0x390000
393 #define P1_MDATA1 0x390001
394 #define P1_MDATA2 0x390002
395 #define P1_MDATA3 0x390003
396 #define P1_MADDR2 0x390004
397 #define P1_MADDR1 0x390005
398 #define P1_MADDR0 0x390006
399 #define P1_RDATA0 0x390008
400 #define P1_RDATA1 0x390009
401 #define P1_RDATA2 0x39000A
402 #define P1_RDATA3 0x39000B
403 #define P1_RADDR0 0x39000C
404 #define P1_RADDR1 0x39000D
405 #define P1_RRDWR  0x39000E
406
407 static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
408 {
409         unsigned long timeout = jiffies + msecs_to_jiffies(1);
410         u32 gpio0,need;
411
412         need = state ? 2 : 0;
413         for (;;) {
414                 gpio0 = cx_read(MO_GP0_IO) & 2;
415                 if (need == gpio0)
416                         return 0;
417                 if (time_after(jiffies,timeout))
418                         return -1;
419                 udelay(1);
420         }
421 }
422
423 static int memory_write(struct cx88_core *core, u32 address, u32 value)
424 {
425         /* Warning: address is dword address (4 bytes) */
426         cx_writeb(P1_MDATA0, (unsigned int)value);
427         cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
428         cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
429         cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
430         cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
431         cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
432         cx_writeb(P1_MADDR0, (unsigned int)address);
433         cx_read(P1_MDATA0);
434         cx_read(P1_MADDR0);
435
436         return wait_ready_gpio0_bit1(core,1);
437 }
438
439 static int memory_read(struct cx88_core *core, u32 address, u32 *value)
440 {
441         int retval;
442         u32 val;
443
444         /* Warning: address is dword address (4 bytes) */
445         cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
446         cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
447         cx_writeb(P1_MADDR0, (unsigned int)address);
448         cx_read(P1_MADDR0);
449
450         retval = wait_ready_gpio0_bit1(core,1);
451
452         cx_writeb(P1_MDATA3, 0);
453         val     = (unsigned char)cx_read(P1_MDATA3) << 24;
454         cx_writeb(P1_MDATA2, 0);
455         val    |= (unsigned char)cx_read(P1_MDATA2) << 16;
456         cx_writeb(P1_MDATA1, 0);
457         val    |= (unsigned char)cx_read(P1_MDATA1) << 8;
458         cx_writeb(P1_MDATA0, 0);
459         val    |= (unsigned char)cx_read(P1_MDATA0);
460
461         *value  = val;
462         return retval;
463 }
464
465 static int register_write(struct cx88_core *core, u32 address, u32 value)
466 {
467         cx_writeb(P1_RDATA0, (unsigned int)value);
468         cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
469         cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
470         cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
471         cx_writeb(P1_RADDR0, (unsigned int)address);
472         cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
473         cx_writeb(P1_RRDWR, 1);
474         cx_read(P1_RDATA0);
475         cx_read(P1_RADDR0);
476
477         return wait_ready_gpio0_bit1(core,1);
478 }
479
480
481 static int register_read(struct cx88_core *core, u32 address, u32 *value)
482 {
483         int retval;
484         u32 val;
485
486         cx_writeb(P1_RADDR0, (unsigned int)address);
487         cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
488         cx_writeb(P1_RRDWR, 0);
489         cx_read(P1_RADDR0);
490
491         retval  = wait_ready_gpio0_bit1(core,1);
492         val     = (unsigned char)cx_read(P1_RDATA0);
493         val    |= (unsigned char)cx_read(P1_RDATA1) << 8;
494         val    |= (unsigned char)cx_read(P1_RDATA2) << 16;
495         val    |= (unsigned char)cx_read(P1_RDATA3) << 24;
496
497         *value  = val;
498         return retval;
499 }
500
501 /* ------------------------------------------------------------------ */
502
503 /* We don't need to call the API often, so using just one mailbox will probably suffice */
504 static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
505                              u32 inputcnt, u32 outputcnt, ...)
506 {
507         unsigned long timeout;
508         u32 value, flag, retval;
509         int i;
510         va_list args;
511         va_start(args, outputcnt);
512
513         dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
514
515         /* this may not be 100% safe if we can't read any memory location
516            without side effects */
517         memory_read(dev->core, dev->mailbox - 4, &value);
518         if (value != 0x12345678) {
519                 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
520                 return -1;
521         }
522
523         memory_read(dev->core, dev->mailbox, &flag);
524         if (flag) {
525                 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
526                 return -1;
527         }
528
529         flag |= 1; /* tell 'em we're working on it */
530         memory_write(dev->core, dev->mailbox, flag);
531
532         /* write command + args + fill remaining with zeros */
533         memory_write(dev->core, dev->mailbox + 1, command); /* command code */
534         memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
535         for (i = 0; i < inputcnt ; i++) {
536                 value = va_arg(args, int);
537                 memory_write(dev->core, dev->mailbox + 4 + i, value);
538                 dprintk(1, "API Input %d = %d\n", i, value);
539         }
540         for (; i < 16 ; i++)
541                 memory_write(dev->core, dev->mailbox + 4 + i, 0);
542
543         flag |= 3; /* tell 'em we're done writing */
544         memory_write(dev->core, dev->mailbox, flag);
545
546         /* wait for firmware to handle the API command */
547         timeout = jiffies + msecs_to_jiffies(10);
548         for (;;) {
549                 memory_read(dev->core, dev->mailbox, &flag);
550                 if (0 != (flag & 4))
551                         break;
552                 if (time_after(jiffies,timeout)) {
553                         dprintk(0, "ERROR: API Mailbox timeout\n");
554                         return -1;
555                 }
556                 udelay(10);
557         }
558
559         /* read output values */
560         for (i = 0; i < outputcnt ; i++) {
561                 int *vptr = va_arg(args, int *);
562                 memory_read(dev->core, dev->mailbox + 4 + i, vptr);
563                 dprintk(1, "API Output %d = %d\n", i, *vptr);
564         }
565         va_end(args);
566
567         memory_read(dev->core, dev->mailbox + 2, &retval);
568         dprintk(1, "API result = %d\n",retval);
569
570         flag = 0;
571         memory_write(dev->core, dev->mailbox, flag);
572         return retval;
573 }
574
575
576 static int blackbird_find_mailbox(struct cx8802_dev *dev)
577 {
578         u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
579         int signaturecnt=0;
580         u32 value;
581         int i;
582
583         for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
584                 memory_read(dev->core, i, &value);
585                 if (value == signature[signaturecnt])
586                         signaturecnt++;
587                 else
588                         signaturecnt = 0;
589                 if (4 == signaturecnt) {
590                         dprintk(1, "Mailbox signature found\n");
591                         return i+1;
592                 }
593         }
594         dprintk(0, "Mailbox signature values not found!\n");
595         return -1;
596 }
597
598 static int blackbird_load_firmware(struct cx8802_dev *dev)
599 {
600         static const unsigned char magic[8] = {
601                 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
602         };
603         const struct firmware *firmware;
604         int i, retval = 0;
605         u32 value = 0;
606         u32 checksum = 0;
607         u32 *dataptr;
608
609         retval  = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
610         retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
611         retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
612         retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
613         msleep(1);
614         retval |= register_write(dev->core, IVTV_REG_APU, 0);
615
616         if (retval < 0)
617                 dprintk(0, "Error with register_write\n");
618
619         retval = request_firmware(&firmware, BLACKBIRD_FIRM_ENC_FILENAME,
620                                   &dev->pci->dev);
621         if (retval != 0) {
622                 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
623                         BLACKBIRD_FIRM_ENC_FILENAME);
624                 dprintk(0, "Please fix your hotplug setup, the board will "
625                         "not work without firmware loaded!\n");
626                 return -1;
627         }
628
629         if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
630                 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
631                         firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
632                 return -1;
633         }
634
635         if (0 != memcmp(firmware->data, magic, 8)) {
636                 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
637                 return -1;
638         }
639
640         /* transfer to the chip */
641         dprintk(1,"Loading firmware ...\n");
642         dataptr = (u32*)firmware->data;
643         for (i = 0; i < (firmware->size >> 2); i++) {
644                 value = *dataptr;
645                 checksum += ~value;
646                 memory_write(dev->core, i, value);
647                 dataptr++;
648         }
649
650         /* read back to verify with the checksum */
651         for (i--; i >= 0; i--) {
652                 memory_read(dev->core, i, &value);
653                 checksum -= ~value;
654         }
655         if (checksum) {
656                 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
657                 return -1;
658         }
659         release_firmware(firmware);
660         dprintk(0, "Firmware upload successful.\n");
661
662         retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
663         retval |= register_read(dev->core, IVTV_REG_SPU, &value);
664         retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
665         msleep(1);
666
667         retval |= register_read(dev->core, IVTV_REG_VPU, &value);
668         retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
669
670         if (retval < 0)
671                 dprintk(0, "Error with register_write\n");
672         return 0;
673 }
674
675 /**
676  Settings used by the windows tv app for PVR2000:
677 =================================================================================================================
678 Profile | Codec | Resolution | CBR/VBR | Video Qlty   | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
679 -----------------------------------------------------------------------------------------------------------------
680 MPEG-1  | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 2000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
681 MPEG-2  | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 4000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
682 VCD     | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 1150 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
683 DVD     | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
684 DB* DVD | MPEG2 | 720x576PAL | CBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
685 =================================================================================================================
686 *DB: "DirectBurn"
687 */
688 static void blackbird_codec_settings(struct cx8802_dev *dev)
689 {
690         int bitrate_mode = 1;
691         int bitrate = 7500000;
692         int bitrate_peak = 7500000;
693         bitrate_mode = BLACKBIRD_VIDEO_CBR;
694         bitrate = 4000*1024;
695         bitrate_peak = 4000*1024;
696
697         /* assign stream type */
698         blackbird_api_cmd(dev, BLACKBIRD_API_SET_STREAM_TYPE, 1, 0, BLACKBIRD_STREAM_PROGRAM);
699         /* blackbird_api_cmd(dev, BLACKBIRD_API_SET_STREAM_TYPE, 1, 0, BLACKBIRD_STREAM_TRANSPORT); */
700
701         /* assign output port */
702         blackbird_api_cmd(dev, BLACKBIRD_API_SET_OUTPUT_PORT, 1, 0, BLACKBIRD_OUTPUT_PORT_STREAMING); /* Host */
703
704         /* assign framerate */
705         blackbird_api_cmd(dev, BLACKBIRD_API_SET_FRAMERATE, 1, 0, BLACKBIRD_FRAMERATE_PAL_25);
706
707         /* assign frame size */
708         blackbird_api_cmd(dev, BLACKBIRD_API_SET_RESOLUTION, 2, 0,
709                           dev->height, dev->width);
710
711         /* assign aspect ratio */
712         blackbird_api_cmd(dev, BLACKBIRD_API_SET_ASPECT_RATIO, 1, 0, BLACKBIRD_ASPECT_RATIO_4_3);
713
714         /* assign bitrates */
715         blackbird_api_cmd(dev, BLACKBIRD_API_SET_VIDEO_BITRATE, 5, 0,
716                          bitrate_mode,         /* mode */
717                          bitrate,              /* bps */
718                          bitrate_peak / BLACKBIRD_PEAK_RATE_DIVISOR,   /* peak/400 */
719                          BLACKBIRD_MUX_RATE_DEFAULT /*, 0x70*/);             /* encoding buffer, ckennedy */
720
721         /* assign gop properties */
722         blackbird_api_cmd(dev, BLACKBIRD_API_SET_GOP_STRUCTURE, 2, 0, 15, 3);
723
724         /* assign 3 2 pulldown */
725         blackbird_api_cmd(dev, BLACKBIRD_API_SET_3_2_PULLDOWN, 1, 0, BLACKBIRD_3_2_PULLDOWN_DISABLED);
726
727         /* assign audio properties */
728         /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
729         /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, (2<<2) | (8<<4));
730            blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, 0 | (2 << 2) | (14 << 4)); */
731         blackbird_api_cmd(dev, BLACKBIRD_API_SET_AUDIO_PARAMS, 1, 0,
732                         BLACKBIRD_AUDIO_BITS_44100HZ |
733                         BLACKBIRD_AUDIO_BITS_LAYER_2 |
734                         BLACKBIRD_AUDIO_BITS_LAYER_2_224 |
735                         BLACKBIRD_AUDIO_BITS_STEREO |
736                         /* BLACKBIRD_AUDIO_BITS_BOUND_4 | */
737                         BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE |
738                         BLACKBIRD_AUDIO_BITS_CRC_OFF |
739                         BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF |
740                         BLACKBIRD_AUDIO_BITS_COPY
741                 );
742
743         /* assign gop closure */
744         blackbird_api_cmd(dev, BLACKBIRD_API_SET_GOP_CLOSURE, 1, 0, BLACKBIRD_GOP_CLOSURE_OFF);
745
746
747         /* assign dnr filter mode */
748         blackbird_api_cmd(dev, BLACKBIRD_API_SET_DNR_MODE, 2, 0,
749                         BLACKBIRD_DNR_BITS_MANUAL,
750                         BLACKBIRD_MEDIAN_FILTER_DISABLED
751                 );
752
753         /* assign dnr filter props*/
754         blackbird_api_cmd(dev, BLACKBIRD_API_SET_MANUAL_DNR, 2, 0, 0, 0);
755
756         /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
757         blackbird_api_cmd(dev, BLACKBIRD_API_SET_DNR_MEDIAN, 4, 0, 0, 255, 0, 255);
758
759         /* assign spatial filter type: luma_t: horiz_only, chroma_t: horiz_only */
760         blackbird_api_cmd(dev, BLACKBIRD_API_SET_SPATIAL_FILTER, 2, 0,
761                         BLACKBIRD_SPATIAL_FILTER_LUMA_1D_HORIZ,
762                         BLACKBIRD_SPATIAL_FILTER_CHROMA_1D_HORIZ
763                 );
764
765         /* assign frame drop rate */
766         /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_DROP_RATE, 1, 0, 0); */
767 }
768
769 static int blackbird_initialize_codec(struct cx8802_dev *dev)
770 {
771         struct cx88_core *core = dev->core;
772         int version;
773         int retval;
774
775         dprintk(1,"Initialize codec\n");
776         retval = blackbird_api_cmd(dev, BLACKBIRD_API_PING, 0, 0); /* ping */
777         if (retval < 0) {
778                 /* ping was not successful, reset and upload firmware */
779                 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
780                 msleep(1);
781                 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
782                 msleep(1);
783                 retval = blackbird_load_firmware(dev);
784                 if (retval < 0)
785                         return retval;
786
787                 dev->mailbox = blackbird_find_mailbox(dev);
788                 if (dev->mailbox < 0)
789                         return -1;
790
791                 retval = blackbird_api_cmd(dev, BLACKBIRD_API_PING, 0, 0); /* ping */
792                 if (retval < 0) {
793                         dprintk(0, "ERROR: Firmware ping failed!\n");
794                         return -1;
795                 }
796
797                 retval = blackbird_api_cmd(dev, BLACKBIRD_API_GET_VERSION, 0, 1, &version);
798                 if (retval < 0) {
799                         dprintk(0, "ERROR: Firmware get encoder version failed!\n");
800                         return -1;
801                 }
802                 dprintk(0, "Firmware version is 0x%08x\n", version);
803         }
804         msleep(1);
805
806         cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
807         cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
808         cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
809         cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
810
811         blackbird_codec_settings(dev);
812         msleep(1);
813
814         /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
815            blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
816            blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
817         blackbird_api_cmd(dev, BLACKBIRD_API_SET_CAPTURE_LINES, 2, 0,
818                         BLACKBIRD_FIELD1_SAA7115,
819                         BLACKBIRD_FIELD1_SAA7115
820                 );
821
822         /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
823         blackbird_api_cmd(dev, BLACKBIRD_API_SET_CUSTOM_DATA, 12, 0,
824                         BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
825                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
826
827         blackbird_api_cmd(dev, BLACKBIRD_API_INIT_VIDEO_INPUT, 0, 0); /* initialize the video input */
828
829         msleep(1);
830
831         blackbird_api_cmd(dev, BLACKBIRD_API_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
832         msleep(1);
833         blackbird_api_cmd(dev, BLACKBIRD_API_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
834         msleep(1);
835
836         /* blackbird_api_cmd(dev, BLACKBIRD_API_BEGIN_CAPTURE, 2, 0, 0, 0x13); // start capturing to the host interface */
837         blackbird_api_cmd(dev, BLACKBIRD_API_BEGIN_CAPTURE, 2, 0,
838                         BLACKBIRD_MPEG_CAPTURE,
839                         BLACKBIRD_RAW_BITS_NONE
840                 ); /* start capturing to the host interface */
841         msleep(10);
842
843         blackbird_api_cmd(dev, BLACKBIRD_API_REFRESH_INPUT, 0,0);
844         return 0;
845 }
846
847 /* ------------------------------------------------------------------ */
848
849 static int bb_buf_setup(struct videobuf_queue *q,
850                         unsigned int *count, unsigned int *size)
851 {
852         struct cx8802_fh *fh = q->priv_data;
853
854         fh->dev->ts_packet_size  = 512;
855         fh->dev->ts_packet_count = 100;
856
857         *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
858         if (0 == *count)
859                 *count = mpegbufs;
860         if (*count < 2)
861                 *count = 2;
862         if (*count > 32)
863                 *count = 32;
864         return 0;
865 }
866
867 static int
868 bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
869                enum v4l2_field field)
870 {
871         struct cx8802_fh *fh = q->priv_data;
872         return cx8802_buf_prepare(fh->dev, (struct cx88_buffer*)vb);
873 }
874
875 static void
876 bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
877 {
878         struct cx8802_fh *fh = q->priv_data;
879         cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
880 }
881
882 static void
883 bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
884 {
885         struct cx8802_fh *fh = q->priv_data;
886         cx88_free_buffer(fh->dev->pci, (struct cx88_buffer*)vb);
887 }
888
889 static struct videobuf_queue_ops blackbird_qops = {
890         .buf_setup    = bb_buf_setup,
891         .buf_prepare  = bb_buf_prepare,
892         .buf_queue    = bb_buf_queue,
893         .buf_release  = bb_buf_release,
894 };
895
896 /* ------------------------------------------------------------------ */
897
898 static int mpeg_do_ioctl(struct inode *inode, struct file *file,
899                          unsigned int cmd, void *arg)
900 {
901         struct cx8802_fh  *fh  = file->private_data;
902         struct cx8802_dev *dev = fh->dev;
903
904         if (debug > 1)
905                 cx88_print_ioctl(dev->core->name,cmd);
906
907         switch (cmd) {
908
909         /* --- capture ioctls ---------------------------------------- */
910         case VIDIOC_ENUM_FMT:
911         {
912                 struct v4l2_fmtdesc *f = arg;
913                 int index;
914
915                 index = f->index;
916                 if (index != 0)
917                         return -EINVAL;
918
919                 memset(f,0,sizeof(*f));
920                 f->index = index;
921                 strlcpy(f->description, "MPEG TS", sizeof(f->description));
922                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
923                 f->pixelformat = V4L2_PIX_FMT_MPEG;
924                 return 0;
925         }
926         case VIDIOC_G_FMT:
927         case VIDIOC_S_FMT:
928         case VIDIOC_TRY_FMT:
929         {
930                 /* FIXME -- quick'n'dirty for exactly one size ... */
931                 struct v4l2_format *f = arg;
932
933                 memset(f,0,sizeof(*f));
934                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
935                 f->fmt.pix.width        = dev->width;
936                 f->fmt.pix.height       = dev->height;
937                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
938                 f->fmt.pix.sizeimage    = 1024 * 512 /* FIXME: BUFFER_SIZE */;
939         }
940
941         /* --- streaming capture ------------------------------------- */
942         case VIDIOC_REQBUFS:
943                 return videobuf_reqbufs(&fh->mpegq, arg);
944
945         case VIDIOC_QUERYBUF:
946                 return videobuf_querybuf(&fh->mpegq, arg);
947
948         case VIDIOC_QBUF:
949                 return videobuf_qbuf(&fh->mpegq, arg);
950
951         case VIDIOC_DQBUF:
952                 return videobuf_dqbuf(&fh->mpegq, arg,
953                                       file->f_flags & O_NONBLOCK);
954
955         case VIDIOC_STREAMON:
956                 return videobuf_streamon(&fh->mpegq);
957
958         case VIDIOC_STREAMOFF:
959                 return videobuf_streamoff(&fh->mpegq);
960
961         default:
962                 return -EINVAL;
963         }
964         return 0;
965 }
966
967 static int mpeg_ioctl(struct inode *inode, struct file *file,
968                        unsigned int cmd, unsigned long arg)
969 {
970         return video_usercopy(inode, file, cmd, arg, mpeg_do_ioctl);
971 }
972
973 static int mpeg_open(struct inode *inode, struct file *file)
974 {
975         int minor = iminor(inode);
976         struct cx8802_dev *h,*dev = NULL;
977         struct cx8802_fh *fh;
978         struct list_head *list;
979
980         list_for_each(list,&cx8802_devlist) {
981                 h = list_entry(list, struct cx8802_dev, devlist);
982                 if (h->mpeg_dev->minor == minor)
983                         dev = h;
984         }
985         if (NULL == dev)
986                 return -ENODEV;
987
988         if (blackbird_initialize_codec(dev) < 0)
989                 return -EINVAL;
990         dprintk(1,"open minor=%d\n",minor);
991
992         /* allocate + initialize per filehandle data */
993         fh = kmalloc(sizeof(*fh),GFP_KERNEL);
994         if (NULL == fh)
995                 return -ENOMEM;
996         memset(fh,0,sizeof(*fh));
997         file->private_data = fh;
998         fh->dev      = dev;
999
1000         /* FIXME: locking against other video device */
1001         cx88_set_scale(dev->core, dev->width, dev->height,
1002                        V4L2_FIELD_INTERLACED);
1003
1004         videobuf_queue_init(&fh->mpegq, &blackbird_qops,
1005                             dev->pci, &dev->slock,
1006                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1007                             V4L2_FIELD_TOP,
1008                             sizeof(struct cx88_buffer),
1009                             fh);
1010         return 0;
1011 }
1012
1013 static int mpeg_release(struct inode *inode, struct file *file)
1014 {
1015         struct cx8802_fh  *fh  = file->private_data;
1016
1017         /* blackbird_api_cmd(fh->dev, BLACKBIRD_API_END_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
1018         blackbird_api_cmd(fh->dev, BLACKBIRD_API_END_CAPTURE, 3, 0,
1019                         BLACKBIRD_END_NOW,
1020                         BLACKBIRD_MPEG_CAPTURE,
1021                         BLACKBIRD_RAW_BITS_NONE
1022                 );
1023
1024         /* stop mpeg capture */
1025         if (fh->mpegq.streaming)
1026                 videobuf_streamoff(&fh->mpegq);
1027         if (fh->mpegq.reading)
1028                 videobuf_read_stop(&fh->mpegq);
1029
1030         videobuf_mmap_free(&fh->mpegq);
1031         file->private_data = NULL;
1032         kfree(fh);
1033         return 0;
1034 }
1035
1036 static ssize_t
1037 mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1038 {
1039         struct cx8802_fh *fh = file->private_data;
1040
1041         return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1042                                     file->f_flags & O_NONBLOCK);
1043 }
1044
1045 static unsigned int
1046 mpeg_poll(struct file *file, struct poll_table_struct *wait)
1047 {
1048         struct cx8802_fh *fh = file->private_data;
1049
1050         return videobuf_poll_stream(file, &fh->mpegq, wait);
1051 }
1052
1053 static int
1054 mpeg_mmap(struct file *file, struct vm_area_struct * vma)
1055 {
1056         struct cx8802_fh *fh = file->private_data;
1057
1058         return videobuf_mmap_mapper(&fh->mpegq, vma);
1059 }
1060
1061 static struct file_operations mpeg_fops =
1062 {
1063         .owner         = THIS_MODULE,
1064         .open          = mpeg_open,
1065         .release       = mpeg_release,
1066         .read          = mpeg_read,
1067         .poll          = mpeg_poll,
1068         .mmap          = mpeg_mmap,
1069         .ioctl         = mpeg_ioctl,
1070         .llseek        = no_llseek,
1071 };
1072
1073 static struct video_device cx8802_mpeg_template =
1074 {
1075         .name          = "cx8802",
1076         .type          = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
1077         .hardware      = 0,
1078         .fops          = &mpeg_fops,
1079         .minor         = -1,
1080 };
1081
1082 /* ------------------------------------------------------------------ */
1083
1084 static void blackbird_unregister_video(struct cx8802_dev *dev)
1085 {
1086         if (dev->mpeg_dev) {
1087                 if (-1 != dev->mpeg_dev->minor)
1088                         video_unregister_device(dev->mpeg_dev);
1089                 else
1090                         video_device_release(dev->mpeg_dev);
1091                 dev->mpeg_dev = NULL;
1092         }
1093 }
1094
1095 static int blackbird_register_video(struct cx8802_dev *dev)
1096 {
1097         int err;
1098
1099         dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
1100                                        &cx8802_mpeg_template,"mpeg");
1101         err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
1102         if (err < 0) {
1103                 printk(KERN_INFO "%s/2: can't register mpeg device\n",
1104                        dev->core->name);
1105                 return err;
1106         }
1107         printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
1108                dev->core->name,dev->mpeg_dev->minor & 0x1f);
1109         return 0;
1110 }
1111
1112 /* ----------------------------------------------------------- */
1113
1114 static int __devinit blackbird_probe(struct pci_dev *pci_dev,
1115                                      const struct pci_device_id *pci_id)
1116 {
1117         struct cx8802_dev *dev;
1118         struct cx88_core  *core;
1119         int err;
1120
1121         /* general setup */
1122         core = cx88_core_get(pci_dev);
1123         if (NULL == core)
1124                 return -EINVAL;
1125
1126         err = -ENODEV;
1127         if (!cx88_boards[core->board].blackbird)
1128                 goto fail_core;
1129
1130         err = -ENOMEM;
1131         dev = kmalloc(sizeof(*dev),GFP_KERNEL);
1132         if (NULL == dev)
1133                 goto fail_core;
1134         memset(dev,0,sizeof(*dev));
1135         dev->pci = pci_dev;
1136         dev->core = core;
1137         dev->width = 720;
1138         dev->height = 480;
1139
1140         err = cx8802_init_common(dev);
1141         if (0 != err)
1142                 goto fail_free;
1143
1144         /* blackbird stuff */
1145         printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1146                core->name);
1147         host_setup(dev->core);
1148
1149         list_add_tail(&dev->devlist,&cx8802_devlist);
1150         blackbird_register_video(dev);
1151         return 0;
1152
1153  fail_free:
1154         kfree(dev);
1155  fail_core:
1156         cx88_core_put(core,pci_dev);
1157         return err;
1158 }
1159
1160 static void __devexit blackbird_remove(struct pci_dev *pci_dev)
1161 {
1162         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
1163
1164         /* blackbird */
1165         blackbird_unregister_video(dev);
1166         list_del(&dev->devlist);
1167
1168         /* common */
1169         cx8802_fini_common(dev);
1170         cx88_core_put(dev->core,dev->pci);
1171         kfree(dev);
1172 }
1173
1174 static struct pci_device_id cx8802_pci_tbl[] = {
1175         {
1176                 .vendor       = 0x14f1,
1177                 .device       = 0x8802,
1178                 .subvendor    = PCI_ANY_ID,
1179                 .subdevice    = PCI_ANY_ID,
1180         },{
1181                 /* --- end of list --- */
1182         }
1183 };
1184 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
1185
1186 static struct pci_driver blackbird_pci_driver = {
1187         .name     = "cx88-blackbird",
1188         .id_table = cx8802_pci_tbl,
1189         .probe    = blackbird_probe,
1190         .remove   = __devexit_p(blackbird_remove),
1191         .suspend  = cx8802_suspend_common,
1192         .resume   = cx8802_resume_common,
1193 };
1194
1195 static int blackbird_init(void)
1196 {
1197         printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
1198                (CX88_VERSION_CODE >> 16) & 0xff,
1199                (CX88_VERSION_CODE >>  8) & 0xff,
1200                CX88_VERSION_CODE & 0xff);
1201 #ifdef SNAPSHOT
1202         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
1203                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1204 #endif
1205         return pci_register_driver(&blackbird_pci_driver);
1206 }
1207
1208 static void blackbird_fini(void)
1209 {
1210         pci_unregister_driver(&blackbird_pci_driver);
1211 }
1212
1213 module_init(blackbird_init);
1214 module_exit(blackbird_fini);
1215
1216 /* ----------------------------------------------------------- */
1217 /*
1218  * Local variables:
1219  * c-basic-offset: 8
1220  * End:
1221  * 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
1222  */