blob: 06b233a7b20bfc3dce4860f6c6d9392ac8759ca0 [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>
Alexey Dobriyan45e95982007-05-01 08:32:24 -030039#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/div64.h>
41
42#include "cx88.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020043#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030045#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080046/* Include V4L1 specific functions. Should be removed soon */
47#include <linux/videodev.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030048#endif
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
51MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
52MODULE_LICENSE("GPL");
53
54/* ------------------------------------------------------------------ */
55
56static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
58static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
59
60module_param_array(video_nr, int, NULL, 0444);
61module_param_array(vbi_nr, int, NULL, 0444);
62module_param_array(radio_nr, int, NULL, 0444);
63
64MODULE_PARM_DESC(video_nr,"video device numbers");
65MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
66MODULE_PARM_DESC(radio_nr,"radio device numbers");
67
68static unsigned int video_debug = 0;
69module_param(video_debug,int,0644);
70MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
71
72static unsigned int irq_debug = 0;
73module_param(irq_debug,int,0644);
74MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
75
76static unsigned int vid_limit = 16;
77module_param(vid_limit,int,0644);
78MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
79
80#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070081 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/* ------------------------------------------------------------------ */
84
85static LIST_HEAD(cx8800_devlist);
86
87/* ------------------------------------------------------------------- */
88/* static data */
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static struct cx8800_fmt formats[] = {
91 {
92 .name = "8 bpp, gray",
93 .fourcc = V4L2_PIX_FMT_GREY,
94 .cxformat = ColorFormatY8,
95 .depth = 8,
96 .flags = FORMAT_FLAGS_PACKED,
97 },{
98 .name = "15 bpp RGB, le",
99 .fourcc = V4L2_PIX_FMT_RGB555,
100 .cxformat = ColorFormatRGB15,
101 .depth = 16,
102 .flags = FORMAT_FLAGS_PACKED,
103 },{
104 .name = "15 bpp RGB, be",
105 .fourcc = V4L2_PIX_FMT_RGB555X,
106 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
107 .depth = 16,
108 .flags = FORMAT_FLAGS_PACKED,
109 },{
110 .name = "16 bpp RGB, le",
111 .fourcc = V4L2_PIX_FMT_RGB565,
112 .cxformat = ColorFormatRGB16,
113 .depth = 16,
114 .flags = FORMAT_FLAGS_PACKED,
115 },{
116 .name = "16 bpp RGB, be",
117 .fourcc = V4L2_PIX_FMT_RGB565X,
118 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
119 .depth = 16,
120 .flags = FORMAT_FLAGS_PACKED,
121 },{
122 .name = "24 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_BGR24,
124 .cxformat = ColorFormatRGB24,
125 .depth = 24,
126 .flags = FORMAT_FLAGS_PACKED,
127 },{
128 .name = "32 bpp RGB, le",
129 .fourcc = V4L2_PIX_FMT_BGR32,
130 .cxformat = ColorFormatRGB32,
131 .depth = 32,
132 .flags = FORMAT_FLAGS_PACKED,
133 },{
134 .name = "32 bpp RGB, be",
135 .fourcc = V4L2_PIX_FMT_RGB32,
136 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
137 .depth = 32,
138 .flags = FORMAT_FLAGS_PACKED,
139 },{
140 .name = "4:2:2, packed, YUYV",
141 .fourcc = V4L2_PIX_FMT_YUYV,
142 .cxformat = ColorFormatYUY2,
143 .depth = 16,
144 .flags = FORMAT_FLAGS_PACKED,
145 },{
146 .name = "4:2:2, packed, UYVY",
147 .fourcc = V4L2_PIX_FMT_UYVY,
148 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
149 .depth = 16,
150 .flags = FORMAT_FLAGS_PACKED,
151 },
152};
153
154static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
155{
156 unsigned int i;
157
158 for (i = 0; i < ARRAY_SIZE(formats); i++)
159 if (formats[i].fourcc == fourcc)
160 return formats+i;
161 return NULL;
162}
163
164/* ------------------------------------------------------------------- */
165
166static const struct v4l2_queryctrl no_ctl = {
167 .name = "42",
168 .flags = V4L2_CTRL_FLAG_DISABLED,
169};
170
171static struct cx88_ctrl cx8800_ctls[] = {
172 /* --- video --- */
173 {
174 .v = {
175 .id = V4L2_CID_BRIGHTNESS,
176 .name = "Brightness",
177 .minimum = 0x00,
178 .maximum = 0xff,
179 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300180 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .type = V4L2_CTRL_TYPE_INTEGER,
182 },
183 .off = 128,
184 .reg = MO_CONTR_BRIGHT,
185 .mask = 0x00ff,
186 .shift = 0,
187 },{
188 .v = {
189 .id = V4L2_CID_CONTRAST,
190 .name = "Contrast",
191 .minimum = 0,
192 .maximum = 0xff,
193 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200194 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .type = V4L2_CTRL_TYPE_INTEGER,
196 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700197 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .reg = MO_CONTR_BRIGHT,
199 .mask = 0xff00,
200 .shift = 8,
201 },{
202 .v = {
203 .id = V4L2_CID_HUE,
204 .name = "Hue",
205 .minimum = 0,
206 .maximum = 0xff,
207 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300208 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .type = V4L2_CTRL_TYPE_INTEGER,
210 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700211 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .reg = MO_HUE,
213 .mask = 0x00ff,
214 .shift = 0,
215 },{
216 /* strictly, this only describes only U saturation.
217 * V saturation is handled specially through code.
218 */
219 .v = {
220 .id = V4L2_CID_SATURATION,
221 .name = "Saturation",
222 .minimum = 0,
223 .maximum = 0xff,
224 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200225 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 .type = V4L2_CTRL_TYPE_INTEGER,
227 },
228 .off = 0,
229 .reg = MO_UV_SATURATION,
230 .mask = 0x00ff,
231 .shift = 0,
232 },{
233 /* --- audio --- */
234 .v = {
235 .id = V4L2_CID_AUDIO_MUTE,
236 .name = "Mute",
237 .minimum = 0,
238 .maximum = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200239 .default_value = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .type = V4L2_CTRL_TYPE_BOOLEAN,
241 },
242 .reg = AUD_VOL_CTL,
243 .sreg = SHADOW_AUD_VOL_CTL,
244 .mask = (1 << 6),
245 .shift = 6,
246 },{
247 .v = {
248 .id = V4L2_CID_AUDIO_VOLUME,
249 .name = "Volume",
250 .minimum = 0,
251 .maximum = 0x3f,
252 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300253 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .type = V4L2_CTRL_TYPE_INTEGER,
255 },
256 .reg = AUD_VOL_CTL,
257 .sreg = SHADOW_AUD_VOL_CTL,
258 .mask = 0x3f,
259 .shift = 0,
260 },{
261 .v = {
262 .id = V4L2_CID_AUDIO_BALANCE,
263 .name = "Balance",
264 .minimum = 0,
265 .maximum = 0x7f,
266 .step = 1,
267 .default_value = 0x40,
268 .type = V4L2_CTRL_TYPE_INTEGER,
269 },
270 .reg = AUD_BAL_CTL,
271 .sreg = SHADOW_AUD_BAL_CTL,
272 .mask = 0x7f,
273 .shift = 0,
274 }
275};
Adrian Bunk408b6642005-05-01 08:59:29 -0700276static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Michael Krufky38a27132006-06-26 23:42:39 -0300278const u32 cx88_user_ctrls[] = {
279 V4L2_CID_USER_CLASS,
280 V4L2_CID_BRIGHTNESS,
281 V4L2_CID_CONTRAST,
282 V4L2_CID_SATURATION,
283 V4L2_CID_HUE,
284 V4L2_CID_AUDIO_VOLUME,
285 V4L2_CID_AUDIO_BALANCE,
286 V4L2_CID_AUDIO_MUTE,
287 0
288};
289EXPORT_SYMBOL(cx88_user_ctrls);
290
291static const u32 *ctrl_classes[] = {
292 cx88_user_ctrls,
293 NULL
294};
295
296int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
297{
298 int i;
299
300 if (qctrl->id < V4L2_CID_BASE ||
301 qctrl->id >= V4L2_CID_LASTP1)
302 return -EINVAL;
303 for (i = 0; i < CX8800_CTLS; i++)
304 if (cx8800_ctls[i].v.id == qctrl->id)
305 break;
306 if (i == CX8800_CTLS) {
307 *qctrl = no_ctl;
308 return 0;
309 }
310 *qctrl = cx8800_ctls[i].v;
311 return 0;
312}
313EXPORT_SYMBOL(cx8800_ctrl_query);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/* ------------------------------------------------------------------- */
316/* resource management */
317
318static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
319{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700320 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (fh->resources & bit)
322 /* have it already allocated */
323 return 1;
324
325 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200326 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (dev->resources & bit) {
328 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200329 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
331 }
332 /* it's free, grab it */
333 fh->resources |= bit;
334 dev->resources |= bit;
335 dprintk(1,"res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200336 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 1;
338}
339
340static
341int res_check(struct cx8800_fh *fh, unsigned int bit)
342{
343 return (fh->resources & bit);
344}
345
346static
347int res_locked(struct cx8800_dev *dev, unsigned int bit)
348{
349 return (dev->resources & bit);
350}
351
352static
353void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
354{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700355 struct cx88_core *core = dev->core;
Eric Sesterhennae246012006-03-13 13:17:11 -0300356 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Ingo Molnar3593cab2006-02-07 06:49:14 -0200358 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 fh->resources &= ~bits;
360 dev->resources &= ~bits;
361 dprintk(1,"res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200362 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/* ------------------------------------------------------------------ */
366
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300367int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700369 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
372 input, INPUT(input)->vmux,
373 INPUT(input)->gpio0,INPUT(input)->gpio1,
374 INPUT(input)->gpio2,INPUT(input)->gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700375 core->input = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
377 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
378 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
379 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
380 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
381
382 switch (INPUT(input)->type) {
383 case CX88_VMUX_SVIDEO:
384 cx_set(MO_AFECFG_IO, 0x00000001);
385 cx_set(MO_INPUT_FORMAT, 0x00010010);
386 cx_set(MO_FILTER_EVEN, 0x00002020);
387 cx_set(MO_FILTER_ODD, 0x00002020);
388 break;
389 default:
390 cx_clear(MO_AFECFG_IO, 0x00000001);
391 cx_clear(MO_INPUT_FORMAT, 0x00010010);
392 cx_clear(MO_FILTER_EVEN, 0x00002020);
393 cx_clear(MO_FILTER_ODD, 0x00002020);
394 break;
395 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300396
397 if (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) {
398 /* sets sound input from external adc */
399 if (INPUT(input)->extadc)
400 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
401 else
402 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300406EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408/* ------------------------------------------------------------------ */
409
410static int start_video_dma(struct cx8800_dev *dev,
411 struct cx88_dmaqueue *q,
412 struct cx88_buffer *buf)
413{
414 struct cx88_core *core = dev->core;
415
416 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700417 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700419 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
421
422 /* reset counter */
423 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
424 q->count = 1;
425
426 /* enable irqs */
427 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700428
429 /* Enables corresponding bits at PCI_INT_STAT:
430 bits 0 to 4: video, audio, transport stream, VIP, Host
431 bit 7: timer
432 bits 8 and 9: DMA complete for: SRC, DST
433 bits 10 and 11: BERR signal asserted for RISC: RD, WR
434 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 cx_set(MO_VID_INTMSK, 0x0f0011);
437
438 /* enable capture */
439 cx_set(VID_CAPTURE_CONTROL,0x06);
440
441 /* start dma */
442 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700443 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return 0;
446}
447
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300448#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static 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}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300464#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466static int restart_video_queue(struct cx8800_dev *dev,
467 struct cx88_dmaqueue *q)
468{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700469 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct cx88_buffer *buf, *prev;
471 struct list_head *item;
472
473 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800474 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
476 buf, buf->vb.i);
477 start_video_dma(dev, q, buf);
478 list_for_each(item,&q->active) {
479 buf = list_entry(item, struct cx88_buffer, vb.queue);
480 buf->count = q->count++;
481 }
482 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
483 return 0;
484 }
485
486 prev = NULL;
487 for (;;) {
488 if (list_empty(&q->queued))
489 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800490 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700492 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 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) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700503 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 buf->vb.state = STATE_ACTIVE;
505 buf->count = q->count++;
506 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
507 dprintk(2,"[%p/%d] restart_queue - move to active\n",
508 buf,buf->vb.i);
509 } else {
510 return 0;
511 }
512 prev = buf;
513 }
514}
515
516/* ------------------------------------------------------------------ */
517
518static int
519buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
520{
521 struct cx8800_fh *fh = q->priv_data;
522
523 *size = fh->fmt->depth*fh->width*fh->height >> 3;
524 if (0 == *count)
525 *count = 32;
526 while (*size * *count > vid_limit * 1024 * 1024)
527 (*count)--;
528 return 0;
529}
530
531static int
532buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
533 enum v4l2_field field)
534{
535 struct cx8800_fh *fh = q->priv_data;
536 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700537 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
539 int rc, init_buffer = 0;
540
541 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700542 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
543 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -EINVAL;
545 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
546 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
547 return -EINVAL;
548
549 if (buf->fmt != fh->fmt ||
550 buf->vb.width != fh->width ||
551 buf->vb.height != fh->height ||
552 buf->vb.field != field) {
553 buf->fmt = fh->fmt;
554 buf->vb.width = fh->width;
555 buf->vb.height = fh->height;
556 buf->vb.field = field;
557 init_buffer = 1;
558 }
559
560 if (STATE_NEEDS_INIT == buf->vb.state) {
561 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300562 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto fail;
564 }
565
566 if (init_buffer) {
567 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
568 switch (buf->vb.field) {
569 case V4L2_FIELD_TOP:
570 cx88_risc_buffer(dev->pci, &buf->risc,
571 buf->vb.dma.sglist, 0, UNSET,
572 buf->bpl, 0, buf->vb.height);
573 break;
574 case V4L2_FIELD_BOTTOM:
575 cx88_risc_buffer(dev->pci, &buf->risc,
576 buf->vb.dma.sglist, UNSET, 0,
577 buf->bpl, 0, buf->vb.height);
578 break;
579 case V4L2_FIELD_INTERLACED:
580 cx88_risc_buffer(dev->pci, &buf->risc,
581 buf->vb.dma.sglist, 0, buf->bpl,
582 buf->bpl, buf->bpl,
583 buf->vb.height >> 1);
584 break;
585 case V4L2_FIELD_SEQ_TB:
586 cx88_risc_buffer(dev->pci, &buf->risc,
587 buf->vb.dma.sglist,
588 0, buf->bpl * (buf->vb.height >> 1),
589 buf->bpl, 0,
590 buf->vb.height >> 1);
591 break;
592 case V4L2_FIELD_SEQ_BT:
593 cx88_risc_buffer(dev->pci, &buf->risc,
594 buf->vb.dma.sglist,
595 buf->bpl * (buf->vb.height >> 1), 0,
596 buf->bpl, 0,
597 buf->vb.height >> 1);
598 break;
599 default:
600 BUG();
601 }
602 }
603 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
604 buf, buf->vb.i,
605 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
606 (unsigned long)buf->risc.dma);
607
608 buf->vb.state = STATE_PREPARED;
609 return 0;
610
611 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300612 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return rc;
614}
615
616static void
617buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
618{
619 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
620 struct cx88_buffer *prev;
621 struct cx8800_fh *fh = vq->priv_data;
622 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700623 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct cx88_dmaqueue *q = &dev->vidq;
625
626 /* add jump to stopper */
627 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
628 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
629
630 if (!list_empty(&q->queued)) {
631 list_add_tail(&buf->vb.queue,&q->queued);
632 buf->vb.state = STATE_QUEUED;
633 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
634 buf, buf->vb.i);
635
636 } else if (list_empty(&q->active)) {
637 list_add_tail(&buf->vb.queue,&q->active);
638 start_video_dma(dev, q, buf);
639 buf->vb.state = STATE_ACTIVE;
640 buf->count = q->count++;
641 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
642 dprintk(2,"[%p/%d] buffer_queue - first active\n",
643 buf, buf->vb.i);
644
645 } else {
646 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
647 if (prev->vb.width == buf->vb.width &&
648 prev->vb.height == buf->vb.height &&
649 prev->fmt == buf->fmt) {
650 list_add_tail(&buf->vb.queue,&q->active);
651 buf->vb.state = STATE_ACTIVE;
652 buf->count = q->count++;
653 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
654 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
655 buf, buf->vb.i);
656
657 } else {
658 list_add_tail(&buf->vb.queue,&q->queued);
659 buf->vb.state = STATE_QUEUED;
660 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
661 buf, buf->vb.i);
662 }
663 }
664}
665
666static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
667{
668 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300670 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Adrian Bunk408b6642005-05-01 08:59:29 -0700673static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .buf_setup = buffer_setup,
675 .buf_prepare = buffer_prepare,
676 .buf_queue = buffer_queue,
677 .buf_release = buffer_release,
678};
679
680/* ------------------------------------------------------------------ */
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683/* ------------------------------------------------------------------ */
684
685static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
686{
687 switch (fh->type) {
688 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
689 return &fh->vidq;
690 case V4L2_BUF_TYPE_VBI_CAPTURE:
691 return &fh->vbiq;
692 default:
693 BUG();
694 return NULL;
695 }
696}
697
698static int get_ressource(struct cx8800_fh *fh)
699{
700 switch (fh->type) {
701 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
702 return RESOURCE_VIDEO;
703 case V4L2_BUF_TYPE_VBI_CAPTURE:
704 return RESOURCE_VBI;
705 default:
706 BUG();
707 return 0;
708 }
709}
710
711static int video_open(struct inode *inode, struct file *file)
712{
713 int minor = iminor(inode);
714 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700715 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct cx8800_fh *fh;
717 struct list_head *list;
718 enum v4l2_buf_type type = 0;
719 int radio = 0;
720
721 list_for_each(list,&cx8800_devlist) {
722 h = list_entry(list, struct cx8800_dev, devlist);
723 if (h->video_dev->minor == minor) {
724 dev = h;
725 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
726 }
727 if (h->vbi_dev->minor == minor) {
728 dev = h;
729 type = V4L2_BUF_TYPE_VBI_CAPTURE;
730 }
731 if (h->radio_dev &&
732 h->radio_dev->minor == minor) {
733 radio = 1;
734 dev = h;
735 }
736 }
737 if (NULL == dev)
738 return -ENODEV;
739
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700740 core = dev->core;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dprintk(1,"open minor=%d radio=%d type=%s\n",
743 minor,radio,v4l2_type_names[type]);
744
745 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200746 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (NULL == fh)
748 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 file->private_data = fh;
750 fh->dev = dev;
751 fh->radio = radio;
752 fh->type = type;
753 fh->width = 320;
754 fh->height = 240;
755 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
756
757 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
758 dev->pci, &dev->slock,
759 V4L2_BUF_TYPE_VIDEO_CAPTURE,
760 V4L2_FIELD_INTERLACED,
761 sizeof(struct cx88_buffer),
762 fh);
763 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
764 dev->pci, &dev->slock,
765 V4L2_BUF_TYPE_VBI_CAPTURE,
766 V4L2_FIELD_SEQ_TB,
767 sizeof(struct cx88_buffer),
768 fh);
769
770 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int board = core->board;
772 dprintk(1,"video_open: setting radio device\n");
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700773 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
775 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
776 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700777 core->tvaudio = WW_FM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 cx88_set_tvaudio(core);
779 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700780 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800787video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct cx8800_fh *fh = file->private_data;
790
791 switch (fh->type) {
792 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
793 if (res_locked(fh->dev,RESOURCE_VIDEO))
794 return -EBUSY;
795 return videobuf_read_one(&fh->vidq, data, count, ppos,
796 file->f_flags & O_NONBLOCK);
797 case V4L2_BUF_TYPE_VBI_CAPTURE:
798 if (!res_get(fh->dev,fh,RESOURCE_VBI))
799 return -EBUSY;
800 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
801 file->f_flags & O_NONBLOCK);
802 default:
803 BUG();
804 return 0;
805 }
806}
807
808static unsigned int
809video_poll(struct file *file, struct poll_table_struct *wait)
810{
811 struct cx8800_fh *fh = file->private_data;
812 struct cx88_buffer *buf;
813
814 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
815 if (!res_get(fh->dev,fh,RESOURCE_VBI))
816 return POLLERR;
817 return videobuf_poll_stream(file, &fh->vbiq, wait);
818 }
819
820 if (res_check(fh,RESOURCE_VIDEO)) {
821 /* streaming capture */
822 if (list_empty(&fh->vidq.stream))
823 return POLLERR;
824 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
825 } else {
826 /* read() capture */
827 buf = (struct cx88_buffer*)fh->vidq.read_buf;
828 if (NULL == buf)
829 return POLLERR;
830 }
831 poll_wait(file, &buf->vb.done, wait);
832 if (buf->vb.state == STATE_DONE ||
833 buf->vb.state == STATE_ERROR)
834 return POLLIN|POLLRDNORM;
835 return 0;
836}
837
838static int video_release(struct inode *inode, struct file *file)
839{
840 struct cx8800_fh *fh = file->private_data;
841 struct cx8800_dev *dev = fh->dev;
842
843 /* turn off overlay */
844 if (res_check(fh, RESOURCE_OVERLAY)) {
845 /* FIXME */
846 res_free(dev,fh,RESOURCE_OVERLAY);
847 }
848
849 /* stop video capture */
850 if (res_check(fh, RESOURCE_VIDEO)) {
851 videobuf_queue_cancel(&fh->vidq);
852 res_free(dev,fh,RESOURCE_VIDEO);
853 }
854 if (fh->vidq.read_buf) {
855 buffer_release(&fh->vidq,fh->vidq.read_buf);
856 kfree(fh->vidq.read_buf);
857 }
858
859 /* stop vbi capture */
860 if (res_check(fh, RESOURCE_VBI)) {
861 if (fh->vbiq.streaming)
862 videobuf_streamoff(&fh->vbiq);
863 if (fh->vbiq.reading)
864 videobuf_read_stop(&fh->vbiq);
865 res_free(dev,fh,RESOURCE_VBI);
866 }
867
868 videobuf_mmap_free(&fh->vidq);
869 videobuf_mmap_free(&fh->vbiq);
870 file->private_data = NULL;
871 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700872
873 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return 0;
876}
877
878static int
879video_mmap(struct file *file, struct vm_area_struct * vma)
880{
881 struct cx8800_fh *fh = file->private_data;
882
883 return videobuf_mmap_mapper(get_queue(fh), vma);
884}
885
886/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300887/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300889int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300891 struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 u32 value;
893 int i;
894
895 for (i = 0; i < CX8800_CTLS; i++)
896 if (cx8800_ctls[i].v.id == ctl->id)
897 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300898 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -EINVAL;
900
901 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
902 switch (ctl->id) {
903 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300904 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
905 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 break;
907 case V4L2_CID_AUDIO_VOLUME:
908 ctl->value = 0x3f - (value & 0x3f);
909 break;
910 default:
911 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
912 break;
913 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300914 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
915 ctl->id, c->v.name, ctl->value, c->reg,
916 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
918}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300919EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300921int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200924 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300926
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200927 for (i = 0; i < CX8800_CTLS; i++) {
928 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200930 }
931 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300932 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 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;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200939 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 switch (ctl->id) {
941 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300942 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944 case V4L2_CID_AUDIO_VOLUME:
945 value = 0x3f - (ctl->value & 0x3f);
946 break;
947 case V4L2_CID_SATURATION:
948 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200949
950 value = ((ctl->value - c->off) << c->shift) & c->mask;
951
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -0300952 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200953 /* For SECAM, both U and V sat should be equal */
954 value=value<<8|value;
955 } else {
956 /* Keeps U Saturation proportional to V Sat */
957 value=(value*0x5a)/0x7f<<8|value;
958 }
959 mask=0xffff;
960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 default:
962 value = ((ctl->value - c->off) << c->shift) & c->mask;
963 break;
964 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300965 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
966 ctl->id, c->v.name, ctl->value, c->reg, value,
967 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200969 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200971 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973 return 0;
974}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300975EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700977static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200979 struct v4l2_control ctrl;
980 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200982 for (i = 0; i < CX8800_CTLS; i++) {
983 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300984 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300985
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300986 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300991/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300993static int vidioc_g_fmt_cap (struct file *file, void *priv,
994 struct v4l2_format *f)
995{
996 struct cx8800_fh *fh = priv;
997
998 f->fmt.pix.width = fh->width;
999 f->fmt.pix.height = fh->height;
1000 f->fmt.pix.field = fh->vidq.field;
1001 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1002 f->fmt.pix.bytesperline =
1003 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1004 f->fmt.pix.sizeimage =
1005 f->fmt.pix.height * f->fmt.pix.bytesperline;
1006 return 0;
1007}
1008
1009static int vidioc_try_fmt_cap (struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct v4l2_format *f)
1011{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001012 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1013 struct cx8800_fmt *fmt;
1014 enum v4l2_field field;
1015 unsigned int maxw, maxh;
1016
1017 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1018 if (NULL == fmt)
1019 return -EINVAL;
1020
1021 field = f->fmt.pix.field;
1022 maxw = norm_maxw(core->tvnorm);
1023 maxh = norm_maxh(core->tvnorm);
1024
1025 if (V4L2_FIELD_ANY == field) {
1026 field = (f->fmt.pix.height > maxh/2)
1027 ? V4L2_FIELD_INTERLACED
1028 : V4L2_FIELD_BOTTOM;
1029 }
1030
1031 switch (field) {
1032 case V4L2_FIELD_TOP:
1033 case V4L2_FIELD_BOTTOM:
1034 maxh = maxh / 2;
1035 break;
1036 case V4L2_FIELD_INTERLACED:
1037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 default:
1039 return -EINVAL;
1040 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001041
1042 f->fmt.pix.field = field;
1043 if (f->fmt.pix.height < 32)
1044 f->fmt.pix.height = 32;
1045 if (f->fmt.pix.height > maxh)
1046 f->fmt.pix.height = maxh;
1047 if (f->fmt.pix.width < 48)
1048 f->fmt.pix.width = 48;
1049 if (f->fmt.pix.width > maxw)
1050 f->fmt.pix.width = maxw;
1051 f->fmt.pix.width &= ~0x03;
1052 f->fmt.pix.bytesperline =
1053 (f->fmt.pix.width * fmt->depth) >> 3;
1054 f->fmt.pix.sizeimage =
1055 f->fmt.pix.height * f->fmt.pix.bytesperline;
1056
1057 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001060static int vidioc_s_fmt_cap (struct file *file, void *priv,
1061 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001063 struct cx8800_fh *fh = priv;
1064 int err = vidioc_try_fmt_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001065
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001066 if (0 != err)
1067 return err;
1068 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1069 fh->width = f->fmt.pix.width;
1070 fh->height = f->fmt.pix.height;
1071 fh->vidq.field = f->fmt.pix.field;
1072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001075static int vidioc_querycap (struct file *file, void *priv,
1076 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001078 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001081 strcpy(cap->driver, "cx8800");
1082 strlcpy(cap->card, cx88_boards[core->board].name,
1083 sizeof(cap->card));
1084 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1085 cap->version = CX88_VERSION_CODE;
1086 cap->capabilities =
1087 V4L2_CAP_VIDEO_CAPTURE |
1088 V4L2_CAP_READWRITE |
1089 V4L2_CAP_STREAMING |
1090 V4L2_CAP_VBI_CAPTURE;
1091 if (UNSET != core->tuner_type)
1092 cap->capabilities |= V4L2_CAP_TUNER;
1093 return 0;
1094}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001095
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001096static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1097 struct v4l2_fmtdesc *f)
1098{
1099 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1100 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001102 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1103 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001105 return 0;
1106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001108#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001109static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001110{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001111 struct cx8800_fh *fh = priv;
1112 struct videobuf_queue *q;
1113 struct v4l2_requestbuffers req;
1114 unsigned int i;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001115 int err;
1116
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001117 q = get_queue(fh);
1118 memset(&req,0,sizeof(req));
1119 req.type = q->type;
1120 req.count = 8;
1121 req.memory = V4L2_MEMORY_MMAP;
1122 err = videobuf_reqbufs(q,&req);
1123 if (err < 0)
1124 return err;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001125
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001126 mbuf->frames = req.count;
1127 mbuf->size = 0;
1128 for (i = 0; i < mbuf->frames; i++) {
1129 mbuf->offsets[i] = q->bufs[i]->boff;
1130 mbuf->size += q->bufs[i]->bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132 return 0;
1133}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001136static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001138 struct cx8800_fh *fh = priv;
1139 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001142static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1143{
1144 struct cx8800_fh *fh = priv;
1145 return (videobuf_querybuf(get_queue(fh), p));
1146}
1147
1148static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1149{
1150 struct cx8800_fh *fh = priv;
1151 return (videobuf_qbuf(get_queue(fh), p));
1152}
1153
1154static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1155{
1156 struct cx8800_fh *fh = priv;
1157 return (videobuf_dqbuf(get_queue(fh), p,
1158 file->f_flags & O_NONBLOCK));
1159}
1160
1161static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1162{
1163 struct cx8800_fh *fh = priv;
1164 struct cx8800_dev *dev = fh->dev;
1165
1166 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1167 return -EINVAL;
1168 if (unlikely(i != fh->type))
1169 return -EINVAL;
1170
1171 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1172 return -EBUSY;
1173 return videobuf_streamon(get_queue(fh));
1174}
1175
1176static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1177{
1178 struct cx8800_fh *fh = priv;
1179 struct cx8800_dev *dev = fh->dev;
1180 int err, res;
1181
1182 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1183 return -EINVAL;
1184 if (i != fh->type)
1185 return -EINVAL;
1186
1187 res = get_ressource(fh);
1188 err = videobuf_streamoff(get_queue(fh));
1189 if (err < 0)
1190 return err;
1191 res_free(dev,fh,res);
1192 return 0;
1193}
1194
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001195static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001196{
1197 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1198
1199 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001200 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001201 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001202
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001203 return 0;
1204}
1205
1206/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001207int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001208{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001209 static const char *iname[] = {
1210 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1211 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1212 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1213 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1214 [ CX88_VMUX_SVIDEO ] = "S-Video",
1215 [ CX88_VMUX_TELEVISION ] = "Television",
1216 [ CX88_VMUX_CABLE ] = "Cable TV",
1217 [ CX88_VMUX_DVB ] = "DVB",
1218 [ CX88_VMUX_DEBUG ] = "for debug only",
1219 };
1220 unsigned int n;
1221
1222 n = i->index;
1223 if (n >= 4)
1224 return -EINVAL;
1225 if (0 == INPUT(n)->type)
1226 return -EINVAL;
1227 memset(i,0,sizeof(*i));
1228 i->index = n;
1229 i->type = V4L2_INPUT_TYPE_CAMERA;
1230 strcpy(i->name,iname[INPUT(n)->type]);
1231 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1232 (CX88_VMUX_CABLE == INPUT(n)->type))
1233 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001234 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001235 return 0;
1236}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001237EXPORT_SYMBOL(cx88_enum_input);
1238
1239static int vidioc_enum_input (struct file *file, void *priv,
1240 struct v4l2_input *i)
1241{
1242 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1243 return cx88_enum_input (core,i);
1244}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001245
1246static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1247{
1248 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1249
1250 *i = core->input;
1251 return 0;
1252}
1253
1254static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1255{
1256 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1257
1258 if (i >= 4)
1259 return -EINVAL;
1260
1261 mutex_lock(&core->lock);
1262 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001263 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001264 mutex_unlock(&core->lock);
1265 return 0;
1266}
1267
1268
1269
1270static int vidioc_queryctrl (struct file *file, void *priv,
1271 struct v4l2_queryctrl *qctrl)
1272{
1273 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1274 if (unlikely(qctrl->id == 0))
1275 return -EINVAL;
1276 return cx8800_ctrl_query(qctrl);
1277}
1278
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001279static int vidioc_g_ctrl (struct file *file, void *priv,
1280 struct v4l2_control *ctl)
1281{
1282 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1283 return
1284 cx88_get_control(core,ctl);
1285}
1286
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001287static int vidioc_s_ctrl (struct file *file, void *priv,
1288 struct v4l2_control *ctl)
1289{
1290 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001291 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001292 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001293}
1294
1295static int vidioc_g_tuner (struct file *file, void *priv,
1296 struct v4l2_tuner *t)
1297{
1298 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1299 u32 reg;
1300
1301 if (unlikely(UNSET == core->tuner_type))
1302 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001303 if (0 != t->index)
1304 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001305
1306 strcpy(t->name, "Television");
1307 t->type = V4L2_TUNER_ANALOG_TV;
1308 t->capability = V4L2_TUNER_CAP_NORM;
1309 t->rangehigh = 0xffffffffUL;
1310
1311 cx88_get_stereo(core ,t);
1312 reg = cx_read(MO_DEVICE_STATUS);
1313 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1314 return 0;
1315}
1316
1317static int vidioc_s_tuner (struct file *file, void *priv,
1318 struct v4l2_tuner *t)
1319{
1320 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1321
1322 if (UNSET == core->tuner_type)
1323 return -EINVAL;
1324 if (0 != t->index)
1325 return -EINVAL;
1326
1327 cx88_set_stereo(core, t->audmode, 1);
1328 return 0;
1329}
1330
1331static int vidioc_g_frequency (struct file *file, void *priv,
1332 struct v4l2_frequency *f)
1333{
1334 struct cx8800_fh *fh = priv;
1335 struct cx88_core *core = fh->dev->core;
1336
1337 if (unlikely(UNSET == core->tuner_type))
1338 return -EINVAL;
1339
1340 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1341 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1342 f->frequency = core->freq;
1343
1344 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1345
1346 return 0;
1347}
1348
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001349int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001350 struct v4l2_frequency *f)
1351{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001352 if (unlikely(UNSET == core->tuner_type))
1353 return -EINVAL;
1354 if (unlikely(f->tuner != 0))
1355 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001356
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001357 mutex_lock(&core->lock);
1358 core->freq = f->frequency;
1359 cx88_newstation(core);
1360 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1361
1362 /* When changing channels it is required to reset TVAUDIO */
1363 msleep (10);
1364 cx88_set_tvaudio(core);
1365
1366 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001367
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001368 return 0;
1369}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001370EXPORT_SYMBOL(cx88_set_freq);
1371
1372static int vidioc_s_frequency (struct file *file, void *priv,
1373 struct v4l2_frequency *f)
1374{
1375 struct cx8800_fh *fh = priv;
1376 struct cx88_core *core = fh->dev->core;
1377
1378 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1379 return -EINVAL;
1380 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1381 return -EINVAL;
1382
1383 return
1384 cx88_set_freq (core,f);
1385}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001386
Trent Piephodbbff482007-01-22 23:31:53 -03001387#ifdef CONFIG_VIDEO_ADV_DEBUG
1388static int vidioc_g_register (struct file *file, void *fh,
1389 struct v4l2_register *reg)
1390{
1391 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1392
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001393 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001394 return -EINVAL;
1395 /* cx2388x has a 24-bit register space */
1396 reg->val = cx_read(reg->reg&0xffffff);
1397 return 0;
1398}
1399
1400static int vidioc_s_register (struct file *file, void *fh,
1401 struct v4l2_register *reg)
1402{
1403 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1404
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001405 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001406 return -EINVAL;
Trent Piephodbbff482007-01-22 23:31:53 -03001407 cx_write(reg->reg&0xffffff, reg->val);
1408 return 0;
1409}
1410#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001413/* RADIO ESPECIFIC IOCTLS */
1414/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001416static int radio_querycap (struct file *file, void *priv,
1417 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001419 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct cx88_core *core = dev->core;
1421
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001422 strcpy(cap->driver, "cx8800");
1423 strlcpy(cap->card, cx88_boards[core->board].name,
1424 sizeof(cap->card));
1425 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1426 cap->version = CX88_VERSION_CODE;
1427 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001429}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001431static int radio_g_tuner (struct file *file, void *priv,
1432 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001434 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1435
1436 if (unlikely(t->index > 0))
1437 return -EINVAL;
1438
1439 strcpy(t->name, "Radio");
1440 t->type = V4L2_TUNER_RADIO;
1441
1442 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1443 return 0;
1444}
1445
1446static int radio_enum_input (struct file *file, void *priv,
1447 struct v4l2_input *i)
1448{
1449 if (i->index != 0)
1450 return -EINVAL;
1451 strcpy(i->name,"Radio");
1452 i->type = V4L2_INPUT_TYPE_TUNER;
1453
1454 return 0;
1455}
1456
1457static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1458{
1459 if (unlikely(a->index))
1460 return -EINVAL;
1461
1462 memset(a,0,sizeof(*a));
1463 strcpy(a->name,"Radio");
1464 return 0;
1465}
1466
1467/* FIXME: Should add a standard for radio */
1468
1469static int radio_s_tuner (struct file *file, void *priv,
1470 struct v4l2_tuner *t)
1471{
1472 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1473
1474 if (0 != t->index)
1475 return -EINVAL;
1476
1477 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1478
1479 return 0;
1480}
1481
1482static int radio_s_audio (struct file *file, void *fh,
1483 struct v4l2_audio *a)
1484{
1485 return 0;
1486}
1487
1488static int radio_s_input (struct file *file, void *fh, unsigned int i)
1489{
1490 return 0;
1491}
1492
1493static int radio_queryctrl (struct file *file, void *priv,
1494 struct v4l2_queryctrl *c)
1495{
1496 int i;
1497
1498 if (c->id < V4L2_CID_BASE ||
1499 c->id >= V4L2_CID_LASTP1)
1500 return -EINVAL;
1501 if (c->id == V4L2_CID_AUDIO_MUTE) {
1502 for (i = 0; i < CX8800_CTLS; i++)
1503 if (cx8800_ctls[i].v.id == c->id)
1504 break;
1505 *c = cx8800_ctls[i].v;
1506 } else
1507 *c = no_ctl;
1508 return 0;
1509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511/* ----------------------------------------------------------- */
1512
1513static void cx8800_vid_timeout(unsigned long data)
1514{
1515 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1516 struct cx88_core *core = dev->core;
1517 struct cx88_dmaqueue *q = &dev->vidq;
1518 struct cx88_buffer *buf;
1519 unsigned long flags;
1520
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001521 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 cx_clear(MO_VID_DMACNTRL, 0x11);
1524 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1525
1526 spin_lock_irqsave(&dev->slock,flags);
1527 while (!list_empty(&q->active)) {
1528 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1529 list_del(&buf->vb.queue);
1530 buf->vb.state = STATE_ERROR;
1531 wake_up(&buf->vb.done);
1532 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1533 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1534 }
1535 restart_video_queue(dev,q);
1536 spin_unlock_irqrestore(&dev->slock,flags);
1537}
1538
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001539static char *cx88_vid_irqs[32] = {
1540 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1541 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1542 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1543 "y_sync", "u_sync", "v_sync", "vbi_sync",
1544 "opc_err", "par_err", "rip_err", "pci_abort",
1545};
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547static void cx8800_vid_irq(struct cx8800_dev *dev)
1548{
1549 struct cx88_core *core = dev->core;
1550 u32 status, mask, count;
1551
1552 status = cx_read(MO_VID_INTSTAT);
1553 mask = cx_read(MO_VID_INTMSK);
1554 if (0 == (status & mask))
1555 return;
1556 cx_write(MO_VID_INTSTAT, status);
1557 if (irq_debug || (status & mask & ~0xff))
1558 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001559 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1560 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /* risc op code error */
1563 if (status & (1 << 16)) {
1564 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1565 cx_clear(MO_VID_DMACNTRL, 0x11);
1566 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001567 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569
1570 /* risc1 y */
1571 if (status & 0x01) {
1572 spin_lock(&dev->slock);
1573 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001574 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 spin_unlock(&dev->slock);
1576 }
1577
1578 /* risc1 vbi */
1579 if (status & 0x08) {
1580 spin_lock(&dev->slock);
1581 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001582 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 spin_unlock(&dev->slock);
1584 }
1585
1586 /* risc2 y */
1587 if (status & 0x10) {
1588 dprintk(2,"stopper video\n");
1589 spin_lock(&dev->slock);
1590 restart_video_queue(dev,&dev->vidq);
1591 spin_unlock(&dev->slock);
1592 }
1593
1594 /* risc2 vbi */
1595 if (status & 0x80) {
1596 dprintk(2,"stopper vbi\n");
1597 spin_lock(&dev->slock);
1598 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1599 spin_unlock(&dev->slock);
1600 }
1601}
1602
David Howells7d12e782006-10-05 14:55:46 +01001603static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 struct cx8800_dev *dev = dev_id;
1606 struct cx88_core *core = dev->core;
1607 u32 status;
1608 int loop, handled = 0;
1609
1610 for (loop = 0; loop < 10; loop++) {
1611 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1612 if (0 == status)
1613 goto out;
1614 cx_write(MO_PCI_INTSTAT, status);
1615 handled = 1;
1616
1617 if (status & core->pci_irqmask)
1618 cx88_core_irq(core,status);
1619 if (status & 0x01)
1620 cx8800_vid_irq(dev);
1621 };
1622 if (10 == loop) {
1623 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1624 core->name);
1625 cx_write(MO_PCI_INTMSK,0);
1626 }
1627
1628 out:
1629 return IRQ_RETVAL(handled);
1630}
1631
1632/* ----------------------------------------------------------- */
1633/* exported stuff */
1634
Arjan van de Venfa027c22007-02-12 00:55:33 -08001635static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 .owner = THIS_MODULE,
1638 .open = video_open,
1639 .release = video_release,
1640 .read = video_read,
1641 .poll = video_poll,
1642 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001643 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001644 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 .llseek = no_llseek,
1646};
1647
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001648static struct video_device cx8800_vbi_template;
Adrian Bunk408b6642005-05-01 08:59:29 -07001649static struct video_device cx8800_video_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001651 .name = "cx8800-video",
1652 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1653 .fops = &video_fops,
1654 .minor = -1,
1655 .vidioc_querycap = vidioc_querycap,
1656 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1657 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1658 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1659 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1660 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1661 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1662 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1663 .vidioc_reqbufs = vidioc_reqbufs,
1664 .vidioc_querybuf = vidioc_querybuf,
1665 .vidioc_qbuf = vidioc_qbuf,
1666 .vidioc_dqbuf = vidioc_dqbuf,
1667 .vidioc_s_std = vidioc_s_std,
1668 .vidioc_enum_input = vidioc_enum_input,
1669 .vidioc_g_input = vidioc_g_input,
1670 .vidioc_s_input = vidioc_s_input,
1671 .vidioc_queryctrl = vidioc_queryctrl,
1672 .vidioc_g_ctrl = vidioc_g_ctrl,
1673 .vidioc_s_ctrl = vidioc_s_ctrl,
1674 .vidioc_streamon = vidioc_streamon,
1675 .vidioc_streamoff = vidioc_streamoff,
1676#ifdef CONFIG_VIDEO_V4L1_COMPAT
1677 .vidiocgmbuf = vidiocgmbuf,
1678#endif
1679 .vidioc_g_tuner = vidioc_g_tuner,
1680 .vidioc_s_tuner = vidioc_s_tuner,
1681 .vidioc_g_frequency = vidioc_g_frequency,
1682 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001683#ifdef CONFIG_VIDEO_ADV_DEBUG
1684 .vidioc_g_register = vidioc_g_register,
1685 .vidioc_s_register = vidioc_s_register,
1686#endif
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001687 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001688 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689};
1690
Arjan van de Venfa027c22007-02-12 00:55:33 -08001691static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 .owner = THIS_MODULE,
1694 .open = video_open,
1695 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001696 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001697 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 .llseek = no_llseek,
1699};
1700
Adrian Bunk408b6642005-05-01 08:59:29 -07001701static struct video_device cx8800_radio_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001703 .name = "cx8800-radio",
1704 .type = VID_TYPE_TUNER,
1705 .hardware = 0,
1706 .fops = &radio_fops,
1707 .minor = -1,
1708 .vidioc_querycap = radio_querycap,
1709 .vidioc_g_tuner = radio_g_tuner,
1710 .vidioc_enum_input = radio_enum_input,
1711 .vidioc_g_audio = radio_g_audio,
1712 .vidioc_s_tuner = radio_s_tuner,
1713 .vidioc_s_audio = radio_s_audio,
1714 .vidioc_s_input = radio_s_input,
1715 .vidioc_queryctrl = radio_queryctrl,
1716 .vidioc_g_ctrl = vidioc_g_ctrl,
1717 .vidioc_s_ctrl = vidioc_s_ctrl,
1718 .vidioc_g_frequency = vidioc_g_frequency,
1719 .vidioc_s_frequency = vidioc_s_frequency,
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 */
1828 if (TUNER_ABSENT != core->tuner_type)
1829 request_module("tuner");
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001830
Steven Toth30579062006-09-25 12:43:42 -03001831 if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
1832 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
1858 if (core->has_radio) {
1859 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 */
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001884 if (core->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 */