blob: f22ccb65de1c0c368e141c50c6e776b0ad946814 [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,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800103 },{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .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)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800473 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 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;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800489 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 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
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800790video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
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;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800925 u32 v_sat_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 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,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001255 struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001256{
1257 int err;
1258
Nickolay V. Shmyrev9f95a0b2005-11-08 21:36:37 -08001259 dprintk( 1, "CORE IOCTL: 0x%x\n", cmd );
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001260 if (video_debug > 1)
1261 cx88_print_ioctl(core->name,cmd);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001262
1263 switch (cmd) {
1264 /* ---------- tv norms ---------- */
1265 case VIDIOC_ENUMSTD:
1266 {
1267 struct v4l2_standard *e = arg;
1268 unsigned int i;
1269
1270 i = e->index;
1271 if (i >= ARRAY_SIZE(tvnorms))
1272 return -EINVAL;
1273 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1274 tvnorms[e->index].name);
1275 e->index = i;
1276 if (err < 0)
1277 return err;
1278 return 0;
1279 }
1280 case VIDIOC_G_STD:
1281 {
1282 v4l2_std_id *id = arg;
1283
1284 *id = core->tvnorm->id;
1285 return 0;
1286 }
1287 case VIDIOC_S_STD:
1288 {
1289 v4l2_std_id *id = arg;
1290 unsigned int i;
1291
1292 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1293 if (*id & tvnorms[i].id)
1294 break;
1295 if (i == ARRAY_SIZE(tvnorms))
1296 return -EINVAL;
1297
1298 down(&core->lock);
1299 cx88_set_tvnorm(core,&tvnorms[i]);
1300 up(&core->lock);
1301 return 0;
1302 }
1303
1304 /* ------ input switching ---------- */
1305 case VIDIOC_ENUMINPUT:
1306 {
1307 static const char *iname[] = {
1308 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1309 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1310 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1311 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1312 [ CX88_VMUX_SVIDEO ] = "S-Video",
1313 [ CX88_VMUX_TELEVISION ] = "Television",
1314 [ CX88_VMUX_CABLE ] = "Cable TV",
1315 [ CX88_VMUX_DVB ] = "DVB",
1316 [ CX88_VMUX_DEBUG ] = "for debug only",
1317 };
1318 struct v4l2_input *i = arg;
1319 unsigned int n;
1320
1321 n = i->index;
1322 if (n >= 4)
1323 return -EINVAL;
1324 if (0 == INPUT(n)->type)
1325 return -EINVAL;
1326 memset(i,0,sizeof(*i));
1327 i->index = n;
1328 i->type = V4L2_INPUT_TYPE_CAMERA;
1329 strcpy(i->name,iname[INPUT(n)->type]);
1330 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1331 (CX88_VMUX_CABLE == INPUT(n)->type))
1332 i->type = V4L2_INPUT_TYPE_TUNER;
1333 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1334 i->std |= tvnorms[n].id;
1335 return 0;
1336 }
1337 case VIDIOC_G_INPUT:
1338 {
1339 unsigned int *i = arg;
1340
1341 *i = core->input;
1342 return 0;
1343 }
1344 case VIDIOC_S_INPUT:
1345 {
1346 unsigned int *i = arg;
1347
1348 if (*i >= 4)
1349 return -EINVAL;
1350 down(&core->lock);
1351 cx88_newstation(core);
1352 video_mux(core,*i);
1353 up(&core->lock);
1354 return 0;
1355 }
1356
1357
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* --- controls ---------------------------------------------- */
1360 case VIDIOC_QUERYCTRL:
1361 {
1362 struct v4l2_queryctrl *c = arg;
1363 int i;
1364
1365 if (c->id < V4L2_CID_BASE ||
1366 c->id >= V4L2_CID_LASTP1)
1367 return -EINVAL;
1368 for (i = 0; i < CX8800_CTLS; i++)
1369 if (cx8800_ctls[i].v.id == c->id)
1370 break;
1371 if (i == CX8800_CTLS) {
1372 *c = no_ctl;
1373 return 0;
1374 }
1375 *c = cx8800_ctls[i].v;
1376 return 0;
1377 }
1378 case VIDIOC_G_CTRL:
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001379 return get_control(core,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 case VIDIOC_S_CTRL:
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001381 return set_control(core,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /* --- tuner ioctls ------------------------------------------ */
1384 case VIDIOC_G_TUNER:
1385 {
1386 struct v4l2_tuner *t = arg;
1387 u32 reg;
1388
1389 if (UNSET == core->tuner_type)
1390 return -EINVAL;
1391 if (0 != t->index)
1392 return -EINVAL;
1393
1394 memset(t,0,sizeof(*t));
1395 strcpy(t->name, "Television");
1396 t->type = V4L2_TUNER_ANALOG_TV;
1397 t->capability = V4L2_TUNER_CAP_NORM;
1398 t->rangehigh = 0xffffffffUL;
1399
1400 cx88_get_stereo(core ,t);
1401 reg = cx_read(MO_DEVICE_STATUS);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001402 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return 0;
1404 }
1405 case VIDIOC_S_TUNER:
1406 {
1407 struct v4l2_tuner *t = arg;
1408
1409 if (UNSET == core->tuner_type)
1410 return -EINVAL;
1411 if (0 != t->index)
1412 return -EINVAL;
1413 cx88_set_stereo(core, t->audmode, 1);
1414 return 0;
1415 }
1416 case VIDIOC_G_FREQUENCY:
1417 {
1418 struct v4l2_frequency *f = arg;
1419
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001420 memset(f,0,sizeof(*f));
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (UNSET == core->tuner_type)
1423 return -EINVAL;
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001424
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001425 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1426 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1427 f->frequency = core->freq;
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001428
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001429 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return 0;
1432 }
1433 case VIDIOC_S_FREQUENCY:
1434 {
1435 struct v4l2_frequency *f = arg;
1436
1437 if (UNSET == core->tuner_type)
1438 return -EINVAL;
1439 if (f->tuner != 0)
1440 return -EINVAL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001441 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EINVAL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001443 if (1 == radio && f->type != V4L2_TUNER_RADIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EINVAL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001445 down(&core->lock);
1446 core->freq = f->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 cx88_newstation(core);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001448 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -07001449
1450 /* When changing channels it is required to reset TVAUDIO */
1451 msleep (10);
1452 cx88_set_tvaudio(core);
1453
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001454 up(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456 }
1457
1458 default:
1459 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001460 driver_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462 return 0;
1463}
1464
1465static int video_ioctl(struct inode *inode, struct file *file,
1466 unsigned int cmd, unsigned long arg)
1467{
1468 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1469}
1470
1471/* ----------------------------------------------------------- */
1472
1473static int radio_do_ioctl(struct inode *inode, struct file *file,
1474 unsigned int cmd, void *arg)
1475{
1476 struct cx8800_fh *fh = file->private_data;
1477 struct cx8800_dev *dev = fh->dev;
1478 struct cx88_core *core = dev->core;
1479
1480 if (video_debug > 1)
1481 cx88_print_ioctl(core->name,cmd);
1482
1483 switch (cmd) {
1484 case VIDIOC_QUERYCAP:
1485 {
1486 struct v4l2_capability *cap = arg;
1487
1488 memset(cap,0,sizeof(*cap));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001489 strcpy(cap->driver, "cx8800");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 strlcpy(cap->card, cx88_boards[core->board].name,
1491 sizeof(cap->card));
1492 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1493 cap->version = CX88_VERSION_CODE;
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001494 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 0;
1496 }
1497 case VIDIOC_G_TUNER:
1498 {
1499 struct v4l2_tuner *t = arg;
1500
1501 if (t->index > 0)
1502 return -EINVAL;
1503
1504 memset(t,0,sizeof(*t));
1505 strcpy(t->name, "Radio");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001507 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 0;
1509 }
1510 case VIDIOC_ENUMINPUT:
1511 {
1512 struct v4l2_input *i = arg;
1513
1514 if (i->index != 0)
1515 return -EINVAL;
1516 strcpy(i->name,"Radio");
1517 i->type = V4L2_INPUT_TYPE_TUNER;
1518 return 0;
1519 }
1520 case VIDIOC_G_INPUT:
1521 {
1522 int *i = arg;
1523 *i = 0;
1524 return 0;
1525 }
1526 case VIDIOC_G_AUDIO:
1527 {
1528 struct v4l2_audio *a = arg;
1529
1530 memset(a,0,sizeof(*a));
1531 strcpy(a->name,"Radio");
1532 return 0;
1533 }
1534 case VIDIOC_G_STD:
1535 {
1536 v4l2_std_id *id = arg;
1537 *id = 0;
1538 return 0;
1539 }
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001540 case VIDIOCSTUNER:
1541 {
1542 struct video_tuner *v = arg;
1543
1544 if (v->tuner) /* Only tuner 0 */
1545 return -EINVAL;
1546
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001547 cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
1548 return 0;
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 case VIDIOC_S_TUNER:
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001551 {
1552 struct v4l2_tuner *t = arg;
1553
1554 if (0 != t->index)
1555 return -EINVAL;
1556
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001557 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
Mauro Carvalho Chehaba82decf2005-07-07 17:58:36 -07001558
1559 return 0;
1560 }
1561
1562 case VIDIOC_S_AUDIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 case VIDIOC_S_INPUT:
1564 case VIDIOC_S_STD:
1565 return 0;
1566
1567 case VIDIOC_QUERYCTRL:
1568 {
1569 struct v4l2_queryctrl *c = arg;
1570 int i;
1571
1572 if (c->id < V4L2_CID_BASE ||
1573 c->id >= V4L2_CID_LASTP1)
1574 return -EINVAL;
1575 if (c->id == V4L2_CID_AUDIO_MUTE) {
1576 for (i = 0; i < CX8800_CTLS; i++)
1577 if (cx8800_ctls[i].v.id == c->id)
1578 break;
1579 *c = cx8800_ctls[i].v;
1580 } else
1581 *c = no_ctl;
1582 return 0;
1583 }
1584
1585
1586 case VIDIOC_G_CTRL:
1587 case VIDIOC_S_CTRL:
1588 case VIDIOC_G_FREQUENCY:
1589 case VIDIOC_S_FREQUENCY:
1590 return video_do_ioctl(inode,file,cmd,arg);
1591
1592 default:
1593 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1594 radio_do_ioctl);
1595 }
1596 return 0;
1597};
1598
1599static int radio_ioctl(struct inode *inode, struct file *file,
1600 unsigned int cmd, unsigned long arg)
1601{
1602 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1603};
1604
1605/* ----------------------------------------------------------- */
1606
1607static void cx8800_vid_timeout(unsigned long data)
1608{
1609 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1610 struct cx88_core *core = dev->core;
1611 struct cx88_dmaqueue *q = &dev->vidq;
1612 struct cx88_buffer *buf;
1613 unsigned long flags;
1614
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001615 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 cx_clear(MO_VID_DMACNTRL, 0x11);
1618 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1619
1620 spin_lock_irqsave(&dev->slock,flags);
1621 while (!list_empty(&q->active)) {
1622 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1623 list_del(&buf->vb.queue);
1624 buf->vb.state = STATE_ERROR;
1625 wake_up(&buf->vb.done);
1626 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1627 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1628 }
1629 restart_video_queue(dev,q);
1630 spin_unlock_irqrestore(&dev->slock,flags);
1631}
1632
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001633static char *cx88_vid_irqs[32] = {
1634 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1635 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1636 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1637 "y_sync", "u_sync", "v_sync", "vbi_sync",
1638 "opc_err", "par_err", "rip_err", "pci_abort",
1639};
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641static void cx8800_vid_irq(struct cx8800_dev *dev)
1642{
1643 struct cx88_core *core = dev->core;
1644 u32 status, mask, count;
1645
1646 status = cx_read(MO_VID_INTSTAT);
1647 mask = cx_read(MO_VID_INTMSK);
1648 if (0 == (status & mask))
1649 return;
1650 cx_write(MO_VID_INTSTAT, status);
1651 if (irq_debug || (status & mask & ~0xff))
1652 cx88_print_irqbits(core->name, "irq vid",
1653 cx88_vid_irqs, status, mask);
1654
1655 /* risc op code error */
1656 if (status & (1 << 16)) {
1657 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1658 cx_clear(MO_VID_DMACNTRL, 0x11);
1659 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001660 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
1663 /* risc1 y */
1664 if (status & 0x01) {
1665 spin_lock(&dev->slock);
1666 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001667 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 spin_unlock(&dev->slock);
1669 }
1670
1671 /* risc1 vbi */
1672 if (status & 0x08) {
1673 spin_lock(&dev->slock);
1674 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001675 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 spin_unlock(&dev->slock);
1677 }
1678
1679 /* risc2 y */
1680 if (status & 0x10) {
1681 dprintk(2,"stopper video\n");
1682 spin_lock(&dev->slock);
1683 restart_video_queue(dev,&dev->vidq);
1684 spin_unlock(&dev->slock);
1685 }
1686
1687 /* risc2 vbi */
1688 if (status & 0x80) {
1689 dprintk(2,"stopper vbi\n");
1690 spin_lock(&dev->slock);
1691 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1692 spin_unlock(&dev->slock);
1693 }
1694}
1695
1696static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1697{
1698 struct cx8800_dev *dev = dev_id;
1699 struct cx88_core *core = dev->core;
1700 u32 status;
1701 int loop, handled = 0;
1702
1703 for (loop = 0; loop < 10; loop++) {
1704 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1705 if (0 == status)
1706 goto out;
1707 cx_write(MO_PCI_INTSTAT, status);
1708 handled = 1;
1709
1710 if (status & core->pci_irqmask)
1711 cx88_core_irq(core,status);
1712 if (status & 0x01)
1713 cx8800_vid_irq(dev);
1714 };
1715 if (10 == loop) {
1716 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1717 core->name);
1718 cx_write(MO_PCI_INTMSK,0);
1719 }
1720
1721 out:
1722 return IRQ_RETVAL(handled);
1723}
1724
1725/* ----------------------------------------------------------- */
1726/* exported stuff */
1727
1728static struct file_operations video_fops =
1729{
1730 .owner = THIS_MODULE,
1731 .open = video_open,
1732 .release = video_release,
1733 .read = video_read,
1734 .poll = video_poll,
1735 .mmap = video_mmap,
1736 .ioctl = video_ioctl,
1737 .llseek = no_llseek,
1738};
1739
Adrian Bunk408b6642005-05-01 08:59:29 -07001740static struct video_device cx8800_video_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 .name = "cx8800-video",
1743 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1744 .hardware = 0,
1745 .fops = &video_fops,
1746 .minor = -1,
1747};
1748
Adrian Bunk408b6642005-05-01 08:59:29 -07001749static struct video_device cx8800_vbi_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 .name = "cx8800-vbi",
1752 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1753 .hardware = 0,
1754 .fops = &video_fops,
1755 .minor = -1,
1756};
1757
1758static struct file_operations radio_fops =
1759{
1760 .owner = THIS_MODULE,
1761 .open = video_open,
1762 .release = video_release,
1763 .ioctl = radio_ioctl,
1764 .llseek = no_llseek,
1765};
1766
Adrian Bunk408b6642005-05-01 08:59:29 -07001767static struct video_device cx8800_radio_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 .name = "cx8800-radio",
1770 .type = VID_TYPE_TUNER,
1771 .hardware = 0,
1772 .fops = &radio_fops,
1773 .minor = -1,
1774};
1775
1776/* ----------------------------------------------------------- */
1777
1778static void cx8800_unregister_video(struct cx8800_dev *dev)
1779{
1780 if (dev->radio_dev) {
1781 if (-1 != dev->radio_dev->minor)
1782 video_unregister_device(dev->radio_dev);
1783 else
1784 video_device_release(dev->radio_dev);
1785 dev->radio_dev = NULL;
1786 }
1787 if (dev->vbi_dev) {
1788 if (-1 != dev->vbi_dev->minor)
1789 video_unregister_device(dev->vbi_dev);
1790 else
1791 video_device_release(dev->vbi_dev);
1792 dev->vbi_dev = NULL;
1793 }
1794 if (dev->video_dev) {
1795 if (-1 != dev->video_dev->minor)
1796 video_unregister_device(dev->video_dev);
1797 else
1798 video_device_release(dev->video_dev);
1799 dev->video_dev = NULL;
1800 }
1801}
1802
1803static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1804 const struct pci_device_id *pci_id)
1805{
1806 struct cx8800_dev *dev;
1807 struct cx88_core *core;
1808 int err;
1809
1810 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
1811 if (NULL == dev)
1812 return -ENOMEM;
1813 memset(dev,0,sizeof(*dev));
1814
1815 /* pci init */
1816 dev->pci = pci_dev;
1817 if (pci_enable_device(pci_dev)) {
1818 err = -EIO;
1819 goto fail_free;
1820 }
1821 core = cx88_core_get(dev->pci);
1822 if (NULL == core) {
1823 err = -EINVAL;
1824 goto fail_free;
1825 }
1826 dev->core = core;
1827
1828 /* print pci info */
1829 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001830 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1831 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 "latency: %d, mmio: 0x%lx\n", core->name,
1833 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1834 dev->pci_lat,pci_resource_start(pci_dev,0));
1835
1836 pci_set_master(pci_dev);
1837 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1838 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1839 err = -EIO;
1840 goto fail_core;
1841 }
1842
1843 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 spin_lock_init(&dev->slock);
1845 core->tvnorm = tvnorms;
1846
1847 /* init video dma queues */
1848 INIT_LIST_HEAD(&dev->vidq.active);
1849 INIT_LIST_HEAD(&dev->vidq.queued);
1850 dev->vidq.timeout.function = cx8800_vid_timeout;
1851 dev->vidq.timeout.data = (unsigned long)dev;
1852 init_timer(&dev->vidq.timeout);
1853 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1854 MO_VID_DMACNTRL,0x11,0x00);
1855
1856 /* init vbi dma queues */
1857 INIT_LIST_HEAD(&dev->vbiq.active);
1858 INIT_LIST_HEAD(&dev->vbiq.queued);
1859 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1860 dev->vbiq.timeout.data = (unsigned long)dev;
1861 init_timer(&dev->vbiq.timeout);
1862 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1863 MO_VID_DMACNTRL,0x88,0x00);
1864
1865 /* get irq */
1866 err = request_irq(pci_dev->irq, cx8800_irq,
1867 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
1868 if (err < 0) {
1869 printk(KERN_ERR "%s: can't get IRQ %d\n",
1870 core->name,pci_dev->irq);
1871 goto fail_core;
1872 }
1873 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1874
1875 /* load and configure helper modules */
1876 if (TUNER_ABSENT != core->tuner_type)
1877 request_module("tuner");
1878 if (core->tda9887_conf)
1879 request_module("tda9887");
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* register v4l devices */
1882 dev->video_dev = cx88_vdev_init(core,dev->pci,
1883 &cx8800_video_template,"video");
1884 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1885 video_nr[core->nr]);
1886 if (err < 0) {
1887 printk(KERN_INFO "%s: can't register video device\n",
1888 core->name);
1889 goto fail_unreg;
1890 }
1891 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1892 core->name,dev->video_dev->minor & 0x1f);
1893
1894 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1895 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1896 vbi_nr[core->nr]);
1897 if (err < 0) {
1898 printk(KERN_INFO "%s/0: can't register vbi device\n",
1899 core->name);
1900 goto fail_unreg;
1901 }
1902 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1903 core->name,dev->vbi_dev->minor & 0x1f);
1904
1905 if (core->has_radio) {
1906 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1907 &cx8800_radio_template,"radio");
1908 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1909 radio_nr[core->nr]);
1910 if (err < 0) {
1911 printk(KERN_INFO "%s/0: can't register radio device\n",
1912 core->name);
1913 goto fail_unreg;
1914 }
1915 printk(KERN_INFO "%s/0: registered device radio%d\n",
1916 core->name,dev->radio_dev->minor & 0x1f);
1917 }
1918
1919 /* everything worked */
1920 list_add_tail(&dev->devlist,&cx8800_devlist);
1921 pci_set_drvdata(pci_dev,dev);
1922
1923 /* initial device configuration */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001924 down(&core->lock);
1925 init_controls(core);
1926 cx88_set_tvnorm(core,tvnorms);
1927 video_mux(core,0);
1928 up(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* start tvaudio thread */
1931 if (core->tuner_type != TUNER_ABSENT)
1932 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1933 return 0;
1934
1935fail_unreg:
1936 cx8800_unregister_video(dev);
1937 free_irq(pci_dev->irq, dev);
1938fail_core:
1939 cx88_core_put(core,dev->pci);
1940fail_free:
1941 kfree(dev);
1942 return err;
1943}
1944
1945static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1946{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001947 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001948 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001951 if (core->kthread) {
1952 kthread_stop(core->kthread);
1953 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001956 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 pci_disable_device(pci_dev);
1958
1959 /* unregister stuff */
1960
1961 free_irq(pci_dev->irq, dev);
1962 cx8800_unregister_video(dev);
1963 pci_set_drvdata(pci_dev, NULL);
1964
1965 /* free memory */
1966 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1967 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001968 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 kfree(dev);
1970}
1971
1972static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1973{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001974 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 struct cx88_core *core = dev->core;
1976
1977 /* stop video+vbi capture */
1978 spin_lock(&dev->slock);
1979 if (!list_empty(&dev->vidq.active)) {
1980 printk("%s: suspend video\n", core->name);
1981 stop_video_dma(dev);
1982 del_timer(&dev->vidq.timeout);
1983 }
1984 if (!list_empty(&dev->vbiq.active)) {
1985 printk("%s: suspend vbi\n", core->name);
1986 cx8800_stop_vbi_dma(dev);
1987 del_timer(&dev->vbiq.timeout);
1988 }
1989 spin_unlock(&dev->slock);
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001992 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 pci_save_state(pci_dev);
1995 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1996 pci_disable_device(pci_dev);
1997 dev->state.disabled = 1;
1998 }
1999 return 0;
2000}
2001
2002static int cx8800_resume(struct pci_dev *pci_dev)
2003{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002004 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002006 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002009 err=pci_enable_device(pci_dev);
2010 if (err) {
2011 printk(KERN_ERR "%s: can't enable device\n",
2012 core->name);
2013 return err;
2014 }
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 dev->state.disabled = 0;
2017 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002018 err= pci_set_power_state(pci_dev, PCI_D0);
2019 if (err) {
2020 printk(KERN_ERR "%s: can't enable device\n",
2021 core->name);
2022
2023 pci_disable_device(pci_dev);
2024 dev->state.disabled = 1;
2025
2026 return err;
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 pci_restore_state(pci_dev);
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002031 cx88_reset(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 /* restart video+vbi capture */
2034 spin_lock(&dev->slock);
2035 if (!list_empty(&dev->vidq.active)) {
2036 printk("%s: resume video\n", core->name);
2037 restart_video_queue(dev,&dev->vidq);
2038 }
2039 if (!list_empty(&dev->vbiq.active)) {
2040 printk("%s: resume vbi\n", core->name);
2041 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2042 }
2043 spin_unlock(&dev->slock);
2044
2045 return 0;
2046}
2047
2048/* ----------------------------------------------------------- */
2049
Adrian Bunk408b6642005-05-01 08:59:29 -07002050static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 {
2052 .vendor = 0x14f1,
2053 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002054 .subvendor = PCI_ANY_ID,
2055 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 },{
2057 /* --- end of list --- */
2058 }
2059};
2060MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2061
2062static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002063 .name = "cx8800",
2064 .id_table = cx8800_pci_tbl,
2065 .probe = cx8800_initdev,
2066 .remove = __devexit_p(cx8800_finidev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 .suspend = cx8800_suspend,
2069 .resume = cx8800_resume,
2070};
2071
2072static int cx8800_init(void)
2073{
2074 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2075 (CX88_VERSION_CODE >> 16) & 0xff,
2076 (CX88_VERSION_CODE >> 8) & 0xff,
2077 CX88_VERSION_CODE & 0xff);
2078#ifdef SNAPSHOT
2079 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2080 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2081#endif
2082 return pci_register_driver(&cx8800_pci_driver);
2083}
2084
2085static void cx8800_fini(void)
2086{
2087 pci_unregister_driver(&cx8800_pci_driver);
2088}
2089
2090module_init(cx8800_init);
2091module_exit(cx8800_fini);
2092
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002093EXPORT_SYMBOL(cx88_do_ioctl);
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095/* ----------------------------------------------------------- */
2096/*
2097 * Local variables:
2098 * c-basic-offset: 8
2099 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002100 * 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 -07002101 */