blob: ec0425d9043aee027649da17c60f97fee24fb4c6 [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) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300431 call_all(core, audio, s_routing,
432 INPUT(input).audioroute, 0, 0);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300433 }
Darron Broad430189d2008-10-15 14:18:42 -0300434 /* cx2388's C-ADC is connected to the tuner only.
435 When used with S-Video, that ADC is busy dealing with
436 chroma, so an external must be used for baseband audio */
437 if (INPUT(input).type != CX88_VMUX_TELEVISION ) {
438 /* "I2S ADC mode" */
439 core->tvaudio = WW_I2SADC;
440 cx88_set_tvaudio(core);
441 } else {
442 /* Normal mode */
443 cx_write(AUD_I2SCNTL, 0x0);
444 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
445 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300446 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0;
449}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300450EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452/* ------------------------------------------------------------------ */
453
454static int start_video_dma(struct cx8800_dev *dev,
455 struct cx88_dmaqueue *q,
456 struct cx88_buffer *buf)
457{
458 struct cx88_core *core = dev->core;
459
460 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700461 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700463 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
465
466 /* reset counter */
467 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
468 q->count = 1;
469
470 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300471 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700472
473 /* Enables corresponding bits at PCI_INT_STAT:
474 bits 0 to 4: video, audio, transport stream, VIP, Host
475 bit 7: timer
476 bits 8 and 9: DMA complete for: SRC, DST
477 bits 10 and 11: BERR signal asserted for RISC: RD, WR
478 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 cx_set(MO_VID_INTMSK, 0x0f0011);
481
482 /* enable capture */
483 cx_set(VID_CAPTURE_CONTROL,0x06);
484
485 /* start dma */
486 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700487 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 return 0;
490}
491
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300492#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int stop_video_dma(struct cx8800_dev *dev)
494{
495 struct cx88_core *core = dev->core;
496
497 /* stop dma */
498 cx_clear(MO_VID_DMACNTRL, 0x11);
499
500 /* disable capture */
501 cx_clear(VID_CAPTURE_CONTROL,0x06);
502
503 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300504 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 cx_clear(MO_VID_INTMSK, 0x0f0011);
506 return 0;
507}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300508#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510static int restart_video_queue(struct cx8800_dev *dev,
511 struct cx88_dmaqueue *q)
512{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700513 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct cx88_buffer *buf, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800517 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
519 buf, buf->vb.i);
520 start_video_dma(dev, q, buf);
Trent Piepho8bb629e22007-10-10 05:37:40 -0300521 list_for_each_entry(buf, &q->active, vb.queue)
522 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
524 return 0;
525 }
526
527 prev = NULL;
528 for (;;) {
529 if (list_empty(&q->queued))
530 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800531 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700533 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300535 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 buf->count = q->count++;
537 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
538 dprintk(2,"[%p/%d] restart_queue - first active\n",
539 buf,buf->vb.i);
540
541 } else if (prev->vb.width == buf->vb.width &&
542 prev->vb.height == buf->vb.height &&
543 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700544 list_move_tail(&buf->vb.queue, &q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300545 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 buf->count = q->count++;
547 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
548 dprintk(2,"[%p/%d] restart_queue - move to active\n",
549 buf,buf->vb.i);
550 } else {
551 return 0;
552 }
553 prev = buf;
554 }
555}
556
557/* ------------------------------------------------------------------ */
558
559static int
560buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
561{
562 struct cx8800_fh *fh = q->priv_data;
563
564 *size = fh->fmt->depth*fh->width*fh->height >> 3;
565 if (0 == *count)
566 *count = 32;
567 while (*size * *count > vid_limit * 1024 * 1024)
568 (*count)--;
569 return 0;
570}
571
572static int
573buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
574 enum v4l2_field field)
575{
576 struct cx8800_fh *fh = q->priv_data;
577 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700578 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300580 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int rc, init_buffer = 0;
582
583 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700584 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
585 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -EINVAL;
587 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
588 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
589 return -EINVAL;
590
591 if (buf->fmt != fh->fmt ||
592 buf->vb.width != fh->width ||
593 buf->vb.height != fh->height ||
594 buf->vb.field != field) {
595 buf->fmt = fh->fmt;
596 buf->vb.width = fh->width;
597 buf->vb.height = fh->height;
598 buf->vb.field = field;
599 init_buffer = 1;
600 }
601
Brandon Philips0fc06862007-11-06 20:02:36 -0300602 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300604 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto fail;
606 }
607
608 if (init_buffer) {
609 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
610 switch (buf->vb.field) {
611 case V4L2_FIELD_TOP:
612 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300613 dma->sglist, 0, UNSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 buf->bpl, 0, buf->vb.height);
615 break;
616 case V4L2_FIELD_BOTTOM:
617 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300618 dma->sglist, UNSET, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 buf->bpl, 0, buf->vb.height);
620 break;
621 case V4L2_FIELD_INTERLACED:
622 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300623 dma->sglist, 0, buf->bpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 buf->bpl, buf->bpl,
625 buf->vb.height >> 1);
626 break;
627 case V4L2_FIELD_SEQ_TB:
628 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300629 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 0, buf->bpl * (buf->vb.height >> 1),
631 buf->bpl, 0,
632 buf->vb.height >> 1);
633 break;
634 case V4L2_FIELD_SEQ_BT:
635 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300636 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 buf->bpl * (buf->vb.height >> 1), 0,
638 buf->bpl, 0,
639 buf->vb.height >> 1);
640 break;
641 default:
642 BUG();
643 }
644 }
645 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
646 buf, buf->vb.i,
647 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
648 (unsigned long)buf->risc.dma);
649
Brandon Philips0fc06862007-11-06 20:02:36 -0300650 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652
653 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300654 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return rc;
656}
657
658static void
659buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
660{
661 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
662 struct cx88_buffer *prev;
663 struct cx8800_fh *fh = vq->priv_data;
664 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700665 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct cx88_dmaqueue *q = &dev->vidq;
667
668 /* add jump to stopper */
669 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
670 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
671
672 if (!list_empty(&q->queued)) {
673 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300674 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
676 buf, buf->vb.i);
677
678 } else if (list_empty(&q->active)) {
679 list_add_tail(&buf->vb.queue,&q->active);
680 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300681 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 buf->count = q->count++;
683 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
684 dprintk(2,"[%p/%d] buffer_queue - first active\n",
685 buf, buf->vb.i);
686
687 } else {
688 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
689 if (prev->vb.width == buf->vb.width &&
690 prev->vb.height == buf->vb.height &&
691 prev->fmt == buf->fmt) {
692 list_add_tail(&buf->vb.queue,&q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300693 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 buf->count = q->count++;
695 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
696 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
697 buf, buf->vb.i);
698
699 } else {
700 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300701 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
703 buf, buf->vb.i);
704 }
705 }
706}
707
708static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
709{
710 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300712 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Adrian Bunk408b6642005-05-01 08:59:29 -0700715static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 .buf_setup = buffer_setup,
717 .buf_prepare = buffer_prepare,
718 .buf_queue = buffer_queue,
719 .buf_release = buffer_release,
720};
721
722/* ------------------------------------------------------------------ */
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725/* ------------------------------------------------------------------ */
726
727static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
728{
729 switch (fh->type) {
730 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
731 return &fh->vidq;
732 case V4L2_BUF_TYPE_VBI_CAPTURE:
733 return &fh->vbiq;
734 default:
735 BUG();
736 return NULL;
737 }
738}
739
740static int get_ressource(struct cx8800_fh *fh)
741{
742 switch (fh->type) {
743 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
744 return RESOURCE_VIDEO;
745 case V4L2_BUF_TYPE_VBI_CAPTURE:
746 return RESOURCE_VBI;
747 default:
748 BUG();
749 return 0;
750 }
751}
752
Hans Verkuilbec43662008-12-30 06:58:20 -0300753static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Hans Verkuilbec43662008-12-30 06:58:20 -0300755 int minor = video_devdata(file)->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700757 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct cx8800_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 enum v4l2_buf_type type = 0;
760 int radio = 0;
761
Hans Verkuild56dc612008-07-30 08:43:36 -0300762 lock_kernel();
Trent Piepho8bb629e22007-10-10 05:37:40 -0300763 list_for_each_entry(h, &cx8800_devlist, devlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (h->video_dev->minor == minor) {
765 dev = h;
766 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
767 }
768 if (h->vbi_dev->minor == minor) {
769 dev = h;
770 type = V4L2_BUF_TYPE_VBI_CAPTURE;
771 }
772 if (h->radio_dev &&
773 h->radio_dev->minor == minor) {
774 radio = 1;
775 dev = h;
776 }
777 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300778 if (NULL == dev) {
779 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -0300781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700783 core = dev->core;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 dprintk(1,"open minor=%d radio=%d type=%s\n",
786 minor,radio,v4l2_type_names[type]);
787
788 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200789 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -0300790 if (NULL == fh) {
791 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -0300793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 file->private_data = fh;
795 fh->dev = dev;
796 fh->radio = radio;
797 fh->type = type;
798 fh->width = 320;
799 fh->height = 240;
800 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
801
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300802 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
803 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 V4L2_BUF_TYPE_VIDEO_CAPTURE,
805 V4L2_FIELD_INTERLACED,
806 sizeof(struct cx88_buffer),
807 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300808 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
809 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 V4L2_BUF_TYPE_VBI_CAPTURE,
811 V4L2_FIELD_SEQ_TB,
812 sizeof(struct cx88_buffer),
813 fh);
814
815 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 dprintk(1,"video_open: setting radio device\n");
Trent Piepho6a59d642007-08-15 14:41:57 -0300817 cx_write(MO_GP3_IO, core->board.radio.gpio3);
818 cx_write(MO_GP0_IO, core->board.radio.gpio0);
819 cx_write(MO_GP1_IO, core->board.radio.gpio1);
820 cx_write(MO_GP2_IO, core->board.radio.gpio2);
Darron Broad430189d2008-10-15 14:18:42 -0300821 if (core->board.radio.audioroute) {
822 if(core->board.audio_chip &&
823 core->board.audio_chip == V4L2_IDENT_WM8775) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300824 call_all(core, audio, s_routing,
825 core->board.radio.audioroute, 0, 0);
Darron Broad430189d2008-10-15 14:18:42 -0300826 }
827 /* "I2S ADC mode" */
828 core->tvaudio = WW_I2SADC;
829 cx88_set_tvaudio(core);
830 } else {
831 /* FM Mode */
832 core->tvaudio = WW_FM;
833 cx88_set_tvaudio(core);
834 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
835 }
Hans Verkuilb8341e12009-03-29 08:26:01 -0300836 call_all(core, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300838 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Darron Broad3e010842008-09-25 16:51:11 -0300840 atomic_inc(&core->users);
841
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800846video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct cx8800_fh *fh = file->private_data;
849
850 switch (fh->type) {
851 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
852 if (res_locked(fh->dev,RESOURCE_VIDEO))
853 return -EBUSY;
854 return videobuf_read_one(&fh->vidq, data, count, ppos,
855 file->f_flags & O_NONBLOCK);
856 case V4L2_BUF_TYPE_VBI_CAPTURE:
857 if (!res_get(fh->dev,fh,RESOURCE_VBI))
858 return -EBUSY;
859 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
860 file->f_flags & O_NONBLOCK);
861 default:
862 BUG();
863 return 0;
864 }
865}
866
867static unsigned int
868video_poll(struct file *file, struct poll_table_struct *wait)
869{
870 struct cx8800_fh *fh = file->private_data;
871 struct cx88_buffer *buf;
872
873 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
874 if (!res_get(fh->dev,fh,RESOURCE_VBI))
875 return POLLERR;
876 return videobuf_poll_stream(file, &fh->vbiq, wait);
877 }
878
879 if (res_check(fh,RESOURCE_VIDEO)) {
880 /* streaming capture */
881 if (list_empty(&fh->vidq.stream))
882 return POLLERR;
883 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
884 } else {
885 /* read() capture */
886 buf = (struct cx88_buffer*)fh->vidq.read_buf;
887 if (NULL == buf)
888 return POLLERR;
889 }
890 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300891 if (buf->vb.state == VIDEOBUF_DONE ||
892 buf->vb.state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return POLLIN|POLLRDNORM;
894 return 0;
895}
896
Hans Verkuilbec43662008-12-30 06:58:20 -0300897static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct cx8800_fh *fh = file->private_data;
900 struct cx8800_dev *dev = fh->dev;
901
902 /* turn off overlay */
903 if (res_check(fh, RESOURCE_OVERLAY)) {
904 /* FIXME */
905 res_free(dev,fh,RESOURCE_OVERLAY);
906 }
907
908 /* stop video capture */
909 if (res_check(fh, RESOURCE_VIDEO)) {
910 videobuf_queue_cancel(&fh->vidq);
911 res_free(dev,fh,RESOURCE_VIDEO);
912 }
913 if (fh->vidq.read_buf) {
914 buffer_release(&fh->vidq,fh->vidq.read_buf);
915 kfree(fh->vidq.read_buf);
916 }
917
918 /* stop vbi capture */
919 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -0300920 videobuf_stop(&fh->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 res_free(dev,fh,RESOURCE_VBI);
922 }
923
924 videobuf_mmap_free(&fh->vidq);
925 videobuf_mmap_free(&fh->vbiq);
926 file->private_data = NULL;
927 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700928
Darron Broad3e010842008-09-25 16:51:11 -0300929 if(atomic_dec_and_test(&dev->core->users))
Hans Verkuil7c9fc9d2009-04-01 03:49:59 -0300930 call_all(dev->core, tuner, s_standby);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933}
934
935static int
936video_mmap(struct file *file, struct vm_area_struct * vma)
937{
938 struct cx8800_fh *fh = file->private_data;
939
940 return videobuf_mmap_mapper(get_queue(fh), vma);
941}
942
943/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300944/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300946int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300948 struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 u32 value;
950 int i;
951
952 for (i = 0; i < CX8800_CTLS; i++)
953 if (cx8800_ctls[i].v.id == ctl->id)
954 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300955 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -EINVAL;
957
958 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
959 switch (ctl->id) {
960 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300961 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
962 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
964 case V4L2_CID_AUDIO_VOLUME:
965 ctl->value = 0x3f - (value & 0x3f);
966 break;
967 default:
968 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
969 break;
970 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300971 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
972 ctl->id, c->v.name, ctl->value, c->reg,
973 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300976EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300978int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200981 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300983
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200984 for (i = 0; i < CX8800_CTLS; i++) {
985 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200987 }
988 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300989 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -EINVAL;
991
992 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700993 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700995 ctl->value = c->v.maximum;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200996 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 switch (ctl->id) {
998 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300999 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 break;
1001 case V4L2_CID_AUDIO_VOLUME:
1002 value = 0x3f - (ctl->value & 0x3f);
1003 break;
1004 case V4L2_CID_SATURATION:
1005 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001006
1007 value = ((ctl->value - c->off) << c->shift) & c->mask;
1008
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001009 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001010 /* For SECAM, both U and V sat should be equal */
1011 value=value<<8|value;
1012 } else {
1013 /* Keeps U Saturation proportional to V Sat */
1014 value=(value*0x5a)/0x7f<<8|value;
1015 }
1016 mask=0xffff;
1017 break;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001018 case V4L2_CID_CHROMA_AGC:
1019 /* Do not allow chroma AGC to be enabled for SECAM */
1020 value = ((ctl->value - c->off) << c->shift) & c->mask;
1021 if (core->tvnorm & V4L2_STD_SECAM && value)
1022 return -EINVAL;
1023 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 default:
1025 value = ((ctl->value - c->off) << c->shift) & c->mask;
1026 break;
1027 }
Ian Pickworth6457af52006-02-27 00:09:45 -03001028 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1029 ctl->id, c->v.name, ctl->value, c->reg, value,
1030 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001032 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001034 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036 return 0;
1037}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001038EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001040static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001042 struct v4l2_control ctrl;
1043 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001045 for (i = 0; i < CX8800_CTLS; i++) {
1046 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -03001047 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001048
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001049 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001054/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Hans Verkuil78b526a2008-05-28 12:16:41 -03001056static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001057 struct v4l2_format *f)
1058{
1059 struct cx8800_fh *fh = priv;
1060
1061 f->fmt.pix.width = fh->width;
1062 f->fmt.pix.height = fh->height;
1063 f->fmt.pix.field = fh->vidq.field;
1064 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1065 f->fmt.pix.bytesperline =
1066 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1067 f->fmt.pix.sizeimage =
1068 f->fmt.pix.height * f->fmt.pix.bytesperline;
1069 return 0;
1070}
1071
Hans Verkuil78b526a2008-05-28 12:16:41 -03001072static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct v4l2_format *f)
1074{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001075 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1076 struct cx8800_fmt *fmt;
1077 enum v4l2_field field;
1078 unsigned int maxw, maxh;
1079
1080 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1081 if (NULL == fmt)
1082 return -EINVAL;
1083
1084 field = f->fmt.pix.field;
1085 maxw = norm_maxw(core->tvnorm);
1086 maxh = norm_maxh(core->tvnorm);
1087
1088 if (V4L2_FIELD_ANY == field) {
1089 field = (f->fmt.pix.height > maxh/2)
1090 ? V4L2_FIELD_INTERLACED
1091 : V4L2_FIELD_BOTTOM;
1092 }
1093
1094 switch (field) {
1095 case V4L2_FIELD_TOP:
1096 case V4L2_FIELD_BOTTOM:
1097 maxh = maxh / 2;
1098 break;
1099 case V4L2_FIELD_INTERLACED:
1100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 default:
1102 return -EINVAL;
1103 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001104
1105 f->fmt.pix.field = field;
1106 if (f->fmt.pix.height < 32)
1107 f->fmt.pix.height = 32;
1108 if (f->fmt.pix.height > maxh)
1109 f->fmt.pix.height = maxh;
1110 if (f->fmt.pix.width < 48)
1111 f->fmt.pix.width = 48;
1112 if (f->fmt.pix.width > maxw)
1113 f->fmt.pix.width = maxw;
1114 f->fmt.pix.width &= ~0x03;
1115 f->fmt.pix.bytesperline =
1116 (f->fmt.pix.width * fmt->depth) >> 3;
1117 f->fmt.pix.sizeimage =
1118 f->fmt.pix.height * f->fmt.pix.bytesperline;
1119
1120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Hans Verkuil78b526a2008-05-28 12:16:41 -03001123static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001124 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001126 struct cx8800_fh *fh = priv;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001127 int err = vidioc_try_fmt_vid_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001128
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001129 if (0 != err)
1130 return err;
1131 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1132 fh->width = f->fmt.pix.width;
1133 fh->height = f->fmt.pix.height;
1134 fh->vidq.field = f->fmt.pix.field;
1135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001138static int vidioc_querycap (struct file *file, void *priv,
1139 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001141 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001144 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001145 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001146 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1147 cap->version = CX88_VERSION_CODE;
1148 cap->capabilities =
1149 V4L2_CAP_VIDEO_CAPTURE |
1150 V4L2_CAP_READWRITE |
1151 V4L2_CAP_STREAMING |
1152 V4L2_CAP_VBI_CAPTURE;
Trent Piepho6a59d642007-08-15 14:41:57 -03001153 if (UNSET != core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001154 cap->capabilities |= V4L2_CAP_TUNER;
1155 return 0;
1156}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001157
Hans Verkuil78b526a2008-05-28 12:16:41 -03001158static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001159 struct v4l2_fmtdesc *f)
1160{
1161 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1162 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001164 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1165 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001167 return 0;
1168}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001170#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001171static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001172{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001173 struct cx8800_fh *fh = priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001174
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001175 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001179static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001181 struct cx8800_fh *fh = priv;
1182 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001185static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1186{
1187 struct cx8800_fh *fh = priv;
1188 return (videobuf_querybuf(get_queue(fh), p));
1189}
1190
1191static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1192{
1193 struct cx8800_fh *fh = priv;
1194 return (videobuf_qbuf(get_queue(fh), p));
1195}
1196
1197static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1198{
1199 struct cx8800_fh *fh = priv;
1200 return (videobuf_dqbuf(get_queue(fh), p,
1201 file->f_flags & O_NONBLOCK));
1202}
1203
1204static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1205{
1206 struct cx8800_fh *fh = priv;
1207 struct cx8800_dev *dev = fh->dev;
1208
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001209 /* We should remember that this driver also supports teletext, */
1210 /* so we have to test if the v4l2_buf_type is VBI capture data. */
1211 if (unlikely((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1212 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001213 return -EINVAL;
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001214
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001215 if (unlikely(i != fh->type))
1216 return -EINVAL;
1217
1218 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1219 return -EBUSY;
1220 return videobuf_streamon(get_queue(fh));
1221}
1222
1223static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1224{
1225 struct cx8800_fh *fh = priv;
1226 struct cx8800_dev *dev = fh->dev;
1227 int err, res;
1228
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001229 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1230 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001231 return -EINVAL;
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001232
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001233 if (i != fh->type)
1234 return -EINVAL;
1235
1236 res = get_ressource(fh);
1237 err = videobuf_streamoff(get_queue(fh));
1238 if (err < 0)
1239 return err;
1240 res_free(dev,fh,res);
1241 return 0;
1242}
1243
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001244static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001245{
1246 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1247
1248 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001249 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001250 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001251
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001252 return 0;
1253}
1254
1255/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001256int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001257{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001258 static const char *iname[] = {
1259 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1260 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1261 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1262 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1263 [ CX88_VMUX_SVIDEO ] = "S-Video",
1264 [ CX88_VMUX_TELEVISION ] = "Television",
1265 [ CX88_VMUX_CABLE ] = "Cable TV",
1266 [ CX88_VMUX_DVB ] = "DVB",
1267 [ CX88_VMUX_DEBUG ] = "for debug only",
1268 };
Trent Piephof3334bc2009-03-04 01:21:03 -03001269 unsigned int n = i->index;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001270
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001271 if (n >= 4)
1272 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001273 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001274 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001275 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001276 strcpy(i->name,iname[INPUT(n).type]);
1277 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1278 (CX88_VMUX_CABLE == INPUT(n).type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001279 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001280 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001281 return 0;
1282}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001283EXPORT_SYMBOL(cx88_enum_input);
1284
1285static int vidioc_enum_input (struct file *file, void *priv,
1286 struct v4l2_input *i)
1287{
1288 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1289 return cx88_enum_input (core,i);
1290}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001291
1292static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1293{
1294 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1295
1296 *i = core->input;
1297 return 0;
1298}
1299
1300static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1301{
1302 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1303
1304 if (i >= 4)
1305 return -EINVAL;
1306
1307 mutex_lock(&core->lock);
1308 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001309 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001310 mutex_unlock(&core->lock);
1311 return 0;
1312}
1313
1314
1315
1316static int vidioc_queryctrl (struct file *file, void *priv,
1317 struct v4l2_queryctrl *qctrl)
1318{
Frej Drejhammar6d042032008-03-23 22:43:21 -03001319 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1320
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001321 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1322 if (unlikely(qctrl->id == 0))
1323 return -EINVAL;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001324 return cx8800_ctrl_query(core, qctrl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001325}
1326
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001327static int vidioc_g_ctrl (struct file *file, void *priv,
1328 struct v4l2_control *ctl)
1329{
1330 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1331 return
1332 cx88_get_control(core,ctl);
1333}
1334
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001335static int vidioc_s_ctrl (struct file *file, void *priv,
1336 struct v4l2_control *ctl)
1337{
1338 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001339 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001340 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001341}
1342
1343static int vidioc_g_tuner (struct file *file, void *priv,
1344 struct v4l2_tuner *t)
1345{
1346 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1347 u32 reg;
1348
Trent Piepho6a59d642007-08-15 14:41:57 -03001349 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001350 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001351 if (0 != t->index)
1352 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001353
1354 strcpy(t->name, "Television");
1355 t->type = V4L2_TUNER_ANALOG_TV;
1356 t->capability = V4L2_TUNER_CAP_NORM;
1357 t->rangehigh = 0xffffffffUL;
1358
1359 cx88_get_stereo(core ,t);
1360 reg = cx_read(MO_DEVICE_STATUS);
1361 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1362 return 0;
1363}
1364
1365static int vidioc_s_tuner (struct file *file, void *priv,
1366 struct v4l2_tuner *t)
1367{
1368 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1369
Trent Piepho6a59d642007-08-15 14:41:57 -03001370 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001371 return -EINVAL;
1372 if (0 != t->index)
1373 return -EINVAL;
1374
1375 cx88_set_stereo(core, t->audmode, 1);
1376 return 0;
1377}
1378
1379static int vidioc_g_frequency (struct file *file, void *priv,
1380 struct v4l2_frequency *f)
1381{
1382 struct cx8800_fh *fh = priv;
1383 struct cx88_core *core = fh->dev->core;
1384
Trent Piepho6a59d642007-08-15 14:41:57 -03001385 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001386 return -EINVAL;
1387
1388 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1389 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1390 f->frequency = core->freq;
1391
Hans Verkuilb8341e12009-03-29 08:26:01 -03001392 call_all(core, tuner, g_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001393
1394 return 0;
1395}
1396
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001397int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001398 struct v4l2_frequency *f)
1399{
Trent Piepho6a59d642007-08-15 14:41:57 -03001400 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001401 return -EINVAL;
1402 if (unlikely(f->tuner != 0))
1403 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001404
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001405 mutex_lock(&core->lock);
1406 core->freq = f->frequency;
1407 cx88_newstation(core);
Hans Verkuilb8341e12009-03-29 08:26:01 -03001408 call_all(core, tuner, s_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001409
1410 /* When changing channels it is required to reset TVAUDIO */
1411 msleep (10);
1412 cx88_set_tvaudio(core);
1413
1414 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001415
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001416 return 0;
1417}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001418EXPORT_SYMBOL(cx88_set_freq);
1419
1420static int vidioc_s_frequency (struct file *file, void *priv,
1421 struct v4l2_frequency *f)
1422{
1423 struct cx8800_fh *fh = priv;
1424 struct cx88_core *core = fh->dev->core;
1425
1426 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1427 return -EINVAL;
1428 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1429 return -EINVAL;
1430
1431 return
1432 cx88_set_freq (core,f);
1433}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001434
Trent Piephodbbff482007-01-22 23:31:53 -03001435#ifdef CONFIG_VIDEO_ADV_DEBUG
1436static int vidioc_g_register (struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001437 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001438{
1439 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1440
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001441 if (!v4l2_chip_match_host(&reg->match))
Trent Piephodbbff482007-01-22 23:31:53 -03001442 return -EINVAL;
1443 /* cx2388x has a 24-bit register space */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001444 reg->val = cx_read(reg->reg & 0xffffff);
1445 reg->size = 4;
Trent Piephodbbff482007-01-22 23:31:53 -03001446 return 0;
1447}
1448
1449static int vidioc_s_register (struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001450 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001451{
1452 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1453
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001454 if (!v4l2_chip_match_host(&reg->match))
Trent Piephodbbff482007-01-22 23:31:53 -03001455 return -EINVAL;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001456 cx_write(reg->reg & 0xffffff, reg->val);
Trent Piephodbbff482007-01-22 23:31:53 -03001457 return 0;
1458}
1459#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001462/* RADIO ESPECIFIC IOCTLS */
1463/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001465static int radio_querycap (struct file *file, void *priv,
1466 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001468 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct cx88_core *core = dev->core;
1470
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001471 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001472 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001473 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1474 cap->version = CX88_VERSION_CODE;
1475 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001479static int radio_g_tuner (struct file *file, void *priv,
1480 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001482 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1483
1484 if (unlikely(t->index > 0))
1485 return -EINVAL;
1486
1487 strcpy(t->name, "Radio");
1488 t->type = V4L2_TUNER_RADIO;
1489
Hans Verkuilb8341e12009-03-29 08:26:01 -03001490 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001491 return 0;
1492}
1493
1494static int radio_enum_input (struct file *file, void *priv,
1495 struct v4l2_input *i)
1496{
1497 if (i->index != 0)
1498 return -EINVAL;
1499 strcpy(i->name,"Radio");
1500 i->type = V4L2_INPUT_TYPE_TUNER;
1501
1502 return 0;
1503}
1504
1505static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1506{
1507 if (unlikely(a->index))
1508 return -EINVAL;
1509
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001510 strcpy(a->name,"Radio");
1511 return 0;
1512}
1513
1514/* FIXME: Should add a standard for radio */
1515
1516static int radio_s_tuner (struct file *file, void *priv,
1517 struct v4l2_tuner *t)
1518{
1519 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1520
1521 if (0 != t->index)
1522 return -EINVAL;
1523
Hans Verkuilb8341e12009-03-29 08:26:01 -03001524 call_all(core, tuner, s_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001525
1526 return 0;
1527}
1528
1529static int radio_s_audio (struct file *file, void *fh,
1530 struct v4l2_audio *a)
1531{
1532 return 0;
1533}
1534
1535static int radio_s_input (struct file *file, void *fh, unsigned int i)
1536{
1537 return 0;
1538}
1539
1540static int radio_queryctrl (struct file *file, void *priv,
1541 struct v4l2_queryctrl *c)
1542{
1543 int i;
1544
1545 if (c->id < V4L2_CID_BASE ||
1546 c->id >= V4L2_CID_LASTP1)
1547 return -EINVAL;
1548 if (c->id == V4L2_CID_AUDIO_MUTE) {
1549 for (i = 0; i < CX8800_CTLS; i++)
1550 if (cx8800_ctls[i].v.id == c->id)
1551 break;
1552 *c = cx8800_ctls[i].v;
1553 } else
1554 *c = no_ctl;
1555 return 0;
1556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558/* ----------------------------------------------------------- */
1559
1560static void cx8800_vid_timeout(unsigned long data)
1561{
1562 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1563 struct cx88_core *core = dev->core;
1564 struct cx88_dmaqueue *q = &dev->vidq;
1565 struct cx88_buffer *buf;
1566 unsigned long flags;
1567
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001568 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 cx_clear(MO_VID_DMACNTRL, 0x11);
1571 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1572
1573 spin_lock_irqsave(&dev->slock,flags);
1574 while (!list_empty(&q->active)) {
1575 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1576 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03001577 buf->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 wake_up(&buf->vb.done);
1579 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1580 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1581 }
1582 restart_video_queue(dev,q);
1583 spin_unlock_irqrestore(&dev->slock,flags);
1584}
1585
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001586static char *cx88_vid_irqs[32] = {
1587 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1588 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1589 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1590 "y_sync", "u_sync", "v_sync", "vbi_sync",
1591 "opc_err", "par_err", "rip_err", "pci_abort",
1592};
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594static void cx8800_vid_irq(struct cx8800_dev *dev)
1595{
1596 struct cx88_core *core = dev->core;
1597 u32 status, mask, count;
1598
1599 status = cx_read(MO_VID_INTSTAT);
1600 mask = cx_read(MO_VID_INTMSK);
1601 if (0 == (status & mask))
1602 return;
1603 cx_write(MO_VID_INTSTAT, status);
1604 if (irq_debug || (status & mask & ~0xff))
1605 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001606 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1607 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 /* risc op code error */
1610 if (status & (1 << 16)) {
1611 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1612 cx_clear(MO_VID_DMACNTRL, 0x11);
1613 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001614 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616
1617 /* risc1 y */
1618 if (status & 0x01) {
1619 spin_lock(&dev->slock);
1620 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001621 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 spin_unlock(&dev->slock);
1623 }
1624
1625 /* risc1 vbi */
1626 if (status & 0x08) {
1627 spin_lock(&dev->slock);
1628 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001629 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 spin_unlock(&dev->slock);
1631 }
1632
1633 /* risc2 y */
1634 if (status & 0x10) {
1635 dprintk(2,"stopper video\n");
1636 spin_lock(&dev->slock);
1637 restart_video_queue(dev,&dev->vidq);
1638 spin_unlock(&dev->slock);
1639 }
1640
1641 /* risc2 vbi */
1642 if (status & 0x80) {
1643 dprintk(2,"stopper vbi\n");
1644 spin_lock(&dev->slock);
1645 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1646 spin_unlock(&dev->slock);
1647 }
1648}
1649
David Howells7d12e782006-10-05 14:55:46 +01001650static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 struct cx8800_dev *dev = dev_id;
1653 struct cx88_core *core = dev->core;
1654 u32 status;
1655 int loop, handled = 0;
1656
1657 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001658 status = cx_read(MO_PCI_INTSTAT) &
1659 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (0 == status)
1661 goto out;
1662 cx_write(MO_PCI_INTSTAT, status);
1663 handled = 1;
1664
1665 if (status & core->pci_irqmask)
1666 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001667 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 cx8800_vid_irq(dev);
1669 };
1670 if (10 == loop) {
1671 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1672 core->name);
1673 cx_write(MO_PCI_INTMSK,0);
1674 }
1675
1676 out:
1677 return IRQ_RETVAL(handled);
1678}
1679
1680/* ----------------------------------------------------------- */
1681/* exported stuff */
1682
Hans Verkuilbec43662008-12-30 06:58:20 -03001683static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 .owner = THIS_MODULE,
1686 .open = video_open,
1687 .release = video_release,
1688 .read = video_read,
1689 .poll = video_poll,
1690 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001691 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692};
1693
Hans Verkuila3998102008-07-21 02:57:38 -03001694static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001695 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001696 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1697 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1698 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1699 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1700 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1701 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1702 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001703 .vidioc_reqbufs = vidioc_reqbufs,
1704 .vidioc_querybuf = vidioc_querybuf,
1705 .vidioc_qbuf = vidioc_qbuf,
1706 .vidioc_dqbuf = vidioc_dqbuf,
1707 .vidioc_s_std = vidioc_s_std,
1708 .vidioc_enum_input = vidioc_enum_input,
1709 .vidioc_g_input = vidioc_g_input,
1710 .vidioc_s_input = vidioc_s_input,
1711 .vidioc_queryctrl = vidioc_queryctrl,
1712 .vidioc_g_ctrl = vidioc_g_ctrl,
1713 .vidioc_s_ctrl = vidioc_s_ctrl,
1714 .vidioc_streamon = vidioc_streamon,
1715 .vidioc_streamoff = vidioc_streamoff,
1716#ifdef CONFIG_VIDEO_V4L1_COMPAT
1717 .vidiocgmbuf = vidiocgmbuf,
1718#endif
1719 .vidioc_g_tuner = vidioc_g_tuner,
1720 .vidioc_s_tuner = vidioc_s_tuner,
1721 .vidioc_g_frequency = vidioc_g_frequency,
1722 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001723#ifdef CONFIG_VIDEO_ADV_DEBUG
1724 .vidioc_g_register = vidioc_g_register,
1725 .vidioc_s_register = vidioc_s_register,
1726#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001727};
1728
1729static struct video_device cx8800_vbi_template;
1730
1731static struct video_device cx8800_video_template = {
1732 .name = "cx8800-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001733 .fops = &video_fops,
1734 .minor = -1,
1735 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001736 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001737 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738};
1739
Hans Verkuilbec43662008-12-30 06:58:20 -03001740static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 .owner = THIS_MODULE,
1743 .open = video_open,
1744 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001745 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746};
1747
Hans Verkuila3998102008-07-21 02:57:38 -03001748static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001749 .vidioc_querycap = radio_querycap,
1750 .vidioc_g_tuner = radio_g_tuner,
1751 .vidioc_enum_input = radio_enum_input,
1752 .vidioc_g_audio = radio_g_audio,
1753 .vidioc_s_tuner = radio_s_tuner,
1754 .vidioc_s_audio = radio_s_audio,
1755 .vidioc_s_input = radio_s_input,
1756 .vidioc_queryctrl = radio_queryctrl,
1757 .vidioc_g_ctrl = vidioc_g_ctrl,
1758 .vidioc_s_ctrl = vidioc_s_ctrl,
1759 .vidioc_g_frequency = vidioc_g_frequency,
1760 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephoa75d2042007-08-01 00:13:28 -03001761#ifdef CONFIG_VIDEO_ADV_DEBUG
1762 .vidioc_g_register = vidioc_g_register,
1763 .vidioc_s_register = vidioc_s_register,
1764#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765};
1766
Hans Verkuila3998102008-07-21 02:57:38 -03001767static struct video_device cx8800_radio_template = {
1768 .name = "cx8800-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001769 .fops = &radio_fops,
1770 .minor = -1,
1771 .ioctl_ops = &radio_ioctl_ops,
1772};
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/* ----------------------------------------------------------- */
1775
1776static void cx8800_unregister_video(struct cx8800_dev *dev)
1777{
1778 if (dev->radio_dev) {
1779 if (-1 != dev->radio_dev->minor)
1780 video_unregister_device(dev->radio_dev);
1781 else
1782 video_device_release(dev->radio_dev);
1783 dev->radio_dev = NULL;
1784 }
1785 if (dev->vbi_dev) {
1786 if (-1 != dev->vbi_dev->minor)
1787 video_unregister_device(dev->vbi_dev);
1788 else
1789 video_device_release(dev->vbi_dev);
1790 dev->vbi_dev = NULL;
1791 }
1792 if (dev->video_dev) {
1793 if (-1 != dev->video_dev->minor)
1794 video_unregister_device(dev->video_dev);
1795 else
1796 video_device_release(dev->video_dev);
1797 dev->video_dev = NULL;
1798 }
1799}
1800
1801static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1802 const struct pci_device_id *pci_id)
1803{
1804 struct cx8800_dev *dev;
1805 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int err;
1808
Panagiotis Issaris74081872006-01-11 19:40:56 -02001809 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (NULL == dev)
1811 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /* pci init */
1814 dev->pci = pci_dev;
1815 if (pci_enable_device(pci_dev)) {
1816 err = -EIO;
1817 goto fail_free;
1818 }
1819 core = cx88_core_get(dev->pci);
1820 if (NULL == core) {
1821 err = -EINVAL;
1822 goto fail_free;
1823 }
1824 dev->core = core;
1825
1826 /* print pci info */
1827 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001828 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1829 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001830 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001832 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 pci_set_master(pci_dev);
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -03001835 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1837 err = -EIO;
1838 goto fail_core;
1839 }
1840
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001841 /* Initialize VBI template */
1842 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1843 sizeof(cx8800_vbi_template) );
1844 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001848 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 /* init video dma queues */
1851 INIT_LIST_HEAD(&dev->vidq.active);
1852 INIT_LIST_HEAD(&dev->vidq.queued);
1853 dev->vidq.timeout.function = cx8800_vid_timeout;
1854 dev->vidq.timeout.data = (unsigned long)dev;
1855 init_timer(&dev->vidq.timeout);
1856 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1857 MO_VID_DMACNTRL,0x11,0x00);
1858
1859 /* init vbi dma queues */
1860 INIT_LIST_HEAD(&dev->vbiq.active);
1861 INIT_LIST_HEAD(&dev->vbiq.queued);
1862 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1863 dev->vbiq.timeout.data = (unsigned long)dev;
1864 init_timer(&dev->vbiq.timeout);
1865 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1866 MO_VID_DMACNTRL,0x88,0x00);
1867
1868 /* get irq */
1869 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001870 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001872 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 core->name,pci_dev->irq);
1874 goto fail_core;
1875 }
1876 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1877
1878 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001879
Hans Verkuil38f9d302008-07-23 05:09:15 -03001880 if (core->board.audio_chip == V4L2_IDENT_WM8775)
Hans Verkuile6574f22009-04-01 03:57:53 -03001881 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
Hans Verkuilb8341e12009-03-29 08:26:01 -03001882 "wm8775", "wm8775", 0x36 >> 1);
1883
1884 if (core->board.audio_chip == V4L2_IDENT_TVAUDIO) {
1885 /* This probes for a tda9874 as is used on some
1886 Pixelview Ultra boards. */
Hans Verkuil1792f682009-03-30 11:47:55 -03001887 v4l2_i2c_new_probed_subdev_addr(&core->v4l2_dev,
1888 &core->i2c_adap,
1889 "tvaudio", "tvaudio", 0xb0 >> 1);
Hans Verkuilb8341e12009-03-29 08:26:01 -03001890 }
Steven Toth30579062006-09-25 12:43:42 -03001891
Michael Krufky6fcecce2007-08-24 01:32:31 -03001892 switch (core->boardnr) {
1893 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Hans Verkuilb8341e12009-03-29 08:26:01 -03001894 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
1895 static struct i2c_board_info rtc_info = {
1896 I2C_BOARD_INFO("isl1208", 0x6f)
1897 };
1898
Michael Krufky6fcecce2007-08-24 01:32:31 -03001899 request_module("rtc-isl1208");
Hans Verkuilb8341e12009-03-29 08:26:01 -03001900 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1901 }
Michael Krufky8efd2e22008-04-22 14:45:14 -03001902 /* break intentionally omitted */
1903 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1904 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001905 }
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* register v4l devices */
1908 dev->video_dev = cx88_vdev_init(core,dev->pci,
1909 &cx8800_video_template,"video");
1910 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1911 video_nr[core->nr]);
1912 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001913 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 core->name);
1915 goto fail_unreg;
1916 }
1917 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001918 core->name, dev->video_dev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1921 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1922 vbi_nr[core->nr]);
1923 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001924 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 core->name);
1926 goto fail_unreg;
1927 }
1928 printk(KERN_INFO "%s/0: registered device vbi%d\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001929 core->name, dev->vbi_dev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Trent Piepho6a59d642007-08-15 14:41:57 -03001931 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1933 &cx8800_radio_template,"radio");
1934 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1935 radio_nr[core->nr]);
1936 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001937 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 core->name);
1939 goto fail_unreg;
1940 }
1941 printk(KERN_INFO "%s/0: registered device radio%d\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001942 core->name, dev->radio_dev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
1945 /* everything worked */
1946 list_add_tail(&dev->devlist,&cx8800_devlist);
1947 pci_set_drvdata(pci_dev,dev);
1948
1949 /* initial device configuration */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001950 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001951 cx88_set_tvnorm(core,core->tvnorm);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001952 init_controls(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001953 cx88_video_mux(core,0);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001954 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001957 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001959 if (IS_ERR(core->kthread)) {
1960 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001961 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1962 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001963 }
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return 0;
1966
1967fail_unreg:
1968 cx8800_unregister_video(dev);
1969 free_irq(pci_dev->irq, dev);
1970fail_core:
1971 cx88_core_put(core,dev->pci);
1972fail_free:
1973 kfree(dev);
1974 return err;
1975}
1976
1977static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1978{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001979 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001980 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001983 if (core->kthread) {
1984 kthread_stop(core->kthread);
1985 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987
Marton Balintb12203d2008-03-26 02:07:35 -03001988 if (core->ir)
1989 cx88_ir_stop(core, core->ir);
1990
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001991 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 pci_disable_device(pci_dev);
1993
1994 /* unregister stuff */
1995
1996 free_irq(pci_dev->irq, dev);
1997 cx8800_unregister_video(dev);
1998 pci_set_drvdata(pci_dev, NULL);
1999
2000 /* free memory */
2001 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2002 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002003 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 kfree(dev);
2005}
2006
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002007#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2009{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002010 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 struct cx88_core *core = dev->core;
2012
2013 /* stop video+vbi capture */
2014 spin_lock(&dev->slock);
2015 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002016 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 stop_video_dma(dev);
2018 del_timer(&dev->vidq.timeout);
2019 }
2020 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002021 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 cx8800_stop_vbi_dma(dev);
2023 del_timer(&dev->vbiq.timeout);
2024 }
2025 spin_unlock(&dev->slock);
2026
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002027 if (core->ir)
2028 cx88_ir_stop(core, core->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002030 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 pci_save_state(pci_dev);
2033 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2034 pci_disable_device(pci_dev);
2035 dev->state.disabled = 1;
2036 }
2037 return 0;
2038}
2039
2040static int cx8800_resume(struct pci_dev *pci_dev)
2041{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002042 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002044 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002047 err=pci_enable_device(pci_dev);
2048 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002049 printk(KERN_ERR "%s/0: can't enable device\n",
2050 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002051 return err;
2052 }
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 dev->state.disabled = 0;
2055 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002056 err= pci_set_power_state(pci_dev, PCI_D0);
2057 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002058 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002059 pci_disable_device(pci_dev);
2060 dev->state.disabled = 1;
2061
2062 return err;
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 pci_restore_state(pci_dev);
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002067 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002068 if (core->ir)
2069 cx88_ir_start(core, core->ir);
2070
2071 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* restart video+vbi capture */
2074 spin_lock(&dev->slock);
2075 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002076 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 restart_video_queue(dev,&dev->vidq);
2078 }
2079 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002080 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2082 }
2083 spin_unlock(&dev->slock);
2084
2085 return 0;
2086}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002087#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089/* ----------------------------------------------------------- */
2090
Adrian Bunk408b6642005-05-01 08:59:29 -07002091static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 {
2093 .vendor = 0x14f1,
2094 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002095 .subvendor = PCI_ANY_ID,
2096 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 },{
2098 /* --- end of list --- */
2099 }
2100};
2101MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2102
2103static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002104 .name = "cx8800",
2105 .id_table = cx8800_pci_tbl,
2106 .probe = cx8800_initdev,
2107 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002108#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 .suspend = cx8800_suspend,
2110 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112};
2113
2114static int cx8800_init(void)
2115{
Trent Piepho5772f812007-08-15 14:41:59 -03002116 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 (CX88_VERSION_CODE >> 16) & 0xff,
2118 (CX88_VERSION_CODE >> 8) & 0xff,
2119 CX88_VERSION_CODE & 0xff);
2120#ifdef SNAPSHOT
2121 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2122 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2123#endif
2124 return pci_register_driver(&cx8800_pci_driver);
2125}
2126
2127static void cx8800_fini(void)
2128{
2129 pci_unregister_driver(&cx8800_pci_driver);
2130}
2131
2132module_init(cx8800_init);
2133module_exit(cx8800_fini);
2134
2135/* ----------------------------------------------------------- */
2136/*
2137 * Local variables:
2138 * c-basic-offset: 8
2139 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002140 * 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 -07002141 */