blob: 3dbc074fb515a10c62783e1bda568b63a8a1c596 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
5 *
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/kmod.h>
28#include <linux/kernel.h>
29#include <linux/slab.h>
30#include <linux/interrupt.h>
31#include <linux/delay.h>
32#include <linux/kthread.h>
33#include <asm/div64.h>
34
35#include "cx88.h"
36
37MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
38MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
39MODULE_LICENSE("GPL");
40
41/* ------------------------------------------------------------------ */
42
43static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
44static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
45static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
46
47module_param_array(video_nr, int, NULL, 0444);
48module_param_array(vbi_nr, int, NULL, 0444);
49module_param_array(radio_nr, int, NULL, 0444);
50
51MODULE_PARM_DESC(video_nr,"video device numbers");
52MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
53MODULE_PARM_DESC(radio_nr,"radio device numbers");
54
55static unsigned int video_debug = 0;
56module_param(video_debug,int,0644);
57MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
58
59static unsigned int irq_debug = 0;
60module_param(irq_debug,int,0644);
61MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
62
63static unsigned int vid_limit = 16;
64module_param(vid_limit,int,0644);
65MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
66
67#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070068 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* ------------------------------------------------------------------ */
71
72static LIST_HEAD(cx8800_devlist);
73
74/* ------------------------------------------------------------------- */
75/* static data */
76
77static struct cx88_tvnorm tvnorms[] = {
78 {
79 .name = "NTSC-M",
80 .id = V4L2_STD_NTSC_M,
81 .cxiformat = VideoFormatNTSC,
82 .cxoformat = 0x181f0008,
83 },{
84 .name = "NTSC-JP",
85 .id = V4L2_STD_NTSC_M_JP,
86 .cxiformat = VideoFormatNTSCJapan,
87 .cxoformat = 0x181f0008,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 },{
89 .name = "PAL-BG",
90 .id = V4L2_STD_PAL_BG,
91 .cxiformat = VideoFormatPAL,
92 .cxoformat = 0x181f0008,
93 },{
94 .name = "PAL-DK",
95 .id = V4L2_STD_PAL_DK,
96 .cxiformat = VideoFormatPAL,
97 .cxoformat = 0x181f0008,
98 },{
99 .name = "PAL-I",
100 .id = V4L2_STD_PAL_I,
101 .cxiformat = VideoFormatPAL,
102 .cxoformat = 0x181f0008,
103 },{
104 .name = "PAL-M",
105 .id = V4L2_STD_PAL_M,
106 .cxiformat = VideoFormatPALM,
107 .cxoformat = 0x1c1f0008,
108 },{
109 .name = "PAL-N",
110 .id = V4L2_STD_PAL_N,
111 .cxiformat = VideoFormatPALN,
112 .cxoformat = 0x1c1f0008,
113 },{
114 .name = "PAL-Nc",
115 .id = V4L2_STD_PAL_Nc,
116 .cxiformat = VideoFormatPALNC,
117 .cxoformat = 0x1c1f0008,
118 },{
119 .name = "PAL-60",
120 .id = V4L2_STD_PAL_60,
121 .cxiformat = VideoFormatPAL60,
122 .cxoformat = 0x181f0008,
123 },{
124 .name = "SECAM-L",
125 .id = V4L2_STD_SECAM_L,
126 .cxiformat = VideoFormatSECAM,
127 .cxoformat = 0x181f0008,
128 },{
129 .name = "SECAM-DK",
130 .id = V4L2_STD_SECAM_DK,
131 .cxiformat = VideoFormatSECAM,
132 .cxoformat = 0x181f0008,
133 }
134};
135
136static struct cx8800_fmt formats[] = {
137 {
138 .name = "8 bpp, gray",
139 .fourcc = V4L2_PIX_FMT_GREY,
140 .cxformat = ColorFormatY8,
141 .depth = 8,
142 .flags = FORMAT_FLAGS_PACKED,
143 },{
144 .name = "15 bpp RGB, le",
145 .fourcc = V4L2_PIX_FMT_RGB555,
146 .cxformat = ColorFormatRGB15,
147 .depth = 16,
148 .flags = FORMAT_FLAGS_PACKED,
149 },{
150 .name = "15 bpp RGB, be",
151 .fourcc = V4L2_PIX_FMT_RGB555X,
152 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
153 .depth = 16,
154 .flags = FORMAT_FLAGS_PACKED,
155 },{
156 .name = "16 bpp RGB, le",
157 .fourcc = V4L2_PIX_FMT_RGB565,
158 .cxformat = ColorFormatRGB16,
159 .depth = 16,
160 .flags = FORMAT_FLAGS_PACKED,
161 },{
162 .name = "16 bpp RGB, be",
163 .fourcc = V4L2_PIX_FMT_RGB565X,
164 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
165 .depth = 16,
166 .flags = FORMAT_FLAGS_PACKED,
167 },{
168 .name = "24 bpp RGB, le",
169 .fourcc = V4L2_PIX_FMT_BGR24,
170 .cxformat = ColorFormatRGB24,
171 .depth = 24,
172 .flags = FORMAT_FLAGS_PACKED,
173 },{
174 .name = "32 bpp RGB, le",
175 .fourcc = V4L2_PIX_FMT_BGR32,
176 .cxformat = ColorFormatRGB32,
177 .depth = 32,
178 .flags = FORMAT_FLAGS_PACKED,
179 },{
180 .name = "32 bpp RGB, be",
181 .fourcc = V4L2_PIX_FMT_RGB32,
182 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
183 .depth = 32,
184 .flags = FORMAT_FLAGS_PACKED,
185 },{
186 .name = "4:2:2, packed, YUYV",
187 .fourcc = V4L2_PIX_FMT_YUYV,
188 .cxformat = ColorFormatYUY2,
189 .depth = 16,
190 .flags = FORMAT_FLAGS_PACKED,
191 },{
192 .name = "4:2:2, packed, UYVY",
193 .fourcc = V4L2_PIX_FMT_UYVY,
194 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
195 .depth = 16,
196 .flags = FORMAT_FLAGS_PACKED,
197 },
198};
199
200static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
201{
202 unsigned int i;
203
204 for (i = 0; i < ARRAY_SIZE(formats); i++)
205 if (formats[i].fourcc == fourcc)
206 return formats+i;
207 return NULL;
208}
209
210/* ------------------------------------------------------------------- */
211
212static const struct v4l2_queryctrl no_ctl = {
213 .name = "42",
214 .flags = V4L2_CTRL_FLAG_DISABLED,
215};
216
217static struct cx88_ctrl cx8800_ctls[] = {
218 /* --- video --- */
219 {
220 .v = {
221 .id = V4L2_CID_BRIGHTNESS,
222 .name = "Brightness",
223 .minimum = 0x00,
224 .maximum = 0xff,
225 .step = 1,
226 .default_value = 0,
227 .type = V4L2_CTRL_TYPE_INTEGER,
228 },
229 .off = 128,
230 .reg = MO_CONTR_BRIGHT,
231 .mask = 0x00ff,
232 .shift = 0,
233 },{
234 .v = {
235 .id = V4L2_CID_CONTRAST,
236 .name = "Contrast",
237 .minimum = 0,
238 .maximum = 0xff,
239 .step = 1,
240 .default_value = 0,
241 .type = V4L2_CTRL_TYPE_INTEGER,
242 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700243 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .reg = MO_CONTR_BRIGHT,
245 .mask = 0xff00,
246 .shift = 8,
247 },{
248 .v = {
249 .id = V4L2_CID_HUE,
250 .name = "Hue",
251 .minimum = 0,
252 .maximum = 0xff,
253 .step = 1,
254 .default_value = 0,
255 .type = V4L2_CTRL_TYPE_INTEGER,
256 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700257 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .reg = MO_HUE,
259 .mask = 0x00ff,
260 .shift = 0,
261 },{
262 /* strictly, this only describes only U saturation.
263 * V saturation is handled specially through code.
264 */
265 .v = {
266 .id = V4L2_CID_SATURATION,
267 .name = "Saturation",
268 .minimum = 0,
269 .maximum = 0xff,
270 .step = 1,
271 .default_value = 0,
272 .type = V4L2_CTRL_TYPE_INTEGER,
273 },
274 .off = 0,
275 .reg = MO_UV_SATURATION,
276 .mask = 0x00ff,
277 .shift = 0,
278 },{
279 /* --- audio --- */
280 .v = {
281 .id = V4L2_CID_AUDIO_MUTE,
282 .name = "Mute",
283 .minimum = 0,
284 .maximum = 1,
285 .type = V4L2_CTRL_TYPE_BOOLEAN,
286 },
287 .reg = AUD_VOL_CTL,
288 .sreg = SHADOW_AUD_VOL_CTL,
289 .mask = (1 << 6),
290 .shift = 6,
291 },{
292 .v = {
293 .id = V4L2_CID_AUDIO_VOLUME,
294 .name = "Volume",
295 .minimum = 0,
296 .maximum = 0x3f,
297 .step = 1,
298 .default_value = 0,
299 .type = V4L2_CTRL_TYPE_INTEGER,
300 },
301 .reg = AUD_VOL_CTL,
302 .sreg = SHADOW_AUD_VOL_CTL,
303 .mask = 0x3f,
304 .shift = 0,
305 },{
306 .v = {
307 .id = V4L2_CID_AUDIO_BALANCE,
308 .name = "Balance",
309 .minimum = 0,
310 .maximum = 0x7f,
311 .step = 1,
312 .default_value = 0x40,
313 .type = V4L2_CTRL_TYPE_INTEGER,
314 },
315 .reg = AUD_BAL_CTL,
316 .sreg = SHADOW_AUD_BAL_CTL,
317 .mask = 0x7f,
318 .shift = 0,
319 }
320};
Adrian Bunk408b6642005-05-01 08:59:29 -0700321static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/* ------------------------------------------------------------------- */
324/* resource management */
325
326static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
327{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700328 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (fh->resources & bit)
330 /* have it already allocated */
331 return 1;
332
333 /* is it free? */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700334 down(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (dev->resources & bit) {
336 /* no, someone else uses it */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700337 up(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339 }
340 /* it's free, grab it */
341 fh->resources |= bit;
342 dev->resources |= bit;
343 dprintk(1,"res: get %d\n",bit);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700344 up(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 1;
346}
347
348static
349int res_check(struct cx8800_fh *fh, unsigned int bit)
350{
351 return (fh->resources & bit);
352}
353
354static
355int res_locked(struct cx8800_dev *dev, unsigned int bit)
356{
357 return (dev->resources & bit);
358}
359
360static
361void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
362{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700363 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if ((fh->resources & bits) != bits)
365 BUG();
366
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700367 down(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 fh->resources &= ~bits;
369 dev->resources &= ~bits;
370 dprintk(1,"res: put %d\n",bits);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700371 up(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374/* ------------------------------------------------------------------ */
375
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700376/* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
377static int video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700379 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
382 input, INPUT(input)->vmux,
383 INPUT(input)->gpio0,INPUT(input)->gpio1,
384 INPUT(input)->gpio2,INPUT(input)->gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700385 core->input = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
387 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
388 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
389 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
390 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
391
392 switch (INPUT(input)->type) {
393 case CX88_VMUX_SVIDEO:
394 cx_set(MO_AFECFG_IO, 0x00000001);
395 cx_set(MO_INPUT_FORMAT, 0x00010010);
396 cx_set(MO_FILTER_EVEN, 0x00002020);
397 cx_set(MO_FILTER_ODD, 0x00002020);
398 break;
399 default:
400 cx_clear(MO_AFECFG_IO, 0x00000001);
401 cx_clear(MO_INPUT_FORMAT, 0x00010010);
402 cx_clear(MO_FILTER_EVEN, 0x00002020);
403 cx_clear(MO_FILTER_ODD, 0x00002020);
404 break;
405 }
406 return 0;
407}
408
409/* ------------------------------------------------------------------ */
410
411static int start_video_dma(struct cx8800_dev *dev,
412 struct cx88_dmaqueue *q,
413 struct cx88_buffer *buf)
414{
415 struct cx88_core *core = dev->core;
416
417 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700418 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700420 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
422
423 /* reset counter */
424 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
425 q->count = 1;
426
427 /* enable irqs */
428 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700429
430 /* Enables corresponding bits at PCI_INT_STAT:
431 bits 0 to 4: video, audio, transport stream, VIP, Host
432 bit 7: timer
433 bits 8 and 9: DMA complete for: SRC, DST
434 bits 10 and 11: BERR signal asserted for RISC: RD, WR
435 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 cx_set(MO_VID_INTMSK, 0x0f0011);
438
439 /* enable capture */
440 cx_set(VID_CAPTURE_CONTROL,0x06);
441
442 /* start dma */
443 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700444 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 return 0;
447}
448
449static int stop_video_dma(struct cx8800_dev *dev)
450{
451 struct cx88_core *core = dev->core;
452
453 /* stop dma */
454 cx_clear(MO_VID_DMACNTRL, 0x11);
455
456 /* disable capture */
457 cx_clear(VID_CAPTURE_CONTROL,0x06);
458
459 /* disable irqs */
460 cx_clear(MO_PCI_INTMSK, 0x000001);
461 cx_clear(MO_VID_INTMSK, 0x0f0011);
462 return 0;
463}
464
465static int restart_video_queue(struct cx8800_dev *dev,
466 struct cx88_dmaqueue *q)
467{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700468 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct cx88_buffer *buf, *prev;
470 struct list_head *item;
471
472 if (!list_empty(&q->active)) {
473 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
474 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
475 buf, buf->vb.i);
476 start_video_dma(dev, q, buf);
477 list_for_each(item,&q->active) {
478 buf = list_entry(item, struct cx88_buffer, vb.queue);
479 buf->count = q->count++;
480 }
481 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
482 return 0;
483 }
484
485 prev = NULL;
486 for (;;) {
487 if (list_empty(&q->queued))
488 return 0;
489 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
490 if (NULL == prev) {
491 list_del(&buf->vb.queue);
492 list_add_tail(&buf->vb.queue,&q->active);
493 start_video_dma(dev, q, buf);
494 buf->vb.state = STATE_ACTIVE;
495 buf->count = q->count++;
496 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
497 dprintk(2,"[%p/%d] restart_queue - first active\n",
498 buf,buf->vb.i);
499
500 } else if (prev->vb.width == buf->vb.width &&
501 prev->vb.height == buf->vb.height &&
502 prev->fmt == buf->fmt) {
503 list_del(&buf->vb.queue);
504 list_add_tail(&buf->vb.queue,&q->active);
505 buf->vb.state = STATE_ACTIVE;
506 buf->count = q->count++;
507 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
508 dprintk(2,"[%p/%d] restart_queue - move to active\n",
509 buf,buf->vb.i);
510 } else {
511 return 0;
512 }
513 prev = buf;
514 }
515}
516
517/* ------------------------------------------------------------------ */
518
519static int
520buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
521{
522 struct cx8800_fh *fh = q->priv_data;
523
524 *size = fh->fmt->depth*fh->width*fh->height >> 3;
525 if (0 == *count)
526 *count = 32;
527 while (*size * *count > vid_limit * 1024 * 1024)
528 (*count)--;
529 return 0;
530}
531
532static int
533buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
534 enum v4l2_field field)
535{
536 struct cx8800_fh *fh = q->priv_data;
537 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700538 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
540 int rc, init_buffer = 0;
541
542 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700543 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
544 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return -EINVAL;
546 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
547 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
548 return -EINVAL;
549
550 if (buf->fmt != fh->fmt ||
551 buf->vb.width != fh->width ||
552 buf->vb.height != fh->height ||
553 buf->vb.field != field) {
554 buf->fmt = fh->fmt;
555 buf->vb.width = fh->width;
556 buf->vb.height = fh->height;
557 buf->vb.field = field;
558 init_buffer = 1;
559 }
560
561 if (STATE_NEEDS_INIT == buf->vb.state) {
562 init_buffer = 1;
563 if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
564 goto fail;
565 }
566
567 if (init_buffer) {
568 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
569 switch (buf->vb.field) {
570 case V4L2_FIELD_TOP:
571 cx88_risc_buffer(dev->pci, &buf->risc,
572 buf->vb.dma.sglist, 0, UNSET,
573 buf->bpl, 0, buf->vb.height);
574 break;
575 case V4L2_FIELD_BOTTOM:
576 cx88_risc_buffer(dev->pci, &buf->risc,
577 buf->vb.dma.sglist, UNSET, 0,
578 buf->bpl, 0, buf->vb.height);
579 break;
580 case V4L2_FIELD_INTERLACED:
581 cx88_risc_buffer(dev->pci, &buf->risc,
582 buf->vb.dma.sglist, 0, buf->bpl,
583 buf->bpl, buf->bpl,
584 buf->vb.height >> 1);
585 break;
586 case V4L2_FIELD_SEQ_TB:
587 cx88_risc_buffer(dev->pci, &buf->risc,
588 buf->vb.dma.sglist,
589 0, buf->bpl * (buf->vb.height >> 1),
590 buf->bpl, 0,
591 buf->vb.height >> 1);
592 break;
593 case V4L2_FIELD_SEQ_BT:
594 cx88_risc_buffer(dev->pci, &buf->risc,
595 buf->vb.dma.sglist,
596 buf->bpl * (buf->vb.height >> 1), 0,
597 buf->bpl, 0,
598 buf->vb.height >> 1);
599 break;
600 default:
601 BUG();
602 }
603 }
604 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
605 buf, buf->vb.i,
606 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
607 (unsigned long)buf->risc.dma);
608
609 buf->vb.state = STATE_PREPARED;
610 return 0;
611
612 fail:
613 cx88_free_buffer(dev->pci,buf);
614 return rc;
615}
616
617static void
618buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
619{
620 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
621 struct cx88_buffer *prev;
622 struct cx8800_fh *fh = vq->priv_data;
623 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700624 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct cx88_dmaqueue *q = &dev->vidq;
626
627 /* add jump to stopper */
628 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
629 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
630
631 if (!list_empty(&q->queued)) {
632 list_add_tail(&buf->vb.queue,&q->queued);
633 buf->vb.state = STATE_QUEUED;
634 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
635 buf, buf->vb.i);
636
637 } else if (list_empty(&q->active)) {
638 list_add_tail(&buf->vb.queue,&q->active);
639 start_video_dma(dev, q, buf);
640 buf->vb.state = STATE_ACTIVE;
641 buf->count = q->count++;
642 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
643 dprintk(2,"[%p/%d] buffer_queue - first active\n",
644 buf, buf->vb.i);
645
646 } else {
647 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
648 if (prev->vb.width == buf->vb.width &&
649 prev->vb.height == buf->vb.height &&
650 prev->fmt == buf->fmt) {
651 list_add_tail(&buf->vb.queue,&q->active);
652 buf->vb.state = STATE_ACTIVE;
653 buf->count = q->count++;
654 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
655 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
656 buf, buf->vb.i);
657
658 } else {
659 list_add_tail(&buf->vb.queue,&q->queued);
660 buf->vb.state = STATE_QUEUED;
661 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
662 buf, buf->vb.i);
663 }
664 }
665}
666
667static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
668{
669 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
670 struct cx8800_fh *fh = q->priv_data;
671
672 cx88_free_buffer(fh->dev->pci,buf);
673}
674
Adrian Bunk408b6642005-05-01 08:59:29 -0700675static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 .buf_setup = buffer_setup,
677 .buf_prepare = buffer_prepare,
678 .buf_queue = buffer_queue,
679 .buf_release = buffer_release,
680};
681
682/* ------------------------------------------------------------------ */
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685/* ------------------------------------------------------------------ */
686
687static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
688{
689 switch (fh->type) {
690 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
691 return &fh->vidq;
692 case V4L2_BUF_TYPE_VBI_CAPTURE:
693 return &fh->vbiq;
694 default:
695 BUG();
696 return NULL;
697 }
698}
699
700static int get_ressource(struct cx8800_fh *fh)
701{
702 switch (fh->type) {
703 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
704 return RESOURCE_VIDEO;
705 case V4L2_BUF_TYPE_VBI_CAPTURE:
706 return RESOURCE_VBI;
707 default:
708 BUG();
709 return 0;
710 }
711}
712
713static int video_open(struct inode *inode, struct file *file)
714{
715 int minor = iminor(inode);
716 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700717 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct cx8800_fh *fh;
719 struct list_head *list;
720 enum v4l2_buf_type type = 0;
721 int radio = 0;
722
723 list_for_each(list,&cx8800_devlist) {
724 h = list_entry(list, struct cx8800_dev, devlist);
725 if (h->video_dev->minor == minor) {
726 dev = h;
727 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
728 }
729 if (h->vbi_dev->minor == minor) {
730 dev = h;
731 type = V4L2_BUF_TYPE_VBI_CAPTURE;
732 }
733 if (h->radio_dev &&
734 h->radio_dev->minor == minor) {
735 radio = 1;
736 dev = h;
737 }
738 }
739 if (NULL == dev)
740 return -ENODEV;
741
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700742 core = dev->core;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dprintk(1,"open minor=%d radio=%d type=%s\n",
745 minor,radio,v4l2_type_names[type]);
746
747 /* allocate + initialize per filehandle data */
748 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
749 if (NULL == fh)
750 return -ENOMEM;
751 memset(fh,0,sizeof(*fh));
752 file->private_data = fh;
753 fh->dev = dev;
754 fh->radio = radio;
755 fh->type = type;
756 fh->width = 320;
757 fh->height = 240;
758 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
759
760 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
761 dev->pci, &dev->slock,
762 V4L2_BUF_TYPE_VIDEO_CAPTURE,
763 V4L2_FIELD_INTERLACED,
764 sizeof(struct cx88_buffer),
765 fh);
766 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
767 dev->pci, &dev->slock,
768 V4L2_BUF_TYPE_VBI_CAPTURE,
769 V4L2_FIELD_SEQ_TB,
770 sizeof(struct cx88_buffer),
771 fh);
772
773 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int board = core->board;
775 dprintk(1,"video_open: setting radio device\n");
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700776 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
778 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
779 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700780 core->tvaudio = WW_FM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 cx88_set_tvaudio(core);
782 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700783 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
786 return 0;
787}
788
789static ssize_t
790video_read(struct file *file, char *data, size_t count, loff_t *ppos)
791{
792 struct cx8800_fh *fh = file->private_data;
793
794 switch (fh->type) {
795 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
796 if (res_locked(fh->dev,RESOURCE_VIDEO))
797 return -EBUSY;
798 return videobuf_read_one(&fh->vidq, data, count, ppos,
799 file->f_flags & O_NONBLOCK);
800 case V4L2_BUF_TYPE_VBI_CAPTURE:
801 if (!res_get(fh->dev,fh,RESOURCE_VBI))
802 return -EBUSY;
803 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
804 file->f_flags & O_NONBLOCK);
805 default:
806 BUG();
807 return 0;
808 }
809}
810
811static unsigned int
812video_poll(struct file *file, struct poll_table_struct *wait)
813{
814 struct cx8800_fh *fh = file->private_data;
815 struct cx88_buffer *buf;
816
817 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
818 if (!res_get(fh->dev,fh,RESOURCE_VBI))
819 return POLLERR;
820 return videobuf_poll_stream(file, &fh->vbiq, wait);
821 }
822
823 if (res_check(fh,RESOURCE_VIDEO)) {
824 /* streaming capture */
825 if (list_empty(&fh->vidq.stream))
826 return POLLERR;
827 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
828 } else {
829 /* read() capture */
830 buf = (struct cx88_buffer*)fh->vidq.read_buf;
831 if (NULL == buf)
832 return POLLERR;
833 }
834 poll_wait(file, &buf->vb.done, wait);
835 if (buf->vb.state == STATE_DONE ||
836 buf->vb.state == STATE_ERROR)
837 return POLLIN|POLLRDNORM;
838 return 0;
839}
840
841static int video_release(struct inode *inode, struct file *file)
842{
843 struct cx8800_fh *fh = file->private_data;
844 struct cx8800_dev *dev = fh->dev;
845
846 /* turn off overlay */
847 if (res_check(fh, RESOURCE_OVERLAY)) {
848 /* FIXME */
849 res_free(dev,fh,RESOURCE_OVERLAY);
850 }
851
852 /* stop video capture */
853 if (res_check(fh, RESOURCE_VIDEO)) {
854 videobuf_queue_cancel(&fh->vidq);
855 res_free(dev,fh,RESOURCE_VIDEO);
856 }
857 if (fh->vidq.read_buf) {
858 buffer_release(&fh->vidq,fh->vidq.read_buf);
859 kfree(fh->vidq.read_buf);
860 }
861
862 /* stop vbi capture */
863 if (res_check(fh, RESOURCE_VBI)) {
864 if (fh->vbiq.streaming)
865 videobuf_streamoff(&fh->vbiq);
866 if (fh->vbiq.reading)
867 videobuf_read_stop(&fh->vbiq);
868 res_free(dev,fh,RESOURCE_VBI);
869 }
870
871 videobuf_mmap_free(&fh->vidq);
872 videobuf_mmap_free(&fh->vbiq);
873 file->private_data = NULL;
874 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700875
876 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879}
880
881static int
882video_mmap(struct file *file, struct vm_area_struct * vma)
883{
884 struct cx8800_fh *fh = file->private_data;
885
886 return videobuf_mmap_mapper(get_queue(fh), vma);
887}
888
889/* ------------------------------------------------------------------ */
890
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700891/* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
892static int get_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700894 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct cx88_ctrl *c = NULL;
896 u32 value;
897 int i;
898
899 for (i = 0; i < CX8800_CTLS; i++)
900 if (cx8800_ctls[i].v.id == ctl->id)
901 c = &cx8800_ctls[i];
902 if (NULL == c)
903 return -EINVAL;
904
905 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
906 switch (ctl->id) {
907 case V4L2_CID_AUDIO_BALANCE:
908 ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
909 break;
910 case V4L2_CID_AUDIO_VOLUME:
911 ctl->value = 0x3f - (value & 0x3f);
912 break;
913 default:
914 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
915 break;
916 }
917 return 0;
918}
919
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700920/* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
921static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700923 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct cx88_ctrl *c = NULL;
925 u32 v_sat_value;
926 u32 value;
927 int i;
928
929 for (i = 0; i < CX8800_CTLS; i++)
930 if (cx8800_ctls[i].v.id == ctl->id)
931 c = &cx8800_ctls[i];
932 if (NULL == c)
933 return -EINVAL;
934
935 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700936 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700938 ctl->value = c->v.maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 switch (ctl->id) {
940 case V4L2_CID_AUDIO_BALANCE:
941 value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
942 break;
943 case V4L2_CID_AUDIO_VOLUME:
944 value = 0x3f - (ctl->value & 0x3f);
945 break;
946 case V4L2_CID_SATURATION:
947 /* special v_sat handling */
948 v_sat_value = ctl->value - (0x7f - 0x5a);
949 if (v_sat_value > 0xff)
950 v_sat_value = 0xff;
951 if (v_sat_value < 0x00)
952 v_sat_value = 0x00;
953 cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
954 /* fall through to default route for u_sat */
955 default:
956 value = ((ctl->value - c->off) << c->shift) & c->mask;
957 break;
958 }
959 dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
960 ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
961 if (c->sreg) {
962 cx_sandor(c->sreg, c->reg, c->mask, value);
963 } else {
964 cx_andor(c->reg, c->mask, value);
965 }
966 return 0;
967}
968
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700969/* static void init_controls(struct cx8800_dev *dev) */
970static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 static struct v4l2_control mute = {
973 .id = V4L2_CID_AUDIO_MUTE,
974 .value = 1,
975 };
976 static struct v4l2_control volume = {
977 .id = V4L2_CID_AUDIO_VOLUME,
978 .value = 0x3f,
979 };
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -0700980 static struct v4l2_control hue = {
981 .id = V4L2_CID_HUE,
982 .value = 0x80,
983 };
984 static struct v4l2_control contrast = {
985 .id = V4L2_CID_CONTRAST,
986 .value = 0x80,
987 };
988 static struct v4l2_control brightness = {
989 .id = V4L2_CID_BRIGHTNESS,
990 .value = 0x80,
991 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700993 set_control(core,&mute);
994 set_control(core,&volume);
995 set_control(core,&hue);
996 set_control(core,&contrast);
997 set_control(core,&brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000/* ------------------------------------------------------------------ */
1001
1002static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1003 struct v4l2_format *f)
1004{
1005 switch (f->type) {
1006 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1007 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1008 f->fmt.pix.width = fh->width;
1009 f->fmt.pix.height = fh->height;
1010 f->fmt.pix.field = fh->vidq.field;
1011 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1012 f->fmt.pix.bytesperline =
1013 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1014 f->fmt.pix.sizeimage =
1015 f->fmt.pix.height * f->fmt.pix.bytesperline;
1016 return 0;
1017 case V4L2_BUF_TYPE_VBI_CAPTURE:
1018 cx8800_vbi_fmt(dev, f);
1019 return 0;
1020 default:
1021 return -EINVAL;
1022 }
1023}
1024
1025static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1026 struct v4l2_format *f)
1027{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001028 struct cx88_core *core = dev->core;
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 switch (f->type) {
1031 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1032 {
1033 struct cx8800_fmt *fmt;
1034 enum v4l2_field field;
1035 unsigned int maxw, maxh;
1036
1037 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1038 if (NULL == fmt)
1039 return -EINVAL;
1040
1041 field = f->fmt.pix.field;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001042 maxw = norm_maxw(core->tvnorm);
1043 maxh = norm_maxh(core->tvnorm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 if (V4L2_FIELD_ANY == field) {
1046 field = (f->fmt.pix.height > maxh/2)
1047 ? V4L2_FIELD_INTERLACED
1048 : V4L2_FIELD_BOTTOM;
1049 }
1050
1051 switch (field) {
1052 case V4L2_FIELD_TOP:
1053 case V4L2_FIELD_BOTTOM:
1054 maxh = maxh / 2;
1055 break;
1056 case V4L2_FIELD_INTERLACED:
1057 break;
1058 default:
1059 return -EINVAL;
1060 }
1061
1062 f->fmt.pix.field = field;
1063 if (f->fmt.pix.height < 32)
1064 f->fmt.pix.height = 32;
1065 if (f->fmt.pix.height > maxh)
1066 f->fmt.pix.height = maxh;
1067 if (f->fmt.pix.width < 48)
1068 f->fmt.pix.width = 48;
1069 if (f->fmt.pix.width > maxw)
1070 f->fmt.pix.width = maxw;
1071 f->fmt.pix.width &= ~0x03;
1072 f->fmt.pix.bytesperline =
1073 (f->fmt.pix.width * fmt->depth) >> 3;
1074 f->fmt.pix.sizeimage =
1075 f->fmt.pix.height * f->fmt.pix.bytesperline;
1076
1077 return 0;
1078 }
1079 case V4L2_BUF_TYPE_VBI_CAPTURE:
1080 cx8800_vbi_fmt(dev, f);
1081 return 0;
1082 default:
1083 return -EINVAL;
1084 }
1085}
1086
1087static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1088 struct v4l2_format *f)
1089{
1090 int err;
1091
1092 switch (f->type) {
1093 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1094 err = cx8800_try_fmt(dev,fh,f);
1095 if (0 != err)
1096 return err;
1097
1098 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1099 fh->width = f->fmt.pix.width;
1100 fh->height = f->fmt.pix.height;
1101 fh->vidq.field = f->fmt.pix.field;
1102 return 0;
1103 case V4L2_BUF_TYPE_VBI_CAPTURE:
1104 cx8800_vbi_fmt(dev, f);
1105 return 0;
1106 default:
1107 return -EINVAL;
1108 }
1109}
1110
1111/*
1112 * This function is _not_ called directly, but from
1113 * video_generic_ioctl (and maybe others). userspace
1114 * copying is done already, arg is a kernel pointer.
1115 */
1116static int video_do_ioctl(struct inode *inode, struct file *file,
1117 unsigned int cmd, void *arg)
1118{
1119 struct cx8800_fh *fh = file->private_data;
1120 struct cx8800_dev *dev = fh->dev;
1121 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 int err;
1123
1124 if (video_debug > 1)
1125 cx88_print_ioctl(core->name,cmd);
1126 switch (cmd) {
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001127
1128 /* --- capabilities ------------------------------------------ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 case VIDIOC_QUERYCAP:
1130 {
1131 struct v4l2_capability *cap = arg;
1132
1133 memset(cap,0,sizeof(*cap));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001134 strcpy(cap->driver, "cx8800");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 strlcpy(cap->card, cx88_boards[core->board].name,
1136 sizeof(cap->card));
1137 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1138 cap->version = CX88_VERSION_CODE;
1139 cap->capabilities =
1140 V4L2_CAP_VIDEO_CAPTURE |
1141 V4L2_CAP_READWRITE |
1142 V4L2_CAP_STREAMING |
1143 V4L2_CAP_VBI_CAPTURE |
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001144 V4L2_CAP_VIDEO_OVERLAY |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 0;
1146 if (UNSET != core->tuner_type)
1147 cap->capabilities |= V4L2_CAP_TUNER;
1148 return 0;
1149 }
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* --- capture ioctls ---------------------------------------- */
1152 case VIDIOC_ENUM_FMT:
1153 {
1154 struct v4l2_fmtdesc *f = arg;
1155 enum v4l2_buf_type type;
1156 unsigned int index;
1157
1158 index = f->index;
1159 type = f->type;
1160 switch (type) {
1161 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1162 if (index >= ARRAY_SIZE(formats))
1163 return -EINVAL;
1164 memset(f,0,sizeof(*f));
1165 f->index = index;
1166 f->type = type;
1167 strlcpy(f->description,formats[index].name,sizeof(f->description));
1168 f->pixelformat = formats[index].fourcc;
1169 break;
1170 default:
1171 return -EINVAL;
1172 }
1173 return 0;
1174 }
1175 case VIDIOC_G_FMT:
1176 {
1177 struct v4l2_format *f = arg;
1178 return cx8800_g_fmt(dev,fh,f);
1179 }
1180 case VIDIOC_S_FMT:
1181 {
1182 struct v4l2_format *f = arg;
1183 return cx8800_s_fmt(dev,fh,f);
1184 }
1185 case VIDIOC_TRY_FMT:
1186 {
1187 struct v4l2_format *f = arg;
1188 return cx8800_try_fmt(dev,fh,f);
1189 }
1190
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001191 /* --- streaming capture ------------------------------------- */
1192 case VIDIOCGMBUF:
1193 {
1194 struct video_mbuf *mbuf = arg;
1195 struct videobuf_queue *q;
1196 struct v4l2_requestbuffers req;
1197 unsigned int i;
1198
1199 q = get_queue(fh);
1200 memset(&req,0,sizeof(req));
1201 req.type = q->type;
1202 req.count = 8;
1203 req.memory = V4L2_MEMORY_MMAP;
1204 err = videobuf_reqbufs(q,&req);
1205 if (err < 0)
1206 return err;
1207 memset(mbuf,0,sizeof(*mbuf));
1208 mbuf->frames = req.count;
1209 mbuf->size = 0;
1210 for (i = 0; i < mbuf->frames; i++) {
1211 mbuf->offsets[i] = q->bufs[i]->boff;
1212 mbuf->size += q->bufs[i]->bsize;
1213 }
1214 return 0;
1215 }
1216 case VIDIOC_REQBUFS:
1217 return videobuf_reqbufs(get_queue(fh), arg);
1218
1219 case VIDIOC_QUERYBUF:
1220 return videobuf_querybuf(get_queue(fh), arg);
1221
1222 case VIDIOC_QBUF:
1223 return videobuf_qbuf(get_queue(fh), arg);
1224
1225 case VIDIOC_DQBUF:
1226 return videobuf_dqbuf(get_queue(fh), arg,
1227 file->f_flags & O_NONBLOCK);
1228
1229 case VIDIOC_STREAMON:
1230 {
1231 int res = get_ressource(fh);
1232
1233 if (!res_get(dev,fh,res))
1234 return -EBUSY;
1235 return videobuf_streamon(get_queue(fh));
1236 }
1237 case VIDIOC_STREAMOFF:
1238 {
1239 int res = get_ressource(fh);
1240
1241 err = videobuf_streamoff(get_queue(fh));
1242 if (err < 0)
1243 return err;
1244 res_free(dev,fh,res);
1245 return 0;
1246 }
1247
1248 default:
1249 return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
1250 }
1251 return 0;
1252}
1253
1254int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
1255 struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
1256{
1257 int err;
1258
1259 if (video_debug > 1)
1260 cx88_print_ioctl(core->name,cmd);
1261 printk( KERN_INFO "CORE IOCTL: 0x%x\n", cmd );
1262 cx88_print_ioctl(core->name,cmd);
1263 dprintk( 1, "CORE IOCTL: 0x%x\n", cmd );
1264
1265 switch (cmd) {
1266 /* ---------- tv norms ---------- */
1267 case VIDIOC_ENUMSTD:
1268 {
1269 struct v4l2_standard *e = arg;
1270 unsigned int i;
1271
1272 i = e->index;
1273 if (i >= ARRAY_SIZE(tvnorms))
1274 return -EINVAL;
1275 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1276 tvnorms[e->index].name);
1277 e->index = i;
1278 if (err < 0)
1279 return err;
1280 return 0;
1281 }
1282 case VIDIOC_G_STD:
1283 {
1284 v4l2_std_id *id = arg;
1285
1286 *id = core->tvnorm->id;
1287 return 0;
1288 }
1289 case VIDIOC_S_STD:
1290 {
1291 v4l2_std_id *id = arg;
1292 unsigned int i;
1293
1294 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1295 if (*id & tvnorms[i].id)
1296 break;
1297 if (i == ARRAY_SIZE(tvnorms))
1298 return -EINVAL;
1299
1300 down(&core->lock);
1301 cx88_set_tvnorm(core,&tvnorms[i]);
1302 up(&core->lock);
1303 return 0;
1304 }
1305
1306 /* ------ input switching ---------- */
1307 case VIDIOC_ENUMINPUT:
1308 {
1309 static const char *iname[] = {
1310 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1311 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1312 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1313 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1314 [ CX88_VMUX_SVIDEO ] = "S-Video",
1315 [ CX88_VMUX_TELEVISION ] = "Television",
1316 [ CX88_VMUX_CABLE ] = "Cable TV",
1317 [ CX88_VMUX_DVB ] = "DVB",
1318 [ CX88_VMUX_DEBUG ] = "for debug only",
1319 };
1320 struct v4l2_input *i = arg;
1321 unsigned int n;
1322
1323 n = i->index;
1324 if (n >= 4)
1325 return -EINVAL;
1326 if (0 == INPUT(n)->type)
1327 return -EINVAL;
1328 memset(i,0,sizeof(*i));
1329 i->index = n;
1330 i->type = V4L2_INPUT_TYPE_CAMERA;
1331 strcpy(i->name,iname[INPUT(n)->type]);
1332 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1333 (CX88_VMUX_CABLE == INPUT(n)->type))
1334 i->type = V4L2_INPUT_TYPE_TUNER;
1335 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1336 i->std |= tvnorms[n].id;
1337 return 0;
1338 }
1339 case VIDIOC_G_INPUT:
1340 {
1341 unsigned int *i = arg;
1342
1343 *i = core->input;
1344 return 0;
1345 }
1346 case VIDIOC_S_INPUT:
1347 {
1348 unsigned int *i = arg;
1349
1350 if (*i >= 4)
1351 return -EINVAL;
1352 down(&core->lock);
1353 cx88_newstation(core);
1354 video_mux(core,*i);
1355 up(&core->lock);
1356 return 0;
1357 }
1358
1359
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /* --- controls ---------------------------------------------- */
1362 case VIDIOC_QUERYCTRL:
1363 {
1364 struct v4l2_queryctrl *c = arg;
1365 int i;
1366
1367 if (c->id < V4L2_CID_BASE ||
1368 c->id >= V4L2_CID_LASTP1)
1369 return -EINVAL;
1370 for (i = 0; i < CX8800_CTLS; i++)
1371 if (cx8800_ctls[i].v.id == c->id)
1372 break;
1373 if (i == CX8800_CTLS) {
1374 *c = no_ctl;
1375 return 0;
1376 }
1377 *c = cx8800_ctls[i].v;
1378 return 0;
1379 }
1380 case VIDIOC_G_CTRL:
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001381 return get_control(core,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 case VIDIOC_S_CTRL:
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001383 return set_control(core,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* --- tuner ioctls ------------------------------------------ */
1386 case VIDIOC_G_TUNER:
1387 {
1388 struct v4l2_tuner *t = arg;
1389 u32 reg;
1390
1391 if (UNSET == core->tuner_type)
1392 return -EINVAL;
1393 if (0 != t->index)
1394 return -EINVAL;
1395
1396 memset(t,0,sizeof(*t));
1397 strcpy(t->name, "Television");
1398 t->type = V4L2_TUNER_ANALOG_TV;
1399 t->capability = V4L2_TUNER_CAP_NORM;
1400 t->rangehigh = 0xffffffffUL;
1401
1402 cx88_get_stereo(core ,t);
1403 reg = cx_read(MO_DEVICE_STATUS);
1404 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1405 return 0;
1406 }
1407 case VIDIOC_S_TUNER:
1408 {
1409 struct v4l2_tuner *t = arg;
1410
1411 if (UNSET == core->tuner_type)
1412 return -EINVAL;
1413 if (0 != t->index)
1414 return -EINVAL;
1415 cx88_set_stereo(core, t->audmode, 1);
1416 return 0;
1417 }
1418 case VIDIOC_G_FREQUENCY:
1419 {
1420 struct v4l2_frequency *f = arg;
1421
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001422 memset(f,0,sizeof(*f));
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (UNSET == core->tuner_type)
1425 return -EINVAL;
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001426
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001427 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1428 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1429 f->frequency = core->freq;
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001430
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001431 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return 0;
1434 }
1435 case VIDIOC_S_FREQUENCY:
1436 {
1437 struct v4l2_frequency *f = arg;
1438
1439 if (UNSET == core->tuner_type)
1440 return -EINVAL;
1441 if (f->tuner != 0)
1442 return -EINVAL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001443 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EINVAL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001445 if (1 == radio && f->type != V4L2_TUNER_RADIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -EINVAL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001447 down(&core->lock);
1448 core->freq = f->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 cx88_newstation(core);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001450 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -07001451
1452 /* When changing channels it is required to reset TVAUDIO */
1453 msleep (10);
1454 cx88_set_tvaudio(core);
1455
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001456 up(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return 0;
1458 }
1459
1460 default:
1461 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001462 driver_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 return 0;
1465}
1466
1467static int video_ioctl(struct inode *inode, struct file *file,
1468 unsigned int cmd, unsigned long arg)
1469{
1470 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1471}
1472
1473/* ----------------------------------------------------------- */
1474
1475static int radio_do_ioctl(struct inode *inode, struct file *file,
1476 unsigned int cmd, void *arg)
1477{
1478 struct cx8800_fh *fh = file->private_data;
1479 struct cx8800_dev *dev = fh->dev;
1480 struct cx88_core *core = dev->core;
1481
1482 if (video_debug > 1)
1483 cx88_print_ioctl(core->name,cmd);
1484
1485 switch (cmd) {
1486 case VIDIOC_QUERYCAP:
1487 {
1488 struct v4l2_capability *cap = arg;
1489
1490 memset(cap,0,sizeof(*cap));
1491 strcpy(cap->driver, "cx8800");
1492 strlcpy(cap->card, cx88_boards[core->board].name,
1493 sizeof(cap->card));
1494 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1495 cap->version = CX88_VERSION_CODE;
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001496 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498 }
1499 case VIDIOC_G_TUNER:
1500 {
1501 struct v4l2_tuner *t = arg;
1502
1503 if (t->index > 0)
1504 return -EINVAL;
1505
1506 memset(t,0,sizeof(*t));
1507 strcpy(t->name, "Radio");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001509 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
1511 }
1512 case VIDIOC_ENUMINPUT:
1513 {
1514 struct v4l2_input *i = arg;
1515
1516 if (i->index != 0)
1517 return -EINVAL;
1518 strcpy(i->name,"Radio");
1519 i->type = V4L2_INPUT_TYPE_TUNER;
1520 return 0;
1521 }
1522 case VIDIOC_G_INPUT:
1523 {
1524 int *i = arg;
1525 *i = 0;
1526 return 0;
1527 }
1528 case VIDIOC_G_AUDIO:
1529 {
1530 struct v4l2_audio *a = arg;
1531
1532 memset(a,0,sizeof(*a));
1533 strcpy(a->name,"Radio");
1534 return 0;
1535 }
1536 case VIDIOC_G_STD:
1537 {
1538 v4l2_std_id *id = arg;
1539 *id = 0;
1540 return 0;
1541 }
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001542 case VIDIOCSTUNER:
1543 {
1544 struct video_tuner *v = arg;
1545
1546 if (v->tuner) /* Only tuner 0 */
1547 return -EINVAL;
1548
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001549 cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
1550 return 0;
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 case VIDIOC_S_TUNER:
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001553 {
1554 struct v4l2_tuner *t = arg;
1555
1556 if (0 != t->index)
1557 return -EINVAL;
1558
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001559 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001560
1561 return 0;
1562 }
1563
1564 case VIDIOC_S_AUDIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 case VIDIOC_S_INPUT:
1566 case VIDIOC_S_STD:
1567 return 0;
1568
1569 case VIDIOC_QUERYCTRL:
1570 {
1571 struct v4l2_queryctrl *c = arg;
1572 int i;
1573
1574 if (c->id < V4L2_CID_BASE ||
1575 c->id >= V4L2_CID_LASTP1)
1576 return -EINVAL;
1577 if (c->id == V4L2_CID_AUDIO_MUTE) {
1578 for (i = 0; i < CX8800_CTLS; i++)
1579 if (cx8800_ctls[i].v.id == c->id)
1580 break;
1581 *c = cx8800_ctls[i].v;
1582 } else
1583 *c = no_ctl;
1584 return 0;
1585 }
1586
1587
1588 case VIDIOC_G_CTRL:
1589 case VIDIOC_S_CTRL:
1590 case VIDIOC_G_FREQUENCY:
1591 case VIDIOC_S_FREQUENCY:
1592 return video_do_ioctl(inode,file,cmd,arg);
1593
1594 default:
1595 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1596 radio_do_ioctl);
1597 }
1598 return 0;
1599};
1600
1601static int radio_ioctl(struct inode *inode, struct file *file,
1602 unsigned int cmd, unsigned long arg)
1603{
1604 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1605};
1606
1607/* ----------------------------------------------------------- */
1608
1609static void cx8800_vid_timeout(unsigned long data)
1610{
1611 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1612 struct cx88_core *core = dev->core;
1613 struct cx88_dmaqueue *q = &dev->vidq;
1614 struct cx88_buffer *buf;
1615 unsigned long flags;
1616
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001617 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 cx_clear(MO_VID_DMACNTRL, 0x11);
1620 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1621
1622 spin_lock_irqsave(&dev->slock,flags);
1623 while (!list_empty(&q->active)) {
1624 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1625 list_del(&buf->vb.queue);
1626 buf->vb.state = STATE_ERROR;
1627 wake_up(&buf->vb.done);
1628 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1629 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1630 }
1631 restart_video_queue(dev,q);
1632 spin_unlock_irqrestore(&dev->slock,flags);
1633}
1634
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001635static char *cx88_vid_irqs[32] = {
1636 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1637 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1638 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1639 "y_sync", "u_sync", "v_sync", "vbi_sync",
1640 "opc_err", "par_err", "rip_err", "pci_abort",
1641};
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643static void cx8800_vid_irq(struct cx8800_dev *dev)
1644{
1645 struct cx88_core *core = dev->core;
1646 u32 status, mask, count;
1647
1648 status = cx_read(MO_VID_INTSTAT);
1649 mask = cx_read(MO_VID_INTMSK);
1650 if (0 == (status & mask))
1651 return;
1652 cx_write(MO_VID_INTSTAT, status);
1653 if (irq_debug || (status & mask & ~0xff))
1654 cx88_print_irqbits(core->name, "irq vid",
1655 cx88_vid_irqs, status, mask);
1656
1657 /* risc op code error */
1658 if (status & (1 << 16)) {
1659 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1660 cx_clear(MO_VID_DMACNTRL, 0x11);
1661 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001662 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664
1665 /* risc1 y */
1666 if (status & 0x01) {
1667 spin_lock(&dev->slock);
1668 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001669 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 spin_unlock(&dev->slock);
1671 }
1672
1673 /* risc1 vbi */
1674 if (status & 0x08) {
1675 spin_lock(&dev->slock);
1676 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001677 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 spin_unlock(&dev->slock);
1679 }
1680
1681 /* risc2 y */
1682 if (status & 0x10) {
1683 dprintk(2,"stopper video\n");
1684 spin_lock(&dev->slock);
1685 restart_video_queue(dev,&dev->vidq);
1686 spin_unlock(&dev->slock);
1687 }
1688
1689 /* risc2 vbi */
1690 if (status & 0x80) {
1691 dprintk(2,"stopper vbi\n");
1692 spin_lock(&dev->slock);
1693 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1694 spin_unlock(&dev->slock);
1695 }
1696}
1697
1698static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1699{
1700 struct cx8800_dev *dev = dev_id;
1701 struct cx88_core *core = dev->core;
1702 u32 status;
1703 int loop, handled = 0;
1704
1705 for (loop = 0; loop < 10; loop++) {
1706 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1707 if (0 == status)
1708 goto out;
1709 cx_write(MO_PCI_INTSTAT, status);
1710 handled = 1;
1711
1712 if (status & core->pci_irqmask)
1713 cx88_core_irq(core,status);
1714 if (status & 0x01)
1715 cx8800_vid_irq(dev);
1716 };
1717 if (10 == loop) {
1718 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1719 core->name);
1720 cx_write(MO_PCI_INTMSK,0);
1721 }
1722
1723 out:
1724 return IRQ_RETVAL(handled);
1725}
1726
1727/* ----------------------------------------------------------- */
1728/* exported stuff */
1729
1730static struct file_operations video_fops =
1731{
1732 .owner = THIS_MODULE,
1733 .open = video_open,
1734 .release = video_release,
1735 .read = video_read,
1736 .poll = video_poll,
1737 .mmap = video_mmap,
1738 .ioctl = video_ioctl,
1739 .llseek = no_llseek,
1740};
1741
Adrian Bunk408b6642005-05-01 08:59:29 -07001742static struct video_device cx8800_video_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 .name = "cx8800-video",
1745 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1746 .hardware = 0,
1747 .fops = &video_fops,
1748 .minor = -1,
1749};
1750
Adrian Bunk408b6642005-05-01 08:59:29 -07001751static struct video_device cx8800_vbi_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 .name = "cx8800-vbi",
1754 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1755 .hardware = 0,
1756 .fops = &video_fops,
1757 .minor = -1,
1758};
1759
1760static struct file_operations radio_fops =
1761{
1762 .owner = THIS_MODULE,
1763 .open = video_open,
1764 .release = video_release,
1765 .ioctl = radio_ioctl,
1766 .llseek = no_llseek,
1767};
1768
Adrian Bunk408b6642005-05-01 08:59:29 -07001769static struct video_device cx8800_radio_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 .name = "cx8800-radio",
1772 .type = VID_TYPE_TUNER,
1773 .hardware = 0,
1774 .fops = &radio_fops,
1775 .minor = -1,
1776};
1777
1778/* ----------------------------------------------------------- */
1779
1780static void cx8800_unregister_video(struct cx8800_dev *dev)
1781{
1782 if (dev->radio_dev) {
1783 if (-1 != dev->radio_dev->minor)
1784 video_unregister_device(dev->radio_dev);
1785 else
1786 video_device_release(dev->radio_dev);
1787 dev->radio_dev = NULL;
1788 }
1789 if (dev->vbi_dev) {
1790 if (-1 != dev->vbi_dev->minor)
1791 video_unregister_device(dev->vbi_dev);
1792 else
1793 video_device_release(dev->vbi_dev);
1794 dev->vbi_dev = NULL;
1795 }
1796 if (dev->video_dev) {
1797 if (-1 != dev->video_dev->minor)
1798 video_unregister_device(dev->video_dev);
1799 else
1800 video_device_release(dev->video_dev);
1801 dev->video_dev = NULL;
1802 }
1803}
1804
1805static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1806 const struct pci_device_id *pci_id)
1807{
1808 struct cx8800_dev *dev;
1809 struct cx88_core *core;
1810 int err;
1811
1812 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
1813 if (NULL == dev)
1814 return -ENOMEM;
1815 memset(dev,0,sizeof(*dev));
1816
1817 /* pci init */
1818 dev->pci = pci_dev;
1819 if (pci_enable_device(pci_dev)) {
1820 err = -EIO;
1821 goto fail_free;
1822 }
1823 core = cx88_core_get(dev->pci);
1824 if (NULL == core) {
1825 err = -EINVAL;
1826 goto fail_free;
1827 }
1828 dev->core = core;
1829
1830 /* print pci info */
1831 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1832 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1833 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1834 "latency: %d, mmio: 0x%lx\n", core->name,
1835 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1836 dev->pci_lat,pci_resource_start(pci_dev,0));
1837
1838 pci_set_master(pci_dev);
1839 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1840 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1841 err = -EIO;
1842 goto fail_core;
1843 }
1844
1845 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 spin_lock_init(&dev->slock);
1847 core->tvnorm = tvnorms;
1848
1849 /* init video dma queues */
1850 INIT_LIST_HEAD(&dev->vidq.active);
1851 INIT_LIST_HEAD(&dev->vidq.queued);
1852 dev->vidq.timeout.function = cx8800_vid_timeout;
1853 dev->vidq.timeout.data = (unsigned long)dev;
1854 init_timer(&dev->vidq.timeout);
1855 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1856 MO_VID_DMACNTRL,0x11,0x00);
1857
1858 /* init vbi dma queues */
1859 INIT_LIST_HEAD(&dev->vbiq.active);
1860 INIT_LIST_HEAD(&dev->vbiq.queued);
1861 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1862 dev->vbiq.timeout.data = (unsigned long)dev;
1863 init_timer(&dev->vbiq.timeout);
1864 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1865 MO_VID_DMACNTRL,0x88,0x00);
1866
1867 /* get irq */
1868 err = request_irq(pci_dev->irq, cx8800_irq,
1869 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
1870 if (err < 0) {
1871 printk(KERN_ERR "%s: can't get IRQ %d\n",
1872 core->name,pci_dev->irq);
1873 goto fail_core;
1874 }
1875 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1876
1877 /* load and configure helper modules */
1878 if (TUNER_ABSENT != core->tuner_type)
1879 request_module("tuner");
1880 if (core->tda9887_conf)
1881 request_module("tda9887");
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /* register v4l devices */
1884 dev->video_dev = cx88_vdev_init(core,dev->pci,
1885 &cx8800_video_template,"video");
1886 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1887 video_nr[core->nr]);
1888 if (err < 0) {
1889 printk(KERN_INFO "%s: can't register video device\n",
1890 core->name);
1891 goto fail_unreg;
1892 }
1893 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1894 core->name,dev->video_dev->minor & 0x1f);
1895
1896 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1897 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1898 vbi_nr[core->nr]);
1899 if (err < 0) {
1900 printk(KERN_INFO "%s/0: can't register vbi device\n",
1901 core->name);
1902 goto fail_unreg;
1903 }
1904 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1905 core->name,dev->vbi_dev->minor & 0x1f);
1906
1907 if (core->has_radio) {
1908 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1909 &cx8800_radio_template,"radio");
1910 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1911 radio_nr[core->nr]);
1912 if (err < 0) {
1913 printk(KERN_INFO "%s/0: can't register radio device\n",
1914 core->name);
1915 goto fail_unreg;
1916 }
1917 printk(KERN_INFO "%s/0: registered device radio%d\n",
1918 core->name,dev->radio_dev->minor & 0x1f);
1919 }
1920
1921 /* everything worked */
1922 list_add_tail(&dev->devlist,&cx8800_devlist);
1923 pci_set_drvdata(pci_dev,dev);
1924
1925 /* initial device configuration */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001926 down(&core->lock);
1927 init_controls(core);
1928 cx88_set_tvnorm(core,tvnorms);
1929 video_mux(core,0);
1930 up(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /* start tvaudio thread */
1933 if (core->tuner_type != TUNER_ABSENT)
1934 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1935 return 0;
1936
1937fail_unreg:
1938 cx8800_unregister_video(dev);
1939 free_irq(pci_dev->irq, dev);
1940fail_core:
1941 cx88_core_put(core,dev->pci);
1942fail_free:
1943 kfree(dev);
1944 return err;
1945}
1946
1947static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1948{
1949 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001950 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001953 if (core->kthread) {
1954 kthread_stop(core->kthread);
1955 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001958 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 pci_disable_device(pci_dev);
1960
1961 /* unregister stuff */
1962
1963 free_irq(pci_dev->irq, dev);
1964 cx8800_unregister_video(dev);
1965 pci_set_drvdata(pci_dev, NULL);
1966
1967 /* free memory */
1968 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1969 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001970 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 kfree(dev);
1972}
1973
1974static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1975{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001976 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 struct cx88_core *core = dev->core;
1978
1979 /* stop video+vbi capture */
1980 spin_lock(&dev->slock);
1981 if (!list_empty(&dev->vidq.active)) {
1982 printk("%s: suspend video\n", core->name);
1983 stop_video_dma(dev);
1984 del_timer(&dev->vidq.timeout);
1985 }
1986 if (!list_empty(&dev->vbiq.active)) {
1987 printk("%s: suspend vbi\n", core->name);
1988 cx8800_stop_vbi_dma(dev);
1989 del_timer(&dev->vbiq.timeout);
1990 }
1991 spin_unlock(&dev->slock);
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001994 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 pci_save_state(pci_dev);
1997 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1998 pci_disable_device(pci_dev);
1999 dev->state.disabled = 1;
2000 }
2001 return 0;
2002}
2003
2004static int cx8800_resume(struct pci_dev *pci_dev)
2005{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002006 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002008 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002011 err=pci_enable_device(pci_dev);
2012 if (err) {
2013 printk(KERN_ERR "%s: can't enable device\n",
2014 core->name);
2015 return err;
2016 }
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 dev->state.disabled = 0;
2019 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002020 err= pci_set_power_state(pci_dev, PCI_D0);
2021 if (err) {
2022 printk(KERN_ERR "%s: can't enable device\n",
2023 core->name);
2024
2025 pci_disable_device(pci_dev);
2026 dev->state.disabled = 1;
2027
2028 return err;
2029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 pci_restore_state(pci_dev);
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002033 cx88_reset(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /* restart video+vbi capture */
2036 spin_lock(&dev->slock);
2037 if (!list_empty(&dev->vidq.active)) {
2038 printk("%s: resume video\n", core->name);
2039 restart_video_queue(dev,&dev->vidq);
2040 }
2041 if (!list_empty(&dev->vbiq.active)) {
2042 printk("%s: resume vbi\n", core->name);
2043 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2044 }
2045 spin_unlock(&dev->slock);
2046
2047 return 0;
2048}
2049
2050/* ----------------------------------------------------------- */
2051
Adrian Bunk408b6642005-05-01 08:59:29 -07002052static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 {
2054 .vendor = 0x14f1,
2055 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002056 .subvendor = PCI_ANY_ID,
2057 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 },{
2059 /* --- end of list --- */
2060 }
2061};
2062MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2063
2064static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002065 .name = "cx8800",
2066 .id_table = cx8800_pci_tbl,
2067 .probe = cx8800_initdev,
2068 .remove = __devexit_p(cx8800_finidev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 .suspend = cx8800_suspend,
2071 .resume = cx8800_resume,
2072};
2073
2074static int cx8800_init(void)
2075{
2076 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2077 (CX88_VERSION_CODE >> 16) & 0xff,
2078 (CX88_VERSION_CODE >> 8) & 0xff,
2079 CX88_VERSION_CODE & 0xff);
2080#ifdef SNAPSHOT
2081 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2082 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2083#endif
2084 return pci_register_driver(&cx8800_pci_driver);
2085}
2086
2087static void cx8800_fini(void)
2088{
2089 pci_unregister_driver(&cx8800_pci_driver);
2090}
2091
2092module_init(cx8800_init);
2093module_exit(cx8800_fini);
2094
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002095EXPORT_SYMBOL(cx88_do_ioctl);
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097/* ----------------------------------------------------------- */
2098/*
2099 * Local variables:
2100 * c-basic-offset: 8
2101 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002102 * 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 -07002103 */