This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / media / video / cx88 / cx88-blackbird.c
1 /*
2  * $Id: cx88-blackbird.c,v 1.17 2004/11/07 13:17:15 kraxel 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/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
36 MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
37 MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
38 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
39 MODULE_LICENSE("GPL");
40
41 static unsigned int mpegbufs = 8;
42 module_param(mpegbufs,int,0644);
43 MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
44
45 static unsigned int debug = 0;
46 module_param(debug,int,0644);
47 MODULE_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
52 static 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
63 /*Firmware API commands*/
64 #define IVTV_API_ENC_PING_FW 0x00000080
65 #define IVTV_API_ENC_GETVER 0x000000C4
66 #define IVTV_API_ENC_HALT_FW 0x000000C3
67 #define IVTV_API_STD_TIMEOUT 0x00010000 /*units??*/
68 //#define IVTV_API_ASSIGN_PGM_INDEX_INFO 0x000000c7
69 #define IVTV_API_ASSIGN_STREAM_TYPE 0x000000b9
70 #define IVTV_API_ASSIGN_OUTPUT_PORT 0x000000bb
71 #define IVTV_API_ASSIGN_FRAMERATE 0x0000008f
72 #define IVTV_API_ASSIGN_FRAME_SIZE 0x00000091
73 #define IVTV_API_ASSIGN_ASPECT_RATIO 0x00000099
74 #define IVTV_API_ASSIGN_BITRATES 0x00000095
75 #define IVTV_API_ASSIGN_GOP_PROPERTIES 0x00000097
76 #define IVTV_API_ASSIGN_3_2_PULLDOWN 0x000000b1
77 #define IVTV_API_ASSIGN_GOP_CLOSURE 0x000000c5
78 #define IVTV_API_ASSIGN_AUDIO_PROPERTIES 0x000000bd
79 #define IVTV_API_ASSIGN_DNR_FILTER_MODE 0x0000009b
80 #define IVTV_API_ASSIGN_DNR_FILTER_PROPS 0x0000009d
81 #define IVTV_API_ASSIGN_CORING_LEVELS 0x0000009f
82 #define IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE 0x000000a1
83 #define IVTV_API_ASSIGN_FRAME_DROP_RATE 0x000000d0
84 #define IVTV_API_ASSIGN_PLACEHOLDER 0x000000d8
85 #define IVTV_API_MUTE_VIDEO 0x000000d9
86 #define IVTV_API_MUTE_AUDIO 0x000000da
87 #define IVTV_API_INITIALIZE_INPUT 0x000000cd
88 #define IVTV_API_REFRESH_INPUT 0x000000d3
89 #define IVTV_API_ASSIGN_NUM_VSYNC_LINES 0x000000d6
90 #define IVTV_API_BEGIN_CAPTURE 0x00000081
91 //#define IVTV_API_PAUSE_ENCODER 0x000000d2
92 //#define IVTV_API_EVENT_NOTIFICATION 0x000000d5
93 #define IVTV_API_END_CAPTURE 0x00000082
94
95 /* Registers */
96 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
97 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
98 #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
99 #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
100 #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
101 #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
102
103 /* ------------------------------------------------------------------ */
104
105 static void host_setup(struct cx88_core *core)
106 {
107         /* toggle reset of the host */
108         cx_write(MO_GPHST_SOFT_RST, 1);
109         udelay(100);
110         cx_write(MO_GPHST_SOFT_RST, 0);
111         udelay(100);
112
113         /* host port setup */
114         cx_write(MO_GPHST_WSC, 0x44444444U);
115         cx_write(MO_GPHST_XFR, 0);
116         cx_write(MO_GPHST_WDTH, 15);
117         cx_write(MO_GPHST_HDSHK, 0);
118         cx_write(MO_GPHST_MUX16, 0x44448888U);
119         cx_write(MO_GPHST_MODE, 0);
120 }
121
122 /* ------------------------------------------------------------------ */
123
124 #define P1_MDATA0 0x390000
125 #define P1_MDATA1 0x390001
126 #define P1_MDATA2 0x390002
127 #define P1_MDATA3 0x390003
128 #define P1_MADDR2 0x390004
129 #define P1_MADDR1 0x390005
130 #define P1_MADDR0 0x390006
131 #define P1_RDATA0 0x390008
132 #define P1_RDATA1 0x390009
133 #define P1_RDATA2 0x39000A
134 #define P1_RDATA3 0x39000B
135 #define P1_RADDR0 0x39000C
136 #define P1_RADDR1 0x39000D
137 #define P1_RRDWR  0x39000E
138
139 static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
140 {
141         unsigned long timeout = jiffies + msecs_to_jiffies(1);
142         u32 gpio0,need;
143
144         need = state ? 2 : 0;
145         for (;;) {
146                 gpio0 = cx_read(MO_GP0_IO) & 2;
147                 if (need == gpio0)
148                         return 0;
149                 if (time_after(jiffies,timeout))
150                         return -1;
151                 udelay(1);
152         }
153 }
154
155 static int memory_write(struct cx88_core *core, u32 address, u32 value)
156 {
157         /* Warning: address is dword address (4 bytes) */
158         cx_writeb(P1_MDATA0, (unsigned int)value);
159         cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
160         cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
161         cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
162         cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
163         cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
164         cx_writeb(P1_MADDR0, (unsigned int)address);
165         cx_read(P1_MDATA0);
166         cx_read(P1_MADDR0);
167
168         return wait_ready_gpio0_bit1(core,1);
169 }
170
171 static int memory_read(struct cx88_core *core, u32 address, u32 *value)
172 {
173         int retval;
174         u32 val;
175
176         /* Warning: address is dword address (4 bytes) */
177         cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
178         cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
179         cx_writeb(P1_MADDR0, (unsigned int)address);
180         cx_read(P1_MADDR0);
181
182         retval = wait_ready_gpio0_bit1(core,1);
183
184         cx_writeb(P1_MDATA3, 0);
185         val     = (unsigned char)cx_read(P1_MDATA3) << 24;
186         cx_writeb(P1_MDATA2, 0);
187         val    |= (unsigned char)cx_read(P1_MDATA2) << 16;
188         cx_writeb(P1_MDATA1, 0);
189         val    |= (unsigned char)cx_read(P1_MDATA1) << 8;
190         cx_writeb(P1_MDATA0, 0);
191         val    |= (unsigned char)cx_read(P1_MDATA0);
192
193         *value  = val;
194         return retval;
195 }
196
197 static int register_write(struct cx88_core *core, u32 address, u32 value)
198 {
199         cx_writeb(P1_RDATA0, (unsigned int)value);
200         cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
201         cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
202         cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
203         cx_writeb(P1_RADDR0, (unsigned int)address);
204         cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
205         cx_writeb(P1_RRDWR, 1);
206         cx_read(P1_RDATA0);
207         cx_read(P1_RADDR0);
208
209         return wait_ready_gpio0_bit1(core,1);
210 #if 0
211         udelay(1000); /* without this, things don't go right (subsequent memory_write()'s don't get through */
212         /* ? would this be safe here? set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(1); */
213 #endif
214 }
215
216
217 static int register_read(struct cx88_core *core, u32 address, u32 *value)
218 {
219         int retval;
220         u32 val;
221
222         cx_writeb(P1_RADDR0, (unsigned int)address);
223         cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
224         cx_writeb(P1_RRDWR, 0);
225         cx_read(P1_RADDR0);
226
227         retval  = wait_ready_gpio0_bit1(core,1);
228         val     = (unsigned char)cx_read(P1_RDATA0);
229         val    |= (unsigned char)cx_read(P1_RDATA1) << 8;
230         val    |= (unsigned char)cx_read(P1_RDATA2) << 16;
231         val    |= (unsigned char)cx_read(P1_RDATA3) << 24;
232
233         *value  = val;
234         return retval;
235 }
236
237 /* ------------------------------------------------------------------ */
238
239 /* We don't need to call the API often, so using just one mailbox will probably suffice */
240 static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
241                              u32 inputcnt, u32 outputcnt, ...)
242 {
243         unsigned long timeout;
244         u32 value, flag, retval;
245         int i;
246         va_list args;
247         va_start(args, outputcnt);
248
249         dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
250
251         /* this may not be 100% safe if we can't read any memory location
252            without side effects */
253         memory_read(dev->core, dev->mailbox - 4, &value);
254         if (value != 0x12345678) {
255                 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
256                 return -1;
257         }
258
259         memory_read(dev->core, dev->mailbox, &flag);
260         if (flag) {
261                 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
262                 return -1;
263         }
264
265         flag |= 1; /* tell 'em we're working on it */
266         memory_write(dev->core, dev->mailbox, flag);
267
268         /* write command + args + fill remaining with zeros */
269         memory_write(dev->core, dev->mailbox + 1, command); /* command code */
270         memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
271         for (i = 0; i < inputcnt ; i++) {
272                 value = va_arg(args, int);
273                 memory_write(dev->core, dev->mailbox + 4 + i, value);
274                 dprintk(1, "API Input %d = %d\n", i, value);
275         }
276         for (; i < 16 ; i++)
277                 memory_write(dev->core, dev->mailbox + 4 + i, 0);
278
279         flag |= 3; /* tell 'em we're done writing */
280         memory_write(dev->core, dev->mailbox, flag);
281
282         /* wait for firmware to handle the API command */
283         timeout = jiffies + msecs_to_jiffies(10);
284         for (;;) {
285                 memory_read(dev->core, dev->mailbox, &flag);
286                 if (0 == (flag & 4))
287                         break;
288                 if (time_after(jiffies,timeout)) {
289                         dprintk(0, "ERROR: API Mailbox timeout\n");
290                         return -1;
291                 }
292                 udelay(10);
293         }
294
295         /* read output values */
296         for (i = 0; i < outputcnt ; i++) {
297                 int *vptr = va_arg(args, int *);
298                 memory_read(dev->core, dev->mailbox + 4 + i, vptr);
299                 dprintk(1, "API Output %d = %d\n", i, *vptr);
300         }
301         va_end(args);
302
303         memory_read(dev->core, dev->mailbox + 2, &retval);
304         dprintk(1, "API result = %d\n",retval);
305
306         flag = 0;
307         memory_write(dev->core, dev->mailbox, flag);
308         return retval;
309 }
310
311
312 static int blackbird_find_mailbox(struct cx8802_dev *dev)
313 {
314         u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
315         int signaturecnt=0;
316         u32 value;
317         int i;
318
319         for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
320                 memory_read(dev->core, i, &value);
321                 if (value == signature[signaturecnt])
322                         signaturecnt++;
323                 else
324                         signaturecnt = 0;
325                 if (4 == signaturecnt) {
326                         dprintk(1, "Mailbox signature found\n");
327                         return i;
328                 }
329         }
330         dprintk(0, "Mailbox signature values not found!\n");
331         return -1;
332 }
333
334 static int blackbird_load_firmware(struct cx8802_dev *dev)
335 {
336         static const unsigned char magic[8] = {
337                 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
338         };
339         const struct firmware *firmware;
340         int i, retval = 0;
341         u32 value = 0;
342         u32 checksum = 0;
343         u32 *dataptr;
344
345         retval  = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
346         retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
347         retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
348         retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
349         msleep(1);
350         retval |= register_write(dev->core, IVTV_REG_APU, 0);
351
352         if (retval < 0)
353                 dprintk(0, "Error with register_write\n");
354
355         retval = request_firmware(&firmware, BLACKBIRD_FIRM_ENC_FILENAME,
356                                   &dev->pci->dev);
357         if (retval != 0) {
358                 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
359                         BLACKBIRD_FIRM_ENC_FILENAME);
360                 dprintk(0, "Please fix your hotplug setup, the board will "
361                         "not work without firmware loaded!\n");
362                 return -1;
363         }
364
365         if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
366                 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
367                         firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
368                 return -1;
369         }
370
371         if (0 != memcmp(firmware->data, magic, 8)) {
372                 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
373                 return -1;
374         }
375
376         /* transfer to the chip */
377         dprintk(1,"Loading firmware ...\n");
378         dataptr = (u32*)firmware->data;
379         for (i = 0; i < (firmware->size >> 2); i++) {
380                 value = *dataptr;
381                 checksum += ~value;
382                 memory_write(dev->core, i, value);
383                 dataptr++;
384         }
385
386         /* read back to verify with the checksum */
387         for (i--; i >= 0; i--) {
388                 memory_read(dev->core, i, &value);
389                 checksum -= ~value;
390         }
391         if (checksum) {
392                 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
393                 return -1;
394         }
395         release_firmware(firmware);
396         dprintk(0, "Firmware upload successful.\n");
397
398         retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
399         retval |= register_read(dev->core, IVTV_REG_SPU, &value);
400         retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
401         msleep(1);
402
403         retval |= register_read(dev->core, IVTV_REG_VPU, &value);
404         retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
405
406         if (retval < 0)
407                 dprintk(0, "Error with register_write\n");
408         return 0;
409 }
410
411 static void blackbird_codec_settings(struct cx8802_dev *dev)
412 {
413         int bitrate_mode = 1;
414         int bitrate = 7500000;
415         int bitrate_peak = 7500000;
416
417         /* assign stream type */
418         blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 0); /* program stream */
419         //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 2); /* MPEG1 stream */
420         //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 3); /* PES A/V */
421         //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 10); /* DVD stream */
422
423         /* assign output port */
424         blackbird_api_cmd(dev, IVTV_API_ASSIGN_OUTPUT_PORT, 1, 0, 1); /* 1 = Host */
425
426         /* assign framerate */
427         blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAMERATE, 1, 0, 0);
428
429         /* assign frame size */
430         blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_SIZE, 2, 0, 480, 720);
431
432         /* assign aspect ratio */
433         blackbird_api_cmd(dev, IVTV_API_ASSIGN_ASPECT_RATIO, 1, 0, 2);
434
435         /* assign bitrates */
436         blackbird_api_cmd(dev, IVTV_API_ASSIGN_BITRATES, 5, 0,
437                          bitrate_mode,         /* mode */
438                          bitrate,              /* bps */
439                          bitrate_peak / 400,   /* peak/400 */
440                          0, 0x70);             /* encoding buffer, ckennedy */
441
442         /* assign gop properties */
443         blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 15, 3);
444         //blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 2, 1);
445
446         /* assign 3 2 pulldown */
447         blackbird_api_cmd(dev, IVTV_API_ASSIGN_3_2_PULLDOWN, 1, 0, 0);
448
449         /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
450         blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, (2<<2) | (8<<4));
451
452         /* assign gop closure */
453         blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_CLOSURE, 1, 0, 0);
454
455         /* assign audio properties */
456         blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, 0 | (2 << 2) | (14 << 4));
457
458         /* assign dnr filter mode */
459         blackbird_api_cmd(dev, IVTV_API_ASSIGN_DNR_FILTER_MODE, 2, 0, 0, 0);
460
461         /* assign dnr filter props*/
462         blackbird_api_cmd(dev, IVTV_API_ASSIGN_DNR_FILTER_PROPS, 2, 0, 0, 0);
463
464         /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
465         blackbird_api_cmd(dev, IVTV_API_ASSIGN_CORING_LEVELS, 4, 0, 0, 255, 0, 255);
466
467         /* assign spatial filter type: luma_t: 1 = horiz_only, chroma_t: 1 = horiz_only */
468         blackbird_api_cmd(dev, IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE, 2, 0, 1, 1);
469
470         /* assign frame drop rate */
471         blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_DROP_RATE, 1, 0, 0);
472 }
473
474 static int blackbird_initialize_codec(struct cx8802_dev *dev)
475 {
476         struct cx88_core *core = dev->core;
477         int version;
478         int retval;
479
480         dprintk(1,"Initialize codec\n");
481         retval = blackbird_api_cmd(dev, IVTV_API_ENC_PING_FW, 0, 0); /* ping */
482         if (retval < 0) {
483                 /* ping was not successful, reset and upload firmware */
484                 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
485                 msleep(1);
486                 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
487                 msleep(1);
488                 retval = blackbird_load_firmware(dev);
489                 if (retval < 0)
490                         return retval;
491
492                 dev->mailbox = blackbird_find_mailbox(dev);
493                 if (dev->mailbox < 0)
494                         return -1;
495
496                 retval = blackbird_api_cmd(dev, IVTV_API_ENC_PING_FW, 0, 0); /* ping */
497                 if (retval < 0) {
498                         dprintk(0, "ERROR: Firmware ping failed!\n");
499                         return -1;
500                 }
501
502                 retval = blackbird_api_cmd(dev, IVTV_API_ENC_GETVER, 0, 1, &version);
503                 if (retval < 0) {
504                         dprintk(0, "ERROR: Firmware get encoder version failed!\n");
505                         return -1;
506                 }
507                 dprintk(0, "Firmware version is 0x%08x\n", version);
508         }
509         msleep(1);
510
511         cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
512         cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
513         cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
514         cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
515
516 #if 0 /* FIXME */
517         set_scale(dev, 720, 480, V4L2_FIELD_INTERLACED);
518 #endif
519         blackbird_codec_settings(dev);
520         msleep(1);
521
522         //blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
523         blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
524         //blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180);
525         blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
526
527         blackbird_api_cmd(dev, IVTV_API_INITIALIZE_INPUT, 0, 0); /* initialize the video input */
528
529         msleep(1);
530
531         blackbird_api_cmd(dev, IVTV_API_MUTE_VIDEO, 1, 0, 0);
532         msleep(1);
533         blackbird_api_cmd(dev, IVTV_API_MUTE_AUDIO, 1, 0, 0);
534         msleep(1);
535
536         blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0x13); /* start capturing to the host interface */
537         //blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0); /* start capturing to the host interface */
538         msleep(1);
539
540         blackbird_api_cmd(dev, IVTV_API_REFRESH_INPUT, 0,0);
541         return 0;
542 }
543
544 /* ------------------------------------------------------------------ */
545
546 static int bb_buf_setup(struct videobuf_queue *q,
547                         unsigned int *count, unsigned int *size)
548 {
549         struct cx8802_fh *fh = q->priv_data;
550
551         fh->dev->ts_packet_size  = 512;
552         fh->dev->ts_packet_count = 100;
553
554         *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
555         if (0 == *count)
556                 *count = mpegbufs;
557         if (*count < 2)
558                 *count = 2;
559         if (*count > 32)
560                 *count = 32;
561         return 0;
562 }
563
564 static int
565 bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
566                enum v4l2_field field)
567 {
568         struct cx8802_fh *fh = q->priv_data;
569         return cx8802_buf_prepare(fh->dev, (struct cx88_buffer*)vb);
570 }
571
572 static void
573 bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
574 {
575         struct cx8802_fh *fh = q->priv_data;
576         cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
577 }
578
579 static void
580 bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
581 {
582         struct cx8802_fh *fh = q->priv_data;
583         cx88_free_buffer(fh->dev->pci, (struct cx88_buffer*)vb);
584 }
585
586 static struct videobuf_queue_ops blackbird_qops = {
587         .buf_setup    = bb_buf_setup,
588         .buf_prepare  = bb_buf_prepare,
589         .buf_queue    = bb_buf_queue,
590         .buf_release  = bb_buf_release,
591 };
592
593 /* ------------------------------------------------------------------ */
594
595 static int mpeg_do_ioctl(struct inode *inode, struct file *file,
596                          unsigned int cmd, void *arg)
597 {
598         struct cx8802_fh  *fh  = file->private_data;
599         struct cx8802_dev *dev = fh->dev;
600
601         if (debug > 1)
602                 cx88_print_ioctl(dev->core->name,cmd);
603
604         switch (cmd) {
605
606         /* --- capture ioctls ---------------------------------------- */
607         case VIDIOC_ENUM_FMT:
608         {
609                 struct v4l2_fmtdesc *f = arg;
610                 int index;
611
612                 index = f->index;
613                 if (index != 0)
614                         return -EINVAL;
615
616                 memset(f,0,sizeof(*f));
617                 f->index = index;
618                 strlcpy(f->description, "MPEG TS", sizeof(f->description));
619                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
620                 f->pixelformat = V4L2_PIX_FMT_MPEG;
621                 return 0;
622         }
623         case VIDIOC_G_FMT:
624         case VIDIOC_S_FMT:
625         case VIDIOC_TRY_FMT:
626         {
627                 /* FIXME -- quick'n'dirty for exactly one size ... */
628                 struct v4l2_format *f = arg;
629
630                 memset(f,0,sizeof(*f));
631                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
632                 f->fmt.pix.width        = 720;
633                 f->fmt.pix.height       = 576;
634                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
635                 f->fmt.pix.sizeimage    = 1024 * 512 /* FIXME: BUFFER_SIZE */;
636         }
637
638         /* --- streaming capture ------------------------------------- */
639         case VIDIOC_REQBUFS:
640                 return videobuf_reqbufs(&fh->mpegq, arg);
641
642         case VIDIOC_QUERYBUF:
643                 return videobuf_querybuf(&fh->mpegq, arg);
644
645         case VIDIOC_QBUF:
646                 return videobuf_qbuf(&fh->mpegq, arg);
647
648         case VIDIOC_DQBUF:
649                 return videobuf_dqbuf(&fh->mpegq, arg,
650                                       file->f_flags & O_NONBLOCK);
651
652         case VIDIOC_STREAMON:
653                 return videobuf_streamon(&fh->mpegq);
654
655         case VIDIOC_STREAMOFF:
656                 return videobuf_streamoff(&fh->mpegq);
657
658         default:
659                 return -EINVAL;
660         }
661         return 0;
662 }
663
664 static int mpeg_ioctl(struct inode *inode, struct file *file,
665                        unsigned int cmd, unsigned long arg)
666 {
667         return video_usercopy(inode, file, cmd, arg, mpeg_do_ioctl);
668 }
669
670 static int mpeg_open(struct inode *inode, struct file *file)
671 {
672         int minor = iminor(inode);
673         struct cx8802_dev *h,*dev = NULL;
674         struct cx8802_fh *fh;
675         struct list_head *list;
676
677         list_for_each(list,&cx8802_devlist) {
678                 h = list_entry(list, struct cx8802_dev, devlist);
679                 if (h->mpeg_dev->minor == minor)
680                         dev = h;
681         }
682         if (NULL == dev)
683                 return -ENODEV;
684
685         if (blackbird_initialize_codec(dev) < 0)
686                 return -EINVAL;
687         dprintk(1,"open minor=%d\n",minor);
688
689         /* allocate + initialize per filehandle data */
690         fh = kmalloc(sizeof(*fh),GFP_KERNEL);
691         if (NULL == fh)
692                 return -ENOMEM;
693         memset(fh,0,sizeof(*fh));
694         file->private_data = fh;
695         fh->dev      = dev;
696
697         videobuf_queue_init(&fh->mpegq, &blackbird_qops,
698                             dev->pci, &dev->slock,
699                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
700                             V4L2_FIELD_TOP,
701                             sizeof(struct cx88_buffer),
702                             fh);
703         return 0;
704 }
705
706 static int mpeg_release(struct inode *inode, struct file *file)
707 {
708         struct cx8802_fh  *fh  = file->private_data;
709
710         blackbird_api_cmd(fh->dev, IVTV_API_END_CAPTURE, 3, 0, 1, 0, 0x13);
711
712         /* stop mpeg capture */
713         if (fh->mpegq.streaming)
714                 videobuf_streamoff(&fh->mpegq);
715         if (fh->mpegq.reading)
716                 videobuf_read_stop(&fh->mpegq);
717
718         file->private_data = NULL;
719         kfree(fh);
720         return 0;
721 }
722
723 static ssize_t
724 mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
725 {
726         struct cx8802_fh *fh = file->private_data;
727
728         return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
729                                     file->f_flags & O_NONBLOCK);
730 }
731
732 static unsigned int
733 mpeg_poll(struct file *file, struct poll_table_struct *wait)
734 {
735         struct cx8802_fh *fh = file->private_data;
736
737         return videobuf_poll_stream(file, &fh->mpegq, wait);
738 }
739
740 static int
741 mpeg_mmap(struct file *file, struct vm_area_struct * vma)
742 {
743         struct cx8802_fh *fh = file->private_data;
744
745         return videobuf_mmap_mapper(&fh->mpegq, vma);
746 }
747
748 static struct file_operations mpeg_fops =
749 {
750         .owner         = THIS_MODULE,
751         .open          = mpeg_open,
752         .release       = mpeg_release,
753         .read          = mpeg_read,
754         .poll          = mpeg_poll,
755         .mmap          = mpeg_mmap,
756         .ioctl         = mpeg_ioctl,
757         .llseek        = no_llseek,
758 };
759
760 static struct video_device cx8802_mpeg_template =
761 {
762         .name          = "cx8802",
763         .type          = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
764         .hardware      = 0,
765         .fops          = &mpeg_fops,
766         .minor         = -1,
767 };
768
769 /* ------------------------------------------------------------------ */
770
771 static void blackbird_unregister_video(struct cx8802_dev *dev)
772 {
773         if (dev->mpeg_dev) {
774                 if (-1 != dev->mpeg_dev->minor)
775                         video_unregister_device(dev->mpeg_dev);
776                 else
777                         video_device_release(dev->mpeg_dev);
778                 dev->mpeg_dev = NULL;
779         }
780 }
781
782 static int blackbird_register_video(struct cx8802_dev *dev)
783 {
784         int err;
785
786         dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
787                                        &cx8802_mpeg_template,"mpeg");
788         err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
789         if (err < 0) {
790                 printk(KERN_INFO "%s/2: can't register mpeg device\n",
791                        dev->core->name);
792                 return err;
793         }
794         printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
795                dev->core->name,dev->mpeg_dev->minor & 0x1f);
796         return 0;
797 }
798
799 /* ----------------------------------------------------------- */
800
801 static int __devinit blackbird_probe(struct pci_dev *pci_dev,
802                                      const struct pci_device_id *pci_id)
803 {
804         struct cx8802_dev *dev;
805         struct cx88_core  *core;
806         int err;
807
808         /* general setup */
809         core = cx88_core_get(pci_dev);
810         if (NULL == core)
811                 return -EINVAL;
812
813         err = -ENODEV;
814         if (!cx88_boards[core->board].blackbird)
815                 goto fail_core;
816
817         err = -ENOMEM;
818         dev = kmalloc(sizeof(*dev),GFP_KERNEL);
819         if (NULL == dev)
820                 goto fail_core;
821         memset(dev,0,sizeof(*dev));
822         dev->pci = pci_dev;
823         dev->core = core;
824
825         err = cx8802_init_common(dev);
826         if (0 != err)
827                 goto fail_free;
828
829         /* blackbird stuff */
830         printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
831                core->name);
832         host_setup(dev->core);
833
834         list_add_tail(&dev->devlist,&cx8802_devlist);
835         blackbird_register_video(dev);
836         return 0;
837
838  fail_free:
839         kfree(dev);
840  fail_core:
841         cx88_core_put(core,pci_dev);
842         return err;
843 }
844
845 static void __devexit blackbird_remove(struct pci_dev *pci_dev)
846 {
847         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
848
849         /* blackbird */
850         blackbird_unregister_video(dev);
851         list_del(&dev->devlist);
852
853         /* common */
854         cx8802_fini_common(dev);
855 }
856
857 static struct pci_device_id cx8802_pci_tbl[] = {
858         {
859                 .vendor       = 0x14f1,
860                 .device       = 0x8802,
861                 .subvendor    = PCI_ANY_ID,
862                 .subdevice    = PCI_ANY_ID,
863         },{
864                 /* --- end of list --- */
865         }
866 };
867 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
868
869 static struct pci_driver blackbird_pci_driver = {
870         .name     = "cx88-blackbird",
871         .id_table = cx8802_pci_tbl,
872         .probe    = blackbird_probe,
873         .remove   = __devexit_p(blackbird_remove),
874         .suspend  = cx8802_suspend_common,
875         .resume   = cx8802_resume_common,
876 };
877
878 static int blackbird_init(void)
879 {
880         printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
881                (CX88_VERSION_CODE >> 16) & 0xff,
882                (CX88_VERSION_CODE >>  8) & 0xff,
883                CX88_VERSION_CODE & 0xff);
884 #ifdef SNAPSHOT
885         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
886                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
887 #endif
888         return pci_module_init(&blackbird_pci_driver);
889 }
890
891 static void blackbird_fini(void)
892 {
893         pci_unregister_driver(&blackbird_pci_driver);
894 }
895
896 module_init(blackbird_init);
897 module_exit(blackbird_fini);
898
899 /* ----------------------------------------------------------- */
900 /*
901  * Local variables:
902  * c-basic-offset: 8
903  * End:
904  */