blob: b0dd4313a841a839078c72b60faf8818a623447d [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 *
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03008 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * - Multituner support
10 * - video_ioctl2 conversion
11 * - PAL/M fixes
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/moduleparam.h>
32#include <linux/kmod.h>
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070036#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/delay.h>
38#include <linux/kthread.h>
39#include <asm/div64.h>
40
41#include "cx88.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020042#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030044#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080045/* Include V4L1 specific functions. Should be removed soon */
46#include <linux/videodev.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030047#endif
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
50MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
51MODULE_LICENSE("GPL");
52
53/* ------------------------------------------------------------------ */
54
55static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
58
59module_param_array(video_nr, int, NULL, 0444);
60module_param_array(vbi_nr, int, NULL, 0444);
61module_param_array(radio_nr, int, NULL, 0444);
62
63MODULE_PARM_DESC(video_nr,"video device numbers");
64MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
65MODULE_PARM_DESC(radio_nr,"radio device numbers");
66
67static unsigned int video_debug = 0;
68module_param(video_debug,int,0644);
69MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
70
71static unsigned int irq_debug = 0;
72module_param(irq_debug,int,0644);
73MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
74
75static unsigned int vid_limit = 16;
76module_param(vid_limit,int,0644);
77MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
78
79#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070080 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* ------------------------------------------------------------------ */
83
84static LIST_HEAD(cx8800_devlist);
85
86/* ------------------------------------------------------------------- */
87/* static data */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct cx8800_fmt formats[] = {
90 {
91 .name = "8 bpp, gray",
92 .fourcc = V4L2_PIX_FMT_GREY,
93 .cxformat = ColorFormatY8,
94 .depth = 8,
95 .flags = FORMAT_FLAGS_PACKED,
96 },{
97 .name = "15 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_RGB555,
99 .cxformat = ColorFormatRGB15,
100 .depth = 16,
101 .flags = FORMAT_FLAGS_PACKED,
102 },{
103 .name = "15 bpp RGB, be",
104 .fourcc = V4L2_PIX_FMT_RGB555X,
105 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
106 .depth = 16,
107 .flags = FORMAT_FLAGS_PACKED,
108 },{
109 .name = "16 bpp RGB, le",
110 .fourcc = V4L2_PIX_FMT_RGB565,
111 .cxformat = ColorFormatRGB16,
112 .depth = 16,
113 .flags = FORMAT_FLAGS_PACKED,
114 },{
115 .name = "16 bpp RGB, be",
116 .fourcc = V4L2_PIX_FMT_RGB565X,
117 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
118 .depth = 16,
119 .flags = FORMAT_FLAGS_PACKED,
120 },{
121 .name = "24 bpp RGB, le",
122 .fourcc = V4L2_PIX_FMT_BGR24,
123 .cxformat = ColorFormatRGB24,
124 .depth = 24,
125 .flags = FORMAT_FLAGS_PACKED,
126 },{
127 .name = "32 bpp RGB, le",
128 .fourcc = V4L2_PIX_FMT_BGR32,
129 .cxformat = ColorFormatRGB32,
130 .depth = 32,
131 .flags = FORMAT_FLAGS_PACKED,
132 },{
133 .name = "32 bpp RGB, be",
134 .fourcc = V4L2_PIX_FMT_RGB32,
135 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
136 .depth = 32,
137 .flags = FORMAT_FLAGS_PACKED,
138 },{
139 .name = "4:2:2, packed, YUYV",
140 .fourcc = V4L2_PIX_FMT_YUYV,
141 .cxformat = ColorFormatYUY2,
142 .depth = 16,
143 .flags = FORMAT_FLAGS_PACKED,
144 },{
145 .name = "4:2:2, packed, UYVY",
146 .fourcc = V4L2_PIX_FMT_UYVY,
147 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
148 .depth = 16,
149 .flags = FORMAT_FLAGS_PACKED,
150 },
151};
152
153static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
154{
155 unsigned int i;
156
157 for (i = 0; i < ARRAY_SIZE(formats); i++)
158 if (formats[i].fourcc == fourcc)
159 return formats+i;
160 return NULL;
161}
162
163/* ------------------------------------------------------------------- */
164
165static const struct v4l2_queryctrl no_ctl = {
166 .name = "42",
167 .flags = V4L2_CTRL_FLAG_DISABLED,
168};
169
170static struct cx88_ctrl cx8800_ctls[] = {
171 /* --- video --- */
172 {
173 .v = {
174 .id = V4L2_CID_BRIGHTNESS,
175 .name = "Brightness",
176 .minimum = 0x00,
177 .maximum = 0xff,
178 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300179 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .type = V4L2_CTRL_TYPE_INTEGER,
181 },
182 .off = 128,
183 .reg = MO_CONTR_BRIGHT,
184 .mask = 0x00ff,
185 .shift = 0,
186 },{
187 .v = {
188 .id = V4L2_CID_CONTRAST,
189 .name = "Contrast",
190 .minimum = 0,
191 .maximum = 0xff,
192 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200193 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .type = V4L2_CTRL_TYPE_INTEGER,
195 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700196 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .reg = MO_CONTR_BRIGHT,
198 .mask = 0xff00,
199 .shift = 8,
200 },{
201 .v = {
202 .id = V4L2_CID_HUE,
203 .name = "Hue",
204 .minimum = 0,
205 .maximum = 0xff,
206 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300207 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .type = V4L2_CTRL_TYPE_INTEGER,
209 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700210 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .reg = MO_HUE,
212 .mask = 0x00ff,
213 .shift = 0,
214 },{
215 /* strictly, this only describes only U saturation.
216 * V saturation is handled specially through code.
217 */
218 .v = {
219 .id = V4L2_CID_SATURATION,
220 .name = "Saturation",
221 .minimum = 0,
222 .maximum = 0xff,
223 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200224 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .type = V4L2_CTRL_TYPE_INTEGER,
226 },
227 .off = 0,
228 .reg = MO_UV_SATURATION,
229 .mask = 0x00ff,
230 .shift = 0,
231 },{
232 /* --- audio --- */
233 .v = {
234 .id = V4L2_CID_AUDIO_MUTE,
235 .name = "Mute",
236 .minimum = 0,
237 .maximum = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200238 .default_value = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .type = V4L2_CTRL_TYPE_BOOLEAN,
240 },
241 .reg = AUD_VOL_CTL,
242 .sreg = SHADOW_AUD_VOL_CTL,
243 .mask = (1 << 6),
244 .shift = 6,
245 },{
246 .v = {
247 .id = V4L2_CID_AUDIO_VOLUME,
248 .name = "Volume",
249 .minimum = 0,
250 .maximum = 0x3f,
251 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300252 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .type = V4L2_CTRL_TYPE_INTEGER,
254 },
255 .reg = AUD_VOL_CTL,
256 .sreg = SHADOW_AUD_VOL_CTL,
257 .mask = 0x3f,
258 .shift = 0,
259 },{
260 .v = {
261 .id = V4L2_CID_AUDIO_BALANCE,
262 .name = "Balance",
263 .minimum = 0,
264 .maximum = 0x7f,
265 .step = 1,
266 .default_value = 0x40,
267 .type = V4L2_CTRL_TYPE_INTEGER,
268 },
269 .reg = AUD_BAL_CTL,
270 .sreg = SHADOW_AUD_BAL_CTL,
271 .mask = 0x7f,
272 .shift = 0,
273 }
274};
Adrian Bunk408b6642005-05-01 08:59:29 -0700275static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Michael Krufky38a27132006-06-26 23:42:39 -0300277const u32 cx88_user_ctrls[] = {
278 V4L2_CID_USER_CLASS,
279 V4L2_CID_BRIGHTNESS,
280 V4L2_CID_CONTRAST,
281 V4L2_CID_SATURATION,
282 V4L2_CID_HUE,
283 V4L2_CID_AUDIO_VOLUME,
284 V4L2_CID_AUDIO_BALANCE,
285 V4L2_CID_AUDIO_MUTE,
286 0
287};
288EXPORT_SYMBOL(cx88_user_ctrls);
289
290static const u32 *ctrl_classes[] = {
291 cx88_user_ctrls,
292 NULL
293};
294
295int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
296{
297 int i;
298
299 if (qctrl->id < V4L2_CID_BASE ||
300 qctrl->id >= V4L2_CID_LASTP1)
301 return -EINVAL;
302 for (i = 0; i < CX8800_CTLS; i++)
303 if (cx8800_ctls[i].v.id == qctrl->id)
304 break;
305 if (i == CX8800_CTLS) {
306 *qctrl = no_ctl;
307 return 0;
308 }
309 *qctrl = cx8800_ctls[i].v;
310 return 0;
311}
312EXPORT_SYMBOL(cx8800_ctrl_query);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* ------------------------------------------------------------------- */
315/* resource management */
316
317static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
318{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700319 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (fh->resources & bit)
321 /* have it already allocated */
322 return 1;
323
324 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200325 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (dev->resources & bit) {
327 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200328 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330 }
331 /* it's free, grab it */
332 fh->resources |= bit;
333 dev->resources |= bit;
334 dprintk(1,"res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200335 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 1;
337}
338
339static
340int res_check(struct cx8800_fh *fh, unsigned int bit)
341{
342 return (fh->resources & bit);
343}
344
345static
346int res_locked(struct cx8800_dev *dev, unsigned int bit)
347{
348 return (dev->resources & bit);
349}
350
351static
352void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
353{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700354 struct cx88_core *core = dev->core;
Eric Sesterhennae246012006-03-13 13:17:11 -0300355 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Ingo Molnar3593cab2006-02-07 06:49:14 -0200357 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 fh->resources &= ~bits;
359 dev->resources &= ~bits;
360 dprintk(1,"res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200361 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364/* ------------------------------------------------------------------ */
365
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300366int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700368 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300371 input, INPUT(input).vmux,
372 INPUT(input).gpio0,INPUT(input).gpio1,
373 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700374 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300375 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
376 cx_write(MO_GP3_IO, INPUT(input).gpio3);
377 cx_write(MO_GP0_IO, INPUT(input).gpio0);
378 cx_write(MO_GP1_IO, INPUT(input).gpio1);
379 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Trent Piepho6a59d642007-08-15 14:41:57 -0300381 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 case CX88_VMUX_SVIDEO:
383 cx_set(MO_AFECFG_IO, 0x00000001);
384 cx_set(MO_INPUT_FORMAT, 0x00010010);
385 cx_set(MO_FILTER_EVEN, 0x00002020);
386 cx_set(MO_FILTER_ODD, 0x00002020);
387 break;
388 default:
389 cx_clear(MO_AFECFG_IO, 0x00000001);
390 cx_clear(MO_INPUT_FORMAT, 0x00010010);
391 cx_clear(MO_FILTER_EVEN, 0x00002020);
392 cx_clear(MO_FILTER_ODD, 0x00002020);
393 break;
394 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300395
Trent Piepho6a59d642007-08-15 14:41:57 -0300396 if (core->board.mpeg & CX88_MPEG_BLACKBIRD) {
Michael Krufkyf24546a2006-10-16 16:51:11 -0300397 /* sets sound input from external adc */
Trent Piepho6a59d642007-08-15 14:41:57 -0300398 if (INPUT(input).extadc)
Michael Krufkyf24546a2006-10-16 16:51:11 -0300399 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
400 else
401 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300405EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407/* ------------------------------------------------------------------ */
408
409static int start_video_dma(struct cx8800_dev *dev,
410 struct cx88_dmaqueue *q,
411 struct cx88_buffer *buf)
412{
413 struct cx88_core *core = dev->core;
414
415 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700416 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700418 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
420
421 /* reset counter */
422 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
423 q->count = 1;
424
425 /* enable irqs */
426 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700427
428 /* Enables corresponding bits at PCI_INT_STAT:
429 bits 0 to 4: video, audio, transport stream, VIP, Host
430 bit 7: timer
431 bits 8 and 9: DMA complete for: SRC, DST
432 bits 10 and 11: BERR signal asserted for RISC: RD, WR
433 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 cx_set(MO_VID_INTMSK, 0x0f0011);
436
437 /* enable capture */
438 cx_set(VID_CAPTURE_CONTROL,0x06);
439
440 /* start dma */
441 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700442 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return 0;
445}
446
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300447#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int stop_video_dma(struct cx8800_dev *dev)
449{
450 struct cx88_core *core = dev->core;
451
452 /* stop dma */
453 cx_clear(MO_VID_DMACNTRL, 0x11);
454
455 /* disable capture */
456 cx_clear(VID_CAPTURE_CONTROL,0x06);
457
458 /* disable irqs */
459 cx_clear(MO_PCI_INTMSK, 0x000001);
460 cx_clear(MO_VID_INTMSK, 0x0f0011);
461 return 0;
462}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300463#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
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) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700491 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 start_video_dma(dev, q, buf);
493 buf->vb.state = STATE_ACTIVE;
494 buf->count = q->count++;
495 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
496 dprintk(2,"[%p/%d] restart_queue - first active\n",
497 buf,buf->vb.i);
498
499 } else if (prev->vb.width == buf->vb.width &&
500 prev->vb.height == buf->vb.height &&
501 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700502 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 buf->vb.state = STATE_ACTIVE;
504 buf->count = q->count++;
505 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
506 dprintk(2,"[%p/%d] restart_queue - move to active\n",
507 buf,buf->vb.i);
508 } else {
509 return 0;
510 }
511 prev = buf;
512 }
513}
514
515/* ------------------------------------------------------------------ */
516
517static int
518buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
519{
520 struct cx8800_fh *fh = q->priv_data;
521
522 *size = fh->fmt->depth*fh->width*fh->height >> 3;
523 if (0 == *count)
524 *count = 32;
525 while (*size * *count > vid_limit * 1024 * 1024)
526 (*count)--;
527 return 0;
528}
529
530static int
531buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
532 enum v4l2_field field)
533{
534 struct cx8800_fh *fh = q->priv_data;
535 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700536 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
538 int rc, init_buffer = 0;
539
540 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700541 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
542 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -EINVAL;
544 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
545 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
546 return -EINVAL;
547
548 if (buf->fmt != fh->fmt ||
549 buf->vb.width != fh->width ||
550 buf->vb.height != fh->height ||
551 buf->vb.field != field) {
552 buf->fmt = fh->fmt;
553 buf->vb.width = fh->width;
554 buf->vb.height = fh->height;
555 buf->vb.field = field;
556 init_buffer = 1;
557 }
558
559 if (STATE_NEEDS_INIT == buf->vb.state) {
560 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300561 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto fail;
563 }
564
565 if (init_buffer) {
566 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
567 switch (buf->vb.field) {
568 case V4L2_FIELD_TOP:
569 cx88_risc_buffer(dev->pci, &buf->risc,
570 buf->vb.dma.sglist, 0, UNSET,
571 buf->bpl, 0, buf->vb.height);
572 break;
573 case V4L2_FIELD_BOTTOM:
574 cx88_risc_buffer(dev->pci, &buf->risc,
575 buf->vb.dma.sglist, UNSET, 0,
576 buf->bpl, 0, buf->vb.height);
577 break;
578 case V4L2_FIELD_INTERLACED:
579 cx88_risc_buffer(dev->pci, &buf->risc,
580 buf->vb.dma.sglist, 0, buf->bpl,
581 buf->bpl, buf->bpl,
582 buf->vb.height >> 1);
583 break;
584 case V4L2_FIELD_SEQ_TB:
585 cx88_risc_buffer(dev->pci, &buf->risc,
586 buf->vb.dma.sglist,
587 0, buf->bpl * (buf->vb.height >> 1),
588 buf->bpl, 0,
589 buf->vb.height >> 1);
590 break;
591 case V4L2_FIELD_SEQ_BT:
592 cx88_risc_buffer(dev->pci, &buf->risc,
593 buf->vb.dma.sglist,
594 buf->bpl * (buf->vb.height >> 1), 0,
595 buf->bpl, 0,
596 buf->vb.height >> 1);
597 break;
598 default:
599 BUG();
600 }
601 }
602 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
603 buf, buf->vb.i,
604 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
605 (unsigned long)buf->risc.dma);
606
607 buf->vb.state = STATE_PREPARED;
608 return 0;
609
610 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300611 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return rc;
613}
614
615static void
616buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
617{
618 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
619 struct cx88_buffer *prev;
620 struct cx8800_fh *fh = vq->priv_data;
621 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700622 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct cx88_dmaqueue *q = &dev->vidq;
624
625 /* add jump to stopper */
626 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
627 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
628
629 if (!list_empty(&q->queued)) {
630 list_add_tail(&buf->vb.queue,&q->queued);
631 buf->vb.state = STATE_QUEUED;
632 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
633 buf, buf->vb.i);
634
635 } else if (list_empty(&q->active)) {
636 list_add_tail(&buf->vb.queue,&q->active);
637 start_video_dma(dev, q, buf);
638 buf->vb.state = STATE_ACTIVE;
639 buf->count = q->count++;
640 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
641 dprintk(2,"[%p/%d] buffer_queue - first active\n",
642 buf, buf->vb.i);
643
644 } else {
645 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
646 if (prev->vb.width == buf->vb.width &&
647 prev->vb.height == buf->vb.height &&
648 prev->fmt == buf->fmt) {
649 list_add_tail(&buf->vb.queue,&q->active);
650 buf->vb.state = STATE_ACTIVE;
651 buf->count = q->count++;
652 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
653 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
654 buf, buf->vb.i);
655
656 } else {
657 list_add_tail(&buf->vb.queue,&q->queued);
658 buf->vb.state = STATE_QUEUED;
659 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
660 buf, buf->vb.i);
661 }
662 }
663}
664
665static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
666{
667 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300669 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Adrian Bunk408b6642005-05-01 08:59:29 -0700672static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 .buf_setup = buffer_setup,
674 .buf_prepare = buffer_prepare,
675 .buf_queue = buffer_queue,
676 .buf_release = buffer_release,
677};
678
679/* ------------------------------------------------------------------ */
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682/* ------------------------------------------------------------------ */
683
684static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
685{
686 switch (fh->type) {
687 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
688 return &fh->vidq;
689 case V4L2_BUF_TYPE_VBI_CAPTURE:
690 return &fh->vbiq;
691 default:
692 BUG();
693 return NULL;
694 }
695}
696
697static int get_ressource(struct cx8800_fh *fh)
698{
699 switch (fh->type) {
700 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
701 return RESOURCE_VIDEO;
702 case V4L2_BUF_TYPE_VBI_CAPTURE:
703 return RESOURCE_VBI;
704 default:
705 BUG();
706 return 0;
707 }
708}
709
710static int video_open(struct inode *inode, struct file *file)
711{
712 int minor = iminor(inode);
713 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700714 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct cx8800_fh *fh;
716 struct list_head *list;
717 enum v4l2_buf_type type = 0;
718 int radio = 0;
719
720 list_for_each(list,&cx8800_devlist) {
721 h = list_entry(list, struct cx8800_dev, devlist);
722 if (h->video_dev->minor == minor) {
723 dev = h;
724 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
725 }
726 if (h->vbi_dev->minor == minor) {
727 dev = h;
728 type = V4L2_BUF_TYPE_VBI_CAPTURE;
729 }
730 if (h->radio_dev &&
731 h->radio_dev->minor == minor) {
732 radio = 1;
733 dev = h;
734 }
735 }
736 if (NULL == dev)
737 return -ENODEV;
738
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700739 core = dev->core;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 dprintk(1,"open minor=%d radio=%d type=%s\n",
742 minor,radio,v4l2_type_names[type]);
743
744 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200745 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (NULL == fh)
747 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 file->private_data = fh;
749 fh->dev = dev;
750 fh->radio = radio;
751 fh->type = type;
752 fh->width = 320;
753 fh->height = 240;
754 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
755
756 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
757 dev->pci, &dev->slock,
758 V4L2_BUF_TYPE_VIDEO_CAPTURE,
759 V4L2_FIELD_INTERLACED,
760 sizeof(struct cx88_buffer),
761 fh);
762 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
763 dev->pci, &dev->slock,
764 V4L2_BUF_TYPE_VBI_CAPTURE,
765 V4L2_FIELD_SEQ_TB,
766 sizeof(struct cx88_buffer),
767 fh);
768
769 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 dprintk(1,"video_open: setting radio device\n");
Trent Piepho6a59d642007-08-15 14:41:57 -0300771 cx_write(MO_GP3_IO, core->board.radio.gpio3);
772 cx_write(MO_GP0_IO, core->board.radio.gpio0);
773 cx_write(MO_GP1_IO, core->board.radio.gpio1);
774 cx_write(MO_GP2_IO, core->board.radio.gpio2);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700775 core->tvaudio = WW_FM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 cx88_set_tvaudio(core);
777 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700778 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800785video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct cx8800_fh *fh = file->private_data;
788
789 switch (fh->type) {
790 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
791 if (res_locked(fh->dev,RESOURCE_VIDEO))
792 return -EBUSY;
793 return videobuf_read_one(&fh->vidq, data, count, ppos,
794 file->f_flags & O_NONBLOCK);
795 case V4L2_BUF_TYPE_VBI_CAPTURE:
796 if (!res_get(fh->dev,fh,RESOURCE_VBI))
797 return -EBUSY;
798 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
799 file->f_flags & O_NONBLOCK);
800 default:
801 BUG();
802 return 0;
803 }
804}
805
806static unsigned int
807video_poll(struct file *file, struct poll_table_struct *wait)
808{
809 struct cx8800_fh *fh = file->private_data;
810 struct cx88_buffer *buf;
811
812 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
813 if (!res_get(fh->dev,fh,RESOURCE_VBI))
814 return POLLERR;
815 return videobuf_poll_stream(file, &fh->vbiq, wait);
816 }
817
818 if (res_check(fh,RESOURCE_VIDEO)) {
819 /* streaming capture */
820 if (list_empty(&fh->vidq.stream))
821 return POLLERR;
822 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
823 } else {
824 /* read() capture */
825 buf = (struct cx88_buffer*)fh->vidq.read_buf;
826 if (NULL == buf)
827 return POLLERR;
828 }
829 poll_wait(file, &buf->vb.done, wait);
830 if (buf->vb.state == STATE_DONE ||
831 buf->vb.state == STATE_ERROR)
832 return POLLIN|POLLRDNORM;
833 return 0;
834}
835
836static int video_release(struct inode *inode, struct file *file)
837{
838 struct cx8800_fh *fh = file->private_data;
839 struct cx8800_dev *dev = fh->dev;
840
841 /* turn off overlay */
842 if (res_check(fh, RESOURCE_OVERLAY)) {
843 /* FIXME */
844 res_free(dev,fh,RESOURCE_OVERLAY);
845 }
846
847 /* stop video capture */
848 if (res_check(fh, RESOURCE_VIDEO)) {
849 videobuf_queue_cancel(&fh->vidq);
850 res_free(dev,fh,RESOURCE_VIDEO);
851 }
852 if (fh->vidq.read_buf) {
853 buffer_release(&fh->vidq,fh->vidq.read_buf);
854 kfree(fh->vidq.read_buf);
855 }
856
857 /* stop vbi capture */
858 if (res_check(fh, RESOURCE_VBI)) {
859 if (fh->vbiq.streaming)
860 videobuf_streamoff(&fh->vbiq);
861 if (fh->vbiq.reading)
862 videobuf_read_stop(&fh->vbiq);
863 res_free(dev,fh,RESOURCE_VBI);
864 }
865
866 videobuf_mmap_free(&fh->vidq);
867 videobuf_mmap_free(&fh->vbiq);
868 file->private_data = NULL;
869 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700870
871 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return 0;
874}
875
876static int
877video_mmap(struct file *file, struct vm_area_struct * vma)
878{
879 struct cx8800_fh *fh = file->private_data;
880
881 return videobuf_mmap_mapper(get_queue(fh), vma);
882}
883
884/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300885/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300887int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300889 struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 u32 value;
891 int i;
892
893 for (i = 0; i < CX8800_CTLS; i++)
894 if (cx8800_ctls[i].v.id == ctl->id)
895 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300896 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -EINVAL;
898
899 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
900 switch (ctl->id) {
901 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300902 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
903 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 break;
905 case V4L2_CID_AUDIO_VOLUME:
906 ctl->value = 0x3f - (value & 0x3f);
907 break;
908 default:
909 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
910 break;
911 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300912 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
913 ctl->id, c->v.name, ctl->value, c->reg,
914 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300917EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300919int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200922 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300924
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200925 for (i = 0; i < CX8800_CTLS; i++) {
926 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200928 }
929 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300930 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -EINVAL;
932
933 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700934 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700936 ctl->value = c->v.maximum;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200937 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 switch (ctl->id) {
939 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300940 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942 case V4L2_CID_AUDIO_VOLUME:
943 value = 0x3f - (ctl->value & 0x3f);
944 break;
945 case V4L2_CID_SATURATION:
946 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200947
948 value = ((ctl->value - c->off) << c->shift) & c->mask;
949
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -0300950 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200951 /* For SECAM, both U and V sat should be equal */
952 value=value<<8|value;
953 } else {
954 /* Keeps U Saturation proportional to V Sat */
955 value=(value*0x5a)/0x7f<<8|value;
956 }
957 mask=0xffff;
958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 default:
960 value = ((ctl->value - c->off) << c->shift) & c->mask;
961 break;
962 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300963 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
964 ctl->id, c->v.name, ctl->value, c->reg, value,
965 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200967 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200969 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 return 0;
972}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300973EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700975static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200977 struct v4l2_control ctrl;
978 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200980 for (i = 0; i < CX8800_CTLS; i++) {
981 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300982 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300983
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300984 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300989/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300991static int vidioc_g_fmt_cap (struct file *file, void *priv,
992 struct v4l2_format *f)
993{
994 struct cx8800_fh *fh = priv;
995
996 f->fmt.pix.width = fh->width;
997 f->fmt.pix.height = fh->height;
998 f->fmt.pix.field = fh->vidq.field;
999 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1000 f->fmt.pix.bytesperline =
1001 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1002 f->fmt.pix.sizeimage =
1003 f->fmt.pix.height * f->fmt.pix.bytesperline;
1004 return 0;
1005}
1006
1007static int vidioc_try_fmt_cap (struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct v4l2_format *f)
1009{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001010 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1011 struct cx8800_fmt *fmt;
1012 enum v4l2_field field;
1013 unsigned int maxw, maxh;
1014
1015 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1016 if (NULL == fmt)
1017 return -EINVAL;
1018
1019 field = f->fmt.pix.field;
1020 maxw = norm_maxw(core->tvnorm);
1021 maxh = norm_maxh(core->tvnorm);
1022
1023 if (V4L2_FIELD_ANY == field) {
1024 field = (f->fmt.pix.height > maxh/2)
1025 ? V4L2_FIELD_INTERLACED
1026 : V4L2_FIELD_BOTTOM;
1027 }
1028
1029 switch (field) {
1030 case V4L2_FIELD_TOP:
1031 case V4L2_FIELD_BOTTOM:
1032 maxh = maxh / 2;
1033 break;
1034 case V4L2_FIELD_INTERLACED:
1035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 default:
1037 return -EINVAL;
1038 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001039
1040 f->fmt.pix.field = field;
1041 if (f->fmt.pix.height < 32)
1042 f->fmt.pix.height = 32;
1043 if (f->fmt.pix.height > maxh)
1044 f->fmt.pix.height = maxh;
1045 if (f->fmt.pix.width < 48)
1046 f->fmt.pix.width = 48;
1047 if (f->fmt.pix.width > maxw)
1048 f->fmt.pix.width = maxw;
1049 f->fmt.pix.width &= ~0x03;
1050 f->fmt.pix.bytesperline =
1051 (f->fmt.pix.width * fmt->depth) >> 3;
1052 f->fmt.pix.sizeimage =
1053 f->fmt.pix.height * f->fmt.pix.bytesperline;
1054
1055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001058static int vidioc_s_fmt_cap (struct file *file, void *priv,
1059 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001061 struct cx8800_fh *fh = priv;
1062 int err = vidioc_try_fmt_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001063
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001064 if (0 != err)
1065 return err;
1066 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1067 fh->width = f->fmt.pix.width;
1068 fh->height = f->fmt.pix.height;
1069 fh->vidq.field = f->fmt.pix.field;
1070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001073static int vidioc_querycap (struct file *file, void *priv,
1074 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001076 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001079 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001080 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001081 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1082 cap->version = CX88_VERSION_CODE;
1083 cap->capabilities =
1084 V4L2_CAP_VIDEO_CAPTURE |
1085 V4L2_CAP_READWRITE |
1086 V4L2_CAP_STREAMING |
1087 V4L2_CAP_VBI_CAPTURE;
Trent Piepho6a59d642007-08-15 14:41:57 -03001088 if (UNSET != core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001089 cap->capabilities |= V4L2_CAP_TUNER;
1090 return 0;
1091}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001092
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001093static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1094 struct v4l2_fmtdesc *f)
1095{
1096 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1097 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001099 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1100 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001102 return 0;
1103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001105#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001106static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001107{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001108 struct cx8800_fh *fh = priv;
1109 struct videobuf_queue *q;
1110 struct v4l2_requestbuffers req;
1111 unsigned int i;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001112 int err;
1113
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001114 q = get_queue(fh);
1115 memset(&req,0,sizeof(req));
1116 req.type = q->type;
1117 req.count = 8;
1118 req.memory = V4L2_MEMORY_MMAP;
1119 err = videobuf_reqbufs(q,&req);
1120 if (err < 0)
1121 return err;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001122
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001123 mbuf->frames = req.count;
1124 mbuf->size = 0;
1125 for (i = 0; i < mbuf->frames; i++) {
1126 mbuf->offsets[i] = q->bufs[i]->boff;
1127 mbuf->size += q->bufs[i]->bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129 return 0;
1130}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001133static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001135 struct cx8800_fh *fh = priv;
1136 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001139static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1140{
1141 struct cx8800_fh *fh = priv;
1142 return (videobuf_querybuf(get_queue(fh), p));
1143}
1144
1145static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1146{
1147 struct cx8800_fh *fh = priv;
1148 return (videobuf_qbuf(get_queue(fh), p));
1149}
1150
1151static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1152{
1153 struct cx8800_fh *fh = priv;
1154 return (videobuf_dqbuf(get_queue(fh), p,
1155 file->f_flags & O_NONBLOCK));
1156}
1157
1158static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1159{
1160 struct cx8800_fh *fh = priv;
1161 struct cx8800_dev *dev = fh->dev;
1162
1163 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1164 return -EINVAL;
1165 if (unlikely(i != fh->type))
1166 return -EINVAL;
1167
1168 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1169 return -EBUSY;
1170 return videobuf_streamon(get_queue(fh));
1171}
1172
1173static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1174{
1175 struct cx8800_fh *fh = priv;
1176 struct cx8800_dev *dev = fh->dev;
1177 int err, res;
1178
1179 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1180 return -EINVAL;
1181 if (i != fh->type)
1182 return -EINVAL;
1183
1184 res = get_ressource(fh);
1185 err = videobuf_streamoff(get_queue(fh));
1186 if (err < 0)
1187 return err;
1188 res_free(dev,fh,res);
1189 return 0;
1190}
1191
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001192static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001193{
1194 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1195
1196 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001197 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001198 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001199
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001200 return 0;
1201}
1202
1203/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001204int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001205{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001206 static const char *iname[] = {
1207 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1208 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1209 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1210 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1211 [ CX88_VMUX_SVIDEO ] = "S-Video",
1212 [ CX88_VMUX_TELEVISION ] = "Television",
1213 [ CX88_VMUX_CABLE ] = "Cable TV",
1214 [ CX88_VMUX_DVB ] = "DVB",
1215 [ CX88_VMUX_DEBUG ] = "for debug only",
1216 };
1217 unsigned int n;
1218
1219 n = i->index;
1220 if (n >= 4)
1221 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001222 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001223 return -EINVAL;
1224 memset(i,0,sizeof(*i));
1225 i->index = n;
1226 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001227 strcpy(i->name,iname[INPUT(n).type]);
1228 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1229 (CX88_VMUX_CABLE == INPUT(n).type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001230 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001231 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001232 return 0;
1233}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001234EXPORT_SYMBOL(cx88_enum_input);
1235
1236static int vidioc_enum_input (struct file *file, void *priv,
1237 struct v4l2_input *i)
1238{
1239 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1240 return cx88_enum_input (core,i);
1241}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001242
1243static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1244{
1245 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1246
1247 *i = core->input;
1248 return 0;
1249}
1250
1251static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1252{
1253 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1254
1255 if (i >= 4)
1256 return -EINVAL;
1257
1258 mutex_lock(&core->lock);
1259 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001260 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001261 mutex_unlock(&core->lock);
1262 return 0;
1263}
1264
1265
1266
1267static int vidioc_queryctrl (struct file *file, void *priv,
1268 struct v4l2_queryctrl *qctrl)
1269{
1270 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1271 if (unlikely(qctrl->id == 0))
1272 return -EINVAL;
1273 return cx8800_ctrl_query(qctrl);
1274}
1275
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001276static int vidioc_g_ctrl (struct file *file, void *priv,
1277 struct v4l2_control *ctl)
1278{
1279 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1280 return
1281 cx88_get_control(core,ctl);
1282}
1283
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001284static int vidioc_s_ctrl (struct file *file, void *priv,
1285 struct v4l2_control *ctl)
1286{
1287 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001288 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001289 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001290}
1291
1292static int vidioc_g_tuner (struct file *file, void *priv,
1293 struct v4l2_tuner *t)
1294{
1295 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1296 u32 reg;
1297
Trent Piepho6a59d642007-08-15 14:41:57 -03001298 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001299 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001300 if (0 != t->index)
1301 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001302
1303 strcpy(t->name, "Television");
1304 t->type = V4L2_TUNER_ANALOG_TV;
1305 t->capability = V4L2_TUNER_CAP_NORM;
1306 t->rangehigh = 0xffffffffUL;
1307
1308 cx88_get_stereo(core ,t);
1309 reg = cx_read(MO_DEVICE_STATUS);
1310 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1311 return 0;
1312}
1313
1314static int vidioc_s_tuner (struct file *file, void *priv,
1315 struct v4l2_tuner *t)
1316{
1317 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1318
Trent Piepho6a59d642007-08-15 14:41:57 -03001319 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001320 return -EINVAL;
1321 if (0 != t->index)
1322 return -EINVAL;
1323
1324 cx88_set_stereo(core, t->audmode, 1);
1325 return 0;
1326}
1327
1328static int vidioc_g_frequency (struct file *file, void *priv,
1329 struct v4l2_frequency *f)
1330{
1331 struct cx8800_fh *fh = priv;
1332 struct cx88_core *core = fh->dev->core;
1333
Trent Piepho6a59d642007-08-15 14:41:57 -03001334 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001335 return -EINVAL;
1336
1337 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1338 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1339 f->frequency = core->freq;
1340
1341 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1342
1343 return 0;
1344}
1345
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001346int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001347 struct v4l2_frequency *f)
1348{
Trent Piepho6a59d642007-08-15 14:41:57 -03001349 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001350 return -EINVAL;
1351 if (unlikely(f->tuner != 0))
1352 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001353
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001354 mutex_lock(&core->lock);
1355 core->freq = f->frequency;
1356 cx88_newstation(core);
1357 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1358
1359 /* When changing channels it is required to reset TVAUDIO */
1360 msleep (10);
1361 cx88_set_tvaudio(core);
1362
1363 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001364
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001365 return 0;
1366}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001367EXPORT_SYMBOL(cx88_set_freq);
1368
1369static int vidioc_s_frequency (struct file *file, void *priv,
1370 struct v4l2_frequency *f)
1371{
1372 struct cx8800_fh *fh = priv;
1373 struct cx88_core *core = fh->dev->core;
1374
1375 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1376 return -EINVAL;
1377 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1378 return -EINVAL;
1379
1380 return
1381 cx88_set_freq (core,f);
1382}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001383
Trent Piephodbbff482007-01-22 23:31:53 -03001384#ifdef CONFIG_VIDEO_ADV_DEBUG
1385static int vidioc_g_register (struct file *file, void *fh,
1386 struct v4l2_register *reg)
1387{
1388 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1389
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001390 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001391 return -EINVAL;
1392 /* cx2388x has a 24-bit register space */
1393 reg->val = cx_read(reg->reg&0xffffff);
1394 return 0;
1395}
1396
1397static int vidioc_s_register (struct file *file, void *fh,
1398 struct v4l2_register *reg)
1399{
1400 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1401
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001402 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001403 return -EINVAL;
Trent Piephodbbff482007-01-22 23:31:53 -03001404 cx_write(reg->reg&0xffffff, reg->val);
1405 return 0;
1406}
1407#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001410/* RADIO ESPECIFIC IOCTLS */
1411/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001413static int radio_querycap (struct file *file, void *priv,
1414 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001416 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct cx88_core *core = dev->core;
1418
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001419 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001420 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001421 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1422 cap->version = CX88_VERSION_CODE;
1423 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001425}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001427static int radio_g_tuner (struct file *file, void *priv,
1428 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001430 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1431
1432 if (unlikely(t->index > 0))
1433 return -EINVAL;
1434
1435 strcpy(t->name, "Radio");
1436 t->type = V4L2_TUNER_RADIO;
1437
1438 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1439 return 0;
1440}
1441
1442static int radio_enum_input (struct file *file, void *priv,
1443 struct v4l2_input *i)
1444{
1445 if (i->index != 0)
1446 return -EINVAL;
1447 strcpy(i->name,"Radio");
1448 i->type = V4L2_INPUT_TYPE_TUNER;
1449
1450 return 0;
1451}
1452
1453static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1454{
1455 if (unlikely(a->index))
1456 return -EINVAL;
1457
1458 memset(a,0,sizeof(*a));
1459 strcpy(a->name,"Radio");
1460 return 0;
1461}
1462
1463/* FIXME: Should add a standard for radio */
1464
1465static int radio_s_tuner (struct file *file, void *priv,
1466 struct v4l2_tuner *t)
1467{
1468 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1469
1470 if (0 != t->index)
1471 return -EINVAL;
1472
1473 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1474
1475 return 0;
1476}
1477
1478static int radio_s_audio (struct file *file, void *fh,
1479 struct v4l2_audio *a)
1480{
1481 return 0;
1482}
1483
1484static int radio_s_input (struct file *file, void *fh, unsigned int i)
1485{
1486 return 0;
1487}
1488
1489static int radio_queryctrl (struct file *file, void *priv,
1490 struct v4l2_queryctrl *c)
1491{
1492 int i;
1493
1494 if (c->id < V4L2_CID_BASE ||
1495 c->id >= V4L2_CID_LASTP1)
1496 return -EINVAL;
1497 if (c->id == V4L2_CID_AUDIO_MUTE) {
1498 for (i = 0; i < CX8800_CTLS; i++)
1499 if (cx8800_ctls[i].v.id == c->id)
1500 break;
1501 *c = cx8800_ctls[i].v;
1502 } else
1503 *c = no_ctl;
1504 return 0;
1505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507/* ----------------------------------------------------------- */
1508
1509static void cx8800_vid_timeout(unsigned long data)
1510{
1511 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1512 struct cx88_core *core = dev->core;
1513 struct cx88_dmaqueue *q = &dev->vidq;
1514 struct cx88_buffer *buf;
1515 unsigned long flags;
1516
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001517 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 cx_clear(MO_VID_DMACNTRL, 0x11);
1520 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1521
1522 spin_lock_irqsave(&dev->slock,flags);
1523 while (!list_empty(&q->active)) {
1524 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1525 list_del(&buf->vb.queue);
1526 buf->vb.state = STATE_ERROR;
1527 wake_up(&buf->vb.done);
1528 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1529 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1530 }
1531 restart_video_queue(dev,q);
1532 spin_unlock_irqrestore(&dev->slock,flags);
1533}
1534
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001535static char *cx88_vid_irqs[32] = {
1536 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1537 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1538 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1539 "y_sync", "u_sync", "v_sync", "vbi_sync",
1540 "opc_err", "par_err", "rip_err", "pci_abort",
1541};
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static void cx8800_vid_irq(struct cx8800_dev *dev)
1544{
1545 struct cx88_core *core = dev->core;
1546 u32 status, mask, count;
1547
1548 status = cx_read(MO_VID_INTSTAT);
1549 mask = cx_read(MO_VID_INTMSK);
1550 if (0 == (status & mask))
1551 return;
1552 cx_write(MO_VID_INTSTAT, status);
1553 if (irq_debug || (status & mask & ~0xff))
1554 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001555 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1556 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* risc op code error */
1559 if (status & (1 << 16)) {
1560 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1561 cx_clear(MO_VID_DMACNTRL, 0x11);
1562 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001563 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565
1566 /* risc1 y */
1567 if (status & 0x01) {
1568 spin_lock(&dev->slock);
1569 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001570 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 spin_unlock(&dev->slock);
1572 }
1573
1574 /* risc1 vbi */
1575 if (status & 0x08) {
1576 spin_lock(&dev->slock);
1577 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001578 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 spin_unlock(&dev->slock);
1580 }
1581
1582 /* risc2 y */
1583 if (status & 0x10) {
1584 dprintk(2,"stopper video\n");
1585 spin_lock(&dev->slock);
1586 restart_video_queue(dev,&dev->vidq);
1587 spin_unlock(&dev->slock);
1588 }
1589
1590 /* risc2 vbi */
1591 if (status & 0x80) {
1592 dprintk(2,"stopper vbi\n");
1593 spin_lock(&dev->slock);
1594 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1595 spin_unlock(&dev->slock);
1596 }
1597}
1598
David Howells7d12e782006-10-05 14:55:46 +01001599static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct cx8800_dev *dev = dev_id;
1602 struct cx88_core *core = dev->core;
1603 u32 status;
1604 int loop, handled = 0;
1605
1606 for (loop = 0; loop < 10; loop++) {
1607 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1608 if (0 == status)
1609 goto out;
1610 cx_write(MO_PCI_INTSTAT, status);
1611 handled = 1;
1612
1613 if (status & core->pci_irqmask)
1614 cx88_core_irq(core,status);
1615 if (status & 0x01)
1616 cx8800_vid_irq(dev);
1617 };
1618 if (10 == loop) {
1619 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1620 core->name);
1621 cx_write(MO_PCI_INTMSK,0);
1622 }
1623
1624 out:
1625 return IRQ_RETVAL(handled);
1626}
1627
1628/* ----------------------------------------------------------- */
1629/* exported stuff */
1630
Arjan van de Venfa027c22007-02-12 00:55:33 -08001631static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 .owner = THIS_MODULE,
1634 .open = video_open,
1635 .release = video_release,
1636 .read = video_read,
1637 .poll = video_poll,
1638 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001639 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001640 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 .llseek = no_llseek,
1642};
1643
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001644static struct video_device cx8800_vbi_template;
Adrian Bunk408b6642005-05-01 08:59:29 -07001645static struct video_device cx8800_video_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001647 .name = "cx8800-video",
1648 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1649 .fops = &video_fops,
1650 .minor = -1,
1651 .vidioc_querycap = vidioc_querycap,
1652 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1653 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1654 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1655 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1656 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1657 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1658 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1659 .vidioc_reqbufs = vidioc_reqbufs,
1660 .vidioc_querybuf = vidioc_querybuf,
1661 .vidioc_qbuf = vidioc_qbuf,
1662 .vidioc_dqbuf = vidioc_dqbuf,
1663 .vidioc_s_std = vidioc_s_std,
1664 .vidioc_enum_input = vidioc_enum_input,
1665 .vidioc_g_input = vidioc_g_input,
1666 .vidioc_s_input = vidioc_s_input,
1667 .vidioc_queryctrl = vidioc_queryctrl,
1668 .vidioc_g_ctrl = vidioc_g_ctrl,
1669 .vidioc_s_ctrl = vidioc_s_ctrl,
1670 .vidioc_streamon = vidioc_streamon,
1671 .vidioc_streamoff = vidioc_streamoff,
1672#ifdef CONFIG_VIDEO_V4L1_COMPAT
1673 .vidiocgmbuf = vidiocgmbuf,
1674#endif
1675 .vidioc_g_tuner = vidioc_g_tuner,
1676 .vidioc_s_tuner = vidioc_s_tuner,
1677 .vidioc_g_frequency = vidioc_g_frequency,
1678 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001679#ifdef CONFIG_VIDEO_ADV_DEBUG
1680 .vidioc_g_register = vidioc_g_register,
1681 .vidioc_s_register = vidioc_s_register,
1682#endif
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001683 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001684 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685};
1686
Arjan van de Venfa027c22007-02-12 00:55:33 -08001687static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 .owner = THIS_MODULE,
1690 .open = video_open,
1691 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001692 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001693 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 .llseek = no_llseek,
1695};
1696
Adrian Bunk408b6642005-05-01 08:59:29 -07001697static struct video_device cx8800_radio_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001699 .name = "cx8800-radio",
1700 .type = VID_TYPE_TUNER,
1701 .hardware = 0,
1702 .fops = &radio_fops,
1703 .minor = -1,
1704 .vidioc_querycap = radio_querycap,
1705 .vidioc_g_tuner = radio_g_tuner,
1706 .vidioc_enum_input = radio_enum_input,
1707 .vidioc_g_audio = radio_g_audio,
1708 .vidioc_s_tuner = radio_s_tuner,
1709 .vidioc_s_audio = radio_s_audio,
1710 .vidioc_s_input = radio_s_input,
1711 .vidioc_queryctrl = radio_queryctrl,
1712 .vidioc_g_ctrl = vidioc_g_ctrl,
1713 .vidioc_s_ctrl = vidioc_s_ctrl,
1714 .vidioc_g_frequency = vidioc_g_frequency,
1715 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephoa75d2042007-08-01 00:13:28 -03001716#ifdef CONFIG_VIDEO_ADV_DEBUG
1717 .vidioc_g_register = vidioc_g_register,
1718 .vidioc_s_register = vidioc_s_register,
1719#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720};
1721
1722/* ----------------------------------------------------------- */
1723
1724static void cx8800_unregister_video(struct cx8800_dev *dev)
1725{
1726 if (dev->radio_dev) {
1727 if (-1 != dev->radio_dev->minor)
1728 video_unregister_device(dev->radio_dev);
1729 else
1730 video_device_release(dev->radio_dev);
1731 dev->radio_dev = NULL;
1732 }
1733 if (dev->vbi_dev) {
1734 if (-1 != dev->vbi_dev->minor)
1735 video_unregister_device(dev->vbi_dev);
1736 else
1737 video_device_release(dev->vbi_dev);
1738 dev->vbi_dev = NULL;
1739 }
1740 if (dev->video_dev) {
1741 if (-1 != dev->video_dev->minor)
1742 video_unregister_device(dev->video_dev);
1743 else
1744 video_device_release(dev->video_dev);
1745 dev->video_dev = NULL;
1746 }
1747}
1748
1749static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1750 const struct pci_device_id *pci_id)
1751{
1752 struct cx8800_dev *dev;
1753 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int err;
1756
Panagiotis Issaris74081872006-01-11 19:40:56 -02001757 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (NULL == dev)
1759 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /* pci init */
1762 dev->pci = pci_dev;
1763 if (pci_enable_device(pci_dev)) {
1764 err = -EIO;
1765 goto fail_free;
1766 }
1767 core = cx88_core_get(dev->pci);
1768 if (NULL == core) {
1769 err = -EINVAL;
1770 goto fail_free;
1771 }
1772 dev->core = core;
1773
1774 /* print pci info */
1775 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001776 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1777 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001778 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001780 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 pci_set_master(pci_dev);
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -03001783 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1785 err = -EIO;
1786 goto fail_core;
1787 }
1788
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001789 /* Initialize VBI template */
1790 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1791 sizeof(cx8800_vbi_template) );
1792 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1793 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001797 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 /* init video dma queues */
1800 INIT_LIST_HEAD(&dev->vidq.active);
1801 INIT_LIST_HEAD(&dev->vidq.queued);
1802 dev->vidq.timeout.function = cx8800_vid_timeout;
1803 dev->vidq.timeout.data = (unsigned long)dev;
1804 init_timer(&dev->vidq.timeout);
1805 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1806 MO_VID_DMACNTRL,0x11,0x00);
1807
1808 /* init vbi dma queues */
1809 INIT_LIST_HEAD(&dev->vbiq.active);
1810 INIT_LIST_HEAD(&dev->vbiq.queued);
1811 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1812 dev->vbiq.timeout.data = (unsigned long)dev;
1813 init_timer(&dev->vbiq.timeout);
1814 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1815 MO_VID_DMACNTRL,0x88,0x00);
1816
1817 /* get irq */
1818 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001819 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (err < 0) {
1821 printk(KERN_ERR "%s: can't get IRQ %d\n",
1822 core->name,pci_dev->irq);
1823 goto fail_core;
1824 }
1825 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1826
1827 /* load and configure helper modules */
Trent Piepho6a59d642007-08-15 14:41:57 -03001828 if (TUNER_ABSENT != core->board.tuner_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 request_module("tuner");
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001830
Trent Piepho6a59d642007-08-15 14:41:57 -03001831 if (core->board.audio_chip == AUDIO_CHIP_WM8775)
Steven Toth30579062006-09-25 12:43:42 -03001832 request_module("wm8775");
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* register v4l devices */
1835 dev->video_dev = cx88_vdev_init(core,dev->pci,
1836 &cx8800_video_template,"video");
1837 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1838 video_nr[core->nr]);
1839 if (err < 0) {
1840 printk(KERN_INFO "%s: can't register video device\n",
1841 core->name);
1842 goto fail_unreg;
1843 }
1844 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1845 core->name,dev->video_dev->minor & 0x1f);
1846
1847 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1848 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1849 vbi_nr[core->nr]);
1850 if (err < 0) {
1851 printk(KERN_INFO "%s/0: can't register vbi device\n",
1852 core->name);
1853 goto fail_unreg;
1854 }
1855 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1856 core->name,dev->vbi_dev->minor & 0x1f);
1857
Trent Piepho6a59d642007-08-15 14:41:57 -03001858 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1860 &cx8800_radio_template,"radio");
1861 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1862 radio_nr[core->nr]);
1863 if (err < 0) {
1864 printk(KERN_INFO "%s/0: can't register radio device\n",
1865 core->name);
1866 goto fail_unreg;
1867 }
1868 printk(KERN_INFO "%s/0: registered device radio%d\n",
1869 core->name,dev->radio_dev->minor & 0x1f);
1870 }
1871
1872 /* everything worked */
1873 list_add_tail(&dev->devlist,&cx8800_devlist);
1874 pci_set_drvdata(pci_dev,dev);
1875
1876 /* initial device configuration */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001877 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001878 cx88_set_tvnorm(core,core->tvnorm);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001879 init_controls(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001880 cx88_video_mux(core,0);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001881 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001884 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001886 if (IS_ERR(core->kthread)) {
1887 err = PTR_ERR(core->kthread);
1888 printk(KERN_ERR "Failed to create cx88 audio thread, err=%d\n",
1889 err);
1890 }
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893
1894fail_unreg:
1895 cx8800_unregister_video(dev);
1896 free_irq(pci_dev->irq, dev);
1897fail_core:
1898 cx88_core_put(core,dev->pci);
1899fail_free:
1900 kfree(dev);
1901 return err;
1902}
1903
1904static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1905{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001906 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001907 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001910 if (core->kthread) {
1911 kthread_stop(core->kthread);
1912 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001915 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 pci_disable_device(pci_dev);
1917
1918 /* unregister stuff */
1919
1920 free_irq(pci_dev->irq, dev);
1921 cx8800_unregister_video(dev);
1922 pci_set_drvdata(pci_dev, NULL);
1923
1924 /* free memory */
1925 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1926 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001927 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 kfree(dev);
1929}
1930
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001931#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1933{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001934 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 struct cx88_core *core = dev->core;
1936
1937 /* stop video+vbi capture */
1938 spin_lock(&dev->slock);
1939 if (!list_empty(&dev->vidq.active)) {
1940 printk("%s: suspend video\n", core->name);
1941 stop_video_dma(dev);
1942 del_timer(&dev->vidq.timeout);
1943 }
1944 if (!list_empty(&dev->vbiq.active)) {
1945 printk("%s: suspend vbi\n", core->name);
1946 cx8800_stop_vbi_dma(dev);
1947 del_timer(&dev->vbiq.timeout);
1948 }
1949 spin_unlock(&dev->slock);
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001952 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 pci_save_state(pci_dev);
1955 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1956 pci_disable_device(pci_dev);
1957 dev->state.disabled = 1;
1958 }
1959 return 0;
1960}
1961
1962static int cx8800_resume(struct pci_dev *pci_dev)
1963{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001964 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001966 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001969 err=pci_enable_device(pci_dev);
1970 if (err) {
1971 printk(KERN_ERR "%s: can't enable device\n",
1972 core->name);
1973 return err;
1974 }
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 dev->state.disabled = 0;
1977 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001978 err= pci_set_power_state(pci_dev, PCI_D0);
1979 if (err) {
1980 printk(KERN_ERR "%s: can't enable device\n",
1981 core->name);
1982
1983 pci_disable_device(pci_dev);
1984 dev->state.disabled = 1;
1985
1986 return err;
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 pci_restore_state(pci_dev);
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001991 cx88_reset(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 /* restart video+vbi capture */
1994 spin_lock(&dev->slock);
1995 if (!list_empty(&dev->vidq.active)) {
1996 printk("%s: resume video\n", core->name);
1997 restart_video_queue(dev,&dev->vidq);
1998 }
1999 if (!list_empty(&dev->vbiq.active)) {
2000 printk("%s: resume vbi\n", core->name);
2001 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2002 }
2003 spin_unlock(&dev->slock);
2004
2005 return 0;
2006}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002007#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009/* ----------------------------------------------------------- */
2010
Adrian Bunk408b6642005-05-01 08:59:29 -07002011static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 {
2013 .vendor = 0x14f1,
2014 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002015 .subvendor = PCI_ANY_ID,
2016 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 },{
2018 /* --- end of list --- */
2019 }
2020};
2021MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2022
2023static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002024 .name = "cx8800",
2025 .id_table = cx8800_pci_tbl,
2026 .probe = cx8800_initdev,
2027 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002028#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 .suspend = cx8800_suspend,
2030 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002031#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032};
2033
2034static int cx8800_init(void)
2035{
2036 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2037 (CX88_VERSION_CODE >> 16) & 0xff,
2038 (CX88_VERSION_CODE >> 8) & 0xff,
2039 CX88_VERSION_CODE & 0xff);
2040#ifdef SNAPSHOT
2041 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2042 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2043#endif
2044 return pci_register_driver(&cx8800_pci_driver);
2045}
2046
2047static void cx8800_fini(void)
2048{
2049 pci_unregister_driver(&cx8800_pci_driver);
2050}
2051
2052module_init(cx8800_init);
2053module_exit(cx8800_fini);
2054
2055/* ----------------------------------------------------------- */
2056/*
2057 * Local variables:
2058 * c-basic-offset: 8
2059 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002060 * 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 -07002061 */