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