blob: c8a5cd5eedf3348ed0b1d37d2b69fb00a1ff24e6 [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
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300395 /* if there are audioroutes defined, we have an external
396 ADC to deal with audio */
Ricardo Cerqueira7b27d452007-09-30 13:02:49 -0300397
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300398 if (INPUT(input).audioroute) {
Ricardo Cerqueira7b27d452007-09-30 13:02:49 -0300399
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300400 /* cx2388's C-ADC is connected to the tuner only.
401 When used with S-Video, that ADC is busy dealing with
402 chroma, so an external must be used for baseband audio */
403
404 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
405 INPUT(input).type != CX88_RADIO) {
406 /* "ADC mode" */
407 cx_write(AUD_I2SCNTL, 0x1);
Michael Krufkyf24546a2006-10-16 16:51:11 -0300408 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300409 } else {
410 /* Normal mode */
411 cx_write(AUD_I2SCNTL, 0x0);
Michael Krufkyf24546a2006-10-16 16:51:11 -0300412 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300413 }
414
415 /* The wm8775 module has the "2" route hardwired into
416 the initialization. Some boards may use different
417 routes for different inputs. HVR-1300 surely does */
418 if (core->board.audio_chip &&
419 core->board.audio_chip == AUDIO_CHIP_WM8775) {
420 struct v4l2_routing route;
421
422 route.input = INPUT(input).audioroute;
423 cx88_call_i2c_clients(core,
424 VIDIOC_INT_S_AUDIO_ROUTING,&route);
425
426 }
427
Michael Krufkyf24546a2006-10-16 16:51:11 -0300428 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300432EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434/* ------------------------------------------------------------------ */
435
436static int start_video_dma(struct cx8800_dev *dev,
437 struct cx88_dmaqueue *q,
438 struct cx88_buffer *buf)
439{
440 struct cx88_core *core = dev->core;
441
442 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700443 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700445 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
447
448 /* reset counter */
449 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
450 q->count = 1;
451
452 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300453 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700454
455 /* Enables corresponding bits at PCI_INT_STAT:
456 bits 0 to 4: video, audio, transport stream, VIP, Host
457 bit 7: timer
458 bits 8 and 9: DMA complete for: SRC, DST
459 bits 10 and 11: BERR signal asserted for RISC: RD, WR
460 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 cx_set(MO_VID_INTMSK, 0x0f0011);
463
464 /* enable capture */
465 cx_set(VID_CAPTURE_CONTROL,0x06);
466
467 /* start dma */
468 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700469 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 return 0;
472}
473
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300474#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static int stop_video_dma(struct cx8800_dev *dev)
476{
477 struct cx88_core *core = dev->core;
478
479 /* stop dma */
480 cx_clear(MO_VID_DMACNTRL, 0x11);
481
482 /* disable capture */
483 cx_clear(VID_CAPTURE_CONTROL,0x06);
484
485 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300486 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 cx_clear(MO_VID_INTMSK, 0x0f0011);
488 return 0;
489}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300490#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492static int restart_video_queue(struct cx8800_dev *dev,
493 struct cx88_dmaqueue *q)
494{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700495 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct cx88_buffer *buf, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800499 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
501 buf, buf->vb.i);
502 start_video_dma(dev, q, buf);
Trent Piepho8bb629e22007-10-10 05:37:40 -0300503 list_for_each_entry(buf, &q->active, vb.queue)
504 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
506 return 0;
507 }
508
509 prev = NULL;
510 for (;;) {
511 if (list_empty(&q->queued))
512 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800513 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700515 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300517 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 buf->count = q->count++;
519 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
520 dprintk(2,"[%p/%d] restart_queue - first active\n",
521 buf,buf->vb.i);
522
523 } else if (prev->vb.width == buf->vb.width &&
524 prev->vb.height == buf->vb.height &&
525 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700526 list_move_tail(&buf->vb.queue, &q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300527 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 buf->count = q->count++;
529 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
530 dprintk(2,"[%p/%d] restart_queue - move to active\n",
531 buf,buf->vb.i);
532 } else {
533 return 0;
534 }
535 prev = buf;
536 }
537}
538
539/* ------------------------------------------------------------------ */
540
541static int
542buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
543{
544 struct cx8800_fh *fh = q->priv_data;
545
546 *size = fh->fmt->depth*fh->width*fh->height >> 3;
547 if (0 == *count)
548 *count = 32;
549 while (*size * *count > vid_limit * 1024 * 1024)
550 (*count)--;
551 return 0;
552}
553
554static int
555buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
556 enum v4l2_field field)
557{
558 struct cx8800_fh *fh = q->priv_data;
559 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700560 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300562 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int rc, init_buffer = 0;
564
565 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700566 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
567 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -EINVAL;
569 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
570 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
571 return -EINVAL;
572
573 if (buf->fmt != fh->fmt ||
574 buf->vb.width != fh->width ||
575 buf->vb.height != fh->height ||
576 buf->vb.field != field) {
577 buf->fmt = fh->fmt;
578 buf->vb.width = fh->width;
579 buf->vb.height = fh->height;
580 buf->vb.field = field;
581 init_buffer = 1;
582 }
583
Brandon Philips0fc06862007-11-06 20:02:36 -0300584 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300586 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto fail;
588 }
589
590 if (init_buffer) {
591 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
592 switch (buf->vb.field) {
593 case V4L2_FIELD_TOP:
594 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300595 dma->sglist, 0, UNSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 buf->bpl, 0, buf->vb.height);
597 break;
598 case V4L2_FIELD_BOTTOM:
599 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300600 dma->sglist, UNSET, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 buf->bpl, 0, buf->vb.height);
602 break;
603 case V4L2_FIELD_INTERLACED:
604 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300605 dma->sglist, 0, buf->bpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 buf->bpl, buf->bpl,
607 buf->vb.height >> 1);
608 break;
609 case V4L2_FIELD_SEQ_TB:
610 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300611 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 0, buf->bpl * (buf->vb.height >> 1),
613 buf->bpl, 0,
614 buf->vb.height >> 1);
615 break;
616 case V4L2_FIELD_SEQ_BT:
617 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300618 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 buf->bpl * (buf->vb.height >> 1), 0,
620 buf->bpl, 0,
621 buf->vb.height >> 1);
622 break;
623 default:
624 BUG();
625 }
626 }
627 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
628 buf, buf->vb.i,
629 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
630 (unsigned long)buf->risc.dma);
631
Brandon Philips0fc06862007-11-06 20:02:36 -0300632 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return 0;
634
635 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300636 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return rc;
638}
639
640static void
641buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
642{
643 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
644 struct cx88_buffer *prev;
645 struct cx8800_fh *fh = vq->priv_data;
646 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700647 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct cx88_dmaqueue *q = &dev->vidq;
649
650 /* add jump to stopper */
651 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
652 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
653
654 if (!list_empty(&q->queued)) {
655 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300656 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
658 buf, buf->vb.i);
659
660 } else if (list_empty(&q->active)) {
661 list_add_tail(&buf->vb.queue,&q->active);
662 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300663 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 buf->count = q->count++;
665 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
666 dprintk(2,"[%p/%d] buffer_queue - first active\n",
667 buf, buf->vb.i);
668
669 } else {
670 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
671 if (prev->vb.width == buf->vb.width &&
672 prev->vb.height == buf->vb.height &&
673 prev->fmt == buf->fmt) {
674 list_add_tail(&buf->vb.queue,&q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300675 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 buf->count = q->count++;
677 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
678 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
679 buf, buf->vb.i);
680
681 } else {
682 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300683 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
685 buf, buf->vb.i);
686 }
687 }
688}
689
690static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
691{
692 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300694 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Adrian Bunk408b6642005-05-01 08:59:29 -0700697static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 .buf_setup = buffer_setup,
699 .buf_prepare = buffer_prepare,
700 .buf_queue = buffer_queue,
701 .buf_release = buffer_release,
702};
703
704/* ------------------------------------------------------------------ */
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707/* ------------------------------------------------------------------ */
708
709static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
710{
711 switch (fh->type) {
712 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
713 return &fh->vidq;
714 case V4L2_BUF_TYPE_VBI_CAPTURE:
715 return &fh->vbiq;
716 default:
717 BUG();
718 return NULL;
719 }
720}
721
722static int get_ressource(struct cx8800_fh *fh)
723{
724 switch (fh->type) {
725 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
726 return RESOURCE_VIDEO;
727 case V4L2_BUF_TYPE_VBI_CAPTURE:
728 return RESOURCE_VBI;
729 default:
730 BUG();
731 return 0;
732 }
733}
734
735static int video_open(struct inode *inode, struct file *file)
736{
737 int minor = iminor(inode);
738 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700739 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct cx8800_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 enum v4l2_buf_type type = 0;
742 int radio = 0;
743
Trent Piepho8bb629e22007-10-10 05:37:40 -0300744 list_for_each_entry(h, &cx8800_devlist, devlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (h->video_dev->minor == minor) {
746 dev = h;
747 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
748 }
749 if (h->vbi_dev->minor == minor) {
750 dev = h;
751 type = V4L2_BUF_TYPE_VBI_CAPTURE;
752 }
753 if (h->radio_dev &&
754 h->radio_dev->minor == minor) {
755 radio = 1;
756 dev = h;
757 }
758 }
759 if (NULL == dev)
760 return -ENODEV;
761
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700762 core = dev->core;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 dprintk(1,"open minor=%d radio=%d type=%s\n",
765 minor,radio,v4l2_type_names[type]);
766
767 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200768 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (NULL == fh)
770 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 file->private_data = fh;
772 fh->dev = dev;
773 fh->radio = radio;
774 fh->type = type;
775 fh->width = 320;
776 fh->height = 240;
777 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
778
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300779 videobuf_queue_pci_init(&fh->vidq, &cx8800_video_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 dev->pci, &dev->slock,
781 V4L2_BUF_TYPE_VIDEO_CAPTURE,
782 V4L2_FIELD_INTERLACED,
783 sizeof(struct cx88_buffer),
784 fh);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300785 videobuf_queue_pci_init(&fh->vbiq, &cx8800_vbi_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 dev->pci, &dev->slock,
787 V4L2_BUF_TYPE_VBI_CAPTURE,
788 V4L2_FIELD_SEQ_TB,
789 sizeof(struct cx88_buffer),
790 fh);
791
792 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 dprintk(1,"video_open: setting radio device\n");
Trent Piepho6a59d642007-08-15 14:41:57 -0300794 cx_write(MO_GP3_IO, core->board.radio.gpio3);
795 cx_write(MO_GP0_IO, core->board.radio.gpio0);
796 cx_write(MO_GP1_IO, core->board.radio.gpio1);
797 cx_write(MO_GP2_IO, core->board.radio.gpio2);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700798 core->tvaudio = WW_FM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 cx88_set_tvaudio(core);
800 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700801 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800808video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct cx8800_fh *fh = file->private_data;
811
812 switch (fh->type) {
813 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
814 if (res_locked(fh->dev,RESOURCE_VIDEO))
815 return -EBUSY;
816 return videobuf_read_one(&fh->vidq, data, count, ppos,
817 file->f_flags & O_NONBLOCK);
818 case V4L2_BUF_TYPE_VBI_CAPTURE:
819 if (!res_get(fh->dev,fh,RESOURCE_VBI))
820 return -EBUSY;
821 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
822 file->f_flags & O_NONBLOCK);
823 default:
824 BUG();
825 return 0;
826 }
827}
828
829static unsigned int
830video_poll(struct file *file, struct poll_table_struct *wait)
831{
832 struct cx8800_fh *fh = file->private_data;
833 struct cx88_buffer *buf;
834
835 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
836 if (!res_get(fh->dev,fh,RESOURCE_VBI))
837 return POLLERR;
838 return videobuf_poll_stream(file, &fh->vbiq, wait);
839 }
840
841 if (res_check(fh,RESOURCE_VIDEO)) {
842 /* streaming capture */
843 if (list_empty(&fh->vidq.stream))
844 return POLLERR;
845 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
846 } else {
847 /* read() capture */
848 buf = (struct cx88_buffer*)fh->vidq.read_buf;
849 if (NULL == buf)
850 return POLLERR;
851 }
852 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300853 if (buf->vb.state == VIDEOBUF_DONE ||
854 buf->vb.state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return POLLIN|POLLRDNORM;
856 return 0;
857}
858
859static int video_release(struct inode *inode, struct file *file)
860{
861 struct cx8800_fh *fh = file->private_data;
862 struct cx8800_dev *dev = fh->dev;
863
864 /* turn off overlay */
865 if (res_check(fh, RESOURCE_OVERLAY)) {
866 /* FIXME */
867 res_free(dev,fh,RESOURCE_OVERLAY);
868 }
869
870 /* stop video capture */
871 if (res_check(fh, RESOURCE_VIDEO)) {
872 videobuf_queue_cancel(&fh->vidq);
873 res_free(dev,fh,RESOURCE_VIDEO);
874 }
875 if (fh->vidq.read_buf) {
876 buffer_release(&fh->vidq,fh->vidq.read_buf);
877 kfree(fh->vidq.read_buf);
878 }
879
880 /* stop vbi capture */
881 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -0300882 videobuf_stop(&fh->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 res_free(dev,fh,RESOURCE_VBI);
884 }
885
886 videobuf_mmap_free(&fh->vidq);
887 videobuf_mmap_free(&fh->vbiq);
888 file->private_data = NULL;
889 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700890
891 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return 0;
894}
895
896static int
897video_mmap(struct file *file, struct vm_area_struct * vma)
898{
899 struct cx8800_fh *fh = file->private_data;
900
901 return videobuf_mmap_mapper(get_queue(fh), vma);
902}
903
904/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300905/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300907int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300909 struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 u32 value;
911 int i;
912
913 for (i = 0; i < CX8800_CTLS; i++)
914 if (cx8800_ctls[i].v.id == ctl->id)
915 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300916 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return -EINVAL;
918
919 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
920 switch (ctl->id) {
921 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300922 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
923 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
925 case V4L2_CID_AUDIO_VOLUME:
926 ctl->value = 0x3f - (value & 0x3f);
927 break;
928 default:
929 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
930 break;
931 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300932 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
933 ctl->id, c->v.name, ctl->value, c->reg,
934 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
936}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300937EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300939int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200942 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300944
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200945 for (i = 0; i < CX8800_CTLS; i++) {
946 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200948 }
949 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300950 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return -EINVAL;
952
953 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700954 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700956 ctl->value = c->v.maximum;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200957 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 switch (ctl->id) {
959 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300960 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 case V4L2_CID_AUDIO_VOLUME:
963 value = 0x3f - (ctl->value & 0x3f);
964 break;
965 case V4L2_CID_SATURATION:
966 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200967
968 value = ((ctl->value - c->off) << c->shift) & c->mask;
969
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -0300970 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200971 /* For SECAM, both U and V sat should be equal */
972 value=value<<8|value;
973 } else {
974 /* Keeps U Saturation proportional to V Sat */
975 value=(value*0x5a)/0x7f<<8|value;
976 }
977 mask=0xffff;
978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 default:
980 value = ((ctl->value - c->off) << c->shift) & c->mask;
981 break;
982 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300983 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
984 ctl->id, c->v.name, ctl->value, c->reg, value,
985 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200987 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200989 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 return 0;
992}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300993EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700995static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200997 struct v4l2_control ctrl;
998 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001000 for (i = 0; i < CX8800_CTLS; i++) {
1001 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -03001002 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001003
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001004 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001009/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001011static int vidioc_g_fmt_cap (struct file *file, void *priv,
1012 struct v4l2_format *f)
1013{
1014 struct cx8800_fh *fh = priv;
1015
1016 f->fmt.pix.width = fh->width;
1017 f->fmt.pix.height = fh->height;
1018 f->fmt.pix.field = fh->vidq.field;
1019 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1020 f->fmt.pix.bytesperline =
1021 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1022 f->fmt.pix.sizeimage =
1023 f->fmt.pix.height * f->fmt.pix.bytesperline;
1024 return 0;
1025}
1026
1027static int vidioc_try_fmt_cap (struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct v4l2_format *f)
1029{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001030 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1031 struct cx8800_fmt *fmt;
1032 enum v4l2_field field;
1033 unsigned int maxw, maxh;
1034
1035 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1036 if (NULL == fmt)
1037 return -EINVAL;
1038
1039 field = f->fmt.pix.field;
1040 maxw = norm_maxw(core->tvnorm);
1041 maxh = norm_maxh(core->tvnorm);
1042
1043 if (V4L2_FIELD_ANY == field) {
1044 field = (f->fmt.pix.height > maxh/2)
1045 ? V4L2_FIELD_INTERLACED
1046 : V4L2_FIELD_BOTTOM;
1047 }
1048
1049 switch (field) {
1050 case V4L2_FIELD_TOP:
1051 case V4L2_FIELD_BOTTOM:
1052 maxh = maxh / 2;
1053 break;
1054 case V4L2_FIELD_INTERLACED:
1055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 default:
1057 return -EINVAL;
1058 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001059
1060 f->fmt.pix.field = field;
1061 if (f->fmt.pix.height < 32)
1062 f->fmt.pix.height = 32;
1063 if (f->fmt.pix.height > maxh)
1064 f->fmt.pix.height = maxh;
1065 if (f->fmt.pix.width < 48)
1066 f->fmt.pix.width = 48;
1067 if (f->fmt.pix.width > maxw)
1068 f->fmt.pix.width = maxw;
1069 f->fmt.pix.width &= ~0x03;
1070 f->fmt.pix.bytesperline =
1071 (f->fmt.pix.width * fmt->depth) >> 3;
1072 f->fmt.pix.sizeimage =
1073 f->fmt.pix.height * f->fmt.pix.bytesperline;
1074
1075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001078static int vidioc_s_fmt_cap (struct file *file, void *priv,
1079 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001081 struct cx8800_fh *fh = priv;
1082 int err = vidioc_try_fmt_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001083
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001084 if (0 != err)
1085 return err;
1086 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1087 fh->width = f->fmt.pix.width;
1088 fh->height = f->fmt.pix.height;
1089 fh->vidq.field = f->fmt.pix.field;
1090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001093static int vidioc_querycap (struct file *file, void *priv,
1094 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001096 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001099 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001100 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001101 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1102 cap->version = CX88_VERSION_CODE;
1103 cap->capabilities =
1104 V4L2_CAP_VIDEO_CAPTURE |
1105 V4L2_CAP_READWRITE |
1106 V4L2_CAP_STREAMING |
1107 V4L2_CAP_VBI_CAPTURE;
Trent Piepho6a59d642007-08-15 14:41:57 -03001108 if (UNSET != core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001109 cap->capabilities |= V4L2_CAP_TUNER;
1110 return 0;
1111}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001112
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001113static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1114 struct v4l2_fmtdesc *f)
1115{
1116 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1117 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001119 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1120 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001122 return 0;
1123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001125#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001126static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001127{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001128 struct cx8800_fh *fh = priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001129
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001130 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001134static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001136 struct cx8800_fh *fh = priv;
1137 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001140static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1141{
1142 struct cx8800_fh *fh = priv;
1143 return (videobuf_querybuf(get_queue(fh), p));
1144}
1145
1146static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1147{
1148 struct cx8800_fh *fh = priv;
1149 return (videobuf_qbuf(get_queue(fh), p));
1150}
1151
1152static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1153{
1154 struct cx8800_fh *fh = priv;
1155 return (videobuf_dqbuf(get_queue(fh), p,
1156 file->f_flags & O_NONBLOCK));
1157}
1158
1159static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1160{
1161 struct cx8800_fh *fh = priv;
1162 struct cx8800_dev *dev = fh->dev;
1163
1164 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1165 return -EINVAL;
1166 if (unlikely(i != fh->type))
1167 return -EINVAL;
1168
1169 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1170 return -EBUSY;
1171 return videobuf_streamon(get_queue(fh));
1172}
1173
1174static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1175{
1176 struct cx8800_fh *fh = priv;
1177 struct cx8800_dev *dev = fh->dev;
1178 int err, res;
1179
1180 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1181 return -EINVAL;
1182 if (i != fh->type)
1183 return -EINVAL;
1184
1185 res = get_ressource(fh);
1186 err = videobuf_streamoff(get_queue(fh));
1187 if (err < 0)
1188 return err;
1189 res_free(dev,fh,res);
1190 return 0;
1191}
1192
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001193static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001194{
1195 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1196
1197 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001198 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001199 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001200
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001201 return 0;
1202}
1203
1204/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001205int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001206{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001207 static const char *iname[] = {
1208 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1209 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1210 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1211 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1212 [ CX88_VMUX_SVIDEO ] = "S-Video",
1213 [ CX88_VMUX_TELEVISION ] = "Television",
1214 [ CX88_VMUX_CABLE ] = "Cable TV",
1215 [ CX88_VMUX_DVB ] = "DVB",
1216 [ CX88_VMUX_DEBUG ] = "for debug only",
1217 };
1218 unsigned int n;
1219
1220 n = i->index;
1221 if (n >= 4)
1222 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001223 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001224 return -EINVAL;
1225 memset(i,0,sizeof(*i));
1226 i->index = n;
1227 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001228 strcpy(i->name,iname[INPUT(n).type]);
1229 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1230 (CX88_VMUX_CABLE == INPUT(n).type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001231 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001232 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001233 return 0;
1234}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001235EXPORT_SYMBOL(cx88_enum_input);
1236
1237static int vidioc_enum_input (struct file *file, void *priv,
1238 struct v4l2_input *i)
1239{
1240 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1241 return cx88_enum_input (core,i);
1242}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001243
1244static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1245{
1246 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1247
1248 *i = core->input;
1249 return 0;
1250}
1251
1252static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1253{
1254 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1255
1256 if (i >= 4)
1257 return -EINVAL;
1258
1259 mutex_lock(&core->lock);
1260 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001261 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001262 mutex_unlock(&core->lock);
1263 return 0;
1264}
1265
1266
1267
1268static int vidioc_queryctrl (struct file *file, void *priv,
1269 struct v4l2_queryctrl *qctrl)
1270{
1271 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1272 if (unlikely(qctrl->id == 0))
1273 return -EINVAL;
1274 return cx8800_ctrl_query(qctrl);
1275}
1276
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001277static int vidioc_g_ctrl (struct file *file, void *priv,
1278 struct v4l2_control *ctl)
1279{
1280 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1281 return
1282 cx88_get_control(core,ctl);
1283}
1284
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001285static int vidioc_s_ctrl (struct file *file, void *priv,
1286 struct v4l2_control *ctl)
1287{
1288 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001289 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001290 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001291}
1292
1293static int vidioc_g_tuner (struct file *file, void *priv,
1294 struct v4l2_tuner *t)
1295{
1296 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1297 u32 reg;
1298
Trent Piepho6a59d642007-08-15 14:41:57 -03001299 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001300 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001301 if (0 != t->index)
1302 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001303
1304 strcpy(t->name, "Television");
1305 t->type = V4L2_TUNER_ANALOG_TV;
1306 t->capability = V4L2_TUNER_CAP_NORM;
1307 t->rangehigh = 0xffffffffUL;
1308
1309 cx88_get_stereo(core ,t);
1310 reg = cx_read(MO_DEVICE_STATUS);
1311 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1312 return 0;
1313}
1314
1315static int vidioc_s_tuner (struct file *file, void *priv,
1316 struct v4l2_tuner *t)
1317{
1318 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1319
Trent Piepho6a59d642007-08-15 14:41:57 -03001320 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001321 return -EINVAL;
1322 if (0 != t->index)
1323 return -EINVAL;
1324
1325 cx88_set_stereo(core, t->audmode, 1);
1326 return 0;
1327}
1328
1329static int vidioc_g_frequency (struct file *file, void *priv,
1330 struct v4l2_frequency *f)
1331{
1332 struct cx8800_fh *fh = priv;
1333 struct cx88_core *core = fh->dev->core;
1334
Trent Piepho6a59d642007-08-15 14:41:57 -03001335 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001336 return -EINVAL;
1337
1338 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1339 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1340 f->frequency = core->freq;
1341
1342 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1343
1344 return 0;
1345}
1346
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001347int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001348 struct v4l2_frequency *f)
1349{
Trent Piepho6a59d642007-08-15 14:41:57 -03001350 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001351 return -EINVAL;
1352 if (unlikely(f->tuner != 0))
1353 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001354
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001355 mutex_lock(&core->lock);
1356 core->freq = f->frequency;
1357 cx88_newstation(core);
1358 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1359
1360 /* When changing channels it is required to reset TVAUDIO */
1361 msleep (10);
1362 cx88_set_tvaudio(core);
1363
1364 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001365
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001366 return 0;
1367}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001368EXPORT_SYMBOL(cx88_set_freq);
1369
1370static int vidioc_s_frequency (struct file *file, void *priv,
1371 struct v4l2_frequency *f)
1372{
1373 struct cx8800_fh *fh = priv;
1374 struct cx88_core *core = fh->dev->core;
1375
1376 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1377 return -EINVAL;
1378 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1379 return -EINVAL;
1380
1381 return
1382 cx88_set_freq (core,f);
1383}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001384
Trent Piephodbbff482007-01-22 23:31:53 -03001385#ifdef CONFIG_VIDEO_ADV_DEBUG
1386static int vidioc_g_register (struct file *file, void *fh,
1387 struct v4l2_register *reg)
1388{
1389 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1390
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001391 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001392 return -EINVAL;
1393 /* cx2388x has a 24-bit register space */
1394 reg->val = cx_read(reg->reg&0xffffff);
1395 return 0;
1396}
1397
1398static int vidioc_s_register (struct file *file, void *fh,
1399 struct v4l2_register *reg)
1400{
1401 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1402
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001403 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001404 return -EINVAL;
Trent Piephodbbff482007-01-22 23:31:53 -03001405 cx_write(reg->reg&0xffffff, reg->val);
1406 return 0;
1407}
1408#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001411/* RADIO ESPECIFIC IOCTLS */
1412/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001414static int radio_querycap (struct file *file, void *priv,
1415 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001417 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 struct cx88_core *core = dev->core;
1419
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001420 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001421 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001422 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1423 cap->version = CX88_VERSION_CODE;
1424 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001428static int radio_g_tuner (struct file *file, void *priv,
1429 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001431 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1432
1433 if (unlikely(t->index > 0))
1434 return -EINVAL;
1435
1436 strcpy(t->name, "Radio");
1437 t->type = V4L2_TUNER_RADIO;
1438
1439 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1440 return 0;
1441}
1442
1443static int radio_enum_input (struct file *file, void *priv,
1444 struct v4l2_input *i)
1445{
1446 if (i->index != 0)
1447 return -EINVAL;
1448 strcpy(i->name,"Radio");
1449 i->type = V4L2_INPUT_TYPE_TUNER;
1450
1451 return 0;
1452}
1453
1454static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1455{
1456 if (unlikely(a->index))
1457 return -EINVAL;
1458
1459 memset(a,0,sizeof(*a));
1460 strcpy(a->name,"Radio");
1461 return 0;
1462}
1463
1464/* FIXME: Should add a standard for radio */
1465
1466static int radio_s_tuner (struct file *file, void *priv,
1467 struct v4l2_tuner *t)
1468{
1469 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1470
1471 if (0 != t->index)
1472 return -EINVAL;
1473
1474 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1475
1476 return 0;
1477}
1478
1479static int radio_s_audio (struct file *file, void *fh,
1480 struct v4l2_audio *a)
1481{
1482 return 0;
1483}
1484
1485static int radio_s_input (struct file *file, void *fh, unsigned int i)
1486{
1487 return 0;
1488}
1489
1490static int radio_queryctrl (struct file *file, void *priv,
1491 struct v4l2_queryctrl *c)
1492{
1493 int i;
1494
1495 if (c->id < V4L2_CID_BASE ||
1496 c->id >= V4L2_CID_LASTP1)
1497 return -EINVAL;
1498 if (c->id == V4L2_CID_AUDIO_MUTE) {
1499 for (i = 0; i < CX8800_CTLS; i++)
1500 if (cx8800_ctls[i].v.id == c->id)
1501 break;
1502 *c = cx8800_ctls[i].v;
1503 } else
1504 *c = no_ctl;
1505 return 0;
1506}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508/* ----------------------------------------------------------- */
1509
1510static void cx8800_vid_timeout(unsigned long data)
1511{
1512 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1513 struct cx88_core *core = dev->core;
1514 struct cx88_dmaqueue *q = &dev->vidq;
1515 struct cx88_buffer *buf;
1516 unsigned long flags;
1517
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001518 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 cx_clear(MO_VID_DMACNTRL, 0x11);
1521 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1522
1523 spin_lock_irqsave(&dev->slock,flags);
1524 while (!list_empty(&q->active)) {
1525 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1526 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03001527 buf->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 wake_up(&buf->vb.done);
1529 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1530 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1531 }
1532 restart_video_queue(dev,q);
1533 spin_unlock_irqrestore(&dev->slock,flags);
1534}
1535
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001536static char *cx88_vid_irqs[32] = {
1537 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1538 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1539 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1540 "y_sync", "u_sync", "v_sync", "vbi_sync",
1541 "opc_err", "par_err", "rip_err", "pci_abort",
1542};
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544static void cx8800_vid_irq(struct cx8800_dev *dev)
1545{
1546 struct cx88_core *core = dev->core;
1547 u32 status, mask, count;
1548
1549 status = cx_read(MO_VID_INTSTAT);
1550 mask = cx_read(MO_VID_INTMSK);
1551 if (0 == (status & mask))
1552 return;
1553 cx_write(MO_VID_INTSTAT, status);
1554 if (irq_debug || (status & mask & ~0xff))
1555 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001556 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1557 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* risc op code error */
1560 if (status & (1 << 16)) {
1561 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1562 cx_clear(MO_VID_DMACNTRL, 0x11);
1563 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001564 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566
1567 /* risc1 y */
1568 if (status & 0x01) {
1569 spin_lock(&dev->slock);
1570 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001571 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 spin_unlock(&dev->slock);
1573 }
1574
1575 /* risc1 vbi */
1576 if (status & 0x08) {
1577 spin_lock(&dev->slock);
1578 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001579 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 spin_unlock(&dev->slock);
1581 }
1582
1583 /* risc2 y */
1584 if (status & 0x10) {
1585 dprintk(2,"stopper video\n");
1586 spin_lock(&dev->slock);
1587 restart_video_queue(dev,&dev->vidq);
1588 spin_unlock(&dev->slock);
1589 }
1590
1591 /* risc2 vbi */
1592 if (status & 0x80) {
1593 dprintk(2,"stopper vbi\n");
1594 spin_lock(&dev->slock);
1595 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1596 spin_unlock(&dev->slock);
1597 }
1598}
1599
David Howells7d12e782006-10-05 14:55:46 +01001600static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct cx8800_dev *dev = dev_id;
1603 struct cx88_core *core = dev->core;
1604 u32 status;
1605 int loop, handled = 0;
1606
1607 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001608 status = cx_read(MO_PCI_INTSTAT) &
1609 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (0 == status)
1611 goto out;
1612 cx_write(MO_PCI_INTSTAT, status);
1613 handled = 1;
1614
1615 if (status & core->pci_irqmask)
1616 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001617 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 cx8800_vid_irq(dev);
1619 };
1620 if (10 == loop) {
1621 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1622 core->name);
1623 cx_write(MO_PCI_INTMSK,0);
1624 }
1625
1626 out:
1627 return IRQ_RETVAL(handled);
1628}
1629
1630/* ----------------------------------------------------------- */
1631/* exported stuff */
1632
Arjan van de Venfa027c22007-02-12 00:55:33 -08001633static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 .owner = THIS_MODULE,
1636 .open = video_open,
1637 .release = video_release,
1638 .read = video_read,
1639 .poll = video_poll,
1640 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001641 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001642 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 .llseek = no_llseek,
1644};
1645
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001646static struct video_device cx8800_vbi_template;
Adrian Bunk408b6642005-05-01 08:59:29 -07001647static struct video_device cx8800_video_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001649 .name = "cx8800-video",
1650 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1651 .fops = &video_fops,
1652 .minor = -1,
1653 .vidioc_querycap = vidioc_querycap,
1654 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1655 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1656 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1657 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1658 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1659 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1660 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1661 .vidioc_reqbufs = vidioc_reqbufs,
1662 .vidioc_querybuf = vidioc_querybuf,
1663 .vidioc_qbuf = vidioc_qbuf,
1664 .vidioc_dqbuf = vidioc_dqbuf,
1665 .vidioc_s_std = vidioc_s_std,
1666 .vidioc_enum_input = vidioc_enum_input,
1667 .vidioc_g_input = vidioc_g_input,
1668 .vidioc_s_input = vidioc_s_input,
1669 .vidioc_queryctrl = vidioc_queryctrl,
1670 .vidioc_g_ctrl = vidioc_g_ctrl,
1671 .vidioc_s_ctrl = vidioc_s_ctrl,
1672 .vidioc_streamon = vidioc_streamon,
1673 .vidioc_streamoff = vidioc_streamoff,
1674#ifdef CONFIG_VIDEO_V4L1_COMPAT
1675 .vidiocgmbuf = vidiocgmbuf,
1676#endif
1677 .vidioc_g_tuner = vidioc_g_tuner,
1678 .vidioc_s_tuner = vidioc_s_tuner,
1679 .vidioc_g_frequency = vidioc_g_frequency,
1680 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001681#ifdef CONFIG_VIDEO_ADV_DEBUG
1682 .vidioc_g_register = vidioc_g_register,
1683 .vidioc_s_register = vidioc_s_register,
1684#endif
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001685 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001686 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687};
1688
Arjan van de Venfa027c22007-02-12 00:55:33 -08001689static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 .owner = THIS_MODULE,
1692 .open = video_open,
1693 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001694 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001695 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 .llseek = no_llseek,
1697};
1698
Adrian Bunk408b6642005-05-01 08:59:29 -07001699static struct video_device cx8800_radio_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001701 .name = "cx8800-radio",
1702 .type = VID_TYPE_TUNER,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001703 .fops = &radio_fops,
1704 .minor = -1,
1705 .vidioc_querycap = radio_querycap,
1706 .vidioc_g_tuner = radio_g_tuner,
1707 .vidioc_enum_input = radio_enum_input,
1708 .vidioc_g_audio = radio_g_audio,
1709 .vidioc_s_tuner = radio_s_tuner,
1710 .vidioc_s_audio = radio_s_audio,
1711 .vidioc_s_input = radio_s_input,
1712 .vidioc_queryctrl = radio_queryctrl,
1713 .vidioc_g_ctrl = vidioc_g_ctrl,
1714 .vidioc_s_ctrl = vidioc_s_ctrl,
1715 .vidioc_g_frequency = vidioc_g_frequency,
1716 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephoa75d2042007-08-01 00:13:28 -03001717#ifdef CONFIG_VIDEO_ADV_DEBUG
1718 .vidioc_g_register = vidioc_g_register,
1719 .vidioc_s_register = vidioc_s_register,
1720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721};
1722
1723/* ----------------------------------------------------------- */
1724
1725static void cx8800_unregister_video(struct cx8800_dev *dev)
1726{
1727 if (dev->radio_dev) {
1728 if (-1 != dev->radio_dev->minor)
1729 video_unregister_device(dev->radio_dev);
1730 else
1731 video_device_release(dev->radio_dev);
1732 dev->radio_dev = NULL;
1733 }
1734 if (dev->vbi_dev) {
1735 if (-1 != dev->vbi_dev->minor)
1736 video_unregister_device(dev->vbi_dev);
1737 else
1738 video_device_release(dev->vbi_dev);
1739 dev->vbi_dev = NULL;
1740 }
1741 if (dev->video_dev) {
1742 if (-1 != dev->video_dev->minor)
1743 video_unregister_device(dev->video_dev);
1744 else
1745 video_device_release(dev->video_dev);
1746 dev->video_dev = NULL;
1747 }
1748}
1749
1750static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1751 const struct pci_device_id *pci_id)
1752{
1753 struct cx8800_dev *dev;
1754 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int err;
1757
Panagiotis Issaris74081872006-01-11 19:40:56 -02001758 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (NULL == dev)
1760 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /* pci init */
1763 dev->pci = pci_dev;
1764 if (pci_enable_device(pci_dev)) {
1765 err = -EIO;
1766 goto fail_free;
1767 }
1768 core = cx88_core_get(dev->pci);
1769 if (NULL == core) {
1770 err = -EINVAL;
1771 goto fail_free;
1772 }
1773 dev->core = core;
1774
1775 /* print pci info */
1776 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001777 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1778 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001779 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001781 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 pci_set_master(pci_dev);
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -03001784 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1786 err = -EIO;
1787 goto fail_core;
1788 }
1789
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001790 /* Initialize VBI template */
1791 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1792 sizeof(cx8800_vbi_template) );
1793 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1794 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001798 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* init video dma queues */
1801 INIT_LIST_HEAD(&dev->vidq.active);
1802 INIT_LIST_HEAD(&dev->vidq.queued);
1803 dev->vidq.timeout.function = cx8800_vid_timeout;
1804 dev->vidq.timeout.data = (unsigned long)dev;
1805 init_timer(&dev->vidq.timeout);
1806 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1807 MO_VID_DMACNTRL,0x11,0x00);
1808
1809 /* init vbi dma queues */
1810 INIT_LIST_HEAD(&dev->vbiq.active);
1811 INIT_LIST_HEAD(&dev->vbiq.queued);
1812 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1813 dev->vbiq.timeout.data = (unsigned long)dev;
1814 init_timer(&dev->vbiq.timeout);
1815 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1816 MO_VID_DMACNTRL,0x88,0x00);
1817
1818 /* get irq */
1819 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001820 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001822 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 core->name,pci_dev->irq);
1824 goto fail_core;
1825 }
1826 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1827
1828 /* load and configure helper modules */
Trent Piepho6a59d642007-08-15 14:41:57 -03001829 if (TUNER_ABSENT != core->board.tuner_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 request_module("tuner");
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001831
Trent Piepho6a59d642007-08-15 14:41:57 -03001832 if (core->board.audio_chip == AUDIO_CHIP_WM8775)
Steven Toth30579062006-09-25 12:43:42 -03001833 request_module("wm8775");
1834
Michael Krufky6fcecce2007-08-24 01:32:31 -03001835 switch (core->boardnr) {
1836 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1837 request_module("ir-kbd-i2c");
1838 request_module("rtc-isl1208");
1839 }
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /* register v4l devices */
1842 dev->video_dev = cx88_vdev_init(core,dev->pci,
1843 &cx8800_video_template,"video");
1844 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1845 video_nr[core->nr]);
1846 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001847 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 core->name);
1849 goto fail_unreg;
1850 }
1851 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1852 core->name,dev->video_dev->minor & 0x1f);
1853
1854 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1855 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1856 vbi_nr[core->nr]);
1857 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001858 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 core->name);
1860 goto fail_unreg;
1861 }
1862 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1863 core->name,dev->vbi_dev->minor & 0x1f);
1864
Trent Piepho6a59d642007-08-15 14:41:57 -03001865 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1867 &cx8800_radio_template,"radio");
1868 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1869 radio_nr[core->nr]);
1870 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001871 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 core->name);
1873 goto fail_unreg;
1874 }
1875 printk(KERN_INFO "%s/0: registered device radio%d\n",
1876 core->name,dev->radio_dev->minor & 0x1f);
1877 }
1878
1879 /* everything worked */
1880 list_add_tail(&dev->devlist,&cx8800_devlist);
1881 pci_set_drvdata(pci_dev,dev);
1882
1883 /* initial device configuration */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001884 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001885 cx88_set_tvnorm(core,core->tvnorm);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001886 init_controls(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001887 cx88_video_mux(core,0);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001888 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001891 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001893 if (IS_ERR(core->kthread)) {
1894 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001895 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1896 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001897 }
1898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return 0;
1900
1901fail_unreg:
1902 cx8800_unregister_video(dev);
1903 free_irq(pci_dev->irq, dev);
1904fail_core:
1905 cx88_core_put(core,dev->pci);
1906fail_free:
1907 kfree(dev);
1908 return err;
1909}
1910
1911static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1912{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001913 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001914 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001917 if (core->kthread) {
1918 kthread_stop(core->kthread);
1919 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001922 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 pci_disable_device(pci_dev);
1924
1925 /* unregister stuff */
1926
1927 free_irq(pci_dev->irq, dev);
1928 cx8800_unregister_video(dev);
1929 pci_set_drvdata(pci_dev, NULL);
1930
1931 /* free memory */
1932 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1933 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001934 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 kfree(dev);
1936}
1937
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001938#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1940{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001941 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 struct cx88_core *core = dev->core;
1943
1944 /* stop video+vbi capture */
1945 spin_lock(&dev->slock);
1946 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001947 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 stop_video_dma(dev);
1949 del_timer(&dev->vidq.timeout);
1950 }
1951 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001952 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 cx8800_stop_vbi_dma(dev);
1954 del_timer(&dev->vbiq.timeout);
1955 }
1956 spin_unlock(&dev->slock);
1957
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001958 if (core->ir)
1959 cx88_ir_stop(core, core->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001961 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 pci_save_state(pci_dev);
1964 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1965 pci_disable_device(pci_dev);
1966 dev->state.disabled = 1;
1967 }
1968 return 0;
1969}
1970
1971static int cx8800_resume(struct pci_dev *pci_dev)
1972{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001973 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001975 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001978 err=pci_enable_device(pci_dev);
1979 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001980 printk(KERN_ERR "%s/0: can't enable device\n",
1981 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001982 return err;
1983 }
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 dev->state.disabled = 0;
1986 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001987 err= pci_set_power_state(pci_dev, PCI_D0);
1988 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001989 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001990 pci_disable_device(pci_dev);
1991 dev->state.disabled = 1;
1992
1993 return err;
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 pci_restore_state(pci_dev);
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001998 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001999 if (core->ir)
2000 cx88_ir_start(core, core->ir);
2001
2002 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /* restart video+vbi capture */
2005 spin_lock(&dev->slock);
2006 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002007 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 restart_video_queue(dev,&dev->vidq);
2009 }
2010 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002011 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2013 }
2014 spin_unlock(&dev->slock);
2015
2016 return 0;
2017}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002018#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020/* ----------------------------------------------------------- */
2021
Adrian Bunk408b6642005-05-01 08:59:29 -07002022static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 {
2024 .vendor = 0x14f1,
2025 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002026 .subvendor = PCI_ANY_ID,
2027 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 },{
2029 /* --- end of list --- */
2030 }
2031};
2032MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2033
2034static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002035 .name = "cx8800",
2036 .id_table = cx8800_pci_tbl,
2037 .probe = cx8800_initdev,
2038 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002039#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 .suspend = cx8800_suspend,
2041 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043};
2044
2045static int cx8800_init(void)
2046{
Trent Piepho5772f812007-08-15 14:41:59 -03002047 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 (CX88_VERSION_CODE >> 16) & 0xff,
2049 (CX88_VERSION_CODE >> 8) & 0xff,
2050 CX88_VERSION_CODE & 0xff);
2051#ifdef SNAPSHOT
2052 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2053 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2054#endif
2055 return pci_register_driver(&cx8800_pci_driver);
2056}
2057
2058static void cx8800_fini(void)
2059{
2060 pci_unregister_driver(&cx8800_pci_driver);
2061}
2062
2063module_init(cx8800_init);
2064module_exit(cx8800_fini);
2065
2066/* ----------------------------------------------------------- */
2067/*
2068 * Local variables:
2069 * c-basic-offset: 8
2070 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002071 * 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 -07002072 */