[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / media / video / cx88 / cx88-blackbird.c
blob46d6778b863b6e29ca14092a12dc105c0a30edf3
1 /*
2 * $Id: cx88-blackbird.c,v 1.26 2005/03/07 15:58:05 kraxel Exp $
4 * Support for a cx23416 mpeg encoder via cx2388x host port.
5 * "blackbird" reference design.
7 * (c) 2004 Jelle Foks <jelle@foks.8m.com>
8 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
10 * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/fs.h>
31 #include <linux/delay.h>
32 #include <linux/device.h>
33 #include <linux/firmware.h>
35 #include "cx88.h"
37 MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
38 MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
39 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40 MODULE_LICENSE("GPL");
42 static unsigned int mpegbufs = 8;
43 module_param(mpegbufs,int,0644);
44 MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
46 static unsigned int debug = 0;
47 module_param(debug,int,0644);
48 MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
50 #define dprintk(level,fmt, arg...) if (debug >= level) \
51 printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
53 static LIST_HEAD(cx8802_devlist);
55 /* ------------------------------------------------------------------ */
57 #define BLACKBIRD_FIRM_ENC_FILENAME "blackbird-fw-enc.bin"
58 #define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
60 /* defines below are from ivtv-driver.h */
62 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
64 /*Firmware API commands*/
65 #define IVTV_API_ENC_PING_FW 0x00000080
66 #define IVTV_API_ENC_GETVER 0x000000C4
67 #define IVTV_API_ENC_HALT_FW 0x000000C3
68 #define IVTV_API_STD_TIMEOUT 0x00010000 /*units??*/
69 //#define IVTV_API_ASSIGN_PGM_INDEX_INFO 0x000000c7
70 #define IVTV_API_ASSIGN_STREAM_TYPE 0x000000b9
71 #define IVTV_API_ASSIGN_OUTPUT_PORT 0x000000bb
72 #define IVTV_API_ASSIGN_FRAMERATE 0x0000008f
73 #define IVTV_API_ASSIGN_FRAME_SIZE 0x00000091
74 #define IVTV_API_ASSIGN_ASPECT_RATIO 0x00000099
75 #define IVTV_API_ASSIGN_BITRATES 0x00000095
76 #define IVTV_API_ASSIGN_GOP_PROPERTIES 0x00000097
77 #define IVTV_API_ASSIGN_3_2_PULLDOWN 0x000000b1
78 #define IVTV_API_ASSIGN_GOP_CLOSURE 0x000000c5
79 #define IVTV_API_ASSIGN_AUDIO_PROPERTIES 0x000000bd
80 #define IVTV_API_ASSIGN_DNR_FILTER_MODE 0x0000009b
81 #define IVTV_API_ASSIGN_DNR_FILTER_PROPS 0x0000009d
82 #define IVTV_API_ASSIGN_CORING_LEVELS 0x0000009f
83 #define IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE 0x000000a1
84 #define IVTV_API_ASSIGN_FRAME_DROP_RATE 0x000000d0
85 #define IVTV_API_ASSIGN_PLACEHOLDER 0x000000d8
86 #define IVTV_API_MUTE_VIDEO 0x000000d9
87 #define IVTV_API_MUTE_AUDIO 0x000000da
88 #define IVTV_API_INITIALIZE_INPUT 0x000000cd
89 #define IVTV_API_REFRESH_INPUT 0x000000d3
90 #define IVTV_API_ASSIGN_NUM_VSYNC_LINES 0x000000d6
91 #define IVTV_API_BEGIN_CAPTURE 0x00000081
92 //#define IVTV_API_PAUSE_ENCODER 0x000000d2
93 //#define IVTV_API_EVENT_NOTIFICATION 0x000000d5
94 #define IVTV_API_END_CAPTURE 0x00000082
96 /* Registers */
97 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
98 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
99 #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
100 #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
101 #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
102 #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
104 /* ------------------------------------------------------------------ */
106 static void host_setup(struct cx88_core *core)
108 /* toggle reset of the host */
109 cx_write(MO_GPHST_SOFT_RST, 1);
110 udelay(100);
111 cx_write(MO_GPHST_SOFT_RST, 0);
112 udelay(100);
114 /* host port setup */
115 cx_write(MO_GPHST_WSC, 0x44444444U);
116 cx_write(MO_GPHST_XFR, 0);
117 cx_write(MO_GPHST_WDTH, 15);
118 cx_write(MO_GPHST_HDSHK, 0);
119 cx_write(MO_GPHST_MUX16, 0x44448888U);
120 cx_write(MO_GPHST_MODE, 0);
123 /* ------------------------------------------------------------------ */
125 #define P1_MDATA0 0x390000
126 #define P1_MDATA1 0x390001
127 #define P1_MDATA2 0x390002
128 #define P1_MDATA3 0x390003
129 #define P1_MADDR2 0x390004
130 #define P1_MADDR1 0x390005
131 #define P1_MADDR0 0x390006
132 #define P1_RDATA0 0x390008
133 #define P1_RDATA1 0x390009
134 #define P1_RDATA2 0x39000A
135 #define P1_RDATA3 0x39000B
136 #define P1_RADDR0 0x39000C
137 #define P1_RADDR1 0x39000D
138 #define P1_RRDWR 0x39000E
140 static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
142 unsigned long timeout = jiffies + msecs_to_jiffies(1);
143 u32 gpio0,need;
145 need = state ? 2 : 0;
146 for (;;) {
147 gpio0 = cx_read(MO_GP0_IO) & 2;
148 if (need == gpio0)
149 return 0;
150 if (time_after(jiffies,timeout))
151 return -1;
152 udelay(1);
156 static int memory_write(struct cx88_core *core, u32 address, u32 value)
158 /* Warning: address is dword address (4 bytes) */
159 cx_writeb(P1_MDATA0, (unsigned int)value);
160 cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
161 cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
162 cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
163 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
164 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
165 cx_writeb(P1_MADDR0, (unsigned int)address);
166 cx_read(P1_MDATA0);
167 cx_read(P1_MADDR0);
169 return wait_ready_gpio0_bit1(core,1);
172 static int memory_read(struct cx88_core *core, u32 address, u32 *value)
174 int retval;
175 u32 val;
177 /* Warning: address is dword address (4 bytes) */
178 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
179 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
180 cx_writeb(P1_MADDR0, (unsigned int)address);
181 cx_read(P1_MADDR0);
183 retval = wait_ready_gpio0_bit1(core,1);
185 cx_writeb(P1_MDATA3, 0);
186 val = (unsigned char)cx_read(P1_MDATA3) << 24;
187 cx_writeb(P1_MDATA2, 0);
188 val |= (unsigned char)cx_read(P1_MDATA2) << 16;
189 cx_writeb(P1_MDATA1, 0);
190 val |= (unsigned char)cx_read(P1_MDATA1) << 8;
191 cx_writeb(P1_MDATA0, 0);
192 val |= (unsigned char)cx_read(P1_MDATA0);
194 *value = val;
195 return retval;
198 static int register_write(struct cx88_core *core, u32 address, u32 value)
200 cx_writeb(P1_RDATA0, (unsigned int)value);
201 cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
202 cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
203 cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
204 cx_writeb(P1_RADDR0, (unsigned int)address);
205 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
206 cx_writeb(P1_RRDWR, 1);
207 cx_read(P1_RDATA0);
208 cx_read(P1_RADDR0);
210 return wait_ready_gpio0_bit1(core,1);
214 static int register_read(struct cx88_core *core, u32 address, u32 *value)
216 int retval;
217 u32 val;
219 cx_writeb(P1_RADDR0, (unsigned int)address);
220 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
221 cx_writeb(P1_RRDWR, 0);
222 cx_read(P1_RADDR0);
224 retval = wait_ready_gpio0_bit1(core,1);
225 val = (unsigned char)cx_read(P1_RDATA0);
226 val |= (unsigned char)cx_read(P1_RDATA1) << 8;
227 val |= (unsigned char)cx_read(P1_RDATA2) << 16;
228 val |= (unsigned char)cx_read(P1_RDATA3) << 24;
230 *value = val;
231 return retval;
234 /* ------------------------------------------------------------------ */
236 /* We don't need to call the API often, so using just one mailbox will probably suffice */
237 static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
238 u32 inputcnt, u32 outputcnt, ...)
240 unsigned long timeout;
241 u32 value, flag, retval;
242 int i;
243 va_list args;
244 va_start(args, outputcnt);
246 dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
248 /* this may not be 100% safe if we can't read any memory location
249 without side effects */
250 memory_read(dev->core, dev->mailbox - 4, &value);
251 if (value != 0x12345678) {
252 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
253 return -1;
256 memory_read(dev->core, dev->mailbox, &flag);
257 if (flag) {
258 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
259 return -1;
262 flag |= 1; /* tell 'em we're working on it */
263 memory_write(dev->core, dev->mailbox, flag);
265 /* write command + args + fill remaining with zeros */
266 memory_write(dev->core, dev->mailbox + 1, command); /* command code */
267 memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
268 for (i = 0; i < inputcnt ; i++) {
269 value = va_arg(args, int);
270 memory_write(dev->core, dev->mailbox + 4 + i, value);
271 dprintk(1, "API Input %d = %d\n", i, value);
273 for (; i < 16 ; i++)
274 memory_write(dev->core, dev->mailbox + 4 + i, 0);
276 flag |= 3; /* tell 'em we're done writing */
277 memory_write(dev->core, dev->mailbox, flag);
279 /* wait for firmware to handle the API command */
280 timeout = jiffies + msecs_to_jiffies(10);
281 for (;;) {
282 memory_read(dev->core, dev->mailbox, &flag);
283 if (0 != (flag & 4))
284 break;
285 if (time_after(jiffies,timeout)) {
286 dprintk(0, "ERROR: API Mailbox timeout\n");
287 return -1;
289 udelay(10);
292 /* read output values */
293 for (i = 0; i < outputcnt ; i++) {
294 int *vptr = va_arg(args, int *);
295 memory_read(dev->core, dev->mailbox + 4 + i, vptr);
296 dprintk(1, "API Output %d = %d\n", i, *vptr);
298 va_end(args);
300 memory_read(dev->core, dev->mailbox + 2, &retval);
301 dprintk(1, "API result = %d\n",retval);
303 flag = 0;
304 memory_write(dev->core, dev->mailbox, flag);
305 return retval;
309 static int blackbird_find_mailbox(struct cx8802_dev *dev)
311 u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
312 int signaturecnt=0;
313 u32 value;
314 int i;
316 for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
317 memory_read(dev->core, i, &value);
318 if (value == signature[signaturecnt])
319 signaturecnt++;
320 else
321 signaturecnt = 0;
322 if (4 == signaturecnt) {
323 dprintk(1, "Mailbox signature found\n");
324 return i+1;
327 dprintk(0, "Mailbox signature values not found!\n");
328 return -1;
331 static int blackbird_load_firmware(struct cx8802_dev *dev)
333 static const unsigned char magic[8] = {
334 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
336 const struct firmware *firmware;
337 int i, retval = 0;
338 u32 value = 0;
339 u32 checksum = 0;
340 u32 *dataptr;
342 retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
343 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
344 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
345 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
346 msleep(1);
347 retval |= register_write(dev->core, IVTV_REG_APU, 0);
349 if (retval < 0)
350 dprintk(0, "Error with register_write\n");
352 retval = request_firmware(&firmware, BLACKBIRD_FIRM_ENC_FILENAME,
353 &dev->pci->dev);
354 if (retval != 0) {
355 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
356 BLACKBIRD_FIRM_ENC_FILENAME);
357 dprintk(0, "Please fix your hotplug setup, the board will "
358 "not work without firmware loaded!\n");
359 return -1;
362 if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
363 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
364 firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
365 return -1;
368 if (0 != memcmp(firmware->data, magic, 8)) {
369 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
370 return -1;
373 /* transfer to the chip */
374 dprintk(1,"Loading firmware ...\n");
375 dataptr = (u32*)firmware->data;
376 for (i = 0; i < (firmware->size >> 2); i++) {
377 value = *dataptr;
378 checksum += ~value;
379 memory_write(dev->core, i, value);
380 dataptr++;
383 /* read back to verify with the checksum */
384 for (i--; i >= 0; i--) {
385 memory_read(dev->core, i, &value);
386 checksum -= ~value;
388 if (checksum) {
389 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
390 return -1;
392 release_firmware(firmware);
393 dprintk(0, "Firmware upload successful.\n");
395 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
396 retval |= register_read(dev->core, IVTV_REG_SPU, &value);
397 retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
398 msleep(1);
400 retval |= register_read(dev->core, IVTV_REG_VPU, &value);
401 retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
403 if (retval < 0)
404 dprintk(0, "Error with register_write\n");
405 return 0;
408 static void blackbird_codec_settings(struct cx8802_dev *dev)
410 int bitrate_mode = 1;
411 int bitrate = 7500000;
412 int bitrate_peak = 7500000;
414 /* assign stream type */
415 blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 0); /* program stream */
416 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 2); /* MPEG1 stream */
417 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 3); /* PES A/V */
418 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 10); /* DVD stream */
420 /* assign output port */
421 blackbird_api_cmd(dev, IVTV_API_ASSIGN_OUTPUT_PORT, 1, 0, 1); /* 1 = Host */
423 /* assign framerate */
424 blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAMERATE, 1, 0, 0);
426 /* assign frame size */
427 blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_SIZE, 2, 0,
428 dev->height, dev->width);
430 /* assign aspect ratio */
431 blackbird_api_cmd(dev, IVTV_API_ASSIGN_ASPECT_RATIO, 1, 0, 2);
433 /* assign bitrates */
434 blackbird_api_cmd(dev, IVTV_API_ASSIGN_BITRATES, 5, 0,
435 bitrate_mode, /* mode */
436 bitrate, /* bps */
437 bitrate_peak / 400, /* peak/400 */
438 0, 0x70); /* encoding buffer, ckennedy */
440 /* assign gop properties */
441 blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 15, 3);
442 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 2, 1);
444 /* assign 3 2 pulldown */
445 blackbird_api_cmd(dev, IVTV_API_ASSIGN_3_2_PULLDOWN, 1, 0, 0);
447 /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
448 blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, (2<<2) | (8<<4));
450 /* assign gop closure */
451 blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_CLOSURE, 1, 0, 0);
453 /* assign audio properties */
454 blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, 0 | (2 << 2) | (14 << 4));
456 /* assign dnr filter mode */
457 blackbird_api_cmd(dev, IVTV_API_ASSIGN_DNR_FILTER_MODE, 2, 0, 0, 0);
459 /* assign dnr filter props*/
460 blackbird_api_cmd(dev, IVTV_API_ASSIGN_DNR_FILTER_PROPS, 2, 0, 0, 0);
462 /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
463 blackbird_api_cmd(dev, IVTV_API_ASSIGN_CORING_LEVELS, 4, 0, 0, 255, 0, 255);
465 /* assign spatial filter type: luma_t: 1 = horiz_only, chroma_t: 1 = horiz_only */
466 blackbird_api_cmd(dev, IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE, 2, 0, 1, 1);
468 /* assign frame drop rate */
469 blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_DROP_RATE, 1, 0, 0);
472 static int blackbird_initialize_codec(struct cx8802_dev *dev)
474 struct cx88_core *core = dev->core;
475 int version;
476 int retval;
478 dprintk(1,"Initialize codec\n");
479 retval = blackbird_api_cmd(dev, IVTV_API_ENC_PING_FW, 0, 0); /* ping */
480 if (retval < 0) {
481 /* ping was not successful, reset and upload firmware */
482 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
483 msleep(1);
484 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
485 msleep(1);
486 retval = blackbird_load_firmware(dev);
487 if (retval < 0)
488 return retval;
490 dev->mailbox = blackbird_find_mailbox(dev);
491 if (dev->mailbox < 0)
492 return -1;
494 retval = blackbird_api_cmd(dev, IVTV_API_ENC_PING_FW, 0, 0); /* ping */
495 if (retval < 0) {
496 dprintk(0, "ERROR: Firmware ping failed!\n");
497 return -1;
500 retval = blackbird_api_cmd(dev, IVTV_API_ENC_GETVER, 0, 1, &version);
501 if (retval < 0) {
502 dprintk(0, "ERROR: Firmware get encoder version failed!\n");
503 return -1;
505 dprintk(0, "Firmware version is 0x%08x\n", version);
507 msleep(1);
509 cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
510 cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
511 cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
512 cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
514 #if 0 /* FIXME */
515 set_scale(dev, 720, 480, V4L2_FIELD_INTERLACED);
516 #endif
517 blackbird_codec_settings(dev);
518 msleep(1);
520 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
521 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
522 //blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180);
523 blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
525 blackbird_api_cmd(dev, IVTV_API_INITIALIZE_INPUT, 0, 0); /* initialize the video input */
527 msleep(1);
529 blackbird_api_cmd(dev, IVTV_API_MUTE_VIDEO, 1, 0, 0);
530 msleep(1);
531 blackbird_api_cmd(dev, IVTV_API_MUTE_AUDIO, 1, 0, 0);
532 msleep(1);
534 blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0x13); /* start capturing to the host interface */
535 //blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0); /* start capturing to the host interface */
536 msleep(1);
538 blackbird_api_cmd(dev, IVTV_API_REFRESH_INPUT, 0,0);
539 return 0;
542 /* ------------------------------------------------------------------ */
544 static int bb_buf_setup(struct videobuf_queue *q,
545 unsigned int *count, unsigned int *size)
547 struct cx8802_fh *fh = q->priv_data;
549 fh->dev->ts_packet_size = 512;
550 fh->dev->ts_packet_count = 100;
552 *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
553 if (0 == *count)
554 *count = mpegbufs;
555 if (*count < 2)
556 *count = 2;
557 if (*count > 32)
558 *count = 32;
559 return 0;
562 static int
563 bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
564 enum v4l2_field field)
566 struct cx8802_fh *fh = q->priv_data;
567 return cx8802_buf_prepare(fh->dev, (struct cx88_buffer*)vb);
570 static void
571 bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
573 struct cx8802_fh *fh = q->priv_data;
574 cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
577 static void
578 bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
580 struct cx8802_fh *fh = q->priv_data;
581 cx88_free_buffer(fh->dev->pci, (struct cx88_buffer*)vb);
584 static struct videobuf_queue_ops blackbird_qops = {
585 .buf_setup = bb_buf_setup,
586 .buf_prepare = bb_buf_prepare,
587 .buf_queue = bb_buf_queue,
588 .buf_release = bb_buf_release,
591 /* ------------------------------------------------------------------ */
593 static int mpeg_do_ioctl(struct inode *inode, struct file *file,
594 unsigned int cmd, void *arg)
596 struct cx8802_fh *fh = file->private_data;
597 struct cx8802_dev *dev = fh->dev;
599 if (debug > 1)
600 cx88_print_ioctl(dev->core->name,cmd);
602 switch (cmd) {
604 /* --- capture ioctls ---------------------------------------- */
605 case VIDIOC_ENUM_FMT:
607 struct v4l2_fmtdesc *f = arg;
608 int index;
610 index = f->index;
611 if (index != 0)
612 return -EINVAL;
614 memset(f,0,sizeof(*f));
615 f->index = index;
616 strlcpy(f->description, "MPEG TS", sizeof(f->description));
617 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
618 f->pixelformat = V4L2_PIX_FMT_MPEG;
619 return 0;
621 case VIDIOC_G_FMT:
622 case VIDIOC_S_FMT:
623 case VIDIOC_TRY_FMT:
625 /* FIXME -- quick'n'dirty for exactly one size ... */
626 struct v4l2_format *f = arg;
628 memset(f,0,sizeof(*f));
629 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
630 f->fmt.pix.width = dev->width;
631 f->fmt.pix.height = dev->height;
632 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
633 f->fmt.pix.sizeimage = 1024 * 512 /* FIXME: BUFFER_SIZE */;
636 /* --- streaming capture ------------------------------------- */
637 case VIDIOC_REQBUFS:
638 return videobuf_reqbufs(&fh->mpegq, arg);
640 case VIDIOC_QUERYBUF:
641 return videobuf_querybuf(&fh->mpegq, arg);
643 case VIDIOC_QBUF:
644 return videobuf_qbuf(&fh->mpegq, arg);
646 case VIDIOC_DQBUF:
647 return videobuf_dqbuf(&fh->mpegq, arg,
648 file->f_flags & O_NONBLOCK);
650 case VIDIOC_STREAMON:
651 return videobuf_streamon(&fh->mpegq);
653 case VIDIOC_STREAMOFF:
654 return videobuf_streamoff(&fh->mpegq);
656 default:
657 return -EINVAL;
659 return 0;
662 static int mpeg_ioctl(struct inode *inode, struct file *file,
663 unsigned int cmd, unsigned long arg)
665 return video_usercopy(inode, file, cmd, arg, mpeg_do_ioctl);
668 static int mpeg_open(struct inode *inode, struct file *file)
670 int minor = iminor(inode);
671 struct cx8802_dev *h,*dev = NULL;
672 struct cx8802_fh *fh;
673 struct list_head *list;
675 list_for_each(list,&cx8802_devlist) {
676 h = list_entry(list, struct cx8802_dev, devlist);
677 if (h->mpeg_dev->minor == minor)
678 dev = h;
680 if (NULL == dev)
681 return -ENODEV;
683 if (blackbird_initialize_codec(dev) < 0)
684 return -EINVAL;
685 dprintk(1,"open minor=%d\n",minor);
687 /* allocate + initialize per filehandle data */
688 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
689 if (NULL == fh)
690 return -ENOMEM;
691 memset(fh,0,sizeof(*fh));
692 file->private_data = fh;
693 fh->dev = dev;
695 /* FIXME: locking against other video device */
696 cx88_set_scale(dev->core, dev->width, dev->height,
697 V4L2_FIELD_INTERLACED);
699 videobuf_queue_init(&fh->mpegq, &blackbird_qops,
700 dev->pci, &dev->slock,
701 V4L2_BUF_TYPE_VIDEO_CAPTURE,
702 V4L2_FIELD_TOP,
703 sizeof(struct cx88_buffer),
704 fh);
705 return 0;
708 static int mpeg_release(struct inode *inode, struct file *file)
710 struct cx8802_fh *fh = file->private_data;
712 blackbird_api_cmd(fh->dev, IVTV_API_END_CAPTURE, 3, 0, 1, 0, 0x13);
714 /* stop mpeg capture */
715 if (fh->mpegq.streaming)
716 videobuf_streamoff(&fh->mpegq);
717 if (fh->mpegq.reading)
718 videobuf_read_stop(&fh->mpegq);
720 videobuf_mmap_free(&fh->mpegq);
721 file->private_data = NULL;
722 kfree(fh);
723 return 0;
726 static ssize_t
727 mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
729 struct cx8802_fh *fh = file->private_data;
731 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
732 file->f_flags & O_NONBLOCK);
735 static unsigned int
736 mpeg_poll(struct file *file, struct poll_table_struct *wait)
738 struct cx8802_fh *fh = file->private_data;
740 return videobuf_poll_stream(file, &fh->mpegq, wait);
743 static int
744 mpeg_mmap(struct file *file, struct vm_area_struct * vma)
746 struct cx8802_fh *fh = file->private_data;
748 return videobuf_mmap_mapper(&fh->mpegq, vma);
751 static struct file_operations mpeg_fops =
753 .owner = THIS_MODULE,
754 .open = mpeg_open,
755 .release = mpeg_release,
756 .read = mpeg_read,
757 .poll = mpeg_poll,
758 .mmap = mpeg_mmap,
759 .ioctl = mpeg_ioctl,
760 .llseek = no_llseek,
763 static struct video_device cx8802_mpeg_template =
765 .name = "cx8802",
766 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
767 .hardware = 0,
768 .fops = &mpeg_fops,
769 .minor = -1,
772 /* ------------------------------------------------------------------ */
774 static void blackbird_unregister_video(struct cx8802_dev *dev)
776 if (dev->mpeg_dev) {
777 if (-1 != dev->mpeg_dev->minor)
778 video_unregister_device(dev->mpeg_dev);
779 else
780 video_device_release(dev->mpeg_dev);
781 dev->mpeg_dev = NULL;
785 static int blackbird_register_video(struct cx8802_dev *dev)
787 int err;
789 dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
790 &cx8802_mpeg_template,"mpeg");
791 err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
792 if (err < 0) {
793 printk(KERN_INFO "%s/2: can't register mpeg device\n",
794 dev->core->name);
795 return err;
797 printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
798 dev->core->name,dev->mpeg_dev->minor & 0x1f);
799 return 0;
802 /* ----------------------------------------------------------- */
804 static int __devinit blackbird_probe(struct pci_dev *pci_dev,
805 const struct pci_device_id *pci_id)
807 struct cx8802_dev *dev;
808 struct cx88_core *core;
809 int err;
811 /* general setup */
812 core = cx88_core_get(pci_dev);
813 if (NULL == core)
814 return -EINVAL;
816 err = -ENODEV;
817 if (!cx88_boards[core->board].blackbird)
818 goto fail_core;
820 err = -ENOMEM;
821 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
822 if (NULL == dev)
823 goto fail_core;
824 memset(dev,0,sizeof(*dev));
825 dev->pci = pci_dev;
826 dev->core = core;
827 dev->width = 720;
828 dev->height = 480;
830 err = cx8802_init_common(dev);
831 if (0 != err)
832 goto fail_free;
834 /* blackbird stuff */
835 printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
836 core->name);
837 host_setup(dev->core);
839 list_add_tail(&dev->devlist,&cx8802_devlist);
840 blackbird_register_video(dev);
841 return 0;
843 fail_free:
844 kfree(dev);
845 fail_core:
846 cx88_core_put(core,pci_dev);
847 return err;
850 static void __devexit blackbird_remove(struct pci_dev *pci_dev)
852 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
854 /* blackbird */
855 blackbird_unregister_video(dev);
856 list_del(&dev->devlist);
858 /* common */
859 cx8802_fini_common(dev);
860 cx88_core_put(dev->core,dev->pci);
861 kfree(dev);
864 static struct pci_device_id cx8802_pci_tbl[] = {
866 .vendor = 0x14f1,
867 .device = 0x8802,
868 .subvendor = PCI_ANY_ID,
869 .subdevice = PCI_ANY_ID,
871 /* --- end of list --- */
874 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
876 static struct pci_driver blackbird_pci_driver = {
877 .name = "cx88-blackbird",
878 .id_table = cx8802_pci_tbl,
879 .probe = blackbird_probe,
880 .remove = __devexit_p(blackbird_remove),
881 .suspend = cx8802_suspend_common,
882 .resume = cx8802_resume_common,
885 static int blackbird_init(void)
887 printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
888 (CX88_VERSION_CODE >> 16) & 0xff,
889 (CX88_VERSION_CODE >> 8) & 0xff,
890 CX88_VERSION_CODE & 0xff);
891 #ifdef SNAPSHOT
892 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
893 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
894 #endif
895 return pci_register_driver(&blackbird_pci_driver);
898 static void blackbird_fini(void)
900 pci_unregister_driver(&blackbird_pci_driver);
903 module_init(blackbird_init);
904 module_exit(blackbird_fini);
906 /* ----------------------------------------------------------- */
908 * Local variables:
909 * c-basic-offset: 8
910 * End: