]> err.no Git - linux-2.6/blob - drivers/media/video/cx88/cx88-blackbird.c
0ba6ef39b04c4689bdd4292265afe4bf5b17eab9
[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                 if (UNSET != core->tuner_type)
821                         cap->capabilities |= V4L2_CAP_TUNER;
822
823                 return 0;
824         }
825
826         /* --- capture ioctls ---------------------------------------- */
827         case VIDIOC_ENUM_FMT:
828         {
829                 struct v4l2_fmtdesc *f = arg;
830                 int index;
831
832                 index = f->index;
833                 if (index != 0)
834                         return -EINVAL;
835
836                 memset(f,0,sizeof(*f));
837                 f->index = index;
838                 strlcpy(f->description, "MPEG", sizeof(f->description));
839                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
840                 f->pixelformat = V4L2_PIX_FMT_MPEG;
841                 return 0;
842         }
843         case VIDIOC_G_FMT:
844         {
845                 struct v4l2_format *f = arg;
846
847                 memset(f,0,sizeof(*f));
848                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
849                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
850                 f->fmt.pix.bytesperline = 0;
851                 f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
852                 f->fmt.pix.colorspace   = 0;
853                 f->fmt.pix.width        = dev->width;
854                 f->fmt.pix.height       = dev->height;
855                 f->fmt.pix.field        = fh->mpegq.field;
856                 dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
857                         dev->width, dev->height, fh->mpegq.field );
858                 return 0;
859         }
860         case VIDIOC_TRY_FMT:
861         {
862                 struct v4l2_format *f = arg;
863
864                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
865                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
866                 f->fmt.pix.bytesperline = 0;
867                 f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
868                 f->fmt.pix.colorspace   = 0;
869                 dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
870                         dev->width, dev->height, fh->mpegq.field );
871                 return 0;
872         }
873         case VIDIOC_S_FMT:
874         {
875                 struct v4l2_format *f = arg;
876
877                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
878                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
879                 f->fmt.pix.bytesperline = 0;
880                 f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
881                 f->fmt.pix.colorspace   = 0;
882                 dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
883                         f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
884                 return 0;
885         }
886
887         /* --- streaming capture ------------------------------------- */
888         case VIDIOC_REQBUFS:
889                 return videobuf_reqbufs(&fh->mpegq, arg);
890
891         case VIDIOC_QUERYBUF:
892                 return videobuf_querybuf(&fh->mpegq, arg);
893
894         case VIDIOC_QBUF:
895                 return videobuf_qbuf(&fh->mpegq, arg);
896
897         case VIDIOC_DQBUF:
898                 return videobuf_dqbuf(&fh->mpegq, arg,
899                                       file->f_flags & O_NONBLOCK);
900
901         case VIDIOC_STREAMON:
902                 return videobuf_streamon(&fh->mpegq);
903
904         case VIDIOC_STREAMOFF:
905                 return videobuf_streamoff(&fh->mpegq);
906
907         /* --- mpeg compression -------------------------------------- */
908         case VIDIOC_G_MPEGCOMP:
909         {
910                 struct v4l2_mpeg_compression *f = arg;
911
912                 printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. "
913                                     "Replace with VIDIOC_G_EXT_CTRLS!");
914                 memcpy(f,&default_mpeg_params,sizeof(*f));
915                 return 0;
916         }
917         case VIDIOC_S_MPEGCOMP:
918                 printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. "
919                                     "Replace with VIDIOC_S_EXT_CTRLS!");
920                 return 0;
921         case VIDIOC_G_EXT_CTRLS:
922         {
923                 struct v4l2_ext_controls *f = arg;
924
925                 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
926                         return -EINVAL;
927                 return cx2341x_ext_ctrls(&dev->params, f, cmd);
928         }
929         case VIDIOC_S_EXT_CTRLS:
930         {
931                 struct v4l2_ext_controls *f = arg;
932                 struct cx2341x_mpeg_params p;
933                 int err;
934
935                 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
936                         return -EINVAL;
937                 p = dev->params;
938                 err = cx2341x_ext_ctrls(&p, VIDIOC_S_EXT_CTRLS, cmd);
939                 if (err == 0 && cmd == VIDIOC_S_EXT_CTRLS) {
940                         err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
941                         dev->params = p;
942                 }
943                 return err;
944         }
945         case VIDIOC_TRY_EXT_CTRLS:
946         {
947                 struct v4l2_ext_controls *f = arg;
948                 struct cx2341x_mpeg_params p;
949                 int err;
950
951                 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
952                         return -EINVAL;
953                 p = dev->params;
954                 err = cx2341x_ext_ctrls(&p, VIDIOC_TRY_EXT_CTRLS, cmd);
955                 return err;
956         }
957         case VIDIOC_S_FREQUENCY:
958         {
959                 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
960                                   BLACKBIRD_END_NOW,
961                                   BLACKBIRD_MPEG_CAPTURE,
962                                   BLACKBIRD_RAW_BITS_NONE);
963
964                 cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
965
966                 blackbird_initialize_codec(dev);
967                 cx88_set_scale(dev->core, dev->width, dev->height,
968                                fh->mpegq.field);
969                 return 0;
970         }
971         case VIDIOC_LOG_STATUS:
972         {
973                 char name[32 + 2];
974
975                 snprintf(name, sizeof(name), "%s/2", core->name);
976                 printk("%s/2: ============  START LOG STATUS  ============\n",
977                        core->name);
978                 cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, NULL);
979                 cx2341x_log_status(&dev->params, name);
980                 printk("%s/2: =============  END LOG STATUS  =============\n",
981                        core->name);
982                 return 0;
983         }
984         case VIDIOC_QUERYMENU:
985                 return blackbird_querymenu(dev, arg);
986         case VIDIOC_QUERYCTRL:
987         {
988                 struct v4l2_queryctrl *c = arg;
989
990                 if (blackbird_queryctrl(dev, c) == 0)
991                         return 0;
992                 return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, mpeg_do_ioctl);
993         }
994
995         default:
996                 return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
997         }
998         return 0;
999 }
1000
1001 int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
1002                   struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
1003 {
1004         int err;
1005
1006        if (debug) {
1007                if (debug > 1) {
1008                        if (_IOC_DIR(cmd) & _IOC_WRITE)
1009                                v4l_printk_ioctl_arg("cx88(w)",cmd, arg);
1010                        else if (!_IOC_DIR(cmd) & _IOC_READ) {
1011                                v4l_print_ioctl("cx88", cmd);
1012                        }
1013                } else
1014                        v4l_print_ioctl(core->name,cmd);
1015
1016        }
1017
1018         switch (cmd) {
1019         /* ------ input switching ---------- */
1020         case VIDIOC_ENUMINPUT:
1021         {
1022                 static const char *iname[] = {
1023                         [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1024                         [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1025                         [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1026                         [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1027                         [ CX88_VMUX_SVIDEO     ] = "S-Video",
1028                         [ CX88_VMUX_TELEVISION ] = "Television",
1029                         [ CX88_VMUX_CABLE      ] = "Cable TV",
1030                         [ CX88_VMUX_DVB        ] = "DVB",
1031                         [ CX88_VMUX_DEBUG      ] = "for debug only",
1032                 };
1033                 struct v4l2_input *i = arg;
1034                 unsigned int n;
1035
1036                 n = i->index;
1037                 if (n >= 4)
1038                         return -EINVAL;
1039                 if (0 == INPUT(n)->type)
1040                         return -EINVAL;
1041                 memset(i,0,sizeof(*i));
1042                 i->index = n;
1043                 i->type  = V4L2_INPUT_TYPE_CAMERA;
1044                 strcpy(i->name,iname[INPUT(n)->type]);
1045                 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1046                     (CX88_VMUX_CABLE      == INPUT(n)->type))
1047                         i->type = V4L2_INPUT_TYPE_TUNER;
1048                 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1049                         i->std |= tvnorms[n].id;
1050                 return 0;
1051         }
1052         case VIDIOC_G_CTRL:
1053                 return cx88_get_control(core,arg);
1054         case VIDIOC_S_CTRL:
1055                 return cx88_set_control(core,arg);
1056
1057         case VIDIOC_G_FREQUENCY:
1058         {
1059                 struct v4l2_frequency *f = arg;
1060
1061                 memset(f,0,sizeof(*f));
1062
1063                 if (UNSET == core->tuner_type)
1064                         return -EINVAL;
1065
1066                 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1067                 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1068                 f->frequency = core->freq;
1069
1070                 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1071
1072                 return 0;
1073         }
1074         case VIDIOC_G_INPUT:
1075         {
1076                 unsigned int *i = arg;
1077
1078                 *i = core->input;
1079                 return 0;
1080         }
1081         case VIDIOC_S_INPUT:
1082         {
1083                 unsigned int *i = arg;
1084
1085                 if (*i >= 4)
1086                         return -EINVAL;
1087                 mutex_lock(&core->lock);
1088                 cx88_newstation(core);
1089                 cx88_video_mux(core,*i);
1090                 mutex_unlock(&core->lock);
1091                 return 0;
1092         }
1093
1094
1095         case VIDIOC_G_TUNER:
1096         {
1097                 struct v4l2_tuner *t = arg;
1098                 u32 reg;
1099
1100                 if (UNSET == core->tuner_type)
1101                         return -EINVAL;
1102                 if (0 != t->index)
1103                         return -EINVAL;
1104
1105                 memset(t,0,sizeof(*t));
1106                 strcpy(t->name, "Television");
1107                 t->type       = V4L2_TUNER_ANALOG_TV;
1108                 t->capability = V4L2_TUNER_CAP_NORM;
1109                 t->rangehigh  = 0xffffffffUL;
1110
1111                 cx88_get_stereo(core ,t);
1112                 reg = cx_read(MO_DEVICE_STATUS);
1113                 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1114                 return 0;
1115         }
1116         case VIDIOC_S_TUNER:
1117         {
1118                 struct v4l2_tuner *t = arg;
1119
1120                 if (UNSET == core->tuner_type)
1121                         return -EINVAL;
1122                 if (0 != t->index)
1123                         return -EINVAL;
1124                 cx88_set_stereo(core, t->audmode, 1);
1125                 return 0;
1126         }
1127         /* ---------- tv norms ---------- */
1128         case VIDIOC_S_STD:
1129         {
1130                 v4l2_std_id *id = arg;
1131                 unsigned int i;
1132
1133                 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1134                         if (*id & tvnorms[i].id)
1135                                 break;
1136                 if (i == ARRAY_SIZE(tvnorms))
1137                         return -EINVAL;
1138
1139                 mutex_lock(&core->lock);
1140                 cx88_set_tvnorm(core,tvnorms[i].id);
1141                 mutex_unlock(&core->lock);
1142                 return 0;
1143         }
1144         case VIDIOC_ENUMSTD:
1145         {
1146                 struct v4l2_standard *e = arg;
1147                 unsigned int i;
1148
1149                 i = e->index;
1150                 if (i >= ARRAY_SIZE(tvnorms))
1151                         return -EINVAL;
1152                 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1153                                                tvnorms[e->index].name);
1154                 e->index = i;
1155                 if (err < 0)
1156                         return err;
1157                 return 0;
1158         }
1159         case VIDIOC_G_STD:
1160         {
1161                 v4l2_std_id *id = arg;
1162
1163                 *id = core->tvnorm;
1164                 return 0;
1165         }
1166
1167
1168         /* --- controls ---------------------------------------------- */
1169         case VIDIOC_QUERYCTRL:
1170         {
1171                 struct v4l2_queryctrl *qctrl = arg;
1172
1173                 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1174                         if (unlikely(qctrl->id == 0))
1175                                 return -EINVAL;
1176                 return cx8800_ctrl_query(qctrl);
1177         }
1178         /* --- tuner ioctls ------------------------------------------ */
1179         case VIDIOC_S_FREQUENCY:
1180         {
1181                 struct v4l2_frequency *f = arg;
1182
1183                 if (UNSET == core->tuner_type)
1184                         return -EINVAL;
1185                 if (f->tuner != 0)
1186                         return -EINVAL;
1187                 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
1188                         return -EINVAL;
1189                 if (1 == radio && f->type != V4L2_TUNER_RADIO)
1190                         return -EINVAL;
1191                 mutex_lock(&core->lock);
1192                 core->freq = f->frequency;
1193                 cx88_newstation(core);
1194                 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1195
1196                 /* When changing channels it is required to reset TVAUDIO */
1197                 msleep (10);
1198                 cx88_set_tvaudio(core);
1199
1200                 mutex_unlock(&core->lock);
1201                 return 0;
1202         }
1203 #ifdef CONFIG_VIDEO_ADV_DEBUG
1204         /* ioctls to allow direct acces to the cx2388x registers */
1205         case VIDIOC_INT_G_REGISTER:
1206         {
1207                 struct v4l2_register *reg = arg;
1208
1209                 if (reg->i2c_id != 0)
1210                         return -EINVAL;
1211                 /* cx2388x has a 24-bit register space */
1212                 reg->val = cx_read(reg->reg&0xffffff);
1213                 return 0;
1214         }
1215         case VIDIOC_INT_S_REGISTER:
1216         {
1217                 struct v4l2_register *reg = arg;
1218
1219                 if (reg->i2c_id != 0)
1220                         return -EINVAL;
1221                 if (!capable(CAP_SYS_ADMIN))
1222                         return -EPERM;
1223                 cx_write(reg->reg&0xffffff, reg->val);
1224                 return 0;
1225         }
1226 #endif
1227
1228         default:
1229                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1230                                                   driver_ioctl);
1231         }
1232         return 0;
1233 }
1234
1235 int (*cx88_ioctl_hook)(struct inode *inode, struct file *file,
1236                         unsigned int cmd, void *arg);
1237 unsigned int (*cx88_ioctl_translator)(unsigned int cmd);
1238
1239 static unsigned int mpeg_translate_ioctl(unsigned int cmd)
1240 {
1241         return cmd;
1242 }
1243
1244 static int mpeg_ioctl(struct inode *inode, struct file *file,
1245                         unsigned int cmd, unsigned long arg)
1246 {
1247         cmd = cx88_ioctl_translator( cmd );
1248         return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
1249 }
1250
1251 static int mpeg_open(struct inode *inode, struct file *file)
1252 {
1253         int minor = iminor(inode);
1254         struct cx8802_dev *dev = NULL;
1255         struct cx8802_fh *fh;
1256         struct cx8802_driver *drv = NULL;
1257         int err;
1258
1259        dev = cx8802_get_device(inode);
1260
1261         dprintk( 1, "%s\n", __FUNCTION__);
1262
1263         if (dev == NULL)
1264                 return -ENODEV;
1265
1266         /* Make sure we can acquire the hardware */
1267         drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
1268         if (drv) {
1269                 err = drv->request_acquire(drv);
1270                 if(err != 0) {
1271                         dprintk(1,"%s: Unable to acquire hardware, %d\n", __FUNCTION__, err);
1272                         return err;
1273                 }
1274         }
1275
1276         if (blackbird_initialize_codec(dev) < 0) {
1277                 if (drv)
1278                         drv->request_release(drv);
1279                 return -EINVAL;
1280         }
1281         dprintk(1,"open minor=%d\n",minor);
1282
1283         /* allocate + initialize per filehandle data */
1284         fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1285         if (NULL == fh) {
1286                 if (drv)
1287                         drv->request_release(drv);
1288                 return -ENOMEM;
1289         }
1290         file->private_data = fh;
1291         fh->dev      = dev;
1292
1293         videobuf_queue_init(&fh->mpegq, &blackbird_qops,
1294                             dev->pci, &dev->slock,
1295                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1296                             V4L2_FIELD_INTERLACED,
1297                             sizeof(struct cx88_buffer),
1298                             fh);
1299
1300         /* FIXME: locking against other video device */
1301         cx88_set_scale(dev->core, dev->width, dev->height,
1302                         fh->mpegq.field);
1303
1304         return 0;
1305 }
1306
1307 static int mpeg_release(struct inode *inode, struct file *file)
1308 {
1309         struct cx8802_fh  *fh  = file->private_data;
1310         struct cx8802_dev *dev = NULL;
1311         struct cx8802_driver *drv = NULL;
1312
1313         /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
1314         blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
1315                         BLACKBIRD_END_NOW,
1316                         BLACKBIRD_MPEG_CAPTURE,
1317                         BLACKBIRD_RAW_BITS_NONE
1318                 );
1319
1320         cx8802_cancel_buffers(fh->dev);
1321         /* stop mpeg capture */
1322         if (fh->mpegq.streaming)
1323                 videobuf_streamoff(&fh->mpegq);
1324         if (fh->mpegq.reading)
1325                 videobuf_read_stop(&fh->mpegq);
1326
1327         videobuf_mmap_free(&fh->mpegq);
1328         file->private_data = NULL;
1329         kfree(fh);
1330
1331         /* Make sure we release the hardware */
1332         dev = cx8802_get_device(inode);
1333         if (dev == NULL)
1334                 return -ENODEV;
1335
1336         drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
1337         if (drv)
1338                 drv->request_release(drv);
1339
1340         return 0;
1341 }
1342
1343 static ssize_t
1344 mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1345 {
1346         struct cx8802_fh *fh = file->private_data;
1347
1348         return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1349                                     file->f_flags & O_NONBLOCK);
1350 }
1351
1352 static unsigned int
1353 mpeg_poll(struct file *file, struct poll_table_struct *wait)
1354 {
1355         struct cx8802_fh *fh = file->private_data;
1356
1357         return videobuf_poll_stream(file, &fh->mpegq, wait);
1358 }
1359
1360 static int
1361 mpeg_mmap(struct file *file, struct vm_area_struct * vma)
1362 {
1363         struct cx8802_fh *fh = file->private_data;
1364
1365         return videobuf_mmap_mapper(&fh->mpegq, vma);
1366 }
1367
1368 static const struct file_operations mpeg_fops =
1369 {
1370         .owner         = THIS_MODULE,
1371         .open          = mpeg_open,
1372         .release       = mpeg_release,
1373         .read          = mpeg_read,
1374         .poll          = mpeg_poll,
1375         .mmap          = mpeg_mmap,
1376         .ioctl         = mpeg_ioctl,
1377         .llseek        = no_llseek,
1378 };
1379
1380 static struct video_device cx8802_mpeg_template =
1381 {
1382         .name          = "cx8802",
1383         .type          = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
1384         .hardware      = 0,
1385         .fops          = &mpeg_fops,
1386         .minor         = -1,
1387 };
1388
1389 /* ------------------------------------------------------------------ */
1390
1391 /* The CX8802 MPEG API will call this when we can use the hardware */
1392 static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
1393 {
1394         struct cx88_core *core = drv->core;
1395         int err = 0;
1396
1397         switch (core->board) {
1398         case CX88_BOARD_HAUPPAUGE_HVR1300:
1399                 /* By default, core setup will leave the cx22702 out of reset, on the bus.
1400                  * We left the hardware on power up with the cx22702 active.
1401                  * We're being given access to re-arrange the GPIOs.
1402                  * Take the bus off the cx22702 and put the cx23416 on it.
1403                  */
1404                 cx_clear(MO_GP0_IO, 0x00000080); /* cx22702 in reset */
1405                 cx_set(MO_GP0_IO,   0x00000004); /* Disable the cx22702 */
1406                 break;
1407         default:
1408                 err = -ENODEV;
1409         }
1410         return err;
1411 }
1412
1413 /* The CX8802 MPEG API will call this when we need to release the hardware */
1414 static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
1415 {
1416         struct cx88_core *core = drv->core;
1417         int err = 0;
1418
1419         switch (core->board) {
1420         case CX88_BOARD_HAUPPAUGE_HVR1300:
1421                 /* Exit leaving the cx23416 on the bus */
1422                 break;
1423         default:
1424                 err = -ENODEV;
1425         }
1426         return err;
1427 }
1428
1429 static void blackbird_unregister_video(struct cx8802_dev *dev)
1430 {
1431         if (dev->mpeg_dev) {
1432                 if (-1 != dev->mpeg_dev->minor)
1433                         video_unregister_device(dev->mpeg_dev);
1434                 else
1435                         video_device_release(dev->mpeg_dev);
1436                 dev->mpeg_dev = NULL;
1437         }
1438 }
1439
1440 static int blackbird_register_video(struct cx8802_dev *dev)
1441 {
1442         int err;
1443
1444         dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
1445                                        &cx8802_mpeg_template,"mpeg");
1446         err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
1447         if (err < 0) {
1448                 printk(KERN_INFO "%s/2: can't register mpeg device\n",
1449                        dev->core->name);
1450                 return err;
1451         }
1452         printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
1453                dev->core->name,dev->mpeg_dev->minor & 0x1f);
1454         return 0;
1455 }
1456
1457 /* ----------------------------------------------------------- */
1458
1459 static int cx8802_blackbird_probe(struct cx8802_driver *drv)
1460 {
1461         struct cx88_core *core = drv->core;
1462         struct cx8802_dev *dev = core->dvbdev;
1463         int err;
1464
1465         dprintk( 1, "%s\n", __FUNCTION__);
1466         dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1467                 core->board,
1468                 core->name,
1469                 core->pci_bus,
1470                 core->pci_slot);
1471
1472         err = -ENODEV;
1473         if (!(cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD))
1474                 goto fail_core;
1475
1476         dev->width = 720;
1477         dev->height = 576;
1478         cx2341x_fill_defaults(&dev->params);
1479         dev->params.port = CX2341X_PORT_STREAMING;
1480
1481         if (core->tvnorm & V4L2_STD_525_60) {
1482                 dev->height = 480;
1483         } else {
1484                 dev->height = 576;
1485         }
1486
1487         /* blackbird stuff */
1488         printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1489                core->name);
1490         host_setup(dev->core);
1491
1492         blackbird_register_video(dev);
1493
1494         /* initial device configuration: needed ? */
1495
1496         return 0;
1497
1498  fail_core:
1499         return err;
1500 }
1501
1502 static int cx8802_blackbird_remove(struct cx8802_driver *drv)
1503 {
1504         /* blackbird */
1505         blackbird_unregister_video(drv->core->dvbdev);
1506
1507         return 0;
1508 }
1509
1510 static struct cx8802_driver cx8802_blackbird_driver = {
1511         .type_id        = CX88_MPEG_BLACKBIRD,
1512         .hw_access      = CX8802_DRVCTL_SHARED,
1513         .probe          = cx8802_blackbird_probe,
1514         .remove         = cx8802_blackbird_remove,
1515         .advise_acquire = cx8802_blackbird_advise_acquire,
1516         .advise_release = cx8802_blackbird_advise_release,
1517 };
1518
1519 static int blackbird_init(void)
1520 {
1521         printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
1522                (CX88_VERSION_CODE >> 16) & 0xff,
1523                (CX88_VERSION_CODE >>  8) & 0xff,
1524                CX88_VERSION_CODE & 0xff);
1525 #ifdef SNAPSHOT
1526         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
1527                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1528 #endif
1529         cx88_ioctl_hook = mpeg_do_ioctl;
1530         cx88_ioctl_translator = mpeg_translate_ioctl;
1531         return cx8802_register_driver(&cx8802_blackbird_driver);
1532 }
1533
1534 static void blackbird_fini(void)
1535 {
1536         cx8802_unregister_driver(&cx8802_blackbird_driver);
1537 }
1538
1539 module_init(blackbird_init);
1540 module_exit(blackbird_fini);
1541
1542 EXPORT_SYMBOL(cx88_ioctl_hook);
1543 EXPORT_SYMBOL(cx88_ioctl_translator);
1544
1545 /* ----------------------------------------------------------- */
1546 /*
1547  * Local variables:
1548  * c-basic-offset: 8
1549  * End:
1550  * 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
1551  */