]> err.no Git - linux-2.6/blob - drivers/media/video/cx88/cx88-blackbird.c
2eb31ff0c1f43d3596ee76646c10188824078a30
[linux-2.6] / drivers / media / video / cx88 / cx88-blackbird.c
1 /*
2  *
3  *  Support for a cx23416 mpeg encoder via cx2388x host port.
4  *  "blackbird" reference design.
5  *
6  *    (c) 2004 Jelle Foks <jelle@foks.8m.com>
7  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
8  *
9  *  Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/firmware.h>
33 #include <media/v4l2-common.h>
34 #include <media/cx2341x.h>
35
36 #include "cx88.h"
37
38 MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
39 MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40 MODULE_LICENSE("GPL");
41
42 static unsigned int mpegbufs = 32;
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
54 /* ------------------------------------------------------------------ */
55
56
57 struct cx88_tvnorm {
58         char                   *name;
59         v4l2_std_id            id;
60         u32                    cxiformat;
61         u32                    cxoformat;
62 };
63
64 static struct cx88_tvnorm tvnorms[] = {
65         {
66                 .name      = "NTSC-M",
67                 .id        = V4L2_STD_NTSC_M,
68                 .cxiformat = VideoFormatNTSC,
69                 .cxoformat = 0x181f0008,
70         },{
71                 .name      = "NTSC-JP",
72                 .id        = V4L2_STD_NTSC_M_JP,
73                 .cxiformat = VideoFormatNTSCJapan,
74                 .cxoformat = 0x181f0008,
75         },{
76                 .name      = "PAL-BG",
77                 .id        = V4L2_STD_PAL_BG,
78                 .cxiformat = VideoFormatPAL,
79                 .cxoformat = 0x181f0008,
80         },{
81                 .name      = "PAL-DK",
82                 .id        = V4L2_STD_PAL_DK,
83                 .cxiformat = VideoFormatPAL,
84                 .cxoformat = 0x181f0008,
85         },{
86                 .name      = "PAL-I",
87                 .id        = V4L2_STD_PAL_I,
88                 .cxiformat = VideoFormatPAL,
89                 .cxoformat = 0x181f0008,
90         },{
91                 .name      = "PAL-M",
92                 .id        = V4L2_STD_PAL_M,
93                 .cxiformat = VideoFormatPALM,
94                 .cxoformat = 0x1c1f0008,
95         },{
96                 .name      = "PAL-N",
97                 .id        = V4L2_STD_PAL_N,
98                 .cxiformat = VideoFormatPALN,
99                 .cxoformat = 0x1c1f0008,
100         },{
101                 .name      = "PAL-Nc",
102                 .id        = V4L2_STD_PAL_Nc,
103                 .cxiformat = VideoFormatPALNC,
104                 .cxoformat = 0x1c1f0008,
105         },{
106                 .name      = "PAL-60",
107                 .id        = V4L2_STD_PAL_60,
108                 .cxiformat = VideoFormatPAL60,
109                 .cxoformat = 0x181f0008,
110         },{
111                 .name      = "SECAM-L",
112                 .id        = V4L2_STD_SECAM_L,
113                 .cxiformat = VideoFormatSECAM,
114                 .cxoformat = 0x181f0008,
115         },{
116                 .name      = "SECAM-DK",
117                 .id        = V4L2_STD_SECAM_DK,
118                 .cxiformat = VideoFormatSECAM,
119                 .cxoformat = 0x181f0008,
120         }
121 };
122 int cx88_do_ioctl( struct inode *inode, struct file *file,
123                    int radio, struct cx88_core *core, unsigned int cmd,
124                    void *arg, v4l2_kioctl driver_ioctl );
125
126 #define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
127
128 /* defines below are from ivtv-driver.h */
129
130 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
131
132 /* Firmware API commands */
133 #define IVTV_API_STD_TIMEOUT 500
134
135 enum blackbird_capture_type {
136         BLACKBIRD_MPEG_CAPTURE,
137         BLACKBIRD_RAW_CAPTURE,
138         BLACKBIRD_RAW_PASSTHRU_CAPTURE
139 };
140 enum blackbird_capture_bits {
141         BLACKBIRD_RAW_BITS_NONE             = 0x00,
142         BLACKBIRD_RAW_BITS_YUV_CAPTURE      = 0x01,
143         BLACKBIRD_RAW_BITS_PCM_CAPTURE      = 0x02,
144         BLACKBIRD_RAW_BITS_VBI_CAPTURE      = 0x04,
145         BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
146         BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE  = 0x10
147 };
148 enum blackbird_capture_end {
149         BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
150         BLACKBIRD_END_NOW, /* stop immediately, no irq */
151 };
152 enum blackbird_framerate {
153         BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
154         BLACKBIRD_FRAMERATE_PAL_25   /* PAL: 25fps */
155 };
156 enum blackbird_stream_port {
157         BLACKBIRD_OUTPUT_PORT_MEMORY,
158         BLACKBIRD_OUTPUT_PORT_STREAMING,
159         BLACKBIRD_OUTPUT_PORT_SERIAL
160 };
161 enum blackbird_data_xfer_status {
162         BLACKBIRD_MORE_BUFFERS_FOLLOW,
163         BLACKBIRD_LAST_BUFFER,
164 };
165 enum blackbird_picture_mask {
166         BLACKBIRD_PICTURE_MASK_NONE,
167         BLACKBIRD_PICTURE_MASK_I_FRAMES,
168         BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
169         BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
170 };
171 enum blackbird_vbi_mode_bits {
172         BLACKBIRD_VBI_BITS_SLICED,
173         BLACKBIRD_VBI_BITS_RAW,
174 };
175 enum blackbird_vbi_insertion_bits {
176         BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
177         BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
178         BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
179         BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
180         BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
181 };
182 enum blackbird_dma_unit {
183         BLACKBIRD_DMA_BYTES,
184         BLACKBIRD_DMA_FRAMES,
185 };
186 enum blackbird_dma_transfer_status_bits {
187         BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
188         BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
189         BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
190 };
191 enum blackbird_pause {
192         BLACKBIRD_PAUSE_ENCODING,
193         BLACKBIRD_RESUME_ENCODING,
194 };
195 enum blackbird_copyright {
196         BLACKBIRD_COPYRIGHT_OFF,
197         BLACKBIRD_COPYRIGHT_ON,
198 };
199 enum blackbird_notification_type {
200         BLACKBIRD_NOTIFICATION_REFRESH,
201 };
202 enum blackbird_notification_status {
203         BLACKBIRD_NOTIFICATION_OFF,
204         BLACKBIRD_NOTIFICATION_ON,
205 };
206 enum blackbird_notification_mailbox {
207         BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
208 };
209 enum blackbird_field1_lines {
210         BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
211         BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
212         BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
213 };
214 enum blackbird_field2_lines {
215         BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
216         BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
217         BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
218 };
219 enum blackbird_custom_data_type {
220         BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
221         BLACKBIRD_CUSTOM_PRIVATE_PACKET,
222 };
223 enum blackbird_mute {
224         BLACKBIRD_UNMUTE,
225         BLACKBIRD_MUTE,
226 };
227 enum blackbird_mute_video_mask {
228         BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
229         BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
230         BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
231 };
232 enum blackbird_mute_video_shift {
233         BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
234         BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
235         BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
236 };
237
238 /* Registers */
239 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
240 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
241 #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
242 #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
243 #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
244 #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
245
246 /* ------------------------------------------------------------------ */
247
248 static void host_setup(struct cx88_core *core)
249 {
250         /* toggle reset of the host */
251         cx_write(MO_GPHST_SOFT_RST, 1);
252         udelay(100);
253         cx_write(MO_GPHST_SOFT_RST, 0);
254         udelay(100);
255
256         /* host port setup */
257         cx_write(MO_GPHST_WSC, 0x44444444U);
258         cx_write(MO_GPHST_XFR, 0);
259         cx_write(MO_GPHST_WDTH, 15);
260         cx_write(MO_GPHST_HDSHK, 0);
261         cx_write(MO_GPHST_MUX16, 0x44448888U);
262         cx_write(MO_GPHST_MODE, 0);
263 }
264
265 /* ------------------------------------------------------------------ */
266
267 #define P1_MDATA0 0x390000
268 #define P1_MDATA1 0x390001
269 #define P1_MDATA2 0x390002
270 #define P1_MDATA3 0x390003
271 #define P1_MADDR2 0x390004
272 #define P1_MADDR1 0x390005
273 #define P1_MADDR0 0x390006
274 #define P1_RDATA0 0x390008
275 #define P1_RDATA1 0x390009
276 #define P1_RDATA2 0x39000A
277 #define P1_RDATA3 0x39000B
278 #define P1_RADDR0 0x39000C
279 #define P1_RADDR1 0x39000D
280 #define P1_RRDWR  0x39000E
281
282 static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
283 {
284         unsigned long timeout = jiffies + msecs_to_jiffies(1);
285         u32 gpio0,need;
286
287         need = state ? 2 : 0;
288         for (;;) {
289                 gpio0 = cx_read(MO_GP0_IO) & 2;
290                 if (need == gpio0)
291                         return 0;
292                 if (time_after(jiffies,timeout))
293                         return -1;
294                 udelay(1);
295         }
296 }
297
298 static int memory_write(struct cx88_core *core, u32 address, u32 value)
299 {
300         /* Warning: address is dword address (4 bytes) */
301         cx_writeb(P1_MDATA0, (unsigned int)value);
302         cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
303         cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
304         cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
305         cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
306         cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
307         cx_writeb(P1_MADDR0, (unsigned int)address);
308         cx_read(P1_MDATA0);
309         cx_read(P1_MADDR0);
310
311         return wait_ready_gpio0_bit1(core,1);
312 }
313
314 static int memory_read(struct cx88_core *core, u32 address, u32 *value)
315 {
316         int retval;
317         u32 val;
318
319         /* Warning: address is dword address (4 bytes) */
320         cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
321         cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
322         cx_writeb(P1_MADDR0, (unsigned int)address);
323         cx_read(P1_MADDR0);
324
325         retval = wait_ready_gpio0_bit1(core,1);
326
327         cx_writeb(P1_MDATA3, 0);
328         val     = (unsigned char)cx_read(P1_MDATA3) << 24;
329         cx_writeb(P1_MDATA2, 0);
330         val    |= (unsigned char)cx_read(P1_MDATA2) << 16;
331         cx_writeb(P1_MDATA1, 0);
332         val    |= (unsigned char)cx_read(P1_MDATA1) << 8;
333         cx_writeb(P1_MDATA0, 0);
334         val    |= (unsigned char)cx_read(P1_MDATA0);
335
336         *value  = val;
337         return retval;
338 }
339
340 static int register_write(struct cx88_core *core, u32 address, u32 value)
341 {
342         cx_writeb(P1_RDATA0, (unsigned int)value);
343         cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
344         cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
345         cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
346         cx_writeb(P1_RADDR0, (unsigned int)address);
347         cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
348         cx_writeb(P1_RRDWR, 1);
349         cx_read(P1_RDATA0);
350         cx_read(P1_RADDR0);
351
352         return wait_ready_gpio0_bit1(core,1);
353 }
354
355
356 static int register_read(struct cx88_core *core, u32 address, u32 *value)
357 {
358         int retval;
359         u32 val;
360
361         cx_writeb(P1_RADDR0, (unsigned int)address);
362         cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
363         cx_writeb(P1_RRDWR, 0);
364         cx_read(P1_RADDR0);
365
366         retval  = wait_ready_gpio0_bit1(core,1);
367         val     = (unsigned char)cx_read(P1_RDATA0);
368         val    |= (unsigned char)cx_read(P1_RDATA1) << 8;
369         val    |= (unsigned char)cx_read(P1_RDATA2) << 16;
370         val    |= (unsigned char)cx_read(P1_RDATA3) << 24;
371
372         *value  = val;
373         return retval;
374 }
375
376 /* ------------------------------------------------------------------ */
377
378 static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
379 {
380         struct cx8802_dev *dev = priv;
381         unsigned long timeout;
382         u32 value, flag, retval;
383         int i;
384
385         dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
386
387         /* this may not be 100% safe if we can't read any memory location
388            without side effects */
389         memory_read(dev->core, dev->mailbox - 4, &value);
390         if (value != 0x12345678) {
391                 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
392                 return -1;
393         }
394
395         memory_read(dev->core, dev->mailbox, &flag);
396         if (flag) {
397                 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
398                 return -1;
399         }
400
401         flag |= 1; /* tell 'em we're working on it */
402         memory_write(dev->core, dev->mailbox, flag);
403
404         /* write command + args + fill remaining with zeros */
405         memory_write(dev->core, dev->mailbox + 1, command); /* command code */
406         memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
407         for (i = 0; i < in; i++) {
408                 memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
409                 dprintk(1, "API Input %d = %d\n", i, data[i]);
410         }
411         for (; i < CX2341X_MBOX_MAX_DATA; i++)
412                 memory_write(dev->core, dev->mailbox + 4 + i, 0);
413
414         flag |= 3; /* tell 'em we're done writing */
415         memory_write(dev->core, dev->mailbox, flag);
416
417         /* wait for firmware to handle the API command */
418         timeout = jiffies + msecs_to_jiffies(10);
419         for (;;) {
420                 memory_read(dev->core, dev->mailbox, &flag);
421                 if (0 != (flag & 4))
422                         break;
423                 if (time_after(jiffies,timeout)) {
424                         dprintk(0, "ERROR: API Mailbox timeout\n");
425                         return -1;
426                 }
427                 udelay(10);
428         }
429
430         /* read output values */
431         for (i = 0; i < out; i++) {
432                 memory_read(dev->core, dev->mailbox + 4 + i, data + i);
433                 dprintk(1, "API Output %d = %d\n", i, data[i]);
434         }
435
436         memory_read(dev->core, dev->mailbox + 2, &retval);
437         dprintk(1, "API result = %d\n",retval);
438
439         flag = 0;
440         memory_write(dev->core, dev->mailbox, flag);
441         return retval;
442 }
443 /* ------------------------------------------------------------------ */
444
445 /* We don't need to call the API often, so using just one mailbox will probably suffice */
446 static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
447                              u32 inputcnt, u32 outputcnt, ...)
448 {
449         u32 data[CX2341X_MBOX_MAX_DATA];
450         va_list vargs;
451         int i, err;
452
453         va_start(vargs, outputcnt);
454
455         for (i = 0; i < inputcnt; i++) {
456                 data[i] = va_arg(vargs, int);
457         }
458         err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
459         for (i = 0; i < outputcnt; i++) {
460                 int *vptr = va_arg(vargs, int *);
461                 *vptr = data[i];
462         }
463         va_end(vargs);
464         return err;
465 }
466
467 static int blackbird_find_mailbox(struct cx8802_dev *dev)
468 {
469         u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
470         int signaturecnt=0;
471         u32 value;
472         int i;
473
474         for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
475                 memory_read(dev->core, i, &value);
476                 if (value == signature[signaturecnt])
477                         signaturecnt++;
478                 else
479                         signaturecnt = 0;
480                 if (4 == signaturecnt) {
481                         dprintk(1, "Mailbox signature found\n");
482                         return i+1;
483                 }
484         }
485         dprintk(0, "Mailbox signature values not found!\n");
486         return -1;
487 }
488
489 static int blackbird_load_firmware(struct cx8802_dev *dev)
490 {
491         static const unsigned char magic[8] = {
492                 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
493         };
494         const struct firmware *firmware;
495         int i, retval = 0;
496         u32 value = 0;
497         u32 checksum = 0;
498         u32 *dataptr;
499
500         retval  = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
501         retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
502         retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
503         retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
504         msleep(1);
505         retval |= register_write(dev->core, IVTV_REG_APU, 0);
506
507         if (retval < 0)
508                 dprintk(0, "Error with register_write\n");
509
510         retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
511                                   &dev->pci->dev);
512
513
514         if (retval != 0) {
515                 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
516                         CX2341X_FIRM_ENC_FILENAME);
517                 dprintk(0, "Please fix your hotplug setup, the board will "
518                         "not work without firmware loaded!\n");
519                 return -1;
520         }
521
522         if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
523                 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
524                         firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
525                 release_firmware(firmware);
526                 return -1;
527         }
528
529         if (0 != memcmp(firmware->data, magic, 8)) {
530                 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
531                 release_firmware(firmware);
532                 return -1;
533         }
534
535         /* transfer to the chip */
536         dprintk(1,"Loading firmware ...\n");
537         dataptr = (u32*)firmware->data;
538         for (i = 0; i < (firmware->size >> 2); i++) {
539                 value = *dataptr;
540                 checksum += ~value;
541                 memory_write(dev->core, i, value);
542                 dataptr++;
543         }
544
545         /* read back to verify with the checksum */
546         for (i--; i >= 0; i--) {
547                 memory_read(dev->core, i, &value);
548                 checksum -= ~value;
549         }
550         if (checksum) {
551                 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
552                 release_firmware(firmware);
553                 return -1;
554         }
555         release_firmware(firmware);
556         dprintk(0, "Firmware upload successful.\n");
557
558         retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
559         retval |= register_read(dev->core, IVTV_REG_SPU, &value);
560         retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
561         msleep(1);
562
563         retval |= register_read(dev->core, IVTV_REG_VPU, &value);
564         retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
565
566         if (retval < 0)
567                 dprintk(0, "Error with register_write\n");
568         return 0;
569 }
570
571 /**
572  Settings used by the windows tv app for PVR2000:
573 =================================================================================================================
574 Profile | Codec | Resolution | CBR/VBR | Video Qlty   | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
575 -----------------------------------------------------------------------------------------------------------------
576 MPEG-1  | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 2000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
577 MPEG-2  | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 4000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
578 VCD     | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 1150 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
579 DVD     | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
580 DB* DVD | MPEG2 | 720x576PAL | CBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
581 =================================================================================================================
582 *DB: "DirectBurn"
583 */
584
585 static void blackbird_codec_settings(struct cx8802_dev *dev)
586 {
587         /* assign frame size */
588         blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
589                                 dev->height, dev->width);
590
591         dev->params.width = dev->width;
592         dev->params.height = dev->height;
593         dev->params.is_50hz = (dev->core->tvnorm & V4L2_STD_625_50) != 0;
594
595         cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params);
596 }
597
598 static struct v4l2_mpeg_compression default_mpeg_params = {
599         .st_type          = V4L2_MPEG_PS_2,
600         .st_bitrate       = {
601                 .mode     = V4L2_BITRATE_CBR,
602                 .min      = 0,
603                 .target   = 0,
604                 .max      = 0
605         },
606         .ts_pid_pmt       = 16,
607         .ts_pid_audio     = 260,
608         .ts_pid_video     = 256,
609         .ts_pid_pcr       = 259,
610         .ps_size          = 0,
611         .au_type          = V4L2_MPEG_AU_2_II,
612         .au_bitrate       = {
613                 .mode     = V4L2_BITRATE_CBR,
614                 .min      = 224,
615                 .target   = 224,
616                 .max      = 224
617         },
618         .au_sample_rate    = 48000,
619         .au_pesid          = 0,
620         .vi_type           = V4L2_MPEG_VI_2,
621         .vi_aspect_ratio   = V4L2_MPEG_ASPECT_4_3,
622         .vi_bitrate        = {
623                 .mode      = V4L2_BITRATE_CBR,
624                 .min       = 4000,
625                 .target    = 4500,
626                 .max       = 6000
627         },
628         .vi_frame_rate     = 25,
629         .vi_frames_per_gop = 12,
630         .vi_bframes_count  = 2,
631         .vi_pesid          = 0,
632         .closed_gops       = 1,
633         .pulldown          = 0
634 };
635
636 static int blackbird_initialize_codec(struct cx8802_dev *dev)
637 {
638         struct cx88_core *core = dev->core;
639         int version;
640         int retval;
641
642         dprintk(1,"Initialize codec\n");
643         retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
644         if (retval < 0) {
645                 /* ping was not successful, reset and upload firmware */
646                 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
647                 msleep(1);
648                 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
649                 msleep(1);
650                 retval = blackbird_load_firmware(dev);
651                 if (retval < 0)
652                         return retval;
653
654                 dev->mailbox = blackbird_find_mailbox(dev);
655                 if (dev->mailbox < 0)
656                         return -1;
657
658                 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
659                 if (retval < 0) {
660                         dprintk(0, "ERROR: Firmware ping failed!\n");
661                         return -1;
662                 }
663
664                 retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
665                 if (retval < 0) {
666                         dprintk(0, "ERROR: Firmware get encoder version failed!\n");
667                         return -1;
668                 }
669                 dprintk(0, "Firmware version is 0x%08x\n", version);
670         }
671         msleep(1);
672
673         cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
674         cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
675         cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
676         cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
677
678         blackbird_codec_settings(dev);
679         msleep(1);
680
681         /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
682            blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
683            blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
684         blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
685                         BLACKBIRD_FIELD1_SAA7115,
686                         BLACKBIRD_FIELD2_SAA7115
687                 );
688
689         /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
690         blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
691                         BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
692                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
693
694         /* initialize the video input */
695         blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
696
697         msleep(1);
698
699         blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
700         msleep(1);
701         blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
702         msleep(1);
703
704         /* start capturing to the host interface */
705         /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
706         blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
707                         BLACKBIRD_MPEG_CAPTURE,
708                         BLACKBIRD_RAW_BITS_NONE
709                 );
710         msleep(10);
711
712         blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
713         return 0;
714 }
715
716 /* ------------------------------------------------------------------ */
717
718 static int bb_buf_setup(struct videobuf_queue *q,
719                         unsigned int *count, unsigned int *size)
720 {
721         struct cx8802_fh *fh = q->priv_data;
722
723         fh->dev->ts_packet_size  = 188 * 4; /* was: 512 */
724         fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
725
726         *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
727         *count = fh->dev->ts_packet_count;
728         return 0;
729 }
730
731 static int
732 bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
733                enum v4l2_field field)
734 {
735         struct cx8802_fh *fh = q->priv_data;
736         return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
737 }
738
739 static void
740 bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
741 {
742         struct cx8802_fh *fh = q->priv_data;
743         cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
744 }
745
746 static void
747 bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
748 {
749         cx88_free_buffer(q, (struct cx88_buffer*)vb);
750 }
751
752 static struct videobuf_queue_ops blackbird_qops = {
753         .buf_setup    = bb_buf_setup,
754         .buf_prepare  = bb_buf_prepare,
755         .buf_queue    = bb_buf_queue,
756         .buf_release  = bb_buf_release,
757 };
758
759 /* ------------------------------------------------------------------ */
760
761 static const u32 *ctrl_classes[] = {
762         cx88_user_ctrls,
763         cx2341x_mpeg_ctrls,
764         NULL
765 };
766
767 static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qctrl)
768 {
769         qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
770         if (qctrl->id == 0)
771                 return -EINVAL;
772
773         /* Standard V4L2 controls */
774         if (cx8800_ctrl_query(qctrl) == 0)
775                 return 0;
776
777         /* MPEG V4L2 controls */
778         if (cx2341x_ctrl_query(&dev->params, qctrl))
779                 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
780         return 0;
781 }
782
783 static int blackbird_querymenu(struct cx8802_dev *dev, struct v4l2_querymenu *qmenu)
784 {
785         struct v4l2_queryctrl qctrl;
786
787         qctrl.id = qmenu->id;
788         blackbird_queryctrl(dev, &qctrl);
789         return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
790 }
791
792 /* ------------------------------------------------------------------ */
793
794 static int mpeg_do_ioctl(struct inode *inode, struct file *file,
795                          unsigned int cmd, void *arg)
796 {
797         struct cx8802_fh  *fh  = file->private_data;
798         struct cx8802_dev *dev = fh->dev;
799         struct cx88_core  *core = dev->core;
800
801         if (debug > 1)
802                 v4l_print_ioctl(core->name,cmd);
803
804         switch (cmd) {
805
806         /* --- capabilities ------------------------------------------ */
807         case VIDIOC_QUERYCAP:
808         {
809                 struct v4l2_capability *cap = arg;
810
811                 memset(cap,0,sizeof(*cap));
812                 strcpy(cap->driver, "cx88_blackbird");
813                 strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card));
814                 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
815                 cap->version = CX88_VERSION_CODE;
816                 cap->capabilities =
817                         V4L2_CAP_VIDEO_CAPTURE |
818                         V4L2_CAP_READWRITE     |
819                         V4L2_CAP_STREAMING     |
820                         0;
821                 if (UNSET != core->tuner_type)
822                         cap->capabilities |= V4L2_CAP_TUNER;
823
824                 return 0;
825         }
826
827         /* --- capture ioctls ---------------------------------------- */
828         case VIDIOC_ENUM_FMT:
829         {
830                 struct v4l2_fmtdesc *f = arg;
831                 int index;
832
833                 index = f->index;
834                 if (index != 0)
835                         return -EINVAL;
836
837                 memset(f,0,sizeof(*f));
838                 f->index = index;
839                 strlcpy(f->description, "MPEG", sizeof(f->description));
840                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
841                 f->pixelformat = V4L2_PIX_FMT_MPEG;
842                 return 0;
843         }
844         case VIDIOC_G_FMT:
845         {
846                 struct v4l2_format *f = arg;
847
848                 memset(f,0,sizeof(*f));
849                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
850                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
851                 f->fmt.pix.bytesperline = 0;
852                 f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
853                 f->fmt.pix.colorspace   = 0;
854                 f->fmt.pix.width        = dev->width;
855                 f->fmt.pix.height       = dev->height;
856                 f->fmt.pix.field        = fh->mpegq.field;
857                 dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
858                         dev->width, dev->height, fh->mpegq.field );
859                 return 0;
860         }
861         case VIDIOC_TRY_FMT:
862         {
863                 struct v4l2_format *f = arg;
864
865                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
866                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
867                 f->fmt.pix.bytesperline = 0;
868                 f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
869                 f->fmt.pix.colorspace   = 0;
870                 dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
871                         dev->width, dev->height, fh->mpegq.field );
872                 return 0;
873         }
874         case VIDIOC_S_FMT:
875         {
876                 struct v4l2_format *f = arg;
877
878                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
879                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
880                 f->fmt.pix.bytesperline = 0;
881                 f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
882                 f->fmt.pix.colorspace   = 0;
883                 dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
884                         f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
885                 return 0;
886         }
887
888         /* --- streaming capture ------------------------------------- */
889         case VIDIOC_REQBUFS:
890                 return videobuf_reqbufs(&fh->mpegq, arg);
891
892         case VIDIOC_QUERYBUF:
893                 return videobuf_querybuf(&fh->mpegq, arg);
894
895         case VIDIOC_QBUF:
896                 return videobuf_qbuf(&fh->mpegq, arg);
897
898         case VIDIOC_DQBUF:
899                 return videobuf_dqbuf(&fh->mpegq, arg,
900                                       file->f_flags & O_NONBLOCK);
901
902         case VIDIOC_STREAMON:
903                 return videobuf_streamon(&fh->mpegq);
904
905         case VIDIOC_STREAMOFF:
906                 return videobuf_streamoff(&fh->mpegq);
907
908         /* --- mpeg compression -------------------------------------- */
909         case VIDIOC_G_MPEGCOMP:
910         {
911                 struct v4l2_mpeg_compression *f = arg;
912
913                 printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. "
914                                     "Replace with VIDIOC_G_EXT_CTRLS!");
915                 memcpy(f,&default_mpeg_params,sizeof(*f));
916                 return 0;
917         }
918         case VIDIOC_S_MPEGCOMP:
919                 printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. "
920                                     "Replace with VIDIOC_S_EXT_CTRLS!");
921                 return 0;
922         case VIDIOC_G_EXT_CTRLS:
923         {
924                 struct v4l2_ext_controls *f = arg;
925
926                 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
927                         return -EINVAL;
928                 return cx2341x_ext_ctrls(&dev->params, f, cmd);
929         }
930         case VIDIOC_S_EXT_CTRLS:
931         case VIDIOC_TRY_EXT_CTRLS:
932         {
933                 struct v4l2_ext_controls *f = arg;
934                 struct cx2341x_mpeg_params p;
935                 int err;
936
937                 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
938                         return -EINVAL;
939                 p = dev->params;
940                 err = cx2341x_ext_ctrls(&p, f, cmd);
941                 if (err == 0 && cmd == VIDIOC_S_EXT_CTRLS) {
942                         err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
943                         dev->params = p;
944                 }
945                 return err;
946         }
947         case VIDIOC_S_FREQUENCY:
948         {
949                 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
950                                   BLACKBIRD_END_NOW,
951                                   BLACKBIRD_MPEG_CAPTURE,
952                                   BLACKBIRD_RAW_BITS_NONE);
953
954                 cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
955
956                 blackbird_initialize_codec(dev);
957                 cx88_set_scale(dev->core, dev->width, dev->height,
958                                fh->mpegq.field);
959                 return 0;
960         }
961         case VIDIOC_LOG_STATUS:
962         {
963                 char name[32 + 2];
964
965                 snprintf(name, sizeof(name), "%s/2", core->name);
966                 printk("%s/2: ============  START LOG STATUS  ============\n",
967                        core->name);
968                 cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, NULL);
969                 cx2341x_log_status(&dev->params, name);
970                 printk("%s/2: =============  END LOG STATUS  =============\n",
971                        core->name);
972                 return 0;
973         }
974         case VIDIOC_QUERYMENU:
975                 return blackbird_querymenu(dev, arg);
976         case VIDIOC_QUERYCTRL:
977         {
978                 struct v4l2_queryctrl *c = arg;
979
980                 if (blackbird_queryctrl(dev, c) == 0)
981                         return 0;
982                 return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, mpeg_do_ioctl);
983         }
984
985         default:
986                 return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
987         }
988         return 0;
989 }
990
991 int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
992                   struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
993 {
994         int err;
995
996        if (debug) {
997                if (debug > 1) {
998                        if (_IOC_DIR(cmd) & _IOC_WRITE)
999                                v4l_printk_ioctl_arg("cx88(w)",cmd, arg);
1000                        else if (!_IOC_DIR(cmd) & _IOC_READ) {
1001                                v4l_print_ioctl("cx88", cmd);
1002                        }
1003                } else
1004                        v4l_print_ioctl(core->name,cmd);
1005
1006        }
1007
1008         switch (cmd) {
1009         /* ---------- tv norms ---------- */
1010         case VIDIOC_ENUMSTD:
1011         {
1012                 struct v4l2_standard *e = arg;
1013                 unsigned int i;
1014
1015                 i = e->index;
1016                 if (i >= ARRAY_SIZE(tvnorms))
1017                         return -EINVAL;
1018                 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1019                                                tvnorms[e->index].name);
1020                 e->index = i;
1021                 if (err < 0)
1022                         return err;
1023                 return 0;
1024         }
1025         case VIDIOC_G_STD:
1026         {
1027                 v4l2_std_id *id = arg;
1028
1029                 *id = core->tvnorm;
1030                 return 0;
1031         }
1032         case VIDIOC_S_STD:
1033         {
1034                 v4l2_std_id *id = arg;
1035                 unsigned int i;
1036
1037                 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1038                         if (*id & tvnorms[i].id)
1039                                 break;
1040                 if (i == ARRAY_SIZE(tvnorms))
1041                         return -EINVAL;
1042
1043                 mutex_lock(&core->lock);
1044                 cx88_set_tvnorm(core,tvnorms[i].id);
1045                 mutex_unlock(&core->lock);
1046                 return 0;
1047         }
1048
1049         /* ------ input switching ---------- */
1050         case VIDIOC_ENUMINPUT:
1051         {
1052                 static const char *iname[] = {
1053                         [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1054                         [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1055                         [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1056                         [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1057                         [ CX88_VMUX_SVIDEO     ] = "S-Video",
1058                         [ CX88_VMUX_TELEVISION ] = "Television",
1059                         [ CX88_VMUX_CABLE      ] = "Cable TV",
1060                         [ CX88_VMUX_DVB        ] = "DVB",
1061                         [ CX88_VMUX_DEBUG      ] = "for debug only",
1062                 };
1063                 struct v4l2_input *i = arg;
1064                 unsigned int n;
1065
1066                 n = i->index;
1067                 if (n >= 4)
1068                         return -EINVAL;
1069                 if (0 == INPUT(n)->type)
1070                         return -EINVAL;
1071                 memset(i,0,sizeof(*i));
1072                 i->index = n;
1073                 i->type  = V4L2_INPUT_TYPE_CAMERA;
1074                 strcpy(i->name,iname[INPUT(n)->type]);
1075                 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1076                     (CX88_VMUX_CABLE      == INPUT(n)->type))
1077                         i->type = V4L2_INPUT_TYPE_TUNER;
1078                 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1079                         i->std |= tvnorms[n].id;
1080                 return 0;
1081         }
1082         case VIDIOC_G_INPUT:
1083         {
1084                 unsigned int *i = arg;
1085
1086                 *i = core->input;
1087                 return 0;
1088         }
1089         case VIDIOC_S_INPUT:
1090         {
1091                 unsigned int *i = arg;
1092
1093                 if (*i >= 4)
1094                         return -EINVAL;
1095                 mutex_lock(&core->lock);
1096                 cx88_newstation(core);
1097                 cx88_video_mux(core,*i);
1098                 mutex_unlock(&core->lock);
1099                 return 0;
1100         }
1101
1102
1103
1104         /* --- controls ---------------------------------------------- */
1105         case VIDIOC_QUERYCTRL:
1106         {
1107                 struct v4l2_queryctrl *qctrl = arg;
1108
1109                 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1110                         if (unlikely(qctrl->id == 0))
1111                                 return -EINVAL;
1112                 return cx8800_ctrl_query(qctrl);
1113         }
1114         case VIDIOC_G_CTRL:
1115                 return cx88_get_control(core,arg);
1116         case VIDIOC_S_CTRL:
1117                 return cx88_set_control(core,arg);
1118
1119         /* --- tuner ioctls ------------------------------------------ */
1120         case VIDIOC_G_TUNER:
1121         {
1122                 struct v4l2_tuner *t = arg;
1123                 u32 reg;
1124
1125                 if (UNSET == core->tuner_type)
1126                         return -EINVAL;
1127                 if (0 != t->index)
1128                         return -EINVAL;
1129
1130                 memset(t,0,sizeof(*t));
1131                 strcpy(t->name, "Television");
1132                 t->type       = V4L2_TUNER_ANALOG_TV;
1133                 t->capability = V4L2_TUNER_CAP_NORM;
1134                 t->rangehigh  = 0xffffffffUL;
1135
1136                 cx88_get_stereo(core ,t);
1137                 reg = cx_read(MO_DEVICE_STATUS);
1138                 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1139                 return 0;
1140         }
1141         case VIDIOC_S_TUNER:
1142         {
1143                 struct v4l2_tuner *t = arg;
1144
1145                 if (UNSET == core->tuner_type)
1146                         return -EINVAL;
1147                 if (0 != t->index)
1148                         return -EINVAL;
1149                 cx88_set_stereo(core, t->audmode, 1);
1150                 return 0;
1151         }
1152         case VIDIOC_G_FREQUENCY:
1153         {
1154                 struct v4l2_frequency *f = arg;
1155
1156                 memset(f,0,sizeof(*f));
1157
1158                 if (UNSET == core->tuner_type)
1159                         return -EINVAL;
1160
1161                 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1162                 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1163                 f->frequency = core->freq;
1164
1165                 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1166
1167                 return 0;
1168         }
1169         case VIDIOC_S_FREQUENCY:
1170         {
1171                 struct v4l2_frequency *f = arg;
1172
1173                 if (UNSET == core->tuner_type)
1174                         return -EINVAL;
1175                 if (f->tuner != 0)
1176                         return -EINVAL;
1177                 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
1178                         return -EINVAL;
1179                 if (1 == radio && f->type != V4L2_TUNER_RADIO)
1180                         return -EINVAL;
1181                 mutex_lock(&core->lock);
1182                 core->freq = f->frequency;
1183                 cx88_newstation(core);
1184                 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1185
1186                 /* When changing channels it is required to reset TVAUDIO */
1187                 msleep (10);
1188                 cx88_set_tvaudio(core);
1189
1190                 mutex_unlock(&core->lock);
1191                 return 0;
1192         }
1193 #ifdef CONFIG_VIDEO_ADV_DEBUG
1194         /* ioctls to allow direct acces to the cx2388x registers */
1195         case VIDIOC_INT_G_REGISTER:
1196         {
1197                 struct v4l2_register *reg = arg;
1198
1199                 if (reg->i2c_id != 0)
1200                         return -EINVAL;
1201                 /* cx2388x has a 24-bit register space */
1202                 reg->val = cx_read(reg->reg&0xffffff);
1203                 return 0;
1204         }
1205         case VIDIOC_INT_S_REGISTER:
1206         {
1207                 struct v4l2_register *reg = arg;
1208
1209                 if (reg->i2c_id != 0)
1210                         return -EINVAL;
1211                 if (!capable(CAP_SYS_ADMIN))
1212                         return -EPERM;
1213                 cx_write(reg->reg&0xffffff, reg->val);
1214                 return 0;
1215         }
1216 #endif
1217
1218         default:
1219                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1220                                                   driver_ioctl);
1221         }
1222         return 0;
1223 }
1224
1225 int (*cx88_ioctl_hook)(struct inode *inode, struct file *file,
1226                         unsigned int cmd, void *arg);
1227 unsigned int (*cx88_ioctl_translator)(unsigned int cmd);
1228
1229 static unsigned int mpeg_translate_ioctl(unsigned int cmd)
1230 {
1231         return cmd;
1232 }
1233
1234 static int mpeg_ioctl(struct inode *inode, struct file *file,
1235                         unsigned int cmd, unsigned long arg)
1236 {
1237         cmd = cx88_ioctl_translator( cmd );
1238         return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
1239 }
1240
1241 static int mpeg_open(struct inode *inode, struct file *file)
1242 {
1243         int minor = iminor(inode);
1244         struct cx8802_dev *dev = NULL;
1245         struct cx8802_fh *fh;
1246         struct cx8802_driver *drv = NULL;
1247         int err;
1248
1249        dev = cx8802_get_device(inode);
1250
1251         dprintk( 1, "%s\n", __FUNCTION__);
1252
1253         if (dev == NULL)
1254                 return -ENODEV;
1255
1256         /* Make sure we can acquire the hardware */
1257         drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
1258         if (drv) {
1259                 err = drv->request_acquire(drv);
1260                 if(err != 0) {
1261                         dprintk(1,"%s: Unable to acquire hardware, %d\n", __FUNCTION__, err);
1262                         return err;
1263                 }
1264         }
1265
1266         if (blackbird_initialize_codec(dev) < 0) {
1267                 if (drv)
1268                         drv->request_release(drv);
1269                 return -EINVAL;
1270         }
1271         dprintk(1,"open minor=%d\n",minor);
1272
1273         /* allocate + initialize per filehandle data */
1274         fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1275         if (NULL == fh) {
1276                 if (drv)
1277                         drv->request_release(drv);
1278                 return -ENOMEM;
1279         }
1280         file->private_data = fh;
1281         fh->dev      = dev;
1282
1283         videobuf_queue_init(&fh->mpegq, &blackbird_qops,
1284                             dev->pci, &dev->slock,
1285                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1286                             V4L2_FIELD_INTERLACED,
1287                             sizeof(struct cx88_buffer),
1288                             fh);
1289
1290         /* FIXME: locking against other video device */
1291         cx88_set_scale(dev->core, dev->width, dev->height,
1292                         fh->mpegq.field);
1293
1294         return 0;
1295 }
1296
1297 static int mpeg_release(struct inode *inode, struct file *file)
1298 {
1299         struct cx8802_fh  *fh  = file->private_data;
1300         struct cx8802_dev *dev = NULL;
1301         struct cx8802_driver *drv = NULL;
1302
1303         /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
1304         blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
1305                         BLACKBIRD_END_NOW,
1306                         BLACKBIRD_MPEG_CAPTURE,
1307                         BLACKBIRD_RAW_BITS_NONE
1308                 );
1309
1310         cx8802_cancel_buffers(fh->dev);
1311         /* stop mpeg capture */
1312         if (fh->mpegq.streaming)
1313                 videobuf_streamoff(&fh->mpegq);
1314         if (fh->mpegq.reading)
1315                 videobuf_read_stop(&fh->mpegq);
1316
1317         videobuf_mmap_free(&fh->mpegq);
1318         file->private_data = NULL;
1319         kfree(fh);
1320
1321         /* Make sure we release the hardware */
1322         dev = cx8802_get_device(inode);
1323         if (dev == NULL)
1324                 return -ENODEV;
1325
1326         drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
1327         if (drv)
1328                 drv->request_release(drv);
1329
1330         return 0;
1331 }
1332
1333 static ssize_t
1334 mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1335 {
1336         struct cx8802_fh *fh = file->private_data;
1337
1338         return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1339                                     file->f_flags & O_NONBLOCK);
1340 }
1341
1342 static unsigned int
1343 mpeg_poll(struct file *file, struct poll_table_struct *wait)
1344 {
1345         struct cx8802_fh *fh = file->private_data;
1346
1347         return videobuf_poll_stream(file, &fh->mpegq, wait);
1348 }
1349
1350 static int
1351 mpeg_mmap(struct file *file, struct vm_area_struct * vma)
1352 {
1353         struct cx8802_fh *fh = file->private_data;
1354
1355         return videobuf_mmap_mapper(&fh->mpegq, vma);
1356 }
1357
1358 static const struct file_operations mpeg_fops =
1359 {
1360         .owner         = THIS_MODULE,
1361         .open          = mpeg_open,
1362         .release       = mpeg_release,
1363         .read          = mpeg_read,
1364         .poll          = mpeg_poll,
1365         .mmap          = mpeg_mmap,
1366         .ioctl         = mpeg_ioctl,
1367         .llseek        = no_llseek,
1368 };
1369
1370 static struct video_device cx8802_mpeg_template =
1371 {
1372         .name          = "cx8802",
1373         .type          = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
1374         .hardware      = 0,
1375         .fops          = &mpeg_fops,
1376         .minor         = -1,
1377 };
1378
1379 /* ------------------------------------------------------------------ */
1380
1381 /* The CX8802 MPEG API will call this when we can use the hardware */
1382 static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
1383 {
1384         struct cx88_core *core = drv->core;
1385         int err = 0;
1386
1387         switch (core->board) {
1388         case CX88_BOARD_HAUPPAUGE_HVR1300:
1389                 /* By default, core setup will leave the cx22702 out of reset, on the bus.
1390                  * We left the hardware on power up with the cx22702 active.
1391                  * We're being given access to re-arrange the GPIOs.
1392                  * Take the bus off the cx22702 and put the cx23416 on it.
1393                  */
1394                 cx_clear(MO_GP0_IO, 0x00000080); /* cx22702 in reset */
1395                 cx_set(MO_GP0_IO,   0x00000004); /* Disable the cx22702 */
1396                 break;
1397         default:
1398                 err = -ENODEV;
1399         }
1400         return err;
1401 }
1402
1403 /* The CX8802 MPEG API will call this when we need to release the hardware */
1404 static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
1405 {
1406         struct cx88_core *core = drv->core;
1407         int err = 0;
1408
1409         switch (core->board) {
1410         case CX88_BOARD_HAUPPAUGE_HVR1300:
1411                 /* Exit leaving the cx23416 on the bus */
1412                 break;
1413         default:
1414                 err = -ENODEV;
1415         }
1416         return err;
1417 }
1418
1419 static void blackbird_unregister_video(struct cx8802_dev *dev)
1420 {
1421         if (dev->mpeg_dev) {
1422                 if (-1 != dev->mpeg_dev->minor)
1423                         video_unregister_device(dev->mpeg_dev);
1424                 else
1425                         video_device_release(dev->mpeg_dev);
1426                 dev->mpeg_dev = NULL;
1427         }
1428 }
1429
1430 static int blackbird_register_video(struct cx8802_dev *dev)
1431 {
1432         int err;
1433
1434         dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
1435                                        &cx8802_mpeg_template,"mpeg");
1436         err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
1437         if (err < 0) {
1438                 printk(KERN_INFO "%s/2: can't register mpeg device\n",
1439                        dev->core->name);
1440                 return err;
1441         }
1442         printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
1443                dev->core->name,dev->mpeg_dev->minor & 0x1f);
1444         return 0;
1445 }
1446
1447 /* ----------------------------------------------------------- */
1448
1449 static int cx8802_blackbird_probe(struct cx8802_driver *drv)
1450 {
1451         struct cx88_core *core = drv->core;
1452         struct cx8802_dev *dev = core->dvbdev;
1453         int err;
1454
1455         dprintk( 1, "%s\n", __FUNCTION__);
1456         dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1457                 core->board,
1458                 core->name,
1459                 core->pci_bus,
1460                 core->pci_slot);
1461
1462         err = -ENODEV;
1463         if (!(cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD))
1464                 goto fail_core;
1465
1466         dev->width = 720;
1467         dev->height = 576;
1468         cx2341x_fill_defaults(&dev->params);
1469         dev->params.port = CX2341X_PORT_STREAMING;
1470
1471         if (core->tvnorm & V4L2_STD_525_60) {
1472                 dev->height = 480;
1473         } else {
1474                 dev->height = 576;
1475         }
1476
1477         /* blackbird stuff */
1478         printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1479                core->name);
1480         host_setup(dev->core);
1481
1482         blackbird_register_video(dev);
1483
1484         /* initial device configuration: needed ? */
1485
1486         return 0;
1487
1488  fail_core:
1489         return err;
1490 }
1491
1492 static int cx8802_blackbird_remove(struct cx8802_driver *drv)
1493 {
1494         /* blackbird */
1495         blackbird_unregister_video(drv->core->dvbdev);
1496
1497         return 0;
1498 }
1499
1500 static struct cx8802_driver cx8802_blackbird_driver = {
1501         .type_id        = CX88_MPEG_BLACKBIRD,
1502         .hw_access      = CX8802_DRVCTL_SHARED,
1503         .probe          = cx8802_blackbird_probe,
1504         .remove         = cx8802_blackbird_remove,
1505         .advise_acquire = cx8802_blackbird_advise_acquire,
1506         .advise_release = cx8802_blackbird_advise_release,
1507 };
1508
1509 static int blackbird_init(void)
1510 {
1511         printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
1512                (CX88_VERSION_CODE >> 16) & 0xff,
1513                (CX88_VERSION_CODE >>  8) & 0xff,
1514                CX88_VERSION_CODE & 0xff);
1515 #ifdef SNAPSHOT
1516         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
1517                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1518 #endif
1519         cx88_ioctl_hook = mpeg_do_ioctl;
1520         cx88_ioctl_translator = mpeg_translate_ioctl;
1521         return cx8802_register_driver(&cx8802_blackbird_driver);
1522 }
1523
1524 static void blackbird_fini(void)
1525 {
1526         cx8802_unregister_driver(&cx8802_blackbird_driver);
1527 }
1528
1529 module_init(blackbird_init);
1530 module_exit(blackbird_fini);
1531
1532 EXPORT_SYMBOL(cx88_ioctl_hook);
1533 EXPORT_SYMBOL(cx88_ioctl_translator);
1534
1535 /* ----------------------------------------------------------- */
1536 /*
1537  * Local variables:
1538  * c-basic-offset: 8
1539  * End:
1540  * 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
1541  */