blob: 1439b72685326f815cdacab0b653f159297ac9f5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070035#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/kthread.h>
38#include <asm/div64.h>
39
40#include "cx88.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020041#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030043#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080044/* Include V4L1 specific functions. Should be removed soon */
45#include <linux/videodev.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030046#endif
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
49MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
50MODULE_LICENSE("GPL");
51
52/* ------------------------------------------------------------------ */
53
54static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57
58module_param_array(video_nr, int, NULL, 0444);
59module_param_array(vbi_nr, int, NULL, 0444);
60module_param_array(radio_nr, int, NULL, 0444);
61
62MODULE_PARM_DESC(video_nr,"video device numbers");
63MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
64MODULE_PARM_DESC(radio_nr,"radio device numbers");
65
66static unsigned int video_debug = 0;
67module_param(video_debug,int,0644);
68MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
69
70static unsigned int irq_debug = 0;
71module_param(irq_debug,int,0644);
72MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
73
74static unsigned int vid_limit = 16;
75module_param(vid_limit,int,0644);
76MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
77
78#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070079 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* ------------------------------------------------------------------ */
82
83static LIST_HEAD(cx8800_devlist);
84
85/* ------------------------------------------------------------------- */
86/* static data */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static struct cx8800_fmt formats[] = {
89 {
90 .name = "8 bpp, gray",
91 .fourcc = V4L2_PIX_FMT_GREY,
92 .cxformat = ColorFormatY8,
93 .depth = 8,
94 .flags = FORMAT_FLAGS_PACKED,
95 },{
96 .name = "15 bpp RGB, le",
97 .fourcc = V4L2_PIX_FMT_RGB555,
98 .cxformat = ColorFormatRGB15,
99 .depth = 16,
100 .flags = FORMAT_FLAGS_PACKED,
101 },{
102 .name = "15 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB555X,
104 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
105 .depth = 16,
106 .flags = FORMAT_FLAGS_PACKED,
107 },{
108 .name = "16 bpp RGB, le",
109 .fourcc = V4L2_PIX_FMT_RGB565,
110 .cxformat = ColorFormatRGB16,
111 .depth = 16,
112 .flags = FORMAT_FLAGS_PACKED,
113 },{
114 .name = "16 bpp RGB, be",
115 .fourcc = V4L2_PIX_FMT_RGB565X,
116 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
117 .depth = 16,
118 .flags = FORMAT_FLAGS_PACKED,
119 },{
120 .name = "24 bpp RGB, le",
121 .fourcc = V4L2_PIX_FMT_BGR24,
122 .cxformat = ColorFormatRGB24,
123 .depth = 24,
124 .flags = FORMAT_FLAGS_PACKED,
125 },{
126 .name = "32 bpp RGB, le",
127 .fourcc = V4L2_PIX_FMT_BGR32,
128 .cxformat = ColorFormatRGB32,
129 .depth = 32,
130 .flags = FORMAT_FLAGS_PACKED,
131 },{
132 .name = "32 bpp RGB, be",
133 .fourcc = V4L2_PIX_FMT_RGB32,
134 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
135 .depth = 32,
136 .flags = FORMAT_FLAGS_PACKED,
137 },{
138 .name = "4:2:2, packed, YUYV",
139 .fourcc = V4L2_PIX_FMT_YUYV,
140 .cxformat = ColorFormatYUY2,
141 .depth = 16,
142 .flags = FORMAT_FLAGS_PACKED,
143 },{
144 .name = "4:2:2, packed, UYVY",
145 .fourcc = V4L2_PIX_FMT_UYVY,
146 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
147 .depth = 16,
148 .flags = FORMAT_FLAGS_PACKED,
149 },
150};
151
152static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
153{
154 unsigned int i;
155
156 for (i = 0; i < ARRAY_SIZE(formats); i++)
157 if (formats[i].fourcc == fourcc)
158 return formats+i;
159 return NULL;
160}
161
162/* ------------------------------------------------------------------- */
163
164static const struct v4l2_queryctrl no_ctl = {
165 .name = "42",
166 .flags = V4L2_CTRL_FLAG_DISABLED,
167};
168
169static struct cx88_ctrl cx8800_ctls[] = {
170 /* --- video --- */
171 {
172 .v = {
173 .id = V4L2_CID_BRIGHTNESS,
174 .name = "Brightness",
175 .minimum = 0x00,
176 .maximum = 0xff,
177 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300178 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .type = V4L2_CTRL_TYPE_INTEGER,
180 },
181 .off = 128,
182 .reg = MO_CONTR_BRIGHT,
183 .mask = 0x00ff,
184 .shift = 0,
185 },{
186 .v = {
187 .id = V4L2_CID_CONTRAST,
188 .name = "Contrast",
189 .minimum = 0,
190 .maximum = 0xff,
191 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200192 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .type = V4L2_CTRL_TYPE_INTEGER,
194 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700195 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .reg = MO_CONTR_BRIGHT,
197 .mask = 0xff00,
198 .shift = 8,
199 },{
200 .v = {
201 .id = V4L2_CID_HUE,
202 .name = "Hue",
203 .minimum = 0,
204 .maximum = 0xff,
205 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300206 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .type = V4L2_CTRL_TYPE_INTEGER,
208 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700209 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .reg = MO_HUE,
211 .mask = 0x00ff,
212 .shift = 0,
213 },{
214 /* strictly, this only describes only U saturation.
215 * V saturation is handled specially through code.
216 */
217 .v = {
218 .id = V4L2_CID_SATURATION,
219 .name = "Saturation",
220 .minimum = 0,
221 .maximum = 0xff,
222 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200223 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .type = V4L2_CTRL_TYPE_INTEGER,
225 },
226 .off = 0,
227 .reg = MO_UV_SATURATION,
228 .mask = 0x00ff,
229 .shift = 0,
230 },{
231 /* --- audio --- */
232 .v = {
233 .id = V4L2_CID_AUDIO_MUTE,
234 .name = "Mute",
235 .minimum = 0,
236 .maximum = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200237 .default_value = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .type = V4L2_CTRL_TYPE_BOOLEAN,
239 },
240 .reg = AUD_VOL_CTL,
241 .sreg = SHADOW_AUD_VOL_CTL,
242 .mask = (1 << 6),
243 .shift = 6,
244 },{
245 .v = {
246 .id = V4L2_CID_AUDIO_VOLUME,
247 .name = "Volume",
248 .minimum = 0,
249 .maximum = 0x3f,
250 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300251 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .type = V4L2_CTRL_TYPE_INTEGER,
253 },
254 .reg = AUD_VOL_CTL,
255 .sreg = SHADOW_AUD_VOL_CTL,
256 .mask = 0x3f,
257 .shift = 0,
258 },{
259 .v = {
260 .id = V4L2_CID_AUDIO_BALANCE,
261 .name = "Balance",
262 .minimum = 0,
263 .maximum = 0x7f,
264 .step = 1,
265 .default_value = 0x40,
266 .type = V4L2_CTRL_TYPE_INTEGER,
267 },
268 .reg = AUD_BAL_CTL,
269 .sreg = SHADOW_AUD_BAL_CTL,
270 .mask = 0x7f,
271 .shift = 0,
272 }
273};
Adrian Bunk408b6642005-05-01 08:59:29 -0700274static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Michael Krufky38a27132006-06-26 23:42:39 -0300276const u32 cx88_user_ctrls[] = {
277 V4L2_CID_USER_CLASS,
278 V4L2_CID_BRIGHTNESS,
279 V4L2_CID_CONTRAST,
280 V4L2_CID_SATURATION,
281 V4L2_CID_HUE,
282 V4L2_CID_AUDIO_VOLUME,
283 V4L2_CID_AUDIO_BALANCE,
284 V4L2_CID_AUDIO_MUTE,
285 0
286};
287EXPORT_SYMBOL(cx88_user_ctrls);
288
289static const u32 *ctrl_classes[] = {
290 cx88_user_ctrls,
291 NULL
292};
293
294int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
295{
296 int i;
297
298 if (qctrl->id < V4L2_CID_BASE ||
299 qctrl->id >= V4L2_CID_LASTP1)
300 return -EINVAL;
301 for (i = 0; i < CX8800_CTLS; i++)
302 if (cx8800_ctls[i].v.id == qctrl->id)
303 break;
304 if (i == CX8800_CTLS) {
305 *qctrl = no_ctl;
306 return 0;
307 }
308 *qctrl = cx8800_ctls[i].v;
309 return 0;
310}
311EXPORT_SYMBOL(cx8800_ctrl_query);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/* ------------------------------------------------------------------- */
314/* resource management */
315
316static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
317{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700318 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (fh->resources & bit)
320 /* have it already allocated */
321 return 1;
322
323 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200324 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (dev->resources & bit) {
326 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200327 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329 }
330 /* it's free, grab it */
331 fh->resources |= bit;
332 dev->resources |= bit;
333 dprintk(1,"res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200334 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 1;
336}
337
338static
339int res_check(struct cx8800_fh *fh, unsigned int bit)
340{
341 return (fh->resources & bit);
342}
343
344static
345int res_locked(struct cx8800_dev *dev, unsigned int bit)
346{
347 return (dev->resources & bit);
348}
349
350static
351void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
352{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700353 struct cx88_core *core = dev->core;
Eric Sesterhennae246012006-03-13 13:17:11 -0300354 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Ingo Molnar3593cab2006-02-07 06:49:14 -0200356 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 fh->resources &= ~bits;
358 dev->resources &= ~bits;
359 dprintk(1,"res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200360 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/* ------------------------------------------------------------------ */
364
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300365int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700367 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300370 input, INPUT(input).vmux,
371 INPUT(input).gpio0,INPUT(input).gpio1,
372 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700373 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300374 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
375 cx_write(MO_GP3_IO, INPUT(input).gpio3);
376 cx_write(MO_GP0_IO, INPUT(input).gpio0);
377 cx_write(MO_GP1_IO, INPUT(input).gpio1);
378 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Trent Piepho6a59d642007-08-15 14:41:57 -0300380 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case CX88_VMUX_SVIDEO:
382 cx_set(MO_AFECFG_IO, 0x00000001);
383 cx_set(MO_INPUT_FORMAT, 0x00010010);
384 cx_set(MO_FILTER_EVEN, 0x00002020);
385 cx_set(MO_FILTER_ODD, 0x00002020);
386 break;
387 default:
388 cx_clear(MO_AFECFG_IO, 0x00000001);
389 cx_clear(MO_INPUT_FORMAT, 0x00010010);
390 cx_clear(MO_FILTER_EVEN, 0x00002020);
391 cx_clear(MO_FILTER_ODD, 0x00002020);
392 break;
393 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300394
Trent Piepho6a59d642007-08-15 14:41:57 -0300395 if (core->board.mpeg & CX88_MPEG_BLACKBIRD) {
Michael Krufkyf24546a2006-10-16 16:51:11 -0300396 /* sets sound input from external adc */
Trent Piepho6a59d642007-08-15 14:41:57 -0300397 if (INPUT(input).extadc)
Michael Krufkyf24546a2006-10-16 16:51:11 -0300398 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
399 else
400 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300404EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/* ------------------------------------------------------------------ */
407
408static int start_video_dma(struct cx8800_dev *dev,
409 struct cx88_dmaqueue *q,
410 struct cx88_buffer *buf)
411{
412 struct cx88_core *core = dev->core;
413
414 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700415 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700417 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
419
420 /* reset counter */
421 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
422 q->count = 1;
423
424 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300425 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700426
427 /* Enables corresponding bits at PCI_INT_STAT:
428 bits 0 to 4: video, audio, transport stream, VIP, Host
429 bit 7: timer
430 bits 8 and 9: DMA complete for: SRC, DST
431 bits 10 and 11: BERR signal asserted for RISC: RD, WR
432 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 cx_set(MO_VID_INTMSK, 0x0f0011);
435
436 /* enable capture */
437 cx_set(VID_CAPTURE_CONTROL,0x06);
438
439 /* start dma */
440 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700441 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 return 0;
444}
445
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300446#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static int stop_video_dma(struct cx8800_dev *dev)
448{
449 struct cx88_core *core = dev->core;
450
451 /* stop dma */
452 cx_clear(MO_VID_DMACNTRL, 0x11);
453
454 /* disable capture */
455 cx_clear(VID_CAPTURE_CONTROL,0x06);
456
457 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300458 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 cx_clear(MO_VID_INTMSK, 0x0f0011);
460 return 0;
461}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300462#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464static int restart_video_queue(struct cx8800_dev *dev,
465 struct cx88_dmaqueue *q)
466{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700467 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct cx88_buffer *buf, *prev;
469 struct list_head *item;
470
471 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800472 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
474 buf, buf->vb.i);
475 start_video_dma(dev, q, buf);
476 list_for_each(item,&q->active) {
477 buf = list_entry(item, struct cx88_buffer, vb.queue);
478 buf->count = q->count++;
479 }
480 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
481 return 0;
482 }
483
484 prev = NULL;
485 for (;;) {
486 if (list_empty(&q->queued))
487 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800488 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700490 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 start_video_dma(dev, q, buf);
492 buf->vb.state = STATE_ACTIVE;
493 buf->count = q->count++;
494 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
495 dprintk(2,"[%p/%d] restart_queue - first active\n",
496 buf,buf->vb.i);
497
498 } else if (prev->vb.width == buf->vb.width &&
499 prev->vb.height == buf->vb.height &&
500 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700501 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 buf->vb.state = STATE_ACTIVE;
503 buf->count = q->count++;
504 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
505 dprintk(2,"[%p/%d] restart_queue - move to active\n",
506 buf,buf->vb.i);
507 } else {
508 return 0;
509 }
510 prev = buf;
511 }
512}
513
514/* ------------------------------------------------------------------ */
515
516static int
517buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
518{
519 struct cx8800_fh *fh = q->priv_data;
520
521 *size = fh->fmt->depth*fh->width*fh->height >> 3;
522 if (0 == *count)
523 *count = 32;
524 while (*size * *count > vid_limit * 1024 * 1024)
525 (*count)--;
526 return 0;
527}
528
529static int
530buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
531 enum v4l2_field field)
532{
533 struct cx8800_fh *fh = q->priv_data;
534 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700535 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300537 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 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,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300570 dma->sglist, 0, UNSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 buf->bpl, 0, buf->vb.height);
572 break;
573 case V4L2_FIELD_BOTTOM:
574 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300575 dma->sglist, UNSET, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 buf->bpl, 0, buf->vb.height);
577 break;
578 case V4L2_FIELD_INTERLACED:
579 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300580 dma->sglist, 0, buf->bpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 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,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300586 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 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,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300593 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 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
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300756 videobuf_queue_pci_init(&fh->vidq, &cx8800_video_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 dev->pci, &dev->slock,
758 V4L2_BUF_TYPE_VIDEO_CAPTURE,
759 V4L2_FIELD_INTERLACED,
760 sizeof(struct cx88_buffer),
761 fh);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300762 videobuf_queue_pci_init(&fh->vbiq, &cx8800_vbi_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 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 Chehabc1accaa2007-08-23 16:37:49 -03001108 struct cx8800_fh *fh = priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001109
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001110 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001114static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001116 struct cx8800_fh *fh = priv;
1117 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001120static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1121{
1122 struct cx8800_fh *fh = priv;
1123 return (videobuf_querybuf(get_queue(fh), p));
1124}
1125
1126static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1127{
1128 struct cx8800_fh *fh = priv;
1129 return (videobuf_qbuf(get_queue(fh), p));
1130}
1131
1132static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1133{
1134 struct cx8800_fh *fh = priv;
1135 return (videobuf_dqbuf(get_queue(fh), p,
1136 file->f_flags & O_NONBLOCK));
1137}
1138
1139static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1140{
1141 struct cx8800_fh *fh = priv;
1142 struct cx8800_dev *dev = fh->dev;
1143
1144 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1145 return -EINVAL;
1146 if (unlikely(i != fh->type))
1147 return -EINVAL;
1148
1149 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1150 return -EBUSY;
1151 return videobuf_streamon(get_queue(fh));
1152}
1153
1154static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1155{
1156 struct cx8800_fh *fh = priv;
1157 struct cx8800_dev *dev = fh->dev;
1158 int err, res;
1159
1160 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1161 return -EINVAL;
1162 if (i != fh->type)
1163 return -EINVAL;
1164
1165 res = get_ressource(fh);
1166 err = videobuf_streamoff(get_queue(fh));
1167 if (err < 0)
1168 return err;
1169 res_free(dev,fh,res);
1170 return 0;
1171}
1172
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001173static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001174{
1175 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1176
1177 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001178 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001179 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001180
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001181 return 0;
1182}
1183
1184/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001185int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001186{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001187 static const char *iname[] = {
1188 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1189 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1190 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1191 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1192 [ CX88_VMUX_SVIDEO ] = "S-Video",
1193 [ CX88_VMUX_TELEVISION ] = "Television",
1194 [ CX88_VMUX_CABLE ] = "Cable TV",
1195 [ CX88_VMUX_DVB ] = "DVB",
1196 [ CX88_VMUX_DEBUG ] = "for debug only",
1197 };
1198 unsigned int n;
1199
1200 n = i->index;
1201 if (n >= 4)
1202 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001203 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001204 return -EINVAL;
1205 memset(i,0,sizeof(*i));
1206 i->index = n;
1207 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001208 strcpy(i->name,iname[INPUT(n).type]);
1209 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1210 (CX88_VMUX_CABLE == INPUT(n).type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001211 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001212 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001213 return 0;
1214}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001215EXPORT_SYMBOL(cx88_enum_input);
1216
1217static int vidioc_enum_input (struct file *file, void *priv,
1218 struct v4l2_input *i)
1219{
1220 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1221 return cx88_enum_input (core,i);
1222}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001223
1224static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1225{
1226 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1227
1228 *i = core->input;
1229 return 0;
1230}
1231
1232static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1233{
1234 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1235
1236 if (i >= 4)
1237 return -EINVAL;
1238
1239 mutex_lock(&core->lock);
1240 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001241 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001242 mutex_unlock(&core->lock);
1243 return 0;
1244}
1245
1246
1247
1248static int vidioc_queryctrl (struct file *file, void *priv,
1249 struct v4l2_queryctrl *qctrl)
1250{
1251 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1252 if (unlikely(qctrl->id == 0))
1253 return -EINVAL;
1254 return cx8800_ctrl_query(qctrl);
1255}
1256
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001257static int vidioc_g_ctrl (struct file *file, void *priv,
1258 struct v4l2_control *ctl)
1259{
1260 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1261 return
1262 cx88_get_control(core,ctl);
1263}
1264
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001265static int vidioc_s_ctrl (struct file *file, void *priv,
1266 struct v4l2_control *ctl)
1267{
1268 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001269 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001270 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001271}
1272
1273static int vidioc_g_tuner (struct file *file, void *priv,
1274 struct v4l2_tuner *t)
1275{
1276 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1277 u32 reg;
1278
Trent Piepho6a59d642007-08-15 14:41:57 -03001279 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001280 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001281 if (0 != t->index)
1282 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001283
1284 strcpy(t->name, "Television");
1285 t->type = V4L2_TUNER_ANALOG_TV;
1286 t->capability = V4L2_TUNER_CAP_NORM;
1287 t->rangehigh = 0xffffffffUL;
1288
1289 cx88_get_stereo(core ,t);
1290 reg = cx_read(MO_DEVICE_STATUS);
1291 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1292 return 0;
1293}
1294
1295static int vidioc_s_tuner (struct file *file, void *priv,
1296 struct v4l2_tuner *t)
1297{
1298 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1299
Trent Piepho6a59d642007-08-15 14:41:57 -03001300 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001301 return -EINVAL;
1302 if (0 != t->index)
1303 return -EINVAL;
1304
1305 cx88_set_stereo(core, t->audmode, 1);
1306 return 0;
1307}
1308
1309static int vidioc_g_frequency (struct file *file, void *priv,
1310 struct v4l2_frequency *f)
1311{
1312 struct cx8800_fh *fh = priv;
1313 struct cx88_core *core = fh->dev->core;
1314
Trent Piepho6a59d642007-08-15 14:41:57 -03001315 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001316 return -EINVAL;
1317
1318 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1319 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1320 f->frequency = core->freq;
1321
1322 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1323
1324 return 0;
1325}
1326
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001327int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001328 struct v4l2_frequency *f)
1329{
Trent Piepho6a59d642007-08-15 14:41:57 -03001330 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001331 return -EINVAL;
1332 if (unlikely(f->tuner != 0))
1333 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001334
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001335 mutex_lock(&core->lock);
1336 core->freq = f->frequency;
1337 cx88_newstation(core);
1338 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1339
1340 /* When changing channels it is required to reset TVAUDIO */
1341 msleep (10);
1342 cx88_set_tvaudio(core);
1343
1344 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001345
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001346 return 0;
1347}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001348EXPORT_SYMBOL(cx88_set_freq);
1349
1350static int vidioc_s_frequency (struct file *file, void *priv,
1351 struct v4l2_frequency *f)
1352{
1353 struct cx8800_fh *fh = priv;
1354 struct cx88_core *core = fh->dev->core;
1355
1356 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1357 return -EINVAL;
1358 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1359 return -EINVAL;
1360
1361 return
1362 cx88_set_freq (core,f);
1363}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001364
Trent Piephodbbff482007-01-22 23:31:53 -03001365#ifdef CONFIG_VIDEO_ADV_DEBUG
1366static int vidioc_g_register (struct file *file, void *fh,
1367 struct v4l2_register *reg)
1368{
1369 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1370
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001371 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001372 return -EINVAL;
1373 /* cx2388x has a 24-bit register space */
1374 reg->val = cx_read(reg->reg&0xffffff);
1375 return 0;
1376}
1377
1378static int vidioc_s_register (struct file *file, void *fh,
1379 struct v4l2_register *reg)
1380{
1381 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1382
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001383 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001384 return -EINVAL;
Trent Piephodbbff482007-01-22 23:31:53 -03001385 cx_write(reg->reg&0xffffff, reg->val);
1386 return 0;
1387}
1388#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001391/* RADIO ESPECIFIC IOCTLS */
1392/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001394static int radio_querycap (struct file *file, void *priv,
1395 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001397 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 struct cx88_core *core = dev->core;
1399
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001400 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001401 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001402 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1403 cap->version = CX88_VERSION_CODE;
1404 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001408static int radio_g_tuner (struct file *file, void *priv,
1409 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001411 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1412
1413 if (unlikely(t->index > 0))
1414 return -EINVAL;
1415
1416 strcpy(t->name, "Radio");
1417 t->type = V4L2_TUNER_RADIO;
1418
1419 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1420 return 0;
1421}
1422
1423static int radio_enum_input (struct file *file, void *priv,
1424 struct v4l2_input *i)
1425{
1426 if (i->index != 0)
1427 return -EINVAL;
1428 strcpy(i->name,"Radio");
1429 i->type = V4L2_INPUT_TYPE_TUNER;
1430
1431 return 0;
1432}
1433
1434static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1435{
1436 if (unlikely(a->index))
1437 return -EINVAL;
1438
1439 memset(a,0,sizeof(*a));
1440 strcpy(a->name,"Radio");
1441 return 0;
1442}
1443
1444/* FIXME: Should add a standard for radio */
1445
1446static int radio_s_tuner (struct file *file, void *priv,
1447 struct v4l2_tuner *t)
1448{
1449 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1450
1451 if (0 != t->index)
1452 return -EINVAL;
1453
1454 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1455
1456 return 0;
1457}
1458
1459static int radio_s_audio (struct file *file, void *fh,
1460 struct v4l2_audio *a)
1461{
1462 return 0;
1463}
1464
1465static int radio_s_input (struct file *file, void *fh, unsigned int i)
1466{
1467 return 0;
1468}
1469
1470static int radio_queryctrl (struct file *file, void *priv,
1471 struct v4l2_queryctrl *c)
1472{
1473 int i;
1474
1475 if (c->id < V4L2_CID_BASE ||
1476 c->id >= V4L2_CID_LASTP1)
1477 return -EINVAL;
1478 if (c->id == V4L2_CID_AUDIO_MUTE) {
1479 for (i = 0; i < CX8800_CTLS; i++)
1480 if (cx8800_ctls[i].v.id == c->id)
1481 break;
1482 *c = cx8800_ctls[i].v;
1483 } else
1484 *c = no_ctl;
1485 return 0;
1486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488/* ----------------------------------------------------------- */
1489
1490static void cx8800_vid_timeout(unsigned long data)
1491{
1492 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1493 struct cx88_core *core = dev->core;
1494 struct cx88_dmaqueue *q = &dev->vidq;
1495 struct cx88_buffer *buf;
1496 unsigned long flags;
1497
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001498 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 cx_clear(MO_VID_DMACNTRL, 0x11);
1501 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1502
1503 spin_lock_irqsave(&dev->slock,flags);
1504 while (!list_empty(&q->active)) {
1505 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1506 list_del(&buf->vb.queue);
1507 buf->vb.state = STATE_ERROR;
1508 wake_up(&buf->vb.done);
1509 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1510 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1511 }
1512 restart_video_queue(dev,q);
1513 spin_unlock_irqrestore(&dev->slock,flags);
1514}
1515
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001516static char *cx88_vid_irqs[32] = {
1517 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1518 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1519 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1520 "y_sync", "u_sync", "v_sync", "vbi_sync",
1521 "opc_err", "par_err", "rip_err", "pci_abort",
1522};
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524static void cx8800_vid_irq(struct cx8800_dev *dev)
1525{
1526 struct cx88_core *core = dev->core;
1527 u32 status, mask, count;
1528
1529 status = cx_read(MO_VID_INTSTAT);
1530 mask = cx_read(MO_VID_INTMSK);
1531 if (0 == (status & mask))
1532 return;
1533 cx_write(MO_VID_INTSTAT, status);
1534 if (irq_debug || (status & mask & ~0xff))
1535 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001536 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1537 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /* risc op code error */
1540 if (status & (1 << 16)) {
1541 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1542 cx_clear(MO_VID_DMACNTRL, 0x11);
1543 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001544 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
1547 /* risc1 y */
1548 if (status & 0x01) {
1549 spin_lock(&dev->slock);
1550 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001551 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 spin_unlock(&dev->slock);
1553 }
1554
1555 /* risc1 vbi */
1556 if (status & 0x08) {
1557 spin_lock(&dev->slock);
1558 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001559 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 spin_unlock(&dev->slock);
1561 }
1562
1563 /* risc2 y */
1564 if (status & 0x10) {
1565 dprintk(2,"stopper video\n");
1566 spin_lock(&dev->slock);
1567 restart_video_queue(dev,&dev->vidq);
1568 spin_unlock(&dev->slock);
1569 }
1570
1571 /* risc2 vbi */
1572 if (status & 0x80) {
1573 dprintk(2,"stopper vbi\n");
1574 spin_lock(&dev->slock);
1575 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1576 spin_unlock(&dev->slock);
1577 }
1578}
1579
David Howells7d12e782006-10-05 14:55:46 +01001580static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct cx8800_dev *dev = dev_id;
1583 struct cx88_core *core = dev->core;
1584 u32 status;
1585 int loop, handled = 0;
1586
1587 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001588 status = cx_read(MO_PCI_INTSTAT) &
1589 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (0 == status)
1591 goto out;
1592 cx_write(MO_PCI_INTSTAT, status);
1593 handled = 1;
1594
1595 if (status & core->pci_irqmask)
1596 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001597 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 cx8800_vid_irq(dev);
1599 };
1600 if (10 == loop) {
1601 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1602 core->name);
1603 cx_write(MO_PCI_INTMSK,0);
1604 }
1605
1606 out:
1607 return IRQ_RETVAL(handled);
1608}
1609
1610/* ----------------------------------------------------------- */
1611/* exported stuff */
1612
Arjan van de Venfa027c22007-02-12 00:55:33 -08001613static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 .owner = THIS_MODULE,
1616 .open = video_open,
1617 .release = video_release,
1618 .read = video_read,
1619 .poll = video_poll,
1620 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001621 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001622 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 .llseek = no_llseek,
1624};
1625
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001626static struct video_device cx8800_vbi_template;
Adrian Bunk408b6642005-05-01 08:59:29 -07001627static struct video_device cx8800_video_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001629 .name = "cx8800-video",
1630 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1631 .fops = &video_fops,
1632 .minor = -1,
1633 .vidioc_querycap = vidioc_querycap,
1634 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1635 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1636 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1637 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1638 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1639 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1640 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1641 .vidioc_reqbufs = vidioc_reqbufs,
1642 .vidioc_querybuf = vidioc_querybuf,
1643 .vidioc_qbuf = vidioc_qbuf,
1644 .vidioc_dqbuf = vidioc_dqbuf,
1645 .vidioc_s_std = vidioc_s_std,
1646 .vidioc_enum_input = vidioc_enum_input,
1647 .vidioc_g_input = vidioc_g_input,
1648 .vidioc_s_input = vidioc_s_input,
1649 .vidioc_queryctrl = vidioc_queryctrl,
1650 .vidioc_g_ctrl = vidioc_g_ctrl,
1651 .vidioc_s_ctrl = vidioc_s_ctrl,
1652 .vidioc_streamon = vidioc_streamon,
1653 .vidioc_streamoff = vidioc_streamoff,
1654#ifdef CONFIG_VIDEO_V4L1_COMPAT
1655 .vidiocgmbuf = vidiocgmbuf,
1656#endif
1657 .vidioc_g_tuner = vidioc_g_tuner,
1658 .vidioc_s_tuner = vidioc_s_tuner,
1659 .vidioc_g_frequency = vidioc_g_frequency,
1660 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001661#ifdef CONFIG_VIDEO_ADV_DEBUG
1662 .vidioc_g_register = vidioc_g_register,
1663 .vidioc_s_register = vidioc_s_register,
1664#endif
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001665 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001666 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667};
1668
Arjan van de Venfa027c22007-02-12 00:55:33 -08001669static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 .owner = THIS_MODULE,
1672 .open = video_open,
1673 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001674 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001675 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 .llseek = no_llseek,
1677};
1678
Adrian Bunk408b6642005-05-01 08:59:29 -07001679static struct video_device cx8800_radio_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001681 .name = "cx8800-radio",
1682 .type = VID_TYPE_TUNER,
1683 .hardware = 0,
1684 .fops = &radio_fops,
1685 .minor = -1,
1686 .vidioc_querycap = radio_querycap,
1687 .vidioc_g_tuner = radio_g_tuner,
1688 .vidioc_enum_input = radio_enum_input,
1689 .vidioc_g_audio = radio_g_audio,
1690 .vidioc_s_tuner = radio_s_tuner,
1691 .vidioc_s_audio = radio_s_audio,
1692 .vidioc_s_input = radio_s_input,
1693 .vidioc_queryctrl = radio_queryctrl,
1694 .vidioc_g_ctrl = vidioc_g_ctrl,
1695 .vidioc_s_ctrl = vidioc_s_ctrl,
1696 .vidioc_g_frequency = vidioc_g_frequency,
1697 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephoa75d2042007-08-01 00:13:28 -03001698#ifdef CONFIG_VIDEO_ADV_DEBUG
1699 .vidioc_g_register = vidioc_g_register,
1700 .vidioc_s_register = vidioc_s_register,
1701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702};
1703
1704/* ----------------------------------------------------------- */
1705
1706static void cx8800_unregister_video(struct cx8800_dev *dev)
1707{
1708 if (dev->radio_dev) {
1709 if (-1 != dev->radio_dev->minor)
1710 video_unregister_device(dev->radio_dev);
1711 else
1712 video_device_release(dev->radio_dev);
1713 dev->radio_dev = NULL;
1714 }
1715 if (dev->vbi_dev) {
1716 if (-1 != dev->vbi_dev->minor)
1717 video_unregister_device(dev->vbi_dev);
1718 else
1719 video_device_release(dev->vbi_dev);
1720 dev->vbi_dev = NULL;
1721 }
1722 if (dev->video_dev) {
1723 if (-1 != dev->video_dev->minor)
1724 video_unregister_device(dev->video_dev);
1725 else
1726 video_device_release(dev->video_dev);
1727 dev->video_dev = NULL;
1728 }
1729}
1730
1731static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1732 const struct pci_device_id *pci_id)
1733{
1734 struct cx8800_dev *dev;
1735 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int err;
1738
Panagiotis Issaris74081872006-01-11 19:40:56 -02001739 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (NULL == dev)
1741 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 /* pci init */
1744 dev->pci = pci_dev;
1745 if (pci_enable_device(pci_dev)) {
1746 err = -EIO;
1747 goto fail_free;
1748 }
1749 core = cx88_core_get(dev->pci);
1750 if (NULL == core) {
1751 err = -EINVAL;
1752 goto fail_free;
1753 }
1754 dev->core = core;
1755
1756 /* print pci info */
1757 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001758 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1759 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001760 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001762 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 pci_set_master(pci_dev);
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -03001765 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1767 err = -EIO;
1768 goto fail_core;
1769 }
1770
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001771 /* Initialize VBI template */
1772 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1773 sizeof(cx8800_vbi_template) );
1774 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1775 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001779 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* init video dma queues */
1782 INIT_LIST_HEAD(&dev->vidq.active);
1783 INIT_LIST_HEAD(&dev->vidq.queued);
1784 dev->vidq.timeout.function = cx8800_vid_timeout;
1785 dev->vidq.timeout.data = (unsigned long)dev;
1786 init_timer(&dev->vidq.timeout);
1787 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1788 MO_VID_DMACNTRL,0x11,0x00);
1789
1790 /* init vbi dma queues */
1791 INIT_LIST_HEAD(&dev->vbiq.active);
1792 INIT_LIST_HEAD(&dev->vbiq.queued);
1793 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1794 dev->vbiq.timeout.data = (unsigned long)dev;
1795 init_timer(&dev->vbiq.timeout);
1796 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1797 MO_VID_DMACNTRL,0x88,0x00);
1798
1799 /* get irq */
1800 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001801 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001803 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 core->name,pci_dev->irq);
1805 goto fail_core;
1806 }
1807 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1808
1809 /* load and configure helper modules */
Trent Piepho6a59d642007-08-15 14:41:57 -03001810 if (TUNER_ABSENT != core->board.tuner_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 request_module("tuner");
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001812
Trent Piepho6a59d642007-08-15 14:41:57 -03001813 if (core->board.audio_chip == AUDIO_CHIP_WM8775)
Steven Toth30579062006-09-25 12:43:42 -03001814 request_module("wm8775");
1815
Michael Krufky6fcecce2007-08-24 01:32:31 -03001816 switch (core->boardnr) {
1817 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1818 request_module("ir-kbd-i2c");
1819 request_module("rtc-isl1208");
1820 }
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /* register v4l devices */
1823 dev->video_dev = cx88_vdev_init(core,dev->pci,
1824 &cx8800_video_template,"video");
1825 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1826 video_nr[core->nr]);
1827 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001828 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 core->name);
1830 goto fail_unreg;
1831 }
1832 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1833 core->name,dev->video_dev->minor & 0x1f);
1834
1835 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1836 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1837 vbi_nr[core->nr]);
1838 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001839 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 core->name);
1841 goto fail_unreg;
1842 }
1843 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1844 core->name,dev->vbi_dev->minor & 0x1f);
1845
Trent Piepho6a59d642007-08-15 14:41:57 -03001846 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1848 &cx8800_radio_template,"radio");
1849 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1850 radio_nr[core->nr]);
1851 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001852 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 core->name);
1854 goto fail_unreg;
1855 }
1856 printk(KERN_INFO "%s/0: registered device radio%d\n",
1857 core->name,dev->radio_dev->minor & 0x1f);
1858 }
1859
1860 /* everything worked */
1861 list_add_tail(&dev->devlist,&cx8800_devlist);
1862 pci_set_drvdata(pci_dev,dev);
1863
1864 /* initial device configuration */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001865 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001866 cx88_set_tvnorm(core,core->tvnorm);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001867 init_controls(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001868 cx88_video_mux(core,0);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001869 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001872 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001874 if (IS_ERR(core->kthread)) {
1875 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001876 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1877 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001878 }
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return 0;
1881
1882fail_unreg:
1883 cx8800_unregister_video(dev);
1884 free_irq(pci_dev->irq, dev);
1885fail_core:
1886 cx88_core_put(core,dev->pci);
1887fail_free:
1888 kfree(dev);
1889 return err;
1890}
1891
1892static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1893{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001894 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001895 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001898 if (core->kthread) {
1899 kthread_stop(core->kthread);
1900 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001903 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 pci_disable_device(pci_dev);
1905
1906 /* unregister stuff */
1907
1908 free_irq(pci_dev->irq, dev);
1909 cx8800_unregister_video(dev);
1910 pci_set_drvdata(pci_dev, NULL);
1911
1912 /* free memory */
1913 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1914 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001915 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 kfree(dev);
1917}
1918
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001919#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1921{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001922 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 struct cx88_core *core = dev->core;
1924
1925 /* stop video+vbi capture */
1926 spin_lock(&dev->slock);
1927 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001928 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 stop_video_dma(dev);
1930 del_timer(&dev->vidq.timeout);
1931 }
1932 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001933 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 cx8800_stop_vbi_dma(dev);
1935 del_timer(&dev->vbiq.timeout);
1936 }
1937 spin_unlock(&dev->slock);
1938
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001939 if (core->ir)
1940 cx88_ir_stop(core, core->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001942 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 pci_save_state(pci_dev);
1945 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1946 pci_disable_device(pci_dev);
1947 dev->state.disabled = 1;
1948 }
1949 return 0;
1950}
1951
1952static int cx8800_resume(struct pci_dev *pci_dev)
1953{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001954 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001956 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001959 err=pci_enable_device(pci_dev);
1960 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001961 printk(KERN_ERR "%s/0: can't enable device\n",
1962 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001963 return err;
1964 }
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 dev->state.disabled = 0;
1967 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001968 err= pci_set_power_state(pci_dev, PCI_D0);
1969 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001970 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001971 pci_disable_device(pci_dev);
1972 dev->state.disabled = 1;
1973
1974 return err;
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 pci_restore_state(pci_dev);
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001979 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001980 if (core->ir)
1981 cx88_ir_start(core, core->ir);
1982
1983 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* restart video+vbi capture */
1986 spin_lock(&dev->slock);
1987 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001988 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 restart_video_queue(dev,&dev->vidq);
1990 }
1991 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001992 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1994 }
1995 spin_unlock(&dev->slock);
1996
1997 return 0;
1998}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001999#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001/* ----------------------------------------------------------- */
2002
Adrian Bunk408b6642005-05-01 08:59:29 -07002003static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 {
2005 .vendor = 0x14f1,
2006 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002007 .subvendor = PCI_ANY_ID,
2008 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 },{
2010 /* --- end of list --- */
2011 }
2012};
2013MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2014
2015static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002016 .name = "cx8800",
2017 .id_table = cx8800_pci_tbl,
2018 .probe = cx8800_initdev,
2019 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002020#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 .suspend = cx8800_suspend,
2022 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024};
2025
2026static int cx8800_init(void)
2027{
Trent Piepho5772f812007-08-15 14:41:59 -03002028 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 (CX88_VERSION_CODE >> 16) & 0xff,
2030 (CX88_VERSION_CODE >> 8) & 0xff,
2031 CX88_VERSION_CODE & 0xff);
2032#ifdef SNAPSHOT
2033 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2034 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2035#endif
2036 return pci_register_driver(&cx8800_pci_driver);
2037}
2038
2039static void cx8800_fini(void)
2040{
2041 pci_unregister_driver(&cx8800_pci_driver);
2042}
2043
2044module_init(cx8800_init);
2045module_exit(cx8800_fini);
2046
2047/* ----------------------------------------------------------- */
2048/*
2049 * Local variables:
2050 * c-basic-offset: 8
2051 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002052 * 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 -07002053 */