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