blob: 349632b48e931a2dff360965703754ba0a3b1d19 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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>
Michael Krufky855dbad2006-05-22 10:31:54 -030033#include <media/v4l2-common.h>
34#include <media/cx2341x.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include "cx88.h"
37
38MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -020039MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040MODULE_LICENSE("GPL");
41
Catalin Climov31629422005-11-08 21:36:17 -080042static unsigned int mpegbufs = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043module_param(mpegbufs,int,0644);
44MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
45
46static unsigned int debug = 0;
47module_param(debug,int,0644);
48MODULE_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
53static LIST_HEAD(cx8802_devlist);
54
55/* ------------------------------------------------------------------ */
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
58
59/* defines below are from ivtv-driver.h */
60
61#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
62
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070063/* Firmware API commands */
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070064#define IVTV_API_STD_TIMEOUT 500
65
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070066enum blackbird_capture_type {
67 BLACKBIRD_MPEG_CAPTURE,
68 BLACKBIRD_RAW_CAPTURE,
69 BLACKBIRD_RAW_PASSTHRU_CAPTURE
70};
71enum blackbird_capture_bits {
72 BLACKBIRD_RAW_BITS_NONE = 0x00,
73 BLACKBIRD_RAW_BITS_YUV_CAPTURE = 0x01,
74 BLACKBIRD_RAW_BITS_PCM_CAPTURE = 0x02,
75 BLACKBIRD_RAW_BITS_VBI_CAPTURE = 0x04,
76 BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
77 BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE = 0x10
78};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070079enum blackbird_capture_end {
80 BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
81 BLACKBIRD_END_NOW, /* stop immediately, no irq */
82};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070083enum blackbird_framerate {
84 BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
85 BLACKBIRD_FRAMERATE_PAL_25 /* PAL: 25fps */
86};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070087enum blackbird_stream_port {
88 BLACKBIRD_OUTPUT_PORT_MEMORY,
89 BLACKBIRD_OUTPUT_PORT_STREAMING,
90 BLACKBIRD_OUTPUT_PORT_SERIAL
91};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070092enum blackbird_data_xfer_status {
93 BLACKBIRD_MORE_BUFFERS_FOLLOW,
94 BLACKBIRD_LAST_BUFFER,
95};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -070096enum blackbird_picture_mask {
97 BLACKBIRD_PICTURE_MASK_NONE,
98 BLACKBIRD_PICTURE_MASK_I_FRAMES,
99 BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
100 BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
101};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700102enum blackbird_vbi_mode_bits {
103 BLACKBIRD_VBI_BITS_SLICED,
104 BLACKBIRD_VBI_BITS_RAW,
105};
106enum blackbird_vbi_insertion_bits {
107 BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
108 BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
109 BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
110 BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
111 BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
112};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700113enum blackbird_dma_unit {
114 BLACKBIRD_DMA_BYTES,
115 BLACKBIRD_DMA_FRAMES,
116};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700117enum blackbird_dma_transfer_status_bits {
118 BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
119 BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
120 BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
121};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700122enum blackbird_pause {
123 BLACKBIRD_PAUSE_ENCODING,
124 BLACKBIRD_RESUME_ENCODING,
125};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700126enum blackbird_copyright {
127 BLACKBIRD_COPYRIGHT_OFF,
128 BLACKBIRD_COPYRIGHT_ON,
129};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700130enum blackbird_notification_type {
131 BLACKBIRD_NOTIFICATION_REFRESH,
132};
133enum blackbird_notification_status {
134 BLACKBIRD_NOTIFICATION_OFF,
135 BLACKBIRD_NOTIFICATION_ON,
136};
137enum blackbird_notification_mailbox {
138 BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
139};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700140enum blackbird_field1_lines {
141 BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
142 BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
143 BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
144};
145enum blackbird_field2_lines {
146 BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
147 BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
148 BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
149};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700150enum blackbird_custom_data_type {
151 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
152 BLACKBIRD_CUSTOM_PRIVATE_PACKET,
153};
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700154enum blackbird_mute {
155 BLACKBIRD_UNMUTE,
156 BLACKBIRD_MUTE,
157};
158enum blackbird_mute_video_mask {
159 BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
160 BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
161 BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
162};
163enum blackbird_mute_video_shift {
164 BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
165 BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
166 BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
167};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/* Registers */
170#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
171#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
172#define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
173#define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
174#define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
175#define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
176
177/* ------------------------------------------------------------------ */
178
179static void host_setup(struct cx88_core *core)
180{
181 /* toggle reset of the host */
182 cx_write(MO_GPHST_SOFT_RST, 1);
183 udelay(100);
184 cx_write(MO_GPHST_SOFT_RST, 0);
185 udelay(100);
186
187 /* host port setup */
188 cx_write(MO_GPHST_WSC, 0x44444444U);
189 cx_write(MO_GPHST_XFR, 0);
190 cx_write(MO_GPHST_WDTH, 15);
191 cx_write(MO_GPHST_HDSHK, 0);
192 cx_write(MO_GPHST_MUX16, 0x44448888U);
193 cx_write(MO_GPHST_MODE, 0);
194}
195
196/* ------------------------------------------------------------------ */
197
198#define P1_MDATA0 0x390000
199#define P1_MDATA1 0x390001
200#define P1_MDATA2 0x390002
201#define P1_MDATA3 0x390003
202#define P1_MADDR2 0x390004
203#define P1_MADDR1 0x390005
204#define P1_MADDR0 0x390006
205#define P1_RDATA0 0x390008
206#define P1_RDATA1 0x390009
207#define P1_RDATA2 0x39000A
208#define P1_RDATA3 0x39000B
209#define P1_RADDR0 0x39000C
210#define P1_RADDR1 0x39000D
211#define P1_RRDWR 0x39000E
212
213static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
214{
215 unsigned long timeout = jiffies + msecs_to_jiffies(1);
216 u32 gpio0,need;
217
218 need = state ? 2 : 0;
219 for (;;) {
220 gpio0 = cx_read(MO_GP0_IO) & 2;
221 if (need == gpio0)
222 return 0;
223 if (time_after(jiffies,timeout))
224 return -1;
225 udelay(1);
226 }
227}
228
229static int memory_write(struct cx88_core *core, u32 address, u32 value)
230{
231 /* Warning: address is dword address (4 bytes) */
232 cx_writeb(P1_MDATA0, (unsigned int)value);
233 cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
234 cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
235 cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
236 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
237 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
238 cx_writeb(P1_MADDR0, (unsigned int)address);
239 cx_read(P1_MDATA0);
240 cx_read(P1_MADDR0);
241
242 return wait_ready_gpio0_bit1(core,1);
243}
244
245static int memory_read(struct cx88_core *core, u32 address, u32 *value)
246{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800247 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 u32 val;
249
250 /* Warning: address is dword address (4 bytes) */
251 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
252 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
253 cx_writeb(P1_MADDR0, (unsigned int)address);
254 cx_read(P1_MADDR0);
255
256 retval = wait_ready_gpio0_bit1(core,1);
257
258 cx_writeb(P1_MDATA3, 0);
259 val = (unsigned char)cx_read(P1_MDATA3) << 24;
260 cx_writeb(P1_MDATA2, 0);
261 val |= (unsigned char)cx_read(P1_MDATA2) << 16;
262 cx_writeb(P1_MDATA1, 0);
263 val |= (unsigned char)cx_read(P1_MDATA1) << 8;
264 cx_writeb(P1_MDATA0, 0);
265 val |= (unsigned char)cx_read(P1_MDATA0);
266
267 *value = val;
268 return retval;
269}
270
271static int register_write(struct cx88_core *core, u32 address, u32 value)
272{
273 cx_writeb(P1_RDATA0, (unsigned int)value);
274 cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
275 cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
276 cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
277 cx_writeb(P1_RADDR0, (unsigned int)address);
278 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
279 cx_writeb(P1_RRDWR, 1);
280 cx_read(P1_RDATA0);
281 cx_read(P1_RADDR0);
282
283 return wait_ready_gpio0_bit1(core,1);
284}
285
286
287static int register_read(struct cx88_core *core, u32 address, u32 *value)
288{
289 int retval;
290 u32 val;
291
292 cx_writeb(P1_RADDR0, (unsigned int)address);
293 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
294 cx_writeb(P1_RRDWR, 0);
295 cx_read(P1_RADDR0);
296
297 retval = wait_ready_gpio0_bit1(core,1);
298 val = (unsigned char)cx_read(P1_RDATA0);
299 val |= (unsigned char)cx_read(P1_RDATA1) << 8;
300 val |= (unsigned char)cx_read(P1_RDATA2) << 16;
301 val |= (unsigned char)cx_read(P1_RDATA3) << 24;
302
303 *value = val;
304 return retval;
305}
306
307/* ------------------------------------------------------------------ */
308
Hans Verkuilf0221562006-06-18 16:11:06 -0300309static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Hans Verkuilf0221562006-06-18 16:11:06 -0300311 struct cx8802_dev *dev = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned long timeout;
313 u32 value, flag, retval;
314 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
317
318 /* this may not be 100% safe if we can't read any memory location
319 without side effects */
320 memory_read(dev->core, dev->mailbox - 4, &value);
321 if (value != 0x12345678) {
322 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
323 return -1;
324 }
325
326 memory_read(dev->core, dev->mailbox, &flag);
327 if (flag) {
328 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
329 return -1;
330 }
331
332 flag |= 1; /* tell 'em we're working on it */
333 memory_write(dev->core, dev->mailbox, flag);
334
335 /* write command + args + fill remaining with zeros */
336 memory_write(dev->core, dev->mailbox + 1, command); /* command code */
337 memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
Hans Verkuilf0221562006-06-18 16:11:06 -0300338 for (i = 0; i < in; i++) {
339 memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
340 dprintk(1, "API Input %d = %d\n", i, data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Hans Verkuilf0221562006-06-18 16:11:06 -0300342 for (; i < CX2341X_MBOX_MAX_DATA; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 memory_write(dev->core, dev->mailbox + 4 + i, 0);
344
345 flag |= 3; /* tell 'em we're done writing */
346 memory_write(dev->core, dev->mailbox, flag);
347
348 /* wait for firmware to handle the API command */
349 timeout = jiffies + msecs_to_jiffies(10);
350 for (;;) {
351 memory_read(dev->core, dev->mailbox, &flag);
352 if (0 != (flag & 4))
353 break;
354 if (time_after(jiffies,timeout)) {
355 dprintk(0, "ERROR: API Mailbox timeout\n");
356 return -1;
357 }
358 udelay(10);
359 }
360
361 /* read output values */
Hans Verkuilf0221562006-06-18 16:11:06 -0300362 for (i = 0; i < out; i++) {
363 memory_read(dev->core, dev->mailbox + 4 + i, data + i);
364 dprintk(1, "API Output %d = %d\n", i, data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 memory_read(dev->core, dev->mailbox + 2, &retval);
368 dprintk(1, "API result = %d\n",retval);
369
370 flag = 0;
371 memory_write(dev->core, dev->mailbox, flag);
372 return retval;
373}
Hans Verkuilf0221562006-06-18 16:11:06 -0300374/* ------------------------------------------------------------------ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Hans Verkuilf0221562006-06-18 16:11:06 -0300376/* We don't need to call the API often, so using just one mailbox will probably suffice */
377static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
378 u32 inputcnt, u32 outputcnt, ...)
379{
380 u32 data[CX2341X_MBOX_MAX_DATA];
381 va_list vargs;
382 int i, err;
383
384 va_start(vargs, outputcnt);
385
386 for (i = 0; i < inputcnt; i++) {
387 data[i] = va_arg(vargs, int);
388 }
389 err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
390 for (i = 0; i < outputcnt; i++) {
391 int *vptr = va_arg(vargs, int *);
392 *vptr = data[i];
393 }
394 va_end(vargs);
395 return err;
396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398static int blackbird_find_mailbox(struct cx8802_dev *dev)
399{
400 u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
401 int signaturecnt=0;
402 u32 value;
403 int i;
404
405 for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
406 memory_read(dev->core, i, &value);
407 if (value == signature[signaturecnt])
408 signaturecnt++;
409 else
410 signaturecnt = 0;
411 if (4 == signaturecnt) {
412 dprintk(1, "Mailbox signature found\n");
413 return i+1;
414 }
415 }
416 dprintk(0, "Mailbox signature values not found!\n");
417 return -1;
418}
419
420static int blackbird_load_firmware(struct cx8802_dev *dev)
421{
422 static const unsigned char magic[8] = {
423 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
424 };
425 const struct firmware *firmware;
426 int i, retval = 0;
427 u32 value = 0;
428 u32 checksum = 0;
429 u32 *dataptr;
430
431 retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800432 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
433 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
434 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 msleep(1);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800436 retval |= register_write(dev->core, IVTV_REG_APU, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (retval < 0)
439 dprintk(0, "Error with register_write\n");
440
Michael Krufky48c35752006-05-22 10:32:00 -0300441 retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 &dev->pci->dev);
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800443
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (retval != 0) {
446 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
Michael Krufky48c35752006-05-22 10:32:00 -0300447 CX2341X_FIRM_ENC_FILENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 dprintk(0, "Please fix your hotplug setup, the board will "
449 "not work without firmware loaded!\n");
450 return -1;
451 }
452
453 if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
454 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
455 firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
456 return -1;
457 }
458
459 if (0 != memcmp(firmware->data, magic, 8)) {
460 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
461 return -1;
462 }
463
464 /* transfer to the chip */
465 dprintk(1,"Loading firmware ...\n");
466 dataptr = (u32*)firmware->data;
467 for (i = 0; i < (firmware->size >> 2); i++) {
468 value = *dataptr;
469 checksum += ~value;
470 memory_write(dev->core, i, value);
471 dataptr++;
472 }
473
474 /* read back to verify with the checksum */
475 for (i--; i >= 0; i--) {
476 memory_read(dev->core, i, &value);
477 checksum -= ~value;
478 }
479 if (checksum) {
480 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
481 return -1;
482 }
483 release_firmware(firmware);
484 dprintk(0, "Firmware upload successful.\n");
485
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800486 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
487 retval |= register_read(dev->core, IVTV_REG_SPU, &value);
488 retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 msleep(1);
490
491 retval |= register_read(dev->core, IVTV_REG_VPU, &value);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800492 retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (retval < 0)
495 dprintk(0, "Error with register_write\n");
496 return 0;
497}
498
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700499/**
500 Settings used by the windows tv app for PVR2000:
501=================================================================================================================
502Profile | Codec | Resolution | CBR/VBR | Video Qlty | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
503-----------------------------------------------------------------------------------------------------------------
504MPEG-1 | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 2000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
505MPEG-2 | MPEG2 | 720x576PAL | VBR | 600 :Good | 4000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
506VCD | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 1150 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
507DVD | MPEG2 | 720x576PAL | VBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
508DB* DVD | MPEG2 | 720x576PAL | CBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
509=================================================================================================================
510*DB: "DirectBurn"
511*/
Catalin Climov31629422005-11-08 21:36:17 -0800512
Hans Verkuilf0221562006-06-18 16:11:06 -0300513static void blackbird_codec_settings(struct cx8802_dev *dev)
514{
515 /* assign frame size */
516 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
517 dev->height, dev->width);
518
519 dev->params.width = dev->width;
520 dev->params.height = dev->height;
521 dev->params.is_50hz = (dev->core->tvnorm->id & V4L2_STD_625_50) != 0;
522
523 cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params);
524}
525
Catalin Climov31629422005-11-08 21:36:17 -0800526static struct v4l2_mpeg_compression default_mpeg_params = {
527 .st_type = V4L2_MPEG_PS_2,
528 .st_bitrate = {
529 .mode = V4L2_BITRATE_CBR,
530 .min = 0,
531 .target = 0,
532 .max = 0
533 },
534 .ts_pid_pmt = 16,
535 .ts_pid_audio = 260,
536 .ts_pid_video = 256,
537 .ts_pid_pcr = 259,
538 .ps_size = 0,
539 .au_type = V4L2_MPEG_AU_2_II,
540 .au_bitrate = {
541 .mode = V4L2_BITRATE_CBR,
542 .min = 224,
543 .target = 224,
544 .max = 224
545 },
Hans Verkuilf0221562006-06-18 16:11:06 -0300546 .au_sample_rate = 48000,
Catalin Climov31629422005-11-08 21:36:17 -0800547 .au_pesid = 0,
548 .vi_type = V4L2_MPEG_VI_2,
549 .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
550 .vi_bitrate = {
551 .mode = V4L2_BITRATE_CBR,
552 .min = 4000,
553 .target = 4500,
554 .max = 6000
555 },
556 .vi_frame_rate = 25,
Hans Verkuilf0221562006-06-18 16:11:06 -0300557 .vi_frames_per_gop = 12,
Catalin Climov31629422005-11-08 21:36:17 -0800558 .vi_bframes_count = 2,
559 .vi_pesid = 0,
Hans Verkuilf0221562006-06-18 16:11:06 -0300560 .closed_gops = 1,
Catalin Climov31629422005-11-08 21:36:17 -0800561 .pulldown = 0
562};
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static int blackbird_initialize_codec(struct cx8802_dev *dev)
565{
566 struct cx88_core *core = dev->core;
567 int version;
568 int retval;
569
570 dprintk(1,"Initialize codec\n");
Michael Krufky855dbad2006-05-22 10:31:54 -0300571 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (retval < 0) {
573 /* ping was not successful, reset and upload firmware */
574 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
575 msleep(1);
576 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
577 msleep(1);
578 retval = blackbird_load_firmware(dev);
579 if (retval < 0)
580 return retval;
581
582 dev->mailbox = blackbird_find_mailbox(dev);
583 if (dev->mailbox < 0)
584 return -1;
585
Michael Krufky855dbad2006-05-22 10:31:54 -0300586 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (retval < 0) {
588 dprintk(0, "ERROR: Firmware ping failed!\n");
589 return -1;
590 }
591
Michael Krufky855dbad2006-05-22 10:31:54 -0300592 retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (retval < 0) {
594 dprintk(0, "ERROR: Firmware get encoder version failed!\n");
595 return -1;
596 }
597 dprintk(0, "Firmware version is 0x%08x\n", version);
598 }
599 msleep(1);
600
601 cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
602 cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
603 cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
604 cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 blackbird_codec_settings(dev);
607 msleep(1);
608
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700609 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
610 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
611 blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
Michael Krufky855dbad2006-05-22 10:31:54 -0300612 blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700613 BLACKBIRD_FIELD1_SAA7115,
Michael Krufky8a17ef92006-04-13 18:43:50 -0300614 BLACKBIRD_FIELD2_SAA7115
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700615 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700617 /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
Michael Krufky855dbad2006-05-22 10:31:54 -0300618 blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700619 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
620 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
621
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700622 /* initialize the video input */
Michael Krufky855dbad2006-05-22 10:31:54 -0300623 blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 msleep(1);
626
Michael Krufky855dbad2006-05-22 10:31:54 -0300627 blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 msleep(1);
Michael Krufky855dbad2006-05-22 10:31:54 -0300629 blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 msleep(1);
631
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700632 /* start capturing to the host interface */
Michael Krufky855dbad2006-05-22 10:31:54 -0300633 /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
634 blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700635 BLACKBIRD_MPEG_CAPTURE,
636 BLACKBIRD_RAW_BITS_NONE
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700637 );
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700638 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Michael Krufky855dbad2006-05-22 10:31:54 -0300640 blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642}
643
644/* ------------------------------------------------------------------ */
645
646static int bb_buf_setup(struct videobuf_queue *q,
647 unsigned int *count, unsigned int *size)
648{
649 struct cx8802_fh *fh = q->priv_data;
650
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700651 fh->dev->ts_packet_size = 188 * 4; /* was: 512 */
Catalin Climov31629422005-11-08 21:36:17 -0800652 fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
Catalin Climov31629422005-11-08 21:36:17 -0800655 *count = fh->dev->ts_packet_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657}
658
659static int
660bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
661 enum v4l2_field field)
662{
663 struct cx8802_fh *fh = q->priv_data;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300664 return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static void
668bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
669{
670 struct cx8802_fh *fh = q->priv_data;
671 cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
672}
673
674static void
675bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
676{
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300677 cx88_free_buffer(q, (struct cx88_buffer*)vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680static struct videobuf_queue_ops blackbird_qops = {
681 .buf_setup = bb_buf_setup,
682 .buf_prepare = bb_buf_prepare,
683 .buf_queue = bb_buf_queue,
684 .buf_release = bb_buf_release,
685};
686
687/* ------------------------------------------------------------------ */
688
Michael Krufky38a27132006-06-26 23:42:39 -0300689static const u32 *ctrl_classes[] = {
690 cx88_user_ctrls,
691 cx2341x_mpeg_ctrls,
692 NULL
693};
694
695static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qctrl)
696{
697 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
698 if (qctrl->id == 0)
699 return -EINVAL;
700
701 /* Standard V4L2 controls */
702 if (cx8800_ctrl_query(qctrl) == 0)
703 return 0;
704
705 /* MPEG V4L2 controls */
706 if (cx2341x_ctrl_query(&dev->params, qctrl))
707 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
708 return 0;
709}
710
711static int blackbird_querymenu(struct cx8802_dev *dev, struct v4l2_querymenu *qmenu)
712{
713 struct v4l2_queryctrl qctrl;
714
715 qctrl.id = qmenu->id;
716 blackbird_queryctrl(dev, &qctrl);
717 return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
718}
719
720/* ------------------------------------------------------------------ */
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static int mpeg_do_ioctl(struct inode *inode, struct file *file,
723 unsigned int cmd, void *arg)
724{
725 struct cx8802_fh *fh = file->private_data;
726 struct cx8802_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700727 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (debug > 1)
Michael Krufky5e453dc2006-01-09 15:32:31 -0200730 v4l_print_ioctl(core->name,cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 switch (cmd) {
733
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700734 /* --- capabilities ------------------------------------------ */
735 case VIDIOC_QUERYCAP:
736 {
737 struct v4l2_capability *cap = arg;
738
739 memset(cap,0,sizeof(*cap));
740 strcpy(cap->driver, "cx88_blackbird");
741 strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card));
742 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
743 cap->version = CX88_VERSION_CODE;
744 cap->capabilities =
745 V4L2_CAP_VIDEO_CAPTURE |
746 V4L2_CAP_READWRITE |
747 V4L2_CAP_STREAMING |
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700748 0;
749 if (UNSET != core->tuner_type)
750 cap->capabilities |= V4L2_CAP_TUNER;
751
752 return 0;
753 }
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* --- capture ioctls ---------------------------------------- */
756 case VIDIOC_ENUM_FMT:
757 {
758 struct v4l2_fmtdesc *f = arg;
759 int index;
760
761 index = f->index;
762 if (index != 0)
763 return -EINVAL;
764
765 memset(f,0,sizeof(*f));
766 f->index = index;
Catalin Climov31629422005-11-08 21:36:17 -0800767 strlcpy(f->description, "MPEG", sizeof(f->description));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
769 f->pixelformat = V4L2_PIX_FMT_MPEG;
770 return 0;
771 }
772 case VIDIOC_G_FMT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 struct v4l2_format *f = arg;
775
776 memset(f,0,sizeof(*f));
777 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Catalin Climov31629422005-11-08 21:36:17 -0800778 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
779 f->fmt.pix.bytesperline = 0;
780 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
781 f->fmt.pix.colorspace = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 f->fmt.pix.width = dev->width;
783 f->fmt.pix.height = dev->height;
Catalin Climov31629422005-11-08 21:36:17 -0800784 f->fmt.pix.field = fh->mpegq.field;
785 dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
786 dev->width, dev->height, fh->mpegq.field );
787 return 0;
788 }
789 case VIDIOC_TRY_FMT:
790 {
791 struct v4l2_format *f = arg;
792
793 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700795 f->fmt.pix.bytesperline = 0;
Catalin Climov31629422005-11-08 21:36:17 -0800796 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700797 f->fmt.pix.colorspace = 0;
Catalin Climov31629422005-11-08 21:36:17 -0800798 dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
799 dev->width, dev->height, fh->mpegq.field );
800 return 0;
801 }
802 case VIDIOC_S_FMT:
803 {
804 struct v4l2_format *f = arg;
805
806 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
807 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
808 f->fmt.pix.bytesperline = 0;
809 f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
810 f->fmt.pix.colorspace = 0;
811 dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
812 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 /* --- streaming capture ------------------------------------- */
817 case VIDIOC_REQBUFS:
818 return videobuf_reqbufs(&fh->mpegq, arg);
819
820 case VIDIOC_QUERYBUF:
821 return videobuf_querybuf(&fh->mpegq, arg);
822
823 case VIDIOC_QBUF:
824 return videobuf_qbuf(&fh->mpegq, arg);
825
826 case VIDIOC_DQBUF:
827 return videobuf_dqbuf(&fh->mpegq, arg,
828 file->f_flags & O_NONBLOCK);
829
830 case VIDIOC_STREAMON:
831 return videobuf_streamon(&fh->mpegq);
832
833 case VIDIOC_STREAMOFF:
834 return videobuf_streamoff(&fh->mpegq);
835
Catalin Climov31629422005-11-08 21:36:17 -0800836 /* --- mpeg compression -------------------------------------- */
837 case VIDIOC_G_MPEGCOMP:
838 {
839 struct v4l2_mpeg_compression *f = arg;
840
Hans Verkuilf81cf752006-06-18 16:54:20 -0300841 printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. "
842 "Replace with VIDIOC_G_EXT_CTRLS!");
Hans Verkuilf0221562006-06-18 16:11:06 -0300843 memcpy(f,&default_mpeg_params,sizeof(*f));
Catalin Climov31629422005-11-08 21:36:17 -0800844 return 0;
845 }
846 case VIDIOC_S_MPEGCOMP:
Hans Verkuilf81cf752006-06-18 16:54:20 -0300847 printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. "
848 "Replace with VIDIOC_S_EXT_CTRLS!");
Catalin Climov31629422005-11-08 21:36:17 -0800849 return 0;
Hans Verkuilf0221562006-06-18 16:11:06 -0300850 case VIDIOC_G_EXT_CTRLS:
851 {
852 struct v4l2_ext_controls *f = arg;
853
854 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
855 return -EINVAL;
856 return cx2341x_ext_ctrls(&dev->params, f, cmd);
857 }
858 case VIDIOC_S_EXT_CTRLS:
859 case VIDIOC_TRY_EXT_CTRLS:
860 {
861 struct v4l2_ext_controls *f = arg;
862 struct cx2341x_mpeg_params p;
863 int err;
864
865 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
866 return -EINVAL;
867 p = dev->params;
868 err = cx2341x_ext_ctrls(&p, f, cmd);
869 if (err == 0 && cmd == VIDIOC_S_EXT_CTRLS) {
870 err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
871 dev->params = p;
872 }
873 return err;
Catalin Climov31629422005-11-08 21:36:17 -0800874 }
Valentin Zagura2544bf22006-05-22 10:31:59 -0300875 case VIDIOC_S_FREQUENCY:
876 {
877 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
878 BLACKBIRD_END_NOW,
879 BLACKBIRD_MPEG_CAPTURE,
880 BLACKBIRD_RAW_BITS_NONE);
881
Adrian Bunkdff47bf2006-06-24 19:25:43 -0300882 cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, mpeg_do_ioctl);
Valentin Zagura2544bf22006-05-22 10:31:59 -0300883
884 blackbird_initialize_codec(dev);
885 cx88_set_scale(dev->core, dev->width, dev->height,
886 fh->mpegq.field);
887 return 0;
888 }
Michael Krufky4656fb62006-06-25 18:35:24 -0300889 case VIDIOC_LOG_STATUS:
Hans Verkuil99eb44f2006-06-26 18:24:05 -0300890 {
891 char name[32 + 2];
892
893 snprintf(name, sizeof(name), "%s/2", core->name);
Michael Krufky4656fb62006-06-25 18:35:24 -0300894 printk("%s/2: ============ START LOG STATUS ============\n",
895 core->name);
896 cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, 0);
Hans Verkuil99eb44f2006-06-26 18:24:05 -0300897 cx2341x_log_status(&dev->params, name);
Michael Krufky4656fb62006-06-25 18:35:24 -0300898 printk("%s/2: ============= END LOG STATUS =============\n",
899 core->name);
900 return 0;
Hans Verkuil99eb44f2006-06-26 18:24:05 -0300901 }
Michael Krufky38a27132006-06-26 23:42:39 -0300902 case VIDIOC_QUERYMENU:
903 return blackbird_querymenu(dev, arg);
904 case VIDIOC_QUERYCTRL:
905 {
906 struct v4l2_queryctrl *c = arg;
907
908 if (blackbird_queryctrl(dev, c) == 0)
909 return 0;
910 return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, mpeg_do_ioctl);
911 }
Catalin Climov31629422005-11-08 21:36:17 -0800912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 default:
Adrian Bunkdff47bf2006-06-24 19:25:43 -0300914 return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, mpeg_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 return 0;
917}
918
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700919static unsigned int mpeg_translate_ioctl(unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700921 return cmd;
922}
923
924static int mpeg_ioctl(struct inode *inode, struct file *file,
925 unsigned int cmd, unsigned long arg)
926{
Adrian Bunkdff47bf2006-06-24 19:25:43 -0300927 cmd = mpeg_translate_ioctl( cmd );
928 return video_usercopy(inode, file, cmd, arg, mpeg_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931static int mpeg_open(struct inode *inode, struct file *file)
932{
933 int minor = iminor(inode);
934 struct cx8802_dev *h,*dev = NULL;
935 struct cx8802_fh *fh;
936 struct list_head *list;
937
938 list_for_each(list,&cx8802_devlist) {
939 h = list_entry(list, struct cx8802_dev, devlist);
940 if (h->mpeg_dev->minor == minor)
941 dev = h;
942 }
943 if (NULL == dev)
944 return -ENODEV;
945
946 if (blackbird_initialize_codec(dev) < 0)
947 return -EINVAL;
948 dprintk(1,"open minor=%d\n",minor);
949
950 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200951 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (NULL == fh)
953 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 file->private_data = fh;
955 fh->dev = dev;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 videobuf_queue_init(&fh->mpegq, &blackbird_qops,
958 dev->pci, &dev->slock,
959 V4L2_BUF_TYPE_VIDEO_CAPTURE,
Catalin Climov31629422005-11-08 21:36:17 -0800960 V4L2_FIELD_INTERLACED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 sizeof(struct cx88_buffer),
962 fh);
Catalin Climov31629422005-11-08 21:36:17 -0800963
964 /* FIXME: locking against other video device */
965 cx88_set_scale(dev->core, dev->width, dev->height,
966 fh->mpegq.field);
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969}
970
971static int mpeg_release(struct inode *inode, struct file *file)
972{
973 struct cx8802_fh *fh = file->private_data;
974
Michael Krufky855dbad2006-05-22 10:31:54 -0300975 /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
976 blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700977 BLACKBIRD_END_NOW,
978 BLACKBIRD_MPEG_CAPTURE,
979 BLACKBIRD_RAW_BITS_NONE
980 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Valentin Zagura0b5f56d2006-04-20 22:56:25 -0300982 cx8802_cancel_buffers(fh->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* stop mpeg capture */
984 if (fh->mpegq.streaming)
985 videobuf_streamoff(&fh->mpegq);
986 if (fh->mpegq.reading)
987 videobuf_read_stop(&fh->mpegq);
988
989 videobuf_mmap_free(&fh->mpegq);
990 file->private_data = NULL;
991 kfree(fh);
992 return 0;
993}
994
995static ssize_t
996mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
997{
998 struct cx8802_fh *fh = file->private_data;
999
1000 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1001 file->f_flags & O_NONBLOCK);
1002}
1003
1004static unsigned int
1005mpeg_poll(struct file *file, struct poll_table_struct *wait)
1006{
1007 struct cx8802_fh *fh = file->private_data;
1008
1009 return videobuf_poll_stream(file, &fh->mpegq, wait);
1010}
1011
1012static int
1013mpeg_mmap(struct file *file, struct vm_area_struct * vma)
1014{
1015 struct cx8802_fh *fh = file->private_data;
1016
1017 return videobuf_mmap_mapper(&fh->mpegq, vma);
1018}
1019
1020static struct file_operations mpeg_fops =
1021{
1022 .owner = THIS_MODULE,
1023 .open = mpeg_open,
1024 .release = mpeg_release,
1025 .read = mpeg_read,
1026 .poll = mpeg_poll,
1027 .mmap = mpeg_mmap,
1028 .ioctl = mpeg_ioctl,
1029 .llseek = no_llseek,
1030};
1031
1032static struct video_device cx8802_mpeg_template =
1033{
1034 .name = "cx8802",
1035 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
1036 .hardware = 0,
1037 .fops = &mpeg_fops,
1038 .minor = -1,
1039};
1040
1041/* ------------------------------------------------------------------ */
1042
1043static void blackbird_unregister_video(struct cx8802_dev *dev)
1044{
1045 if (dev->mpeg_dev) {
1046 if (-1 != dev->mpeg_dev->minor)
1047 video_unregister_device(dev->mpeg_dev);
1048 else
1049 video_device_release(dev->mpeg_dev);
1050 dev->mpeg_dev = NULL;
1051 }
1052}
1053
1054static int blackbird_register_video(struct cx8802_dev *dev)
1055{
1056 int err;
1057
1058 dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
1059 &cx8802_mpeg_template,"mpeg");
1060 err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
1061 if (err < 0) {
1062 printk(KERN_INFO "%s/2: can't register mpeg device\n",
1063 dev->core->name);
1064 return err;
1065 }
1066 printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
1067 dev->core->name,dev->mpeg_dev->minor & 0x1f);
1068 return 0;
1069}
1070
1071/* ----------------------------------------------------------- */
1072
1073static int __devinit blackbird_probe(struct pci_dev *pci_dev,
1074 const struct pci_device_id *pci_id)
1075{
1076 struct cx8802_dev *dev;
1077 struct cx88_core *core;
1078 int err;
1079
1080 /* general setup */
1081 core = cx88_core_get(pci_dev);
1082 if (NULL == core)
1083 return -EINVAL;
1084
1085 err = -ENODEV;
1086 if (!cx88_boards[core->board].blackbird)
1087 goto fail_core;
1088
1089 err = -ENOMEM;
Panagiotis Issaris74081872006-01-11 19:40:56 -02001090 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (NULL == dev)
1092 goto fail_core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 dev->pci = pci_dev;
1094 dev->core = core;
1095 dev->width = 720;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001096 dev->height = 576;
Hans Verkuilf0221562006-06-18 16:11:06 -03001097 cx2341x_fill_defaults(&dev->params);
Hans Verkuil45ad9f82006-06-21 17:04:13 -03001098 dev->params.port = CX2341X_PORT_STREAMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Michael Krufky45f87a22006-06-22 21:47:07 -03001100 if (core->tvnorm->id & V4L2_STD_525_60) {
1101 dev->height = 480;
1102 } else {
1103 dev->height = 576;
Steven Toth0345c382006-01-09 15:25:17 -02001104 }
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 err = cx8802_init_common(dev);
1107 if (0 != err)
1108 goto fail_free;
1109
1110 /* blackbird stuff */
1111 printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1112 core->name);
1113 host_setup(dev->core);
1114
1115 list_add_tail(&dev->devlist,&cx8802_devlist);
1116 blackbird_register_video(dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001117
1118 /* initial device configuration: needed ? */
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return 0;
1121
1122 fail_free:
1123 kfree(dev);
1124 fail_core:
1125 cx88_core_put(core,pci_dev);
1126 return err;
1127}
1128
1129static void __devexit blackbird_remove(struct pci_dev *pci_dev)
1130{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001131 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 /* blackbird */
1134 blackbird_unregister_video(dev);
1135 list_del(&dev->devlist);
1136
1137 /* common */
1138 cx8802_fini_common(dev);
1139 cx88_core_put(dev->core,dev->pci);
1140 kfree(dev);
1141}
1142
1143static struct pci_device_id cx8802_pci_tbl[] = {
1144 {
1145 .vendor = 0x14f1,
1146 .device = 0x8802,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001147 .subvendor = PCI_ANY_ID,
1148 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 },{
1150 /* --- end of list --- */
1151 }
1152};
1153MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
1154
1155static struct pci_driver blackbird_pci_driver = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001156 .name = "cx88-blackbird",
1157 .id_table = cx8802_pci_tbl,
1158 .probe = blackbird_probe,
1159 .remove = __devexit_p(blackbird_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 .suspend = cx8802_suspend_common,
1161 .resume = cx8802_resume_common,
1162};
1163
1164static int blackbird_init(void)
1165{
1166 printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
1167 (CX88_VERSION_CODE >> 16) & 0xff,
1168 (CX88_VERSION_CODE >> 8) & 0xff,
1169 CX88_VERSION_CODE & 0xff);
1170#ifdef SNAPSHOT
1171 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
1172 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1173#endif
1174 return pci_register_driver(&blackbird_pci_driver);
1175}
1176
1177static void blackbird_fini(void)
1178{
1179 pci_unregister_driver(&blackbird_pci_driver);
1180}
1181
1182module_init(blackbird_init);
1183module_exit(blackbird_fini);
1184
1185/* ----------------------------------------------------------- */
1186/*
1187 * Local variables:
1188 * c-basic-offset: 8
1189 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001190 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 */