blob: 24b403b238d1581f1eaa4de8c274cbcbff689006 [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
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030044#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080045/* Include V4L1 specific functions. Should be removed soon */
46#include <linux/videodev.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030047#endif
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
50MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
51MODULE_LICENSE("GPL");
52
53/* ------------------------------------------------------------------ */
54
55static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
58
59module_param_array(video_nr, int, NULL, 0444);
60module_param_array(vbi_nr, int, NULL, 0444);
61module_param_array(radio_nr, int, NULL, 0444);
62
63MODULE_PARM_DESC(video_nr,"video device numbers");
64MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
65MODULE_PARM_DESC(radio_nr,"radio device numbers");
66
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030067static unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068module_param(video_debug,int,0644);
69MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
70
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030071static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072module_param(irq_debug,int,0644);
73MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
74
75static unsigned int vid_limit = 16;
76module_param(vid_limit,int,0644);
77MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
78
79#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070080 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* ------------------------------------------------------------------ */
83
84static LIST_HEAD(cx8800_devlist);
85
86/* ------------------------------------------------------------------- */
87/* static data */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct cx8800_fmt formats[] = {
90 {
91 .name = "8 bpp, gray",
92 .fourcc = V4L2_PIX_FMT_GREY,
93 .cxformat = ColorFormatY8,
94 .depth = 8,
95 .flags = FORMAT_FLAGS_PACKED,
96 },{
97 .name = "15 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_RGB555,
99 .cxformat = ColorFormatRGB15,
100 .depth = 16,
101 .flags = FORMAT_FLAGS_PACKED,
102 },{
103 .name = "15 bpp RGB, be",
104 .fourcc = V4L2_PIX_FMT_RGB555X,
105 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
106 .depth = 16,
107 .flags = FORMAT_FLAGS_PACKED,
108 },{
109 .name = "16 bpp RGB, le",
110 .fourcc = V4L2_PIX_FMT_RGB565,
111 .cxformat = ColorFormatRGB16,
112 .depth = 16,
113 .flags = FORMAT_FLAGS_PACKED,
114 },{
115 .name = "16 bpp RGB, be",
116 .fourcc = V4L2_PIX_FMT_RGB565X,
117 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
118 .depth = 16,
119 .flags = FORMAT_FLAGS_PACKED,
120 },{
121 .name = "24 bpp RGB, le",
122 .fourcc = V4L2_PIX_FMT_BGR24,
123 .cxformat = ColorFormatRGB24,
124 .depth = 24,
125 .flags = FORMAT_FLAGS_PACKED,
126 },{
127 .name = "32 bpp RGB, le",
128 .fourcc = V4L2_PIX_FMT_BGR32,
129 .cxformat = ColorFormatRGB32,
130 .depth = 32,
131 .flags = FORMAT_FLAGS_PACKED,
132 },{
133 .name = "32 bpp RGB, be",
134 .fourcc = V4L2_PIX_FMT_RGB32,
135 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
136 .depth = 32,
137 .flags = FORMAT_FLAGS_PACKED,
138 },{
139 .name = "4:2:2, packed, YUYV",
140 .fourcc = V4L2_PIX_FMT_YUYV,
141 .cxformat = ColorFormatYUY2,
142 .depth = 16,
143 .flags = FORMAT_FLAGS_PACKED,
144 },{
145 .name = "4:2:2, packed, UYVY",
146 .fourcc = V4L2_PIX_FMT_UYVY,
147 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
148 .depth = 16,
149 .flags = FORMAT_FLAGS_PACKED,
150 },
151};
152
153static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
154{
155 unsigned int i;
156
157 for (i = 0; i < ARRAY_SIZE(formats); i++)
158 if (formats[i].fourcc == fourcc)
159 return formats+i;
160 return NULL;
161}
162
163/* ------------------------------------------------------------------- */
164
165static const struct v4l2_queryctrl no_ctl = {
166 .name = "42",
167 .flags = V4L2_CTRL_FLAG_DISABLED,
168};
169
170static struct cx88_ctrl cx8800_ctls[] = {
171 /* --- video --- */
172 {
173 .v = {
174 .id = V4L2_CID_BRIGHTNESS,
175 .name = "Brightness",
176 .minimum = 0x00,
177 .maximum = 0xff,
178 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300179 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .type = V4L2_CTRL_TYPE_INTEGER,
181 },
182 .off = 128,
183 .reg = MO_CONTR_BRIGHT,
184 .mask = 0x00ff,
185 .shift = 0,
186 },{
187 .v = {
188 .id = V4L2_CID_CONTRAST,
189 .name = "Contrast",
190 .minimum = 0,
191 .maximum = 0xff,
192 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200193 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .type = V4L2_CTRL_TYPE_INTEGER,
195 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700196 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .reg = MO_CONTR_BRIGHT,
198 .mask = 0xff00,
199 .shift = 8,
200 },{
201 .v = {
202 .id = V4L2_CID_HUE,
203 .name = "Hue",
204 .minimum = 0,
205 .maximum = 0xff,
206 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300207 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .type = V4L2_CTRL_TYPE_INTEGER,
209 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700210 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .reg = MO_HUE,
212 .mask = 0x00ff,
213 .shift = 0,
214 },{
215 /* strictly, this only describes only U saturation.
216 * V saturation is handled specially through code.
217 */
218 .v = {
219 .id = V4L2_CID_SATURATION,
220 .name = "Saturation",
221 .minimum = 0,
222 .maximum = 0xff,
223 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200224 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .type = V4L2_CTRL_TYPE_INTEGER,
226 },
227 .off = 0,
228 .reg = MO_UV_SATURATION,
229 .mask = 0x00ff,
230 .shift = 0,
231 },{
Frej Drejhammar6d042032008-03-23 22:43:21 -0300232 .v = {
233 .id = V4L2_CID_CHROMA_AGC,
234 .name = "Chroma AGC",
235 .minimum = 0,
236 .maximum = 1,
Frej Drejhammar87a17382008-03-23 22:43:22 -0300237 .default_value = 0x1,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300238 .type = V4L2_CTRL_TYPE_BOOLEAN,
239 },
240 .reg = MO_INPUT_FORMAT,
241 .mask = 1 << 10,
242 .shift = 10,
243 }, {
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300244 .v = {
245 .id = V4L2_CID_COLOR_KILLER,
246 .name = "Color killer",
247 .minimum = 0,
248 .maximum = 1,
Frej Drejhammar0b5afdd2008-03-23 22:43:25 -0300249 .default_value = 0x1,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300250 .type = V4L2_CTRL_TYPE_BOOLEAN,
251 },
252 .reg = MO_INPUT_FORMAT,
253 .mask = 1 << 9,
254 .shift = 9,
255 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* --- audio --- */
257 .v = {
258 .id = V4L2_CID_AUDIO_MUTE,
259 .name = "Mute",
260 .minimum = 0,
261 .maximum = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200262 .default_value = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 .type = V4L2_CTRL_TYPE_BOOLEAN,
264 },
265 .reg = AUD_VOL_CTL,
266 .sreg = SHADOW_AUD_VOL_CTL,
267 .mask = (1 << 6),
268 .shift = 6,
269 },{
270 .v = {
271 .id = V4L2_CID_AUDIO_VOLUME,
272 .name = "Volume",
273 .minimum = 0,
274 .maximum = 0x3f,
275 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300276 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .type = V4L2_CTRL_TYPE_INTEGER,
278 },
279 .reg = AUD_VOL_CTL,
280 .sreg = SHADOW_AUD_VOL_CTL,
281 .mask = 0x3f,
282 .shift = 0,
283 },{
284 .v = {
285 .id = V4L2_CID_AUDIO_BALANCE,
286 .name = "Balance",
287 .minimum = 0,
288 .maximum = 0x7f,
289 .step = 1,
290 .default_value = 0x40,
291 .type = V4L2_CTRL_TYPE_INTEGER,
292 },
293 .reg = AUD_BAL_CTL,
294 .sreg = SHADOW_AUD_BAL_CTL,
295 .mask = 0x7f,
296 .shift = 0,
297 }
298};
Adrian Bunk408b6642005-05-01 08:59:29 -0700299static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Michael Krufky38a27132006-06-26 23:42:39 -0300301const u32 cx88_user_ctrls[] = {
302 V4L2_CID_USER_CLASS,
303 V4L2_CID_BRIGHTNESS,
304 V4L2_CID_CONTRAST,
305 V4L2_CID_SATURATION,
306 V4L2_CID_HUE,
307 V4L2_CID_AUDIO_VOLUME,
308 V4L2_CID_AUDIO_BALANCE,
309 V4L2_CID_AUDIO_MUTE,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300310 V4L2_CID_CHROMA_AGC,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300311 V4L2_CID_COLOR_KILLER,
Michael Krufky38a27132006-06-26 23:42:39 -0300312 0
313};
314EXPORT_SYMBOL(cx88_user_ctrls);
315
316static const u32 *ctrl_classes[] = {
317 cx88_user_ctrls,
318 NULL
319};
320
Frej Drejhammar6d042032008-03-23 22:43:21 -0300321int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
Michael Krufky38a27132006-06-26 23:42:39 -0300322{
323 int i;
324
325 if (qctrl->id < V4L2_CID_BASE ||
326 qctrl->id >= V4L2_CID_LASTP1)
327 return -EINVAL;
328 for (i = 0; i < CX8800_CTLS; i++)
329 if (cx8800_ctls[i].v.id == qctrl->id)
330 break;
331 if (i == CX8800_CTLS) {
332 *qctrl = no_ctl;
333 return 0;
334 }
335 *qctrl = cx8800_ctls[i].v;
Frej Drejhammar6d042032008-03-23 22:43:21 -0300336 /* Report chroma AGC as inactive when SECAM is selected */
337 if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
338 core->tvnorm & V4L2_STD_SECAM)
339 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
340
Michael Krufky38a27132006-06-26 23:42:39 -0300341 return 0;
342}
343EXPORT_SYMBOL(cx8800_ctrl_query);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/* ------------------------------------------------------------------- */
346/* resource management */
347
348static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
349{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700350 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (fh->resources & bit)
352 /* have it already allocated */
353 return 1;
354
355 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200356 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (dev->resources & bit) {
358 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200359 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361 }
362 /* it's free, grab it */
363 fh->resources |= bit;
364 dev->resources |= bit;
365 dprintk(1,"res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200366 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 1;
368}
369
370static
371int res_check(struct cx8800_fh *fh, unsigned int bit)
372{
373 return (fh->resources & bit);
374}
375
376static
377int res_locked(struct cx8800_dev *dev, unsigned int bit)
378{
379 return (dev->resources & bit);
380}
381
382static
383void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
384{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700385 struct cx88_core *core = dev->core;
Eric Sesterhennae246012006-03-13 13:17:11 -0300386 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Ingo Molnar3593cab2006-02-07 06:49:14 -0200388 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 fh->resources &= ~bits;
390 dev->resources &= ~bits;
391 dprintk(1,"res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200392 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/* ------------------------------------------------------------------ */
396
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300397int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700399 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300402 input, INPUT(input).vmux,
403 INPUT(input).gpio0,INPUT(input).gpio1,
404 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700405 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300406 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
407 cx_write(MO_GP3_IO, INPUT(input).gpio3);
408 cx_write(MO_GP0_IO, INPUT(input).gpio0);
409 cx_write(MO_GP1_IO, INPUT(input).gpio1);
410 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Trent Piepho6a59d642007-08-15 14:41:57 -0300412 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 case CX88_VMUX_SVIDEO:
414 cx_set(MO_AFECFG_IO, 0x00000001);
415 cx_set(MO_INPUT_FORMAT, 0x00010010);
416 cx_set(MO_FILTER_EVEN, 0x00002020);
417 cx_set(MO_FILTER_ODD, 0x00002020);
418 break;
419 default:
420 cx_clear(MO_AFECFG_IO, 0x00000001);
421 cx_clear(MO_INPUT_FORMAT, 0x00010010);
422 cx_clear(MO_FILTER_EVEN, 0x00002020);
423 cx_clear(MO_FILTER_ODD, 0x00002020);
424 break;
425 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300426
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300427 /* if there are audioroutes defined, we have an external
428 ADC to deal with audio */
Ricardo Cerqueira7b27d452007-09-30 13:02:49 -0300429
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300430 if (INPUT(input).audioroute) {
Ricardo Cerqueira7b27d452007-09-30 13:02:49 -0300431
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300432 /* cx2388's C-ADC is connected to the tuner only.
433 When used with S-Video, that ADC is busy dealing with
434 chroma, so an external must be used for baseband audio */
435
436 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
437 INPUT(input).type != CX88_RADIO) {
438 /* "ADC mode" */
439 cx_write(AUD_I2SCNTL, 0x1);
Michael Krufkyf24546a2006-10-16 16:51:11 -0300440 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300441 } else {
442 /* Normal mode */
443 cx_write(AUD_I2SCNTL, 0x0);
Michael Krufkyf24546a2006-10-16 16:51:11 -0300444 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300445 }
446
447 /* The wm8775 module has the "2" route hardwired into
448 the initialization. Some boards may use different
449 routes for different inputs. HVR-1300 surely does */
450 if (core->board.audio_chip &&
Hans Verkuil38f9d302008-07-23 05:09:15 -0300451 core->board.audio_chip == V4L2_IDENT_WM8775) {
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300452 struct v4l2_routing route;
453
454 route.input = INPUT(input).audioroute;
455 cx88_call_i2c_clients(core,
Mauro Carvalho Chehabd8f69972007-12-17 10:35:59 -0300456 VIDIOC_INT_S_AUDIO_ROUTING, &route);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300457
458 }
459
Michael Krufkyf24546a2006-10-16 16:51:11 -0300460 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
463}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300464EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466/* ------------------------------------------------------------------ */
467
468static int start_video_dma(struct cx8800_dev *dev,
469 struct cx88_dmaqueue *q,
470 struct cx88_buffer *buf)
471{
472 struct cx88_core *core = dev->core;
473
474 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700475 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700477 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
479
480 /* reset counter */
481 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
482 q->count = 1;
483
484 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300485 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700486
487 /* Enables corresponding bits at PCI_INT_STAT:
488 bits 0 to 4: video, audio, transport stream, VIP, Host
489 bit 7: timer
490 bits 8 and 9: DMA complete for: SRC, DST
491 bits 10 and 11: BERR signal asserted for RISC: RD, WR
492 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 cx_set(MO_VID_INTMSK, 0x0f0011);
495
496 /* enable capture */
497 cx_set(VID_CAPTURE_CONTROL,0x06);
498
499 /* start dma */
500 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700501 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 return 0;
504}
505
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300506#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static int stop_video_dma(struct cx8800_dev *dev)
508{
509 struct cx88_core *core = dev->core;
510
511 /* stop dma */
512 cx_clear(MO_VID_DMACNTRL, 0x11);
513
514 /* disable capture */
515 cx_clear(VID_CAPTURE_CONTROL,0x06);
516
517 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300518 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 cx_clear(MO_VID_INTMSK, 0x0f0011);
520 return 0;
521}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524static int restart_video_queue(struct cx8800_dev *dev,
525 struct cx88_dmaqueue *q)
526{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700527 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct cx88_buffer *buf, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800531 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
533 buf, buf->vb.i);
534 start_video_dma(dev, q, buf);
Trent Piepho8bb629e22007-10-10 05:37:40 -0300535 list_for_each_entry(buf, &q->active, vb.queue)
536 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
538 return 0;
539 }
540
541 prev = NULL;
542 for (;;) {
543 if (list_empty(&q->queued))
544 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800545 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700547 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300549 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 buf->count = q->count++;
551 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
552 dprintk(2,"[%p/%d] restart_queue - first active\n",
553 buf,buf->vb.i);
554
555 } else if (prev->vb.width == buf->vb.width &&
556 prev->vb.height == buf->vb.height &&
557 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700558 list_move_tail(&buf->vb.queue, &q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300559 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 buf->count = q->count++;
561 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
562 dprintk(2,"[%p/%d] restart_queue - move to active\n",
563 buf,buf->vb.i);
564 } else {
565 return 0;
566 }
567 prev = buf;
568 }
569}
570
571/* ------------------------------------------------------------------ */
572
573static int
574buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
575{
576 struct cx8800_fh *fh = q->priv_data;
577
578 *size = fh->fmt->depth*fh->width*fh->height >> 3;
579 if (0 == *count)
580 *count = 32;
581 while (*size * *count > vid_limit * 1024 * 1024)
582 (*count)--;
583 return 0;
584}
585
586static int
587buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
588 enum v4l2_field field)
589{
590 struct cx8800_fh *fh = q->priv_data;
591 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700592 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300594 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int rc, init_buffer = 0;
596
597 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700598 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
599 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return -EINVAL;
601 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
602 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
603 return -EINVAL;
604
605 if (buf->fmt != fh->fmt ||
606 buf->vb.width != fh->width ||
607 buf->vb.height != fh->height ||
608 buf->vb.field != field) {
609 buf->fmt = fh->fmt;
610 buf->vb.width = fh->width;
611 buf->vb.height = fh->height;
612 buf->vb.field = field;
613 init_buffer = 1;
614 }
615
Brandon Philips0fc06862007-11-06 20:02:36 -0300616 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300618 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 goto fail;
620 }
621
622 if (init_buffer) {
623 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
624 switch (buf->vb.field) {
625 case V4L2_FIELD_TOP:
626 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300627 dma->sglist, 0, UNSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 buf->bpl, 0, buf->vb.height);
629 break;
630 case V4L2_FIELD_BOTTOM:
631 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300632 dma->sglist, UNSET, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 buf->bpl, 0, buf->vb.height);
634 break;
635 case V4L2_FIELD_INTERLACED:
636 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300637 dma->sglist, 0, buf->bpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 buf->bpl, buf->bpl,
639 buf->vb.height >> 1);
640 break;
641 case V4L2_FIELD_SEQ_TB:
642 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300643 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 0, buf->bpl * (buf->vb.height >> 1),
645 buf->bpl, 0,
646 buf->vb.height >> 1);
647 break;
648 case V4L2_FIELD_SEQ_BT:
649 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300650 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 buf->bpl * (buf->vb.height >> 1), 0,
652 buf->bpl, 0,
653 buf->vb.height >> 1);
654 break;
655 default:
656 BUG();
657 }
658 }
659 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
660 buf, buf->vb.i,
661 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
662 (unsigned long)buf->risc.dma);
663
Brandon Philips0fc06862007-11-06 20:02:36 -0300664 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 0;
666
667 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300668 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return rc;
670}
671
672static void
673buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
674{
675 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
676 struct cx88_buffer *prev;
677 struct cx8800_fh *fh = vq->priv_data;
678 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700679 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct cx88_dmaqueue *q = &dev->vidq;
681
682 /* add jump to stopper */
683 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
684 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
685
686 if (!list_empty(&q->queued)) {
687 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300688 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
690 buf, buf->vb.i);
691
692 } else if (list_empty(&q->active)) {
693 list_add_tail(&buf->vb.queue,&q->active);
694 start_video_dma(dev, q, buf);
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 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
698 dprintk(2,"[%p/%d] buffer_queue - first active\n",
699 buf, buf->vb.i);
700
701 } else {
702 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
703 if (prev->vb.width == buf->vb.width &&
704 prev->vb.height == buf->vb.height &&
705 prev->fmt == buf->fmt) {
706 list_add_tail(&buf->vb.queue,&q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300707 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 buf->count = q->count++;
709 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
710 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
711 buf, buf->vb.i);
712
713 } else {
714 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300715 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
717 buf, buf->vb.i);
718 }
719 }
720}
721
722static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
723{
724 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300726 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Adrian Bunk408b6642005-05-01 08:59:29 -0700729static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 .buf_setup = buffer_setup,
731 .buf_prepare = buffer_prepare,
732 .buf_queue = buffer_queue,
733 .buf_release = buffer_release,
734};
735
736/* ------------------------------------------------------------------ */
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739/* ------------------------------------------------------------------ */
740
741static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
742{
743 switch (fh->type) {
744 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
745 return &fh->vidq;
746 case V4L2_BUF_TYPE_VBI_CAPTURE:
747 return &fh->vbiq;
748 default:
749 BUG();
750 return NULL;
751 }
752}
753
754static int get_ressource(struct cx8800_fh *fh)
755{
756 switch (fh->type) {
757 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
758 return RESOURCE_VIDEO;
759 case V4L2_BUF_TYPE_VBI_CAPTURE:
760 return RESOURCE_VBI;
761 default:
762 BUG();
763 return 0;
764 }
765}
766
767static int video_open(struct inode *inode, struct file *file)
768{
769 int minor = iminor(inode);
770 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700771 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct cx8800_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 enum v4l2_buf_type type = 0;
774 int radio = 0;
775
Trent Piepho8bb629e22007-10-10 05:37:40 -0300776 list_for_each_entry(h, &cx8800_devlist, devlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (h->video_dev->minor == minor) {
778 dev = h;
779 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
780 }
781 if (h->vbi_dev->minor == minor) {
782 dev = h;
783 type = V4L2_BUF_TYPE_VBI_CAPTURE;
784 }
785 if (h->radio_dev &&
786 h->radio_dev->minor == minor) {
787 radio = 1;
788 dev = h;
789 }
790 }
791 if (NULL == dev)
792 return -ENODEV;
793
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700794 core = dev->core;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 dprintk(1,"open minor=%d radio=%d type=%s\n",
797 minor,radio,v4l2_type_names[type]);
798
799 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200800 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (NULL == fh)
802 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 file->private_data = fh;
804 fh->dev = dev;
805 fh->radio = radio;
806 fh->type = type;
807 fh->width = 320;
808 fh->height = 240;
809 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
810
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300811 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
812 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 V4L2_BUF_TYPE_VIDEO_CAPTURE,
814 V4L2_FIELD_INTERLACED,
815 sizeof(struct cx88_buffer),
816 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300817 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
818 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 V4L2_BUF_TYPE_VBI_CAPTURE,
820 V4L2_FIELD_SEQ_TB,
821 sizeof(struct cx88_buffer),
822 fh);
823
824 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 dprintk(1,"video_open: setting radio device\n");
Trent Piepho6a59d642007-08-15 14:41:57 -0300826 cx_write(MO_GP3_IO, core->board.radio.gpio3);
827 cx_write(MO_GP0_IO, core->board.radio.gpio0);
828 cx_write(MO_GP1_IO, core->board.radio.gpio1);
829 cx_write(MO_GP2_IO, core->board.radio.gpio2);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700830 core->tvaudio = WW_FM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 cx88_set_tvaudio(core);
832 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700833 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800840video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct cx8800_fh *fh = file->private_data;
843
844 switch (fh->type) {
845 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
846 if (res_locked(fh->dev,RESOURCE_VIDEO))
847 return -EBUSY;
848 return videobuf_read_one(&fh->vidq, data, count, ppos,
849 file->f_flags & O_NONBLOCK);
850 case V4L2_BUF_TYPE_VBI_CAPTURE:
851 if (!res_get(fh->dev,fh,RESOURCE_VBI))
852 return -EBUSY;
853 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
854 file->f_flags & O_NONBLOCK);
855 default:
856 BUG();
857 return 0;
858 }
859}
860
861static unsigned int
862video_poll(struct file *file, struct poll_table_struct *wait)
863{
864 struct cx8800_fh *fh = file->private_data;
865 struct cx88_buffer *buf;
866
867 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
868 if (!res_get(fh->dev,fh,RESOURCE_VBI))
869 return POLLERR;
870 return videobuf_poll_stream(file, &fh->vbiq, wait);
871 }
872
873 if (res_check(fh,RESOURCE_VIDEO)) {
874 /* streaming capture */
875 if (list_empty(&fh->vidq.stream))
876 return POLLERR;
877 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
878 } else {
879 /* read() capture */
880 buf = (struct cx88_buffer*)fh->vidq.read_buf;
881 if (NULL == buf)
882 return POLLERR;
883 }
884 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300885 if (buf->vb.state == VIDEOBUF_DONE ||
886 buf->vb.state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return POLLIN|POLLRDNORM;
888 return 0;
889}
890
891static int video_release(struct inode *inode, struct file *file)
892{
893 struct cx8800_fh *fh = file->private_data;
894 struct cx8800_dev *dev = fh->dev;
895
896 /* turn off overlay */
897 if (res_check(fh, RESOURCE_OVERLAY)) {
898 /* FIXME */
899 res_free(dev,fh,RESOURCE_OVERLAY);
900 }
901
902 /* stop video capture */
903 if (res_check(fh, RESOURCE_VIDEO)) {
904 videobuf_queue_cancel(&fh->vidq);
905 res_free(dev,fh,RESOURCE_VIDEO);
906 }
907 if (fh->vidq.read_buf) {
908 buffer_release(&fh->vidq,fh->vidq.read_buf);
909 kfree(fh->vidq.read_buf);
910 }
911
912 /* stop vbi capture */
913 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -0300914 videobuf_stop(&fh->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 res_free(dev,fh,RESOURCE_VBI);
916 }
917
918 videobuf_mmap_free(&fh->vidq);
919 videobuf_mmap_free(&fh->vbiq);
920 file->private_data = NULL;
921 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700922
923 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
927
928static int
929video_mmap(struct file *file, struct vm_area_struct * vma)
930{
931 struct cx8800_fh *fh = file->private_data;
932
933 return videobuf_mmap_mapper(get_queue(fh), vma);
934}
935
936/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300937/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300939int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300941 struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 u32 value;
943 int i;
944
945 for (i = 0; i < CX8800_CTLS; i++)
946 if (cx8800_ctls[i].v.id == ctl->id)
947 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300948 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return -EINVAL;
950
951 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
952 switch (ctl->id) {
953 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300954 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
955 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957 case V4L2_CID_AUDIO_VOLUME:
958 ctl->value = 0x3f - (value & 0x3f);
959 break;
960 default:
961 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
962 break;
963 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300964 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
965 ctl->id, c->v.name, ctl->value, c->reg,
966 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300969EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300971int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200974 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300976
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200977 for (i = 0; i < CX8800_CTLS; i++) {
978 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200980 }
981 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300982 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EINVAL;
984
985 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700986 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700988 ctl->value = c->v.maximum;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200989 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 switch (ctl->id) {
991 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300992 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 break;
994 case V4L2_CID_AUDIO_VOLUME:
995 value = 0x3f - (ctl->value & 0x3f);
996 break;
997 case V4L2_CID_SATURATION:
998 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200999
1000 value = ((ctl->value - c->off) << c->shift) & c->mask;
1001
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001002 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001003 /* For SECAM, both U and V sat should be equal */
1004 value=value<<8|value;
1005 } else {
1006 /* Keeps U Saturation proportional to V Sat */
1007 value=(value*0x5a)/0x7f<<8|value;
1008 }
1009 mask=0xffff;
1010 break;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001011 case V4L2_CID_CHROMA_AGC:
1012 /* Do not allow chroma AGC to be enabled for SECAM */
1013 value = ((ctl->value - c->off) << c->shift) & c->mask;
1014 if (core->tvnorm & V4L2_STD_SECAM && value)
1015 return -EINVAL;
1016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 default:
1018 value = ((ctl->value - c->off) << c->shift) & c->mask;
1019 break;
1020 }
Ian Pickworth6457af52006-02-27 00:09:45 -03001021 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1022 ctl->id, c->v.name, ctl->value, c->reg, value,
1023 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001025 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001027 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 return 0;
1030}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001031EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001033static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001035 struct v4l2_control ctrl;
1036 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001038 for (i = 0; i < CX8800_CTLS; i++) {
1039 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -03001040 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001041
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001042 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001047/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Hans Verkuil78b526a2008-05-28 12:16:41 -03001049static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001050 struct v4l2_format *f)
1051{
1052 struct cx8800_fh *fh = priv;
1053
1054 f->fmt.pix.width = fh->width;
1055 f->fmt.pix.height = fh->height;
1056 f->fmt.pix.field = fh->vidq.field;
1057 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1058 f->fmt.pix.bytesperline =
1059 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1060 f->fmt.pix.sizeimage =
1061 f->fmt.pix.height * f->fmt.pix.bytesperline;
1062 return 0;
1063}
1064
Hans Verkuil78b526a2008-05-28 12:16:41 -03001065static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct v4l2_format *f)
1067{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001068 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1069 struct cx8800_fmt *fmt;
1070 enum v4l2_field field;
1071 unsigned int maxw, maxh;
1072
1073 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1074 if (NULL == fmt)
1075 return -EINVAL;
1076
1077 field = f->fmt.pix.field;
1078 maxw = norm_maxw(core->tvnorm);
1079 maxh = norm_maxh(core->tvnorm);
1080
1081 if (V4L2_FIELD_ANY == field) {
1082 field = (f->fmt.pix.height > maxh/2)
1083 ? V4L2_FIELD_INTERLACED
1084 : V4L2_FIELD_BOTTOM;
1085 }
1086
1087 switch (field) {
1088 case V4L2_FIELD_TOP:
1089 case V4L2_FIELD_BOTTOM:
1090 maxh = maxh / 2;
1091 break;
1092 case V4L2_FIELD_INTERLACED:
1093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 default:
1095 return -EINVAL;
1096 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001097
1098 f->fmt.pix.field = field;
1099 if (f->fmt.pix.height < 32)
1100 f->fmt.pix.height = 32;
1101 if (f->fmt.pix.height > maxh)
1102 f->fmt.pix.height = maxh;
1103 if (f->fmt.pix.width < 48)
1104 f->fmt.pix.width = 48;
1105 if (f->fmt.pix.width > maxw)
1106 f->fmt.pix.width = maxw;
1107 f->fmt.pix.width &= ~0x03;
1108 f->fmt.pix.bytesperline =
1109 (f->fmt.pix.width * fmt->depth) >> 3;
1110 f->fmt.pix.sizeimage =
1111 f->fmt.pix.height * f->fmt.pix.bytesperline;
1112
1113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Hans Verkuil78b526a2008-05-28 12:16:41 -03001116static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001117 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001119 struct cx8800_fh *fh = priv;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001120 int err = vidioc_try_fmt_vid_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001121
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001122 if (0 != err)
1123 return err;
1124 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1125 fh->width = f->fmt.pix.width;
1126 fh->height = f->fmt.pix.height;
1127 fh->vidq.field = f->fmt.pix.field;
1128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001131static int vidioc_querycap (struct file *file, void *priv,
1132 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001134 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001137 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001138 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001139 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1140 cap->version = CX88_VERSION_CODE;
1141 cap->capabilities =
1142 V4L2_CAP_VIDEO_CAPTURE |
1143 V4L2_CAP_READWRITE |
1144 V4L2_CAP_STREAMING |
1145 V4L2_CAP_VBI_CAPTURE;
Trent Piepho6a59d642007-08-15 14:41:57 -03001146 if (UNSET != core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001147 cap->capabilities |= V4L2_CAP_TUNER;
1148 return 0;
1149}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001150
Hans Verkuil78b526a2008-05-28 12:16:41 -03001151static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001152 struct v4l2_fmtdesc *f)
1153{
1154 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001157 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1158 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001160 return 0;
1161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001163#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001164static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001165{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001166 struct cx8800_fh *fh = priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001167
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001168 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001172static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001174 struct cx8800_fh *fh = priv;
1175 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001178static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1179{
1180 struct cx8800_fh *fh = priv;
1181 return (videobuf_querybuf(get_queue(fh), p));
1182}
1183
1184static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1185{
1186 struct cx8800_fh *fh = priv;
1187 return (videobuf_qbuf(get_queue(fh), p));
1188}
1189
1190static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1191{
1192 struct cx8800_fh *fh = priv;
1193 return (videobuf_dqbuf(get_queue(fh), p,
1194 file->f_flags & O_NONBLOCK));
1195}
1196
1197static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1198{
1199 struct cx8800_fh *fh = priv;
1200 struct cx8800_dev *dev = fh->dev;
1201
1202 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1203 return -EINVAL;
1204 if (unlikely(i != fh->type))
1205 return -EINVAL;
1206
1207 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1208 return -EBUSY;
1209 return videobuf_streamon(get_queue(fh));
1210}
1211
1212static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1213{
1214 struct cx8800_fh *fh = priv;
1215 struct cx8800_dev *dev = fh->dev;
1216 int err, res;
1217
1218 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1219 return -EINVAL;
1220 if (i != fh->type)
1221 return -EINVAL;
1222
1223 res = get_ressource(fh);
1224 err = videobuf_streamoff(get_queue(fh));
1225 if (err < 0)
1226 return err;
1227 res_free(dev,fh,res);
1228 return 0;
1229}
1230
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001231static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001232{
1233 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1234
1235 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001236 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001237 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001238
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001239 return 0;
1240}
1241
1242/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001243int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001244{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001245 static const char *iname[] = {
1246 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1247 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1248 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1249 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1250 [ CX88_VMUX_SVIDEO ] = "S-Video",
1251 [ CX88_VMUX_TELEVISION ] = "Television",
1252 [ CX88_VMUX_CABLE ] = "Cable TV",
1253 [ CX88_VMUX_DVB ] = "DVB",
1254 [ CX88_VMUX_DEBUG ] = "for debug only",
1255 };
1256 unsigned int n;
1257
1258 n = i->index;
1259 if (n >= 4)
1260 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001261 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001262 return -EINVAL;
1263 memset(i,0,sizeof(*i));
1264 i->index = n;
1265 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001266 strcpy(i->name,iname[INPUT(n).type]);
1267 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1268 (CX88_VMUX_CABLE == INPUT(n).type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001269 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001270 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001271 return 0;
1272}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001273EXPORT_SYMBOL(cx88_enum_input);
1274
1275static int vidioc_enum_input (struct file *file, void *priv,
1276 struct v4l2_input *i)
1277{
1278 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1279 return cx88_enum_input (core,i);
1280}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001281
1282static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1283{
1284 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1285
1286 *i = core->input;
1287 return 0;
1288}
1289
1290static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1291{
1292 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1293
1294 if (i >= 4)
1295 return -EINVAL;
1296
1297 mutex_lock(&core->lock);
1298 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001299 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001300 mutex_unlock(&core->lock);
1301 return 0;
1302}
1303
1304
1305
1306static int vidioc_queryctrl (struct file *file, void *priv,
1307 struct v4l2_queryctrl *qctrl)
1308{
Frej Drejhammar6d042032008-03-23 22:43:21 -03001309 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1310
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001311 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1312 if (unlikely(qctrl->id == 0))
1313 return -EINVAL;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001314 return cx8800_ctrl_query(core, qctrl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001315}
1316
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001317static int vidioc_g_ctrl (struct file *file, void *priv,
1318 struct v4l2_control *ctl)
1319{
1320 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1321 return
1322 cx88_get_control(core,ctl);
1323}
1324
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001325static int vidioc_s_ctrl (struct file *file, void *priv,
1326 struct v4l2_control *ctl)
1327{
1328 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001329 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001330 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001331}
1332
1333static int vidioc_g_tuner (struct file *file, void *priv,
1334 struct v4l2_tuner *t)
1335{
1336 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1337 u32 reg;
1338
Trent Piepho6a59d642007-08-15 14:41:57 -03001339 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001340 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001341 if (0 != t->index)
1342 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001343
1344 strcpy(t->name, "Television");
1345 t->type = V4L2_TUNER_ANALOG_TV;
1346 t->capability = V4L2_TUNER_CAP_NORM;
1347 t->rangehigh = 0xffffffffUL;
1348
1349 cx88_get_stereo(core ,t);
1350 reg = cx_read(MO_DEVICE_STATUS);
1351 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1352 return 0;
1353}
1354
1355static int vidioc_s_tuner (struct file *file, void *priv,
1356 struct v4l2_tuner *t)
1357{
1358 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1359
Trent Piepho6a59d642007-08-15 14:41:57 -03001360 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001361 return -EINVAL;
1362 if (0 != t->index)
1363 return -EINVAL;
1364
1365 cx88_set_stereo(core, t->audmode, 1);
1366 return 0;
1367}
1368
1369static int vidioc_g_frequency (struct file *file, void *priv,
1370 struct v4l2_frequency *f)
1371{
1372 struct cx8800_fh *fh = priv;
1373 struct cx88_core *core = fh->dev->core;
1374
Trent Piepho6a59d642007-08-15 14:41:57 -03001375 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001376 return -EINVAL;
1377
1378 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1379 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1380 f->frequency = core->freq;
1381
1382 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1383
1384 return 0;
1385}
1386
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001387int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001388 struct v4l2_frequency *f)
1389{
Trent Piepho6a59d642007-08-15 14:41:57 -03001390 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001391 return -EINVAL;
1392 if (unlikely(f->tuner != 0))
1393 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001394
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001395 mutex_lock(&core->lock);
1396 core->freq = f->frequency;
1397 cx88_newstation(core);
1398 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1399
1400 /* When changing channels it is required to reset TVAUDIO */
1401 msleep (10);
1402 cx88_set_tvaudio(core);
1403
1404 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001405
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001406 return 0;
1407}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001408EXPORT_SYMBOL(cx88_set_freq);
1409
1410static int vidioc_s_frequency (struct file *file, void *priv,
1411 struct v4l2_frequency *f)
1412{
1413 struct cx8800_fh *fh = priv;
1414 struct cx88_core *core = fh->dev->core;
1415
1416 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1417 return -EINVAL;
1418 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1419 return -EINVAL;
1420
1421 return
1422 cx88_set_freq (core,f);
1423}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001424
Trent Piephodbbff482007-01-22 23:31:53 -03001425#ifdef CONFIG_VIDEO_ADV_DEBUG
1426static int vidioc_g_register (struct file *file, void *fh,
1427 struct v4l2_register *reg)
1428{
1429 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1430
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001431 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001432 return -EINVAL;
1433 /* cx2388x has a 24-bit register space */
1434 reg->val = cx_read(reg->reg&0xffffff);
1435 return 0;
1436}
1437
1438static int vidioc_s_register (struct file *file, void *fh,
1439 struct v4l2_register *reg)
1440{
1441 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1442
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001443 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001444 return -EINVAL;
Trent Piephodbbff482007-01-22 23:31:53 -03001445 cx_write(reg->reg&0xffffff, reg->val);
1446 return 0;
1447}
1448#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001451/* RADIO ESPECIFIC IOCTLS */
1452/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001454static int radio_querycap (struct file *file, void *priv,
1455 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001457 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct cx88_core *core = dev->core;
1459
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001460 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001461 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001462 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1463 cap->version = CX88_VERSION_CODE;
1464 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001466}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001468static int radio_g_tuner (struct file *file, void *priv,
1469 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001471 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1472
1473 if (unlikely(t->index > 0))
1474 return -EINVAL;
1475
1476 strcpy(t->name, "Radio");
1477 t->type = V4L2_TUNER_RADIO;
1478
1479 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1480 return 0;
1481}
1482
1483static int radio_enum_input (struct file *file, void *priv,
1484 struct v4l2_input *i)
1485{
1486 if (i->index != 0)
1487 return -EINVAL;
1488 strcpy(i->name,"Radio");
1489 i->type = V4L2_INPUT_TYPE_TUNER;
1490
1491 return 0;
1492}
1493
1494static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1495{
1496 if (unlikely(a->index))
1497 return -EINVAL;
1498
1499 memset(a,0,sizeof(*a));
1500 strcpy(a->name,"Radio");
1501 return 0;
1502}
1503
1504/* FIXME: Should add a standard for radio */
1505
1506static int radio_s_tuner (struct file *file, void *priv,
1507 struct v4l2_tuner *t)
1508{
1509 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1510
1511 if (0 != t->index)
1512 return -EINVAL;
1513
1514 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1515
1516 return 0;
1517}
1518
1519static int radio_s_audio (struct file *file, void *fh,
1520 struct v4l2_audio *a)
1521{
1522 return 0;
1523}
1524
1525static int radio_s_input (struct file *file, void *fh, unsigned int i)
1526{
1527 return 0;
1528}
1529
1530static int radio_queryctrl (struct file *file, void *priv,
1531 struct v4l2_queryctrl *c)
1532{
1533 int i;
1534
1535 if (c->id < V4L2_CID_BASE ||
1536 c->id >= V4L2_CID_LASTP1)
1537 return -EINVAL;
1538 if (c->id == V4L2_CID_AUDIO_MUTE) {
1539 for (i = 0; i < CX8800_CTLS; i++)
1540 if (cx8800_ctls[i].v.id == c->id)
1541 break;
1542 *c = cx8800_ctls[i].v;
1543 } else
1544 *c = no_ctl;
1545 return 0;
1546}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548/* ----------------------------------------------------------- */
1549
1550static void cx8800_vid_timeout(unsigned long data)
1551{
1552 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1553 struct cx88_core *core = dev->core;
1554 struct cx88_dmaqueue *q = &dev->vidq;
1555 struct cx88_buffer *buf;
1556 unsigned long flags;
1557
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001558 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 cx_clear(MO_VID_DMACNTRL, 0x11);
1561 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1562
1563 spin_lock_irqsave(&dev->slock,flags);
1564 while (!list_empty(&q->active)) {
1565 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1566 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03001567 buf->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 wake_up(&buf->vb.done);
1569 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1570 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1571 }
1572 restart_video_queue(dev,q);
1573 spin_unlock_irqrestore(&dev->slock,flags);
1574}
1575
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001576static char *cx88_vid_irqs[32] = {
1577 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1578 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1579 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1580 "y_sync", "u_sync", "v_sync", "vbi_sync",
1581 "opc_err", "par_err", "rip_err", "pci_abort",
1582};
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584static void cx8800_vid_irq(struct cx8800_dev *dev)
1585{
1586 struct cx88_core *core = dev->core;
1587 u32 status, mask, count;
1588
1589 status = cx_read(MO_VID_INTSTAT);
1590 mask = cx_read(MO_VID_INTMSK);
1591 if (0 == (status & mask))
1592 return;
1593 cx_write(MO_VID_INTSTAT, status);
1594 if (irq_debug || (status & mask & ~0xff))
1595 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001596 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1597 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /* risc op code error */
1600 if (status & (1 << 16)) {
1601 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1602 cx_clear(MO_VID_DMACNTRL, 0x11);
1603 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001604 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606
1607 /* risc1 y */
1608 if (status & 0x01) {
1609 spin_lock(&dev->slock);
1610 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001611 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 spin_unlock(&dev->slock);
1613 }
1614
1615 /* risc1 vbi */
1616 if (status & 0x08) {
1617 spin_lock(&dev->slock);
1618 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001619 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 spin_unlock(&dev->slock);
1621 }
1622
1623 /* risc2 y */
1624 if (status & 0x10) {
1625 dprintk(2,"stopper video\n");
1626 spin_lock(&dev->slock);
1627 restart_video_queue(dev,&dev->vidq);
1628 spin_unlock(&dev->slock);
1629 }
1630
1631 /* risc2 vbi */
1632 if (status & 0x80) {
1633 dprintk(2,"stopper vbi\n");
1634 spin_lock(&dev->slock);
1635 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1636 spin_unlock(&dev->slock);
1637 }
1638}
1639
David Howells7d12e782006-10-05 14:55:46 +01001640static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct cx8800_dev *dev = dev_id;
1643 struct cx88_core *core = dev->core;
1644 u32 status;
1645 int loop, handled = 0;
1646
1647 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001648 status = cx_read(MO_PCI_INTSTAT) &
1649 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (0 == status)
1651 goto out;
1652 cx_write(MO_PCI_INTSTAT, status);
1653 handled = 1;
1654
1655 if (status & core->pci_irqmask)
1656 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001657 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 cx8800_vid_irq(dev);
1659 };
1660 if (10 == loop) {
1661 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1662 core->name);
1663 cx_write(MO_PCI_INTMSK,0);
1664 }
1665
1666 out:
1667 return IRQ_RETVAL(handled);
1668}
1669
1670/* ----------------------------------------------------------- */
1671/* exported stuff */
1672
Arjan van de Venfa027c22007-02-12 00:55:33 -08001673static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 .owner = THIS_MODULE,
1676 .open = video_open,
1677 .release = video_release,
1678 .read = video_read,
1679 .poll = video_poll,
1680 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001681 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001682 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 .llseek = no_llseek,
1684};
1685
Hans Verkuila3998102008-07-21 02:57:38 -03001686static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001687 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001688 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1689 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1690 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1691 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1692 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1693 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1694 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001695 .vidioc_reqbufs = vidioc_reqbufs,
1696 .vidioc_querybuf = vidioc_querybuf,
1697 .vidioc_qbuf = vidioc_qbuf,
1698 .vidioc_dqbuf = vidioc_dqbuf,
1699 .vidioc_s_std = vidioc_s_std,
1700 .vidioc_enum_input = vidioc_enum_input,
1701 .vidioc_g_input = vidioc_g_input,
1702 .vidioc_s_input = vidioc_s_input,
1703 .vidioc_queryctrl = vidioc_queryctrl,
1704 .vidioc_g_ctrl = vidioc_g_ctrl,
1705 .vidioc_s_ctrl = vidioc_s_ctrl,
1706 .vidioc_streamon = vidioc_streamon,
1707 .vidioc_streamoff = vidioc_streamoff,
1708#ifdef CONFIG_VIDEO_V4L1_COMPAT
1709 .vidiocgmbuf = vidiocgmbuf,
1710#endif
1711 .vidioc_g_tuner = vidioc_g_tuner,
1712 .vidioc_s_tuner = vidioc_s_tuner,
1713 .vidioc_g_frequency = vidioc_g_frequency,
1714 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001715#ifdef CONFIG_VIDEO_ADV_DEBUG
1716 .vidioc_g_register = vidioc_g_register,
1717 .vidioc_s_register = vidioc_s_register,
1718#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001719};
1720
1721static struct video_device cx8800_vbi_template;
1722
1723static struct video_device cx8800_video_template = {
1724 .name = "cx8800-video",
1725 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1726 .fops = &video_fops,
1727 .minor = -1,
1728 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001729 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001730 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731};
1732
Arjan van de Venfa027c22007-02-12 00:55:33 -08001733static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 .owner = THIS_MODULE,
1736 .open = video_open,
1737 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001738 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001739 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 .llseek = no_llseek,
1741};
1742
Hans Verkuila3998102008-07-21 02:57:38 -03001743static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001744 .vidioc_querycap = radio_querycap,
1745 .vidioc_g_tuner = radio_g_tuner,
1746 .vidioc_enum_input = radio_enum_input,
1747 .vidioc_g_audio = radio_g_audio,
1748 .vidioc_s_tuner = radio_s_tuner,
1749 .vidioc_s_audio = radio_s_audio,
1750 .vidioc_s_input = radio_s_input,
1751 .vidioc_queryctrl = radio_queryctrl,
1752 .vidioc_g_ctrl = vidioc_g_ctrl,
1753 .vidioc_s_ctrl = vidioc_s_ctrl,
1754 .vidioc_g_frequency = vidioc_g_frequency,
1755 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephoa75d2042007-08-01 00:13:28 -03001756#ifdef CONFIG_VIDEO_ADV_DEBUG
1757 .vidioc_g_register = vidioc_g_register,
1758 .vidioc_s_register = vidioc_s_register,
1759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760};
1761
Hans Verkuila3998102008-07-21 02:57:38 -03001762static struct video_device cx8800_radio_template = {
1763 .name = "cx8800-radio",
1764 .type = VID_TYPE_TUNER,
1765 .fops = &radio_fops,
1766 .minor = -1,
1767 .ioctl_ops = &radio_ioctl_ops,
1768};
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770/* ----------------------------------------------------------- */
1771
1772static void cx8800_unregister_video(struct cx8800_dev *dev)
1773{
1774 if (dev->radio_dev) {
1775 if (-1 != dev->radio_dev->minor)
1776 video_unregister_device(dev->radio_dev);
1777 else
1778 video_device_release(dev->radio_dev);
1779 dev->radio_dev = NULL;
1780 }
1781 if (dev->vbi_dev) {
1782 if (-1 != dev->vbi_dev->minor)
1783 video_unregister_device(dev->vbi_dev);
1784 else
1785 video_device_release(dev->vbi_dev);
1786 dev->vbi_dev = NULL;
1787 }
1788 if (dev->video_dev) {
1789 if (-1 != dev->video_dev->minor)
1790 video_unregister_device(dev->video_dev);
1791 else
1792 video_device_release(dev->video_dev);
1793 dev->video_dev = NULL;
1794 }
1795}
1796
1797static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1798 const struct pci_device_id *pci_id)
1799{
1800 struct cx8800_dev *dev;
1801 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 int err;
1804
Panagiotis Issaris74081872006-01-11 19:40:56 -02001805 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (NULL == dev)
1807 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /* pci init */
1810 dev->pci = pci_dev;
1811 if (pci_enable_device(pci_dev)) {
1812 err = -EIO;
1813 goto fail_free;
1814 }
1815 core = cx88_core_get(dev->pci);
1816 if (NULL == core) {
1817 err = -EINVAL;
1818 goto fail_free;
1819 }
1820 dev->core = core;
1821
1822 /* print pci info */
1823 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001824 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1825 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001826 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001828 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 pci_set_master(pci_dev);
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -03001831 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1833 err = -EIO;
1834 goto fail_core;
1835 }
1836
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001837 /* Initialize VBI template */
1838 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1839 sizeof(cx8800_vbi_template) );
1840 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1841 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001845 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 /* init video dma queues */
1848 INIT_LIST_HEAD(&dev->vidq.active);
1849 INIT_LIST_HEAD(&dev->vidq.queued);
1850 dev->vidq.timeout.function = cx8800_vid_timeout;
1851 dev->vidq.timeout.data = (unsigned long)dev;
1852 init_timer(&dev->vidq.timeout);
1853 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1854 MO_VID_DMACNTRL,0x11,0x00);
1855
1856 /* init vbi dma queues */
1857 INIT_LIST_HEAD(&dev->vbiq.active);
1858 INIT_LIST_HEAD(&dev->vbiq.queued);
1859 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1860 dev->vbiq.timeout.data = (unsigned long)dev;
1861 init_timer(&dev->vbiq.timeout);
1862 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1863 MO_VID_DMACNTRL,0x88,0x00);
1864
1865 /* get irq */
1866 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001867 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001869 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 core->name,pci_dev->irq);
1871 goto fail_core;
1872 }
1873 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1874
1875 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001876
Hans Verkuil38f9d302008-07-23 05:09:15 -03001877 if (core->board.audio_chip == V4L2_IDENT_WM8775)
Steven Toth30579062006-09-25 12:43:42 -03001878 request_module("wm8775");
1879
Michael Krufky6fcecce2007-08-24 01:32:31 -03001880 switch (core->boardnr) {
1881 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Michael Krufky3c66e4e2008-04-22 14:45:35 -03001882 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
Michael Krufky6fcecce2007-08-24 01:32:31 -03001883 request_module("rtc-isl1208");
Michael Krufky8efd2e22008-04-22 14:45:14 -03001884 /* break intentionally omitted */
1885 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1886 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001887 }
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /* register v4l devices */
1890 dev->video_dev = cx88_vdev_init(core,dev->pci,
1891 &cx8800_video_template,"video");
1892 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1893 video_nr[core->nr]);
1894 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001895 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 core->name);
1897 goto fail_unreg;
1898 }
1899 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1900 core->name,dev->video_dev->minor & 0x1f);
1901
1902 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1903 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1904 vbi_nr[core->nr]);
1905 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001906 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 core->name);
1908 goto fail_unreg;
1909 }
1910 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1911 core->name,dev->vbi_dev->minor & 0x1f);
1912
Trent Piepho6a59d642007-08-15 14:41:57 -03001913 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1915 &cx8800_radio_template,"radio");
1916 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1917 radio_nr[core->nr]);
1918 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001919 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 core->name);
1921 goto fail_unreg;
1922 }
1923 printk(KERN_INFO "%s/0: registered device radio%d\n",
1924 core->name,dev->radio_dev->minor & 0x1f);
1925 }
1926
1927 /* everything worked */
1928 list_add_tail(&dev->devlist,&cx8800_devlist);
1929 pci_set_drvdata(pci_dev,dev);
1930
1931 /* initial device configuration */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001932 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001933 cx88_set_tvnorm(core,core->tvnorm);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001934 init_controls(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001935 cx88_video_mux(core,0);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001936 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001939 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001941 if (IS_ERR(core->kthread)) {
1942 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001943 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1944 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001945 }
1946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return 0;
1948
1949fail_unreg:
1950 cx8800_unregister_video(dev);
1951 free_irq(pci_dev->irq, dev);
1952fail_core:
1953 cx88_core_put(core,dev->pci);
1954fail_free:
1955 kfree(dev);
1956 return err;
1957}
1958
1959static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1960{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001961 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001962 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001965 if (core->kthread) {
1966 kthread_stop(core->kthread);
1967 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969
Marton Balintb12203d2008-03-26 02:07:35 -03001970 if (core->ir)
1971 cx88_ir_stop(core, core->ir);
1972
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001973 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 pci_disable_device(pci_dev);
1975
1976 /* unregister stuff */
1977
1978 free_irq(pci_dev->irq, dev);
1979 cx8800_unregister_video(dev);
1980 pci_set_drvdata(pci_dev, NULL);
1981
1982 /* free memory */
1983 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1984 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001985 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 kfree(dev);
1987}
1988
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001989#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1991{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001992 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 struct cx88_core *core = dev->core;
1994
1995 /* stop video+vbi capture */
1996 spin_lock(&dev->slock);
1997 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001998 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 stop_video_dma(dev);
2000 del_timer(&dev->vidq.timeout);
2001 }
2002 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002003 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 cx8800_stop_vbi_dma(dev);
2005 del_timer(&dev->vbiq.timeout);
2006 }
2007 spin_unlock(&dev->slock);
2008
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002009 if (core->ir)
2010 cx88_ir_stop(core, core->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002012 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 pci_save_state(pci_dev);
2015 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2016 pci_disable_device(pci_dev);
2017 dev->state.disabled = 1;
2018 }
2019 return 0;
2020}
2021
2022static int cx8800_resume(struct pci_dev *pci_dev)
2023{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002024 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002026 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002029 err=pci_enable_device(pci_dev);
2030 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002031 printk(KERN_ERR "%s/0: can't enable device\n",
2032 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002033 return err;
2034 }
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 dev->state.disabled = 0;
2037 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002038 err= pci_set_power_state(pci_dev, PCI_D0);
2039 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002040 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002041 pci_disable_device(pci_dev);
2042 dev->state.disabled = 1;
2043
2044 return err;
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 pci_restore_state(pci_dev);
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002049 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002050 if (core->ir)
2051 cx88_ir_start(core, core->ir);
2052
2053 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 /* restart video+vbi capture */
2056 spin_lock(&dev->slock);
2057 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002058 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 restart_video_queue(dev,&dev->vidq);
2060 }
2061 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002062 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2064 }
2065 spin_unlock(&dev->slock);
2066
2067 return 0;
2068}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071/* ----------------------------------------------------------- */
2072
Adrian Bunk408b6642005-05-01 08:59:29 -07002073static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 {
2075 .vendor = 0x14f1,
2076 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002077 .subvendor = PCI_ANY_ID,
2078 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 },{
2080 /* --- end of list --- */
2081 }
2082};
2083MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2084
2085static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002086 .name = "cx8800",
2087 .id_table = cx8800_pci_tbl,
2088 .probe = cx8800_initdev,
2089 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002090#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 .suspend = cx8800_suspend,
2092 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002093#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094};
2095
2096static int cx8800_init(void)
2097{
Trent Piepho5772f812007-08-15 14:41:59 -03002098 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 (CX88_VERSION_CODE >> 16) & 0xff,
2100 (CX88_VERSION_CODE >> 8) & 0xff,
2101 CX88_VERSION_CODE & 0xff);
2102#ifdef SNAPSHOT
2103 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2104 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2105#endif
2106 return pci_register_driver(&cx8800_pci_driver);
2107}
2108
2109static void cx8800_fini(void)
2110{
2111 pci_unregister_driver(&cx8800_pci_driver);
2112}
2113
2114module_init(cx8800_init);
2115module_exit(cx8800_fini);
2116
2117/* ----------------------------------------------------------- */
2118/*
2119 * Local variables:
2120 * c-basic-offset: 8
2121 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002122 * 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 -07002123 */