blob: d7d4d2a6ed9db0f5f710659c0f8f037d6c6e02b6 [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>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
45MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
46MODULE_LICENSE("GPL");
47
48/* ------------------------------------------------------------------ */
49
50static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
51static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
52static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
53
54module_param_array(video_nr, int, NULL, 0444);
55module_param_array(vbi_nr, int, NULL, 0444);
56module_param_array(radio_nr, int, NULL, 0444);
57
58MODULE_PARM_DESC(video_nr,"video device numbers");
59MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
60MODULE_PARM_DESC(radio_nr,"radio device numbers");
61
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030062static unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063module_param(video_debug,int,0644);
64MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
65
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030066static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067module_param(irq_debug,int,0644);
68MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
69
70static unsigned int vid_limit = 16;
71module_param(vid_limit,int,0644);
72MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
73
74#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070075 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/* ------------------------------------------------------------------ */
78
79static LIST_HEAD(cx8800_devlist);
80
81/* ------------------------------------------------------------------- */
82/* static data */
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static struct cx8800_fmt formats[] = {
85 {
86 .name = "8 bpp, gray",
87 .fourcc = V4L2_PIX_FMT_GREY,
88 .cxformat = ColorFormatY8,
89 .depth = 8,
90 .flags = FORMAT_FLAGS_PACKED,
91 },{
92 .name = "15 bpp RGB, le",
93 .fourcc = V4L2_PIX_FMT_RGB555,
94 .cxformat = ColorFormatRGB15,
95 .depth = 16,
96 .flags = FORMAT_FLAGS_PACKED,
97 },{
98 .name = "15 bpp RGB, be",
99 .fourcc = V4L2_PIX_FMT_RGB555X,
100 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
101 .depth = 16,
102 .flags = FORMAT_FLAGS_PACKED,
103 },{
104 .name = "16 bpp RGB, le",
105 .fourcc = V4L2_PIX_FMT_RGB565,
106 .cxformat = ColorFormatRGB16,
107 .depth = 16,
108 .flags = FORMAT_FLAGS_PACKED,
109 },{
110 .name = "16 bpp RGB, be",
111 .fourcc = V4L2_PIX_FMT_RGB565X,
112 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
113 .depth = 16,
114 .flags = FORMAT_FLAGS_PACKED,
115 },{
116 .name = "24 bpp RGB, le",
117 .fourcc = V4L2_PIX_FMT_BGR24,
118 .cxformat = ColorFormatRGB24,
119 .depth = 24,
120 .flags = FORMAT_FLAGS_PACKED,
121 },{
122 .name = "32 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_BGR32,
124 .cxformat = ColorFormatRGB32,
125 .depth = 32,
126 .flags = FORMAT_FLAGS_PACKED,
127 },{
128 .name = "32 bpp RGB, be",
129 .fourcc = V4L2_PIX_FMT_RGB32,
130 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
131 .depth = 32,
132 .flags = FORMAT_FLAGS_PACKED,
133 },{
134 .name = "4:2:2, packed, YUYV",
135 .fourcc = V4L2_PIX_FMT_YUYV,
136 .cxformat = ColorFormatYUY2,
137 .depth = 16,
138 .flags = FORMAT_FLAGS_PACKED,
139 },{
140 .name = "4:2:2, packed, UYVY",
141 .fourcc = V4L2_PIX_FMT_UYVY,
142 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
143 .depth = 16,
144 .flags = FORMAT_FLAGS_PACKED,
145 },
146};
147
148static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
149{
150 unsigned int i;
151
152 for (i = 0; i < ARRAY_SIZE(formats); i++)
153 if (formats[i].fourcc == fourcc)
154 return formats+i;
155 return NULL;
156}
157
158/* ------------------------------------------------------------------- */
159
160static const struct v4l2_queryctrl no_ctl = {
161 .name = "42",
162 .flags = V4L2_CTRL_FLAG_DISABLED,
163};
164
165static struct cx88_ctrl cx8800_ctls[] = {
166 /* --- video --- */
167 {
168 .v = {
169 .id = V4L2_CID_BRIGHTNESS,
170 .name = "Brightness",
171 .minimum = 0x00,
172 .maximum = 0xff,
173 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300174 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .type = V4L2_CTRL_TYPE_INTEGER,
176 },
177 .off = 128,
178 .reg = MO_CONTR_BRIGHT,
179 .mask = 0x00ff,
180 .shift = 0,
181 },{
182 .v = {
183 .id = V4L2_CID_CONTRAST,
184 .name = "Contrast",
185 .minimum = 0,
186 .maximum = 0xff,
187 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200188 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .type = V4L2_CTRL_TYPE_INTEGER,
190 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700191 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .reg = MO_CONTR_BRIGHT,
193 .mask = 0xff00,
194 .shift = 8,
195 },{
196 .v = {
197 .id = V4L2_CID_HUE,
198 .name = "Hue",
199 .minimum = 0,
200 .maximum = 0xff,
201 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300202 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .type = V4L2_CTRL_TYPE_INTEGER,
204 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700205 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .reg = MO_HUE,
207 .mask = 0x00ff,
208 .shift = 0,
209 },{
210 /* strictly, this only describes only U saturation.
211 * V saturation is handled specially through code.
212 */
213 .v = {
214 .id = V4L2_CID_SATURATION,
215 .name = "Saturation",
216 .minimum = 0,
217 .maximum = 0xff,
218 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200219 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .type = V4L2_CTRL_TYPE_INTEGER,
221 },
222 .off = 0,
223 .reg = MO_UV_SATURATION,
224 .mask = 0x00ff,
225 .shift = 0,
226 },{
Frej Drejhammar6d042032008-03-23 22:43:21 -0300227 .v = {
228 .id = V4L2_CID_CHROMA_AGC,
229 .name = "Chroma AGC",
230 .minimum = 0,
231 .maximum = 1,
Frej Drejhammar87a17382008-03-23 22:43:22 -0300232 .default_value = 0x1,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300233 .type = V4L2_CTRL_TYPE_BOOLEAN,
234 },
235 .reg = MO_INPUT_FORMAT,
236 .mask = 1 << 10,
237 .shift = 10,
238 }, {
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300239 .v = {
240 .id = V4L2_CID_COLOR_KILLER,
241 .name = "Color killer",
242 .minimum = 0,
243 .maximum = 1,
Frej Drejhammar0b5afdd2008-03-23 22:43:25 -0300244 .default_value = 0x1,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300245 .type = V4L2_CTRL_TYPE_BOOLEAN,
246 },
247 .reg = MO_INPUT_FORMAT,
248 .mask = 1 << 9,
249 .shift = 9,
250 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* --- audio --- */
252 .v = {
253 .id = V4L2_CID_AUDIO_MUTE,
254 .name = "Mute",
255 .minimum = 0,
256 .maximum = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200257 .default_value = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .type = V4L2_CTRL_TYPE_BOOLEAN,
259 },
260 .reg = AUD_VOL_CTL,
261 .sreg = SHADOW_AUD_VOL_CTL,
262 .mask = (1 << 6),
263 .shift = 6,
264 },{
265 .v = {
266 .id = V4L2_CID_AUDIO_VOLUME,
267 .name = "Volume",
268 .minimum = 0,
269 .maximum = 0x3f,
270 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300271 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .type = V4L2_CTRL_TYPE_INTEGER,
273 },
274 .reg = AUD_VOL_CTL,
275 .sreg = SHADOW_AUD_VOL_CTL,
276 .mask = 0x3f,
277 .shift = 0,
278 },{
279 .v = {
280 .id = V4L2_CID_AUDIO_BALANCE,
281 .name = "Balance",
282 .minimum = 0,
283 .maximum = 0x7f,
284 .step = 1,
285 .default_value = 0x40,
286 .type = V4L2_CTRL_TYPE_INTEGER,
287 },
288 .reg = AUD_BAL_CTL,
289 .sreg = SHADOW_AUD_BAL_CTL,
290 .mask = 0x7f,
291 .shift = 0,
292 }
293};
Adrian Bunk408b6642005-05-01 08:59:29 -0700294static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Hans Verkuil2ba58892009-02-13 10:57:48 -0300296/* Must be sorted from low to high control ID! */
Michael Krufky38a27132006-06-26 23:42:39 -0300297const u32 cx88_user_ctrls[] = {
298 V4L2_CID_USER_CLASS,
299 V4L2_CID_BRIGHTNESS,
300 V4L2_CID_CONTRAST,
301 V4L2_CID_SATURATION,
302 V4L2_CID_HUE,
303 V4L2_CID_AUDIO_VOLUME,
304 V4L2_CID_AUDIO_BALANCE,
305 V4L2_CID_AUDIO_MUTE,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300306 V4L2_CID_CHROMA_AGC,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300307 V4L2_CID_COLOR_KILLER,
Michael Krufky38a27132006-06-26 23:42:39 -0300308 0
309};
310EXPORT_SYMBOL(cx88_user_ctrls);
311
312static const u32 *ctrl_classes[] = {
313 cx88_user_ctrls,
314 NULL
315};
316
Frej Drejhammar6d042032008-03-23 22:43:21 -0300317int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
Michael Krufky38a27132006-06-26 23:42:39 -0300318{
319 int i;
320
321 if (qctrl->id < V4L2_CID_BASE ||
322 qctrl->id >= V4L2_CID_LASTP1)
323 return -EINVAL;
324 for (i = 0; i < CX8800_CTLS; i++)
325 if (cx8800_ctls[i].v.id == qctrl->id)
326 break;
327 if (i == CX8800_CTLS) {
328 *qctrl = no_ctl;
329 return 0;
330 }
331 *qctrl = cx8800_ctls[i].v;
Frej Drejhammar6d042032008-03-23 22:43:21 -0300332 /* Report chroma AGC as inactive when SECAM is selected */
333 if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
334 core->tvnorm & V4L2_STD_SECAM)
335 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
336
Michael Krufky38a27132006-06-26 23:42:39 -0300337 return 0;
338}
339EXPORT_SYMBOL(cx8800_ctrl_query);
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/* ------------------------------------------------------------------- */
342/* resource management */
343
344static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
345{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700346 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (fh->resources & bit)
348 /* have it already allocated */
349 return 1;
350
351 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200352 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (dev->resources & bit) {
354 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200355 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357 }
358 /* it's free, grab it */
359 fh->resources |= bit;
360 dev->resources |= bit;
361 dprintk(1,"res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200362 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 1;
364}
365
366static
367int res_check(struct cx8800_fh *fh, unsigned int bit)
368{
369 return (fh->resources & bit);
370}
371
372static
373int res_locked(struct cx8800_dev *dev, unsigned int bit)
374{
375 return (dev->resources & bit);
376}
377
378static
379void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
380{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700381 struct cx88_core *core = dev->core;
Eric Sesterhennae246012006-03-13 13:17:11 -0300382 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Ingo Molnar3593cab2006-02-07 06:49:14 -0200384 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 fh->resources &= ~bits;
386 dev->resources &= ~bits;
387 dprintk(1,"res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200388 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/* ------------------------------------------------------------------ */
392
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300393int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700395 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300398 input, INPUT(input).vmux,
399 INPUT(input).gpio0,INPUT(input).gpio1,
400 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700401 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300402 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
403 cx_write(MO_GP3_IO, INPUT(input).gpio3);
404 cx_write(MO_GP0_IO, INPUT(input).gpio0);
405 cx_write(MO_GP1_IO, INPUT(input).gpio1);
406 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Trent Piepho6a59d642007-08-15 14:41:57 -0300408 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 case CX88_VMUX_SVIDEO:
410 cx_set(MO_AFECFG_IO, 0x00000001);
411 cx_set(MO_INPUT_FORMAT, 0x00010010);
412 cx_set(MO_FILTER_EVEN, 0x00002020);
413 cx_set(MO_FILTER_ODD, 0x00002020);
414 break;
415 default:
416 cx_clear(MO_AFECFG_IO, 0x00000001);
417 cx_clear(MO_INPUT_FORMAT, 0x00010010);
418 cx_clear(MO_FILTER_EVEN, 0x00002020);
419 cx_clear(MO_FILTER_ODD, 0x00002020);
420 break;
421 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300422
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300423 /* if there are audioroutes defined, we have an external
424 ADC to deal with audio */
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300425 if (INPUT(input).audioroute) {
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300426 /* The wm8775 module has the "2" route hardwired into
427 the initialization. Some boards may use different
428 routes for different inputs. HVR-1300 surely does */
429 if (core->board.audio_chip &&
Hans Verkuil38f9d302008-07-23 05:09:15 -0300430 core->board.audio_chip == V4L2_IDENT_WM8775) {
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300431 struct v4l2_routing route;
432
433 route.input = INPUT(input).audioroute;
Hans Verkuilb8341e12009-03-29 08:26:01 -0300434 call_all(core, audio, s_routing, &route);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300435 }
Darron Broad430189d2008-10-15 14:18:42 -0300436 /* cx2388's C-ADC is connected to the tuner only.
437 When used with S-Video, that ADC is busy dealing with
438 chroma, so an external must be used for baseband audio */
439 if (INPUT(input).type != CX88_VMUX_TELEVISION ) {
440 /* "I2S ADC mode" */
441 core->tvaudio = WW_I2SADC;
442 cx88_set_tvaudio(core);
443 } else {
444 /* Normal mode */
445 cx_write(AUD_I2SCNTL, 0x0);
446 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
447 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300448 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300452EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/* ------------------------------------------------------------------ */
455
456static int start_video_dma(struct cx8800_dev *dev,
457 struct cx88_dmaqueue *q,
458 struct cx88_buffer *buf)
459{
460 struct cx88_core *core = dev->core;
461
462 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700463 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700465 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
467
468 /* reset counter */
469 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
470 q->count = 1;
471
472 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300473 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700474
475 /* Enables corresponding bits at PCI_INT_STAT:
476 bits 0 to 4: video, audio, transport stream, VIP, Host
477 bit 7: timer
478 bits 8 and 9: DMA complete for: SRC, DST
479 bits 10 and 11: BERR signal asserted for RISC: RD, WR
480 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 cx_set(MO_VID_INTMSK, 0x0f0011);
483
484 /* enable capture */
485 cx_set(VID_CAPTURE_CONTROL,0x06);
486
487 /* start dma */
488 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700489 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 return 0;
492}
493
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300494#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static int stop_video_dma(struct cx8800_dev *dev)
496{
497 struct cx88_core *core = dev->core;
498
499 /* stop dma */
500 cx_clear(MO_VID_DMACNTRL, 0x11);
501
502 /* disable capture */
503 cx_clear(VID_CAPTURE_CONTROL,0x06);
504
505 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300506 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 cx_clear(MO_VID_INTMSK, 0x0f0011);
508 return 0;
509}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512static int restart_video_queue(struct cx8800_dev *dev,
513 struct cx88_dmaqueue *q)
514{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700515 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct cx88_buffer *buf, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800519 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
521 buf, buf->vb.i);
522 start_video_dma(dev, q, buf);
Trent Piepho8bb629e22007-10-10 05:37:40 -0300523 list_for_each_entry(buf, &q->active, vb.queue)
524 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
526 return 0;
527 }
528
529 prev = NULL;
530 for (;;) {
531 if (list_empty(&q->queued))
532 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800533 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700535 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300537 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 buf->count = q->count++;
539 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
540 dprintk(2,"[%p/%d] restart_queue - first active\n",
541 buf,buf->vb.i);
542
543 } else if (prev->vb.width == buf->vb.width &&
544 prev->vb.height == buf->vb.height &&
545 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700546 list_move_tail(&buf->vb.queue, &q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300547 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 buf->count = q->count++;
549 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
550 dprintk(2,"[%p/%d] restart_queue - move to active\n",
551 buf,buf->vb.i);
552 } else {
553 return 0;
554 }
555 prev = buf;
556 }
557}
558
559/* ------------------------------------------------------------------ */
560
561static int
562buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
563{
564 struct cx8800_fh *fh = q->priv_data;
565
566 *size = fh->fmt->depth*fh->width*fh->height >> 3;
567 if (0 == *count)
568 *count = 32;
569 while (*size * *count > vid_limit * 1024 * 1024)
570 (*count)--;
571 return 0;
572}
573
574static int
575buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
576 enum v4l2_field field)
577{
578 struct cx8800_fh *fh = q->priv_data;
579 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700580 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300582 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int rc, init_buffer = 0;
584
585 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700586 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
587 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return -EINVAL;
589 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
590 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
591 return -EINVAL;
592
593 if (buf->fmt != fh->fmt ||
594 buf->vb.width != fh->width ||
595 buf->vb.height != fh->height ||
596 buf->vb.field != field) {
597 buf->fmt = fh->fmt;
598 buf->vb.width = fh->width;
599 buf->vb.height = fh->height;
600 buf->vb.field = field;
601 init_buffer = 1;
602 }
603
Brandon Philips0fc06862007-11-06 20:02:36 -0300604 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300606 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goto fail;
608 }
609
610 if (init_buffer) {
611 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
612 switch (buf->vb.field) {
613 case V4L2_FIELD_TOP:
614 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300615 dma->sglist, 0, UNSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 buf->bpl, 0, buf->vb.height);
617 break;
618 case V4L2_FIELD_BOTTOM:
619 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300620 dma->sglist, UNSET, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 buf->bpl, 0, buf->vb.height);
622 break;
623 case V4L2_FIELD_INTERLACED:
624 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300625 dma->sglist, 0, buf->bpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 buf->bpl, buf->bpl,
627 buf->vb.height >> 1);
628 break;
629 case V4L2_FIELD_SEQ_TB:
630 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300631 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 0, buf->bpl * (buf->vb.height >> 1),
633 buf->bpl, 0,
634 buf->vb.height >> 1);
635 break;
636 case V4L2_FIELD_SEQ_BT:
637 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300638 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 buf->bpl * (buf->vb.height >> 1), 0,
640 buf->bpl, 0,
641 buf->vb.height >> 1);
642 break;
643 default:
644 BUG();
645 }
646 }
647 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
648 buf, buf->vb.i,
649 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
650 (unsigned long)buf->risc.dma);
651
Brandon Philips0fc06862007-11-06 20:02:36 -0300652 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654
655 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300656 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return rc;
658}
659
660static void
661buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
662{
663 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
664 struct cx88_buffer *prev;
665 struct cx8800_fh *fh = vq->priv_data;
666 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700667 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct cx88_dmaqueue *q = &dev->vidq;
669
670 /* add jump to stopper */
671 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
672 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
673
674 if (!list_empty(&q->queued)) {
675 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300676 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
678 buf, buf->vb.i);
679
680 } else if (list_empty(&q->active)) {
681 list_add_tail(&buf->vb.queue,&q->active);
682 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300683 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 buf->count = q->count++;
685 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
686 dprintk(2,"[%p/%d] buffer_queue - first active\n",
687 buf, buf->vb.i);
688
689 } else {
690 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
691 if (prev->vb.width == buf->vb.width &&
692 prev->vb.height == buf->vb.height &&
693 prev->fmt == buf->fmt) {
694 list_add_tail(&buf->vb.queue,&q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300695 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 buf->count = q->count++;
697 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
698 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
699 buf, buf->vb.i);
700
701 } else {
702 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300703 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
705 buf, buf->vb.i);
706 }
707 }
708}
709
710static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
711{
712 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300714 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Adrian Bunk408b6642005-05-01 08:59:29 -0700717static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .buf_setup = buffer_setup,
719 .buf_prepare = buffer_prepare,
720 .buf_queue = buffer_queue,
721 .buf_release = buffer_release,
722};
723
724/* ------------------------------------------------------------------ */
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727/* ------------------------------------------------------------------ */
728
729static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
730{
731 switch (fh->type) {
732 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
733 return &fh->vidq;
734 case V4L2_BUF_TYPE_VBI_CAPTURE:
735 return &fh->vbiq;
736 default:
737 BUG();
738 return NULL;
739 }
740}
741
742static int get_ressource(struct cx8800_fh *fh)
743{
744 switch (fh->type) {
745 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
746 return RESOURCE_VIDEO;
747 case V4L2_BUF_TYPE_VBI_CAPTURE:
748 return RESOURCE_VBI;
749 default:
750 BUG();
751 return 0;
752 }
753}
754
Hans Verkuilbec43662008-12-30 06:58:20 -0300755static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Hans Verkuilbec43662008-12-30 06:58:20 -0300757 int minor = video_devdata(file)->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700759 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct cx8800_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 enum v4l2_buf_type type = 0;
762 int radio = 0;
763
Hans Verkuild56dc612008-07-30 08:43:36 -0300764 lock_kernel();
Trent Piepho8bb629e22007-10-10 05:37:40 -0300765 list_for_each_entry(h, &cx8800_devlist, devlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (h->video_dev->minor == minor) {
767 dev = h;
768 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
769 }
770 if (h->vbi_dev->minor == minor) {
771 dev = h;
772 type = V4L2_BUF_TYPE_VBI_CAPTURE;
773 }
774 if (h->radio_dev &&
775 h->radio_dev->minor == minor) {
776 radio = 1;
777 dev = h;
778 }
779 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300780 if (NULL == dev) {
781 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -0300783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700785 core = dev->core;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 dprintk(1,"open minor=%d radio=%d type=%s\n",
788 minor,radio,v4l2_type_names[type]);
789
790 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200791 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -0300792 if (NULL == fh) {
793 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -0300795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 file->private_data = fh;
797 fh->dev = dev;
798 fh->radio = radio;
799 fh->type = type;
800 fh->width = 320;
801 fh->height = 240;
802 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
803
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300804 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
805 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 V4L2_BUF_TYPE_VIDEO_CAPTURE,
807 V4L2_FIELD_INTERLACED,
808 sizeof(struct cx88_buffer),
809 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300810 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
811 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 V4L2_BUF_TYPE_VBI_CAPTURE,
813 V4L2_FIELD_SEQ_TB,
814 sizeof(struct cx88_buffer),
815 fh);
816
817 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 dprintk(1,"video_open: setting radio device\n");
Trent Piepho6a59d642007-08-15 14:41:57 -0300819 cx_write(MO_GP3_IO, core->board.radio.gpio3);
820 cx_write(MO_GP0_IO, core->board.radio.gpio0);
821 cx_write(MO_GP1_IO, core->board.radio.gpio1);
822 cx_write(MO_GP2_IO, core->board.radio.gpio2);
Darron Broad430189d2008-10-15 14:18:42 -0300823 if (core->board.radio.audioroute) {
824 if(core->board.audio_chip &&
825 core->board.audio_chip == V4L2_IDENT_WM8775) {
826 struct v4l2_routing route;
827
828 route.input = core->board.radio.audioroute;
Hans Verkuilb8341e12009-03-29 08:26:01 -0300829 call_all(core, audio, s_routing, &route);
Darron Broad430189d2008-10-15 14:18:42 -0300830 }
831 /* "I2S ADC mode" */
832 core->tvaudio = WW_I2SADC;
833 cx88_set_tvaudio(core);
834 } else {
835 /* FM Mode */
836 core->tvaudio = WW_FM;
837 cx88_set_tvaudio(core);
838 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
839 }
Hans Verkuilb8341e12009-03-29 08:26:01 -0300840 call_all(core, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300842 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Darron Broad3e010842008-09-25 16:51:11 -0300844 atomic_inc(&core->users);
845
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800850video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct cx8800_fh *fh = file->private_data;
853
854 switch (fh->type) {
855 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
856 if (res_locked(fh->dev,RESOURCE_VIDEO))
857 return -EBUSY;
858 return videobuf_read_one(&fh->vidq, data, count, ppos,
859 file->f_flags & O_NONBLOCK);
860 case V4L2_BUF_TYPE_VBI_CAPTURE:
861 if (!res_get(fh->dev,fh,RESOURCE_VBI))
862 return -EBUSY;
863 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
864 file->f_flags & O_NONBLOCK);
865 default:
866 BUG();
867 return 0;
868 }
869}
870
871static unsigned int
872video_poll(struct file *file, struct poll_table_struct *wait)
873{
874 struct cx8800_fh *fh = file->private_data;
875 struct cx88_buffer *buf;
876
877 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
878 if (!res_get(fh->dev,fh,RESOURCE_VBI))
879 return POLLERR;
880 return videobuf_poll_stream(file, &fh->vbiq, wait);
881 }
882
883 if (res_check(fh,RESOURCE_VIDEO)) {
884 /* streaming capture */
885 if (list_empty(&fh->vidq.stream))
886 return POLLERR;
887 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
888 } else {
889 /* read() capture */
890 buf = (struct cx88_buffer*)fh->vidq.read_buf;
891 if (NULL == buf)
892 return POLLERR;
893 }
894 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300895 if (buf->vb.state == VIDEOBUF_DONE ||
896 buf->vb.state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return POLLIN|POLLRDNORM;
898 return 0;
899}
900
Hans Verkuilbec43662008-12-30 06:58:20 -0300901static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct cx8800_fh *fh = file->private_data;
904 struct cx8800_dev *dev = fh->dev;
905
906 /* turn off overlay */
907 if (res_check(fh, RESOURCE_OVERLAY)) {
908 /* FIXME */
909 res_free(dev,fh,RESOURCE_OVERLAY);
910 }
911
912 /* stop video capture */
913 if (res_check(fh, RESOURCE_VIDEO)) {
914 videobuf_queue_cancel(&fh->vidq);
915 res_free(dev,fh,RESOURCE_VIDEO);
916 }
917 if (fh->vidq.read_buf) {
918 buffer_release(&fh->vidq,fh->vidq.read_buf);
919 kfree(fh->vidq.read_buf);
920 }
921
922 /* stop vbi capture */
923 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -0300924 videobuf_stop(&fh->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 res_free(dev,fh,RESOURCE_VBI);
926 }
927
928 videobuf_mmap_free(&fh->vidq);
929 videobuf_mmap_free(&fh->vbiq);
930 file->private_data = NULL;
931 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700932
Darron Broad3e010842008-09-25 16:51:11 -0300933 if(atomic_dec_and_test(&dev->core->users))
Hans Verkuil7c9fc9d2009-04-01 03:49:59 -0300934 call_all(dev->core, tuner, s_standby);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
939static int
940video_mmap(struct file *file, struct vm_area_struct * vma)
941{
942 struct cx8800_fh *fh = file->private_data;
943
944 return videobuf_mmap_mapper(get_queue(fh), vma);
945}
946
947/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300948/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300950int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300952 struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 u32 value;
954 int i;
955
956 for (i = 0; i < CX8800_CTLS; i++)
957 if (cx8800_ctls[i].v.id == ctl->id)
958 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300959 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -EINVAL;
961
962 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
963 switch (ctl->id) {
964 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300965 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
966 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 case V4L2_CID_AUDIO_VOLUME:
969 ctl->value = 0x3f - (value & 0x3f);
970 break;
971 default:
972 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
973 break;
974 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300975 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
976 ctl->id, c->v.name, ctl->value, c->reg,
977 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
979}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300980EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300982int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200985 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300987
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200988 for (i = 0; i < CX8800_CTLS; i++) {
989 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200991 }
992 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300993 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -EINVAL;
995
996 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700997 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700999 ctl->value = c->v.maximum;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001000 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 switch (ctl->id) {
1002 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -03001003 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
1005 case V4L2_CID_AUDIO_VOLUME:
1006 value = 0x3f - (ctl->value & 0x3f);
1007 break;
1008 case V4L2_CID_SATURATION:
1009 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001010
1011 value = ((ctl->value - c->off) << c->shift) & c->mask;
1012
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001013 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001014 /* For SECAM, both U and V sat should be equal */
1015 value=value<<8|value;
1016 } else {
1017 /* Keeps U Saturation proportional to V Sat */
1018 value=(value*0x5a)/0x7f<<8|value;
1019 }
1020 mask=0xffff;
1021 break;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001022 case V4L2_CID_CHROMA_AGC:
1023 /* Do not allow chroma AGC to be enabled for SECAM */
1024 value = ((ctl->value - c->off) << c->shift) & c->mask;
1025 if (core->tvnorm & V4L2_STD_SECAM && value)
1026 return -EINVAL;
1027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 default:
1029 value = ((ctl->value - c->off) << c->shift) & c->mask;
1030 break;
1031 }
Ian Pickworth6457af52006-02-27 00:09:45 -03001032 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1033 ctl->id, c->v.name, ctl->value, c->reg, value,
1034 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001036 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001038 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040 return 0;
1041}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001042EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001044static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001046 struct v4l2_control ctrl;
1047 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001049 for (i = 0; i < CX8800_CTLS; i++) {
1050 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -03001051 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001052
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001053 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001058/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Hans Verkuil78b526a2008-05-28 12:16:41 -03001060static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001061 struct v4l2_format *f)
1062{
1063 struct cx8800_fh *fh = priv;
1064
1065 f->fmt.pix.width = fh->width;
1066 f->fmt.pix.height = fh->height;
1067 f->fmt.pix.field = fh->vidq.field;
1068 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1069 f->fmt.pix.bytesperline =
1070 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1071 f->fmt.pix.sizeimage =
1072 f->fmt.pix.height * f->fmt.pix.bytesperline;
1073 return 0;
1074}
1075
Hans Verkuil78b526a2008-05-28 12:16:41 -03001076static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct v4l2_format *f)
1078{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001079 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1080 struct cx8800_fmt *fmt;
1081 enum v4l2_field field;
1082 unsigned int maxw, maxh;
1083
1084 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1085 if (NULL == fmt)
1086 return -EINVAL;
1087
1088 field = f->fmt.pix.field;
1089 maxw = norm_maxw(core->tvnorm);
1090 maxh = norm_maxh(core->tvnorm);
1091
1092 if (V4L2_FIELD_ANY == field) {
1093 field = (f->fmt.pix.height > maxh/2)
1094 ? V4L2_FIELD_INTERLACED
1095 : V4L2_FIELD_BOTTOM;
1096 }
1097
1098 switch (field) {
1099 case V4L2_FIELD_TOP:
1100 case V4L2_FIELD_BOTTOM:
1101 maxh = maxh / 2;
1102 break;
1103 case V4L2_FIELD_INTERLACED:
1104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 default:
1106 return -EINVAL;
1107 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001108
1109 f->fmt.pix.field = field;
1110 if (f->fmt.pix.height < 32)
1111 f->fmt.pix.height = 32;
1112 if (f->fmt.pix.height > maxh)
1113 f->fmt.pix.height = maxh;
1114 if (f->fmt.pix.width < 48)
1115 f->fmt.pix.width = 48;
1116 if (f->fmt.pix.width > maxw)
1117 f->fmt.pix.width = maxw;
1118 f->fmt.pix.width &= ~0x03;
1119 f->fmt.pix.bytesperline =
1120 (f->fmt.pix.width * fmt->depth) >> 3;
1121 f->fmt.pix.sizeimage =
1122 f->fmt.pix.height * f->fmt.pix.bytesperline;
1123
1124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Hans Verkuil78b526a2008-05-28 12:16:41 -03001127static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001128 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001130 struct cx8800_fh *fh = priv;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001131 int err = vidioc_try_fmt_vid_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001132
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001133 if (0 != err)
1134 return err;
1135 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1136 fh->width = f->fmt.pix.width;
1137 fh->height = f->fmt.pix.height;
1138 fh->vidq.field = f->fmt.pix.field;
1139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001142static int vidioc_querycap (struct file *file, void *priv,
1143 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001145 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001148 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001149 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001150 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1151 cap->version = CX88_VERSION_CODE;
1152 cap->capabilities =
1153 V4L2_CAP_VIDEO_CAPTURE |
1154 V4L2_CAP_READWRITE |
1155 V4L2_CAP_STREAMING |
1156 V4L2_CAP_VBI_CAPTURE;
Trent Piepho6a59d642007-08-15 14:41:57 -03001157 if (UNSET != core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001158 cap->capabilities |= V4L2_CAP_TUNER;
1159 return 0;
1160}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001161
Hans Verkuil78b526a2008-05-28 12:16:41 -03001162static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001163 struct v4l2_fmtdesc *f)
1164{
1165 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1166 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001168 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1169 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001171 return 0;
1172}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001174#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001175static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001176{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001177 struct cx8800_fh *fh = priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001178
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001179 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001183static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001185 struct cx8800_fh *fh = priv;
1186 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001189static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1190{
1191 struct cx8800_fh *fh = priv;
1192 return (videobuf_querybuf(get_queue(fh), p));
1193}
1194
1195static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1196{
1197 struct cx8800_fh *fh = priv;
1198 return (videobuf_qbuf(get_queue(fh), p));
1199}
1200
1201static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1202{
1203 struct cx8800_fh *fh = priv;
1204 return (videobuf_dqbuf(get_queue(fh), p,
1205 file->f_flags & O_NONBLOCK));
1206}
1207
1208static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1209{
1210 struct cx8800_fh *fh = priv;
1211 struct cx8800_dev *dev = fh->dev;
1212
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001213 /* We should remember that this driver also supports teletext, */
1214 /* so we have to test if the v4l2_buf_type is VBI capture data. */
1215 if (unlikely((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1216 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001217 return -EINVAL;
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001218
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001219 if (unlikely(i != fh->type))
1220 return -EINVAL;
1221
1222 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1223 return -EBUSY;
1224 return videobuf_streamon(get_queue(fh));
1225}
1226
1227static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1228{
1229 struct cx8800_fh *fh = priv;
1230 struct cx8800_dev *dev = fh->dev;
1231 int err, res;
1232
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001233 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1234 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001235 return -EINVAL;
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001236
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001237 if (i != fh->type)
1238 return -EINVAL;
1239
1240 res = get_ressource(fh);
1241 err = videobuf_streamoff(get_queue(fh));
1242 if (err < 0)
1243 return err;
1244 res_free(dev,fh,res);
1245 return 0;
1246}
1247
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001248static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001249{
1250 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1251
1252 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001253 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001254 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001255
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001256 return 0;
1257}
1258
1259/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001260int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001261{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001262 static const char *iname[] = {
1263 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1264 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1265 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1266 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1267 [ CX88_VMUX_SVIDEO ] = "S-Video",
1268 [ CX88_VMUX_TELEVISION ] = "Television",
1269 [ CX88_VMUX_CABLE ] = "Cable TV",
1270 [ CX88_VMUX_DVB ] = "DVB",
1271 [ CX88_VMUX_DEBUG ] = "for debug only",
1272 };
Trent Piephof3334bc2009-03-04 01:21:03 -03001273 unsigned int n = i->index;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001274
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001275 if (n >= 4)
1276 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001277 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001278 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001279 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001280 strcpy(i->name,iname[INPUT(n).type]);
1281 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1282 (CX88_VMUX_CABLE == INPUT(n).type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001283 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001284 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001285 return 0;
1286}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001287EXPORT_SYMBOL(cx88_enum_input);
1288
1289static int vidioc_enum_input (struct file *file, void *priv,
1290 struct v4l2_input *i)
1291{
1292 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1293 return cx88_enum_input (core,i);
1294}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001295
1296static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1297{
1298 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1299
1300 *i = core->input;
1301 return 0;
1302}
1303
1304static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1305{
1306 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1307
1308 if (i >= 4)
1309 return -EINVAL;
1310
1311 mutex_lock(&core->lock);
1312 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001313 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001314 mutex_unlock(&core->lock);
1315 return 0;
1316}
1317
1318
1319
1320static int vidioc_queryctrl (struct file *file, void *priv,
1321 struct v4l2_queryctrl *qctrl)
1322{
Frej Drejhammar6d042032008-03-23 22:43:21 -03001323 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1324
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001325 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1326 if (unlikely(qctrl->id == 0))
1327 return -EINVAL;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001328 return cx8800_ctrl_query(core, qctrl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001329}
1330
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001331static int vidioc_g_ctrl (struct file *file, void *priv,
1332 struct v4l2_control *ctl)
1333{
1334 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1335 return
1336 cx88_get_control(core,ctl);
1337}
1338
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001339static int vidioc_s_ctrl (struct file *file, void *priv,
1340 struct v4l2_control *ctl)
1341{
1342 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001343 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001344 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001345}
1346
1347static int vidioc_g_tuner (struct file *file, void *priv,
1348 struct v4l2_tuner *t)
1349{
1350 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1351 u32 reg;
1352
Trent Piepho6a59d642007-08-15 14:41:57 -03001353 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001354 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001355 if (0 != t->index)
1356 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001357
1358 strcpy(t->name, "Television");
1359 t->type = V4L2_TUNER_ANALOG_TV;
1360 t->capability = V4L2_TUNER_CAP_NORM;
1361 t->rangehigh = 0xffffffffUL;
1362
1363 cx88_get_stereo(core ,t);
1364 reg = cx_read(MO_DEVICE_STATUS);
1365 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1366 return 0;
1367}
1368
1369static int vidioc_s_tuner (struct file *file, void *priv,
1370 struct v4l2_tuner *t)
1371{
1372 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1373
Trent Piepho6a59d642007-08-15 14:41:57 -03001374 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001375 return -EINVAL;
1376 if (0 != t->index)
1377 return -EINVAL;
1378
1379 cx88_set_stereo(core, t->audmode, 1);
1380 return 0;
1381}
1382
1383static int vidioc_g_frequency (struct file *file, void *priv,
1384 struct v4l2_frequency *f)
1385{
1386 struct cx8800_fh *fh = priv;
1387 struct cx88_core *core = fh->dev->core;
1388
Trent Piepho6a59d642007-08-15 14:41:57 -03001389 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001390 return -EINVAL;
1391
1392 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1393 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1394 f->frequency = core->freq;
1395
Hans Verkuilb8341e12009-03-29 08:26:01 -03001396 call_all(core, tuner, g_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001397
1398 return 0;
1399}
1400
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001401int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001402 struct v4l2_frequency *f)
1403{
Trent Piepho6a59d642007-08-15 14:41:57 -03001404 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001405 return -EINVAL;
1406 if (unlikely(f->tuner != 0))
1407 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001408
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001409 mutex_lock(&core->lock);
1410 core->freq = f->frequency;
1411 cx88_newstation(core);
Hans Verkuilb8341e12009-03-29 08:26:01 -03001412 call_all(core, tuner, s_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001413
1414 /* When changing channels it is required to reset TVAUDIO */
1415 msleep (10);
1416 cx88_set_tvaudio(core);
1417
1418 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001419
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001420 return 0;
1421}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001422EXPORT_SYMBOL(cx88_set_freq);
1423
1424static int vidioc_s_frequency (struct file *file, void *priv,
1425 struct v4l2_frequency *f)
1426{
1427 struct cx8800_fh *fh = priv;
1428 struct cx88_core *core = fh->dev->core;
1429
1430 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1431 return -EINVAL;
1432 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1433 return -EINVAL;
1434
1435 return
1436 cx88_set_freq (core,f);
1437}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001438
Trent Piephodbbff482007-01-22 23:31:53 -03001439#ifdef CONFIG_VIDEO_ADV_DEBUG
1440static int vidioc_g_register (struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001441 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001442{
1443 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1444
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001445 if (!v4l2_chip_match_host(&reg->match))
Trent Piephodbbff482007-01-22 23:31:53 -03001446 return -EINVAL;
1447 /* cx2388x has a 24-bit register space */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001448 reg->val = cx_read(reg->reg & 0xffffff);
1449 reg->size = 4;
Trent Piephodbbff482007-01-22 23:31:53 -03001450 return 0;
1451}
1452
1453static int vidioc_s_register (struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001454 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001455{
1456 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1457
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001458 if (!v4l2_chip_match_host(&reg->match))
Trent Piephodbbff482007-01-22 23:31:53 -03001459 return -EINVAL;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001460 cx_write(reg->reg & 0xffffff, reg->val);
Trent Piephodbbff482007-01-22 23:31:53 -03001461 return 0;
1462}
1463#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001466/* RADIO ESPECIFIC IOCTLS */
1467/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001469static int radio_querycap (struct file *file, void *priv,
1470 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001472 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 struct cx88_core *core = dev->core;
1474
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001475 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001476 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001477 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1478 cap->version = CX88_VERSION_CODE;
1479 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001483static int radio_g_tuner (struct file *file, void *priv,
1484 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001486 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1487
1488 if (unlikely(t->index > 0))
1489 return -EINVAL;
1490
1491 strcpy(t->name, "Radio");
1492 t->type = V4L2_TUNER_RADIO;
1493
Hans Verkuilb8341e12009-03-29 08:26:01 -03001494 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001495 return 0;
1496}
1497
1498static int radio_enum_input (struct file *file, void *priv,
1499 struct v4l2_input *i)
1500{
1501 if (i->index != 0)
1502 return -EINVAL;
1503 strcpy(i->name,"Radio");
1504 i->type = V4L2_INPUT_TYPE_TUNER;
1505
1506 return 0;
1507}
1508
1509static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1510{
1511 if (unlikely(a->index))
1512 return -EINVAL;
1513
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001514 strcpy(a->name,"Radio");
1515 return 0;
1516}
1517
1518/* FIXME: Should add a standard for radio */
1519
1520static int radio_s_tuner (struct file *file, void *priv,
1521 struct v4l2_tuner *t)
1522{
1523 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1524
1525 if (0 != t->index)
1526 return -EINVAL;
1527
Hans Verkuilb8341e12009-03-29 08:26:01 -03001528 call_all(core, tuner, s_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001529
1530 return 0;
1531}
1532
1533static int radio_s_audio (struct file *file, void *fh,
1534 struct v4l2_audio *a)
1535{
1536 return 0;
1537}
1538
1539static int radio_s_input (struct file *file, void *fh, unsigned int i)
1540{
1541 return 0;
1542}
1543
1544static int radio_queryctrl (struct file *file, void *priv,
1545 struct v4l2_queryctrl *c)
1546{
1547 int i;
1548
1549 if (c->id < V4L2_CID_BASE ||
1550 c->id >= V4L2_CID_LASTP1)
1551 return -EINVAL;
1552 if (c->id == V4L2_CID_AUDIO_MUTE) {
1553 for (i = 0; i < CX8800_CTLS; i++)
1554 if (cx8800_ctls[i].v.id == c->id)
1555 break;
1556 *c = cx8800_ctls[i].v;
1557 } else
1558 *c = no_ctl;
1559 return 0;
1560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562/* ----------------------------------------------------------- */
1563
1564static void cx8800_vid_timeout(unsigned long data)
1565{
1566 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1567 struct cx88_core *core = dev->core;
1568 struct cx88_dmaqueue *q = &dev->vidq;
1569 struct cx88_buffer *buf;
1570 unsigned long flags;
1571
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001572 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 cx_clear(MO_VID_DMACNTRL, 0x11);
1575 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1576
1577 spin_lock_irqsave(&dev->slock,flags);
1578 while (!list_empty(&q->active)) {
1579 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1580 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03001581 buf->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 wake_up(&buf->vb.done);
1583 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1584 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1585 }
1586 restart_video_queue(dev,q);
1587 spin_unlock_irqrestore(&dev->slock,flags);
1588}
1589
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001590static char *cx88_vid_irqs[32] = {
1591 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1592 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1593 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1594 "y_sync", "u_sync", "v_sync", "vbi_sync",
1595 "opc_err", "par_err", "rip_err", "pci_abort",
1596};
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598static void cx8800_vid_irq(struct cx8800_dev *dev)
1599{
1600 struct cx88_core *core = dev->core;
1601 u32 status, mask, count;
1602
1603 status = cx_read(MO_VID_INTSTAT);
1604 mask = cx_read(MO_VID_INTMSK);
1605 if (0 == (status & mask))
1606 return;
1607 cx_write(MO_VID_INTSTAT, status);
1608 if (irq_debug || (status & mask & ~0xff))
1609 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001610 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1611 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 /* risc op code error */
1614 if (status & (1 << 16)) {
1615 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1616 cx_clear(MO_VID_DMACNTRL, 0x11);
1617 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001618 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
1620
1621 /* risc1 y */
1622 if (status & 0x01) {
1623 spin_lock(&dev->slock);
1624 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001625 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 spin_unlock(&dev->slock);
1627 }
1628
1629 /* risc1 vbi */
1630 if (status & 0x08) {
1631 spin_lock(&dev->slock);
1632 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001633 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 spin_unlock(&dev->slock);
1635 }
1636
1637 /* risc2 y */
1638 if (status & 0x10) {
1639 dprintk(2,"stopper video\n");
1640 spin_lock(&dev->slock);
1641 restart_video_queue(dev,&dev->vidq);
1642 spin_unlock(&dev->slock);
1643 }
1644
1645 /* risc2 vbi */
1646 if (status & 0x80) {
1647 dprintk(2,"stopper vbi\n");
1648 spin_lock(&dev->slock);
1649 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1650 spin_unlock(&dev->slock);
1651 }
1652}
1653
David Howells7d12e782006-10-05 14:55:46 +01001654static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 struct cx8800_dev *dev = dev_id;
1657 struct cx88_core *core = dev->core;
1658 u32 status;
1659 int loop, handled = 0;
1660
1661 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001662 status = cx_read(MO_PCI_INTSTAT) &
1663 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (0 == status)
1665 goto out;
1666 cx_write(MO_PCI_INTSTAT, status);
1667 handled = 1;
1668
1669 if (status & core->pci_irqmask)
1670 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001671 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 cx8800_vid_irq(dev);
1673 };
1674 if (10 == loop) {
1675 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1676 core->name);
1677 cx_write(MO_PCI_INTMSK,0);
1678 }
1679
1680 out:
1681 return IRQ_RETVAL(handled);
1682}
1683
1684/* ----------------------------------------------------------- */
1685/* exported stuff */
1686
Hans Verkuilbec43662008-12-30 06:58:20 -03001687static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 .owner = THIS_MODULE,
1690 .open = video_open,
1691 .release = video_release,
1692 .read = video_read,
1693 .poll = video_poll,
1694 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001695 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696};
1697
Hans Verkuila3998102008-07-21 02:57:38 -03001698static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001699 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001700 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1701 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1702 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1703 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1704 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1705 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1706 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001707 .vidioc_reqbufs = vidioc_reqbufs,
1708 .vidioc_querybuf = vidioc_querybuf,
1709 .vidioc_qbuf = vidioc_qbuf,
1710 .vidioc_dqbuf = vidioc_dqbuf,
1711 .vidioc_s_std = vidioc_s_std,
1712 .vidioc_enum_input = vidioc_enum_input,
1713 .vidioc_g_input = vidioc_g_input,
1714 .vidioc_s_input = vidioc_s_input,
1715 .vidioc_queryctrl = vidioc_queryctrl,
1716 .vidioc_g_ctrl = vidioc_g_ctrl,
1717 .vidioc_s_ctrl = vidioc_s_ctrl,
1718 .vidioc_streamon = vidioc_streamon,
1719 .vidioc_streamoff = vidioc_streamoff,
1720#ifdef CONFIG_VIDEO_V4L1_COMPAT
1721 .vidiocgmbuf = vidiocgmbuf,
1722#endif
1723 .vidioc_g_tuner = vidioc_g_tuner,
1724 .vidioc_s_tuner = vidioc_s_tuner,
1725 .vidioc_g_frequency = vidioc_g_frequency,
1726 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001727#ifdef CONFIG_VIDEO_ADV_DEBUG
1728 .vidioc_g_register = vidioc_g_register,
1729 .vidioc_s_register = vidioc_s_register,
1730#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001731};
1732
1733static struct video_device cx8800_vbi_template;
1734
1735static struct video_device cx8800_video_template = {
1736 .name = "cx8800-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001737 .fops = &video_fops,
1738 .minor = -1,
1739 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001740 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001741 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742};
1743
Hans Verkuilbec43662008-12-30 06:58:20 -03001744static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
1746 .owner = THIS_MODULE,
1747 .open = video_open,
1748 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001749 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750};
1751
Hans Verkuila3998102008-07-21 02:57:38 -03001752static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001753 .vidioc_querycap = radio_querycap,
1754 .vidioc_g_tuner = radio_g_tuner,
1755 .vidioc_enum_input = radio_enum_input,
1756 .vidioc_g_audio = radio_g_audio,
1757 .vidioc_s_tuner = radio_s_tuner,
1758 .vidioc_s_audio = radio_s_audio,
1759 .vidioc_s_input = radio_s_input,
1760 .vidioc_queryctrl = radio_queryctrl,
1761 .vidioc_g_ctrl = vidioc_g_ctrl,
1762 .vidioc_s_ctrl = vidioc_s_ctrl,
1763 .vidioc_g_frequency = vidioc_g_frequency,
1764 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephoa75d2042007-08-01 00:13:28 -03001765#ifdef CONFIG_VIDEO_ADV_DEBUG
1766 .vidioc_g_register = vidioc_g_register,
1767 .vidioc_s_register = vidioc_s_register,
1768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769};
1770
Hans Verkuila3998102008-07-21 02:57:38 -03001771static struct video_device cx8800_radio_template = {
1772 .name = "cx8800-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001773 .fops = &radio_fops,
1774 .minor = -1,
1775 .ioctl_ops = &radio_ioctl_ops,
1776};
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/* ----------------------------------------------------------- */
1779
1780static void cx8800_unregister_video(struct cx8800_dev *dev)
1781{
1782 if (dev->radio_dev) {
1783 if (-1 != dev->radio_dev->minor)
1784 video_unregister_device(dev->radio_dev);
1785 else
1786 video_device_release(dev->radio_dev);
1787 dev->radio_dev = NULL;
1788 }
1789 if (dev->vbi_dev) {
1790 if (-1 != dev->vbi_dev->minor)
1791 video_unregister_device(dev->vbi_dev);
1792 else
1793 video_device_release(dev->vbi_dev);
1794 dev->vbi_dev = NULL;
1795 }
1796 if (dev->video_dev) {
1797 if (-1 != dev->video_dev->minor)
1798 video_unregister_device(dev->video_dev);
1799 else
1800 video_device_release(dev->video_dev);
1801 dev->video_dev = NULL;
1802 }
1803}
1804
1805static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1806 const struct pci_device_id *pci_id)
1807{
1808 struct cx8800_dev *dev;
1809 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 int err;
1812
Panagiotis Issaris74081872006-01-11 19:40:56 -02001813 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (NULL == dev)
1815 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 /* pci init */
1818 dev->pci = pci_dev;
1819 if (pci_enable_device(pci_dev)) {
1820 err = -EIO;
1821 goto fail_free;
1822 }
1823 core = cx88_core_get(dev->pci);
1824 if (NULL == core) {
1825 err = -EINVAL;
1826 goto fail_free;
1827 }
1828 dev->core = core;
1829
1830 /* print pci info */
1831 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001832 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1833 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001834 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001836 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 pci_set_master(pci_dev);
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -03001839 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1841 err = -EIO;
1842 goto fail_core;
1843 }
1844
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001845 /* Initialize VBI template */
1846 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1847 sizeof(cx8800_vbi_template) );
1848 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001852 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 /* init video dma queues */
1855 INIT_LIST_HEAD(&dev->vidq.active);
1856 INIT_LIST_HEAD(&dev->vidq.queued);
1857 dev->vidq.timeout.function = cx8800_vid_timeout;
1858 dev->vidq.timeout.data = (unsigned long)dev;
1859 init_timer(&dev->vidq.timeout);
1860 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1861 MO_VID_DMACNTRL,0x11,0x00);
1862
1863 /* init vbi dma queues */
1864 INIT_LIST_HEAD(&dev->vbiq.active);
1865 INIT_LIST_HEAD(&dev->vbiq.queued);
1866 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1867 dev->vbiq.timeout.data = (unsigned long)dev;
1868 init_timer(&dev->vbiq.timeout);
1869 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1870 MO_VID_DMACNTRL,0x88,0x00);
1871
1872 /* get irq */
1873 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001874 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001876 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 core->name,pci_dev->irq);
1878 goto fail_core;
1879 }
1880 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1881
1882 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001883
Hans Verkuil38f9d302008-07-23 05:09:15 -03001884 if (core->board.audio_chip == V4L2_IDENT_WM8775)
Hans Verkuile6574f22009-04-01 03:57:53 -03001885 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
Hans Verkuilb8341e12009-03-29 08:26:01 -03001886 "wm8775", "wm8775", 0x36 >> 1);
1887
1888 if (core->board.audio_chip == V4L2_IDENT_TVAUDIO) {
1889 /* This probes for a tda9874 as is used on some
1890 Pixelview Ultra boards. */
1891 static const unsigned short i2c_addr[] = {
1892 0xb0 >> 1, I2C_CLIENT_END
1893 };
1894
Hans Verkuile6574f22009-04-01 03:57:53 -03001895 v4l2_i2c_new_probed_subdev(&core->v4l2_dev, &core->i2c_adap,
Hans Verkuilb8341e12009-03-29 08:26:01 -03001896 "tvaudio", "tvaudio", i2c_addr);
1897 }
Steven Toth30579062006-09-25 12:43:42 -03001898
Michael Krufky6fcecce2007-08-24 01:32:31 -03001899 switch (core->boardnr) {
1900 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Hans Verkuilb8341e12009-03-29 08:26:01 -03001901 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
1902 static struct i2c_board_info rtc_info = {
1903 I2C_BOARD_INFO("isl1208", 0x6f)
1904 };
1905
Michael Krufky6fcecce2007-08-24 01:32:31 -03001906 request_module("rtc-isl1208");
Hans Verkuilb8341e12009-03-29 08:26:01 -03001907 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1908 }
Michael Krufky8efd2e22008-04-22 14:45:14 -03001909 /* break intentionally omitted */
1910 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1911 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001912 }
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /* register v4l devices */
1915 dev->video_dev = cx88_vdev_init(core,dev->pci,
1916 &cx8800_video_template,"video");
1917 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1918 video_nr[core->nr]);
1919 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001920 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 core->name);
1922 goto fail_unreg;
1923 }
1924 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001925 core->name, dev->video_dev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1928 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1929 vbi_nr[core->nr]);
1930 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001931 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 core->name);
1933 goto fail_unreg;
1934 }
1935 printk(KERN_INFO "%s/0: registered device vbi%d\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001936 core->name, dev->vbi_dev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Trent Piepho6a59d642007-08-15 14:41:57 -03001938 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1940 &cx8800_radio_template,"radio");
1941 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1942 radio_nr[core->nr]);
1943 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001944 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 core->name);
1946 goto fail_unreg;
1947 }
1948 printk(KERN_INFO "%s/0: registered device radio%d\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001949 core->name, dev->radio_dev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
1952 /* everything worked */
1953 list_add_tail(&dev->devlist,&cx8800_devlist);
1954 pci_set_drvdata(pci_dev,dev);
1955
1956 /* initial device configuration */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001957 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001958 cx88_set_tvnorm(core,core->tvnorm);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001959 init_controls(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001960 cx88_video_mux(core,0);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001961 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001964 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001966 if (IS_ERR(core->kthread)) {
1967 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001968 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1969 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001970 }
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return 0;
1973
1974fail_unreg:
1975 cx8800_unregister_video(dev);
1976 free_irq(pci_dev->irq, dev);
1977fail_core:
1978 cx88_core_put(core,dev->pci);
1979fail_free:
1980 kfree(dev);
1981 return err;
1982}
1983
1984static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1985{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001986 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001987 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001990 if (core->kthread) {
1991 kthread_stop(core->kthread);
1992 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994
Marton Balintb12203d2008-03-26 02:07:35 -03001995 if (core->ir)
1996 cx88_ir_stop(core, core->ir);
1997
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001998 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 pci_disable_device(pci_dev);
2000
2001 /* unregister stuff */
2002
2003 free_irq(pci_dev->irq, dev);
2004 cx8800_unregister_video(dev);
2005 pci_set_drvdata(pci_dev, NULL);
2006
2007 /* free memory */
2008 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2009 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002010 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 kfree(dev);
2012}
2013
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002014#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2016{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002017 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct cx88_core *core = dev->core;
2019
2020 /* stop video+vbi capture */
2021 spin_lock(&dev->slock);
2022 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002023 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 stop_video_dma(dev);
2025 del_timer(&dev->vidq.timeout);
2026 }
2027 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002028 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 cx8800_stop_vbi_dma(dev);
2030 del_timer(&dev->vbiq.timeout);
2031 }
2032 spin_unlock(&dev->slock);
2033
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002034 if (core->ir)
2035 cx88_ir_stop(core, core->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002037 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 pci_save_state(pci_dev);
2040 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2041 pci_disable_device(pci_dev);
2042 dev->state.disabled = 1;
2043 }
2044 return 0;
2045}
2046
2047static int cx8800_resume(struct pci_dev *pci_dev)
2048{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002049 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002051 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002054 err=pci_enable_device(pci_dev);
2055 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002056 printk(KERN_ERR "%s/0: can't enable device\n",
2057 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002058 return err;
2059 }
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 dev->state.disabled = 0;
2062 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002063 err= pci_set_power_state(pci_dev, PCI_D0);
2064 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002065 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002066 pci_disable_device(pci_dev);
2067 dev->state.disabled = 1;
2068
2069 return err;
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 pci_restore_state(pci_dev);
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002074 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002075 if (core->ir)
2076 cx88_ir_start(core, core->ir);
2077
2078 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /* restart video+vbi capture */
2081 spin_lock(&dev->slock);
2082 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002083 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 restart_video_queue(dev,&dev->vidq);
2085 }
2086 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002087 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2089 }
2090 spin_unlock(&dev->slock);
2091
2092 return 0;
2093}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096/* ----------------------------------------------------------- */
2097
Adrian Bunk408b6642005-05-01 08:59:29 -07002098static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 {
2100 .vendor = 0x14f1,
2101 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002102 .subvendor = PCI_ANY_ID,
2103 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 },{
2105 /* --- end of list --- */
2106 }
2107};
2108MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2109
2110static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002111 .name = "cx8800",
2112 .id_table = cx8800_pci_tbl,
2113 .probe = cx8800_initdev,
2114 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002115#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 .suspend = cx8800_suspend,
2117 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119};
2120
2121static int cx8800_init(void)
2122{
Trent Piepho5772f812007-08-15 14:41:59 -03002123 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 (CX88_VERSION_CODE >> 16) & 0xff,
2125 (CX88_VERSION_CODE >> 8) & 0xff,
2126 CX88_VERSION_CODE & 0xff);
2127#ifdef SNAPSHOT
2128 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2129 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2130#endif
2131 return pci_register_driver(&cx8800_pci_driver);
2132}
2133
2134static void cx8800_fini(void)
2135{
2136 pci_unregister_driver(&cx8800_pci_driver);
2137}
2138
2139module_init(cx8800_init);
2140module_exit(cx8800_fini);
2141
2142/* ----------------------------------------------------------- */
2143/*
2144 * Local variables:
2145 * c-basic-offset: 8
2146 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002147 * 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 -07002148 */