blob: 0fed5cd2cceaa7caa808afba923e70e47c765b8f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
5 *
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03008 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * - Multituner support
10 * - video_ioctl2 conversion
11 * - PAL/M fixes
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070035#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/kthread.h>
38#include <asm/div64.h>
39
40#include "cx88.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020041#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030043#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080044/* Include V4L1 specific functions. Should be removed soon */
45#include <linux/videodev.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030046#endif
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
49MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
50MODULE_LICENSE("GPL");
51
52/* ------------------------------------------------------------------ */
53
54static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57
58module_param_array(video_nr, int, NULL, 0444);
59module_param_array(vbi_nr, int, NULL, 0444);
60module_param_array(radio_nr, int, NULL, 0444);
61
62MODULE_PARM_DESC(video_nr,"video device numbers");
63MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
64MODULE_PARM_DESC(radio_nr,"radio device numbers");
65
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030066static unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067module_param(video_debug,int,0644);
68MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
69
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030070static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071module_param(irq_debug,int,0644);
72MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
73
74static unsigned int vid_limit = 16;
75module_param(vid_limit,int,0644);
76MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
77
78#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070079 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* ------------------------------------------------------------------ */
82
83static LIST_HEAD(cx8800_devlist);
84
85/* ------------------------------------------------------------------- */
86/* static data */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static struct cx8800_fmt formats[] = {
89 {
90 .name = "8 bpp, gray",
91 .fourcc = V4L2_PIX_FMT_GREY,
92 .cxformat = ColorFormatY8,
93 .depth = 8,
94 .flags = FORMAT_FLAGS_PACKED,
95 },{
96 .name = "15 bpp RGB, le",
97 .fourcc = V4L2_PIX_FMT_RGB555,
98 .cxformat = ColorFormatRGB15,
99 .depth = 16,
100 .flags = FORMAT_FLAGS_PACKED,
101 },{
102 .name = "15 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB555X,
104 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
105 .depth = 16,
106 .flags = FORMAT_FLAGS_PACKED,
107 },{
108 .name = "16 bpp RGB, le",
109 .fourcc = V4L2_PIX_FMT_RGB565,
110 .cxformat = ColorFormatRGB16,
111 .depth = 16,
112 .flags = FORMAT_FLAGS_PACKED,
113 },{
114 .name = "16 bpp RGB, be",
115 .fourcc = V4L2_PIX_FMT_RGB565X,
116 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
117 .depth = 16,
118 .flags = FORMAT_FLAGS_PACKED,
119 },{
120 .name = "24 bpp RGB, le",
121 .fourcc = V4L2_PIX_FMT_BGR24,
122 .cxformat = ColorFormatRGB24,
123 .depth = 24,
124 .flags = FORMAT_FLAGS_PACKED,
125 },{
126 .name = "32 bpp RGB, le",
127 .fourcc = V4L2_PIX_FMT_BGR32,
128 .cxformat = ColorFormatRGB32,
129 .depth = 32,
130 .flags = FORMAT_FLAGS_PACKED,
131 },{
132 .name = "32 bpp RGB, be",
133 .fourcc = V4L2_PIX_FMT_RGB32,
134 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
135 .depth = 32,
136 .flags = FORMAT_FLAGS_PACKED,
137 },{
138 .name = "4:2:2, packed, YUYV",
139 .fourcc = V4L2_PIX_FMT_YUYV,
140 .cxformat = ColorFormatYUY2,
141 .depth = 16,
142 .flags = FORMAT_FLAGS_PACKED,
143 },{
144 .name = "4:2:2, packed, UYVY",
145 .fourcc = V4L2_PIX_FMT_UYVY,
146 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
147 .depth = 16,
148 .flags = FORMAT_FLAGS_PACKED,
149 },
150};
151
152static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
153{
154 unsigned int i;
155
156 for (i = 0; i < ARRAY_SIZE(formats); i++)
157 if (formats[i].fourcc == fourcc)
158 return formats+i;
159 return NULL;
160}
161
162/* ------------------------------------------------------------------- */
163
164static const struct v4l2_queryctrl no_ctl = {
165 .name = "42",
166 .flags = V4L2_CTRL_FLAG_DISABLED,
167};
168
169static struct cx88_ctrl cx8800_ctls[] = {
170 /* --- video --- */
171 {
172 .v = {
173 .id = V4L2_CID_BRIGHTNESS,
174 .name = "Brightness",
175 .minimum = 0x00,
176 .maximum = 0xff,
177 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300178 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .type = V4L2_CTRL_TYPE_INTEGER,
180 },
181 .off = 128,
182 .reg = MO_CONTR_BRIGHT,
183 .mask = 0x00ff,
184 .shift = 0,
185 },{
186 .v = {
187 .id = V4L2_CID_CONTRAST,
188 .name = "Contrast",
189 .minimum = 0,
190 .maximum = 0xff,
191 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200192 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .type = V4L2_CTRL_TYPE_INTEGER,
194 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700195 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .reg = MO_CONTR_BRIGHT,
197 .mask = 0xff00,
198 .shift = 8,
199 },{
200 .v = {
201 .id = V4L2_CID_HUE,
202 .name = "Hue",
203 .minimum = 0,
204 .maximum = 0xff,
205 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300206 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .type = V4L2_CTRL_TYPE_INTEGER,
208 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700209 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .reg = MO_HUE,
211 .mask = 0x00ff,
212 .shift = 0,
213 },{
214 /* strictly, this only describes only U saturation.
215 * V saturation is handled specially through code.
216 */
217 .v = {
218 .id = V4L2_CID_SATURATION,
219 .name = "Saturation",
220 .minimum = 0,
221 .maximum = 0xff,
222 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200223 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .type = V4L2_CTRL_TYPE_INTEGER,
225 },
226 .off = 0,
227 .reg = MO_UV_SATURATION,
228 .mask = 0x00ff,
229 .shift = 0,
230 },{
Frej Drejhammar6d042032008-03-23 22:43:21 -0300231 .v = {
232 .id = V4L2_CID_CHROMA_AGC,
233 .name = "Chroma AGC",
234 .minimum = 0,
235 .maximum = 1,
Frej Drejhammar87a17382008-03-23 22:43:22 -0300236 .default_value = 0x1,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300237 .type = V4L2_CTRL_TYPE_BOOLEAN,
238 },
239 .reg = MO_INPUT_FORMAT,
240 .mask = 1 << 10,
241 .shift = 10,
242 }, {
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300243 .v = {
244 .id = V4L2_CID_COLOR_KILLER,
245 .name = "Color killer",
246 .minimum = 0,
247 .maximum = 1,
Frej Drejhammar0b5afdd2008-03-23 22:43:25 -0300248 .default_value = 0x1,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300249 .type = V4L2_CTRL_TYPE_BOOLEAN,
250 },
251 .reg = MO_INPUT_FORMAT,
252 .mask = 1 << 9,
253 .shift = 9,
254 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* --- audio --- */
256 .v = {
257 .id = V4L2_CID_AUDIO_MUTE,
258 .name = "Mute",
259 .minimum = 0,
260 .maximum = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200261 .default_value = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .type = V4L2_CTRL_TYPE_BOOLEAN,
263 },
264 .reg = AUD_VOL_CTL,
265 .sreg = SHADOW_AUD_VOL_CTL,
266 .mask = (1 << 6),
267 .shift = 6,
268 },{
269 .v = {
270 .id = V4L2_CID_AUDIO_VOLUME,
271 .name = "Volume",
272 .minimum = 0,
273 .maximum = 0x3f,
274 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300275 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .type = V4L2_CTRL_TYPE_INTEGER,
277 },
278 .reg = AUD_VOL_CTL,
279 .sreg = SHADOW_AUD_VOL_CTL,
280 .mask = 0x3f,
281 .shift = 0,
282 },{
283 .v = {
284 .id = V4L2_CID_AUDIO_BALANCE,
285 .name = "Balance",
286 .minimum = 0,
287 .maximum = 0x7f,
288 .step = 1,
289 .default_value = 0x40,
290 .type = V4L2_CTRL_TYPE_INTEGER,
291 },
292 .reg = AUD_BAL_CTL,
293 .sreg = SHADOW_AUD_BAL_CTL,
294 .mask = 0x7f,
295 .shift = 0,
296 }
297};
Adrian Bunk408b6642005-05-01 08:59:29 -0700298static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Michael Krufky38a27132006-06-26 23:42:39 -0300300const u32 cx88_user_ctrls[] = {
301 V4L2_CID_USER_CLASS,
302 V4L2_CID_BRIGHTNESS,
303 V4L2_CID_CONTRAST,
304 V4L2_CID_SATURATION,
305 V4L2_CID_HUE,
306 V4L2_CID_AUDIO_VOLUME,
307 V4L2_CID_AUDIO_BALANCE,
308 V4L2_CID_AUDIO_MUTE,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300309 V4L2_CID_CHROMA_AGC,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300310 V4L2_CID_COLOR_KILLER,
Michael Krufky38a27132006-06-26 23:42:39 -0300311 0
312};
313EXPORT_SYMBOL(cx88_user_ctrls);
314
315static const u32 *ctrl_classes[] = {
316 cx88_user_ctrls,
317 NULL
318};
319
Frej Drejhammar6d042032008-03-23 22:43:21 -0300320int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
Michael Krufky38a27132006-06-26 23:42:39 -0300321{
322 int i;
323
324 if (qctrl->id < V4L2_CID_BASE ||
325 qctrl->id >= V4L2_CID_LASTP1)
326 return -EINVAL;
327 for (i = 0; i < CX8800_CTLS; i++)
328 if (cx8800_ctls[i].v.id == qctrl->id)
329 break;
330 if (i == CX8800_CTLS) {
331 *qctrl = no_ctl;
332 return 0;
333 }
334 *qctrl = cx8800_ctls[i].v;
Frej Drejhammar6d042032008-03-23 22:43:21 -0300335 /* Report chroma AGC as inactive when SECAM is selected */
336 if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
337 core->tvnorm & V4L2_STD_SECAM)
338 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
339
Michael Krufky38a27132006-06-26 23:42:39 -0300340 return 0;
341}
342EXPORT_SYMBOL(cx8800_ctrl_query);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/* ------------------------------------------------------------------- */
345/* resource management */
346
347static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
348{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700349 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (fh->resources & bit)
351 /* have it already allocated */
352 return 1;
353
354 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200355 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (dev->resources & bit) {
357 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200358 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360 }
361 /* it's free, grab it */
362 fh->resources |= bit;
363 dev->resources |= bit;
364 dprintk(1,"res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200365 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 1;
367}
368
369static
370int res_check(struct cx8800_fh *fh, unsigned int bit)
371{
372 return (fh->resources & bit);
373}
374
375static
376int res_locked(struct cx8800_dev *dev, unsigned int bit)
377{
378 return (dev->resources & bit);
379}
380
381static
382void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
383{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700384 struct cx88_core *core = dev->core;
Eric Sesterhennae246012006-03-13 13:17:11 -0300385 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Ingo Molnar3593cab2006-02-07 06:49:14 -0200387 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 fh->resources &= ~bits;
389 dev->resources &= ~bits;
390 dprintk(1,"res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200391 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/* ------------------------------------------------------------------ */
395
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300396int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700398 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300401 input, INPUT(input).vmux,
402 INPUT(input).gpio0,INPUT(input).gpio1,
403 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700404 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300405 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
406 cx_write(MO_GP3_IO, INPUT(input).gpio3);
407 cx_write(MO_GP0_IO, INPUT(input).gpio0);
408 cx_write(MO_GP1_IO, INPUT(input).gpio1);
409 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Trent Piepho6a59d642007-08-15 14:41:57 -0300411 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 case CX88_VMUX_SVIDEO:
413 cx_set(MO_AFECFG_IO, 0x00000001);
414 cx_set(MO_INPUT_FORMAT, 0x00010010);
415 cx_set(MO_FILTER_EVEN, 0x00002020);
416 cx_set(MO_FILTER_ODD, 0x00002020);
417 break;
418 default:
419 cx_clear(MO_AFECFG_IO, 0x00000001);
420 cx_clear(MO_INPUT_FORMAT, 0x00010010);
421 cx_clear(MO_FILTER_EVEN, 0x00002020);
422 cx_clear(MO_FILTER_ODD, 0x00002020);
423 break;
424 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300425
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300426 /* if there are audioroutes defined, we have an external
427 ADC to deal with audio */
Ricardo Cerqueira7b27d452007-09-30 13:02:49 -0300428
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300429 if (INPUT(input).audioroute) {
Ricardo Cerqueira7b27d452007-09-30 13:02:49 -0300430
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300431 /* cx2388's C-ADC is connected to the tuner only.
432 When used with S-Video, that ADC is busy dealing with
433 chroma, so an external must be used for baseband audio */
434
435 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
436 INPUT(input).type != CX88_RADIO) {
437 /* "ADC mode" */
438 cx_write(AUD_I2SCNTL, 0x1);
Michael Krufkyf24546a2006-10-16 16:51:11 -0300439 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300440 } else {
441 /* Normal mode */
442 cx_write(AUD_I2SCNTL, 0x0);
Michael Krufkyf24546a2006-10-16 16:51:11 -0300443 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300444 }
445
446 /* The wm8775 module has the "2" route hardwired into
447 the initialization. Some boards may use different
448 routes for different inputs. HVR-1300 surely does */
449 if (core->board.audio_chip &&
450 core->board.audio_chip == AUDIO_CHIP_WM8775) {
451 struct v4l2_routing route;
452
453 route.input = INPUT(input).audioroute;
454 cx88_call_i2c_clients(core,
Mauro Carvalho Chehabd8f69972007-12-17 10:35:59 -0300455 VIDIOC_INT_S_AUDIO_ROUTING, &route);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300456
457 }
458
Michael Krufkyf24546a2006-10-16 16:51:11 -0300459 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300463EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465/* ------------------------------------------------------------------ */
466
467static int start_video_dma(struct cx8800_dev *dev,
468 struct cx88_dmaqueue *q,
469 struct cx88_buffer *buf)
470{
471 struct cx88_core *core = dev->core;
472
473 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700474 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700476 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
478
479 /* reset counter */
480 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
481 q->count = 1;
482
483 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300484 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700485
486 /* Enables corresponding bits at PCI_INT_STAT:
487 bits 0 to 4: video, audio, transport stream, VIP, Host
488 bit 7: timer
489 bits 8 and 9: DMA complete for: SRC, DST
490 bits 10 and 11: BERR signal asserted for RISC: RD, WR
491 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 cx_set(MO_VID_INTMSK, 0x0f0011);
494
495 /* enable capture */
496 cx_set(VID_CAPTURE_CONTROL,0x06);
497
498 /* start dma */
499 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700500 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 return 0;
503}
504
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300505#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int stop_video_dma(struct cx8800_dev *dev)
507{
508 struct cx88_core *core = dev->core;
509
510 /* stop dma */
511 cx_clear(MO_VID_DMACNTRL, 0x11);
512
513 /* disable capture */
514 cx_clear(VID_CAPTURE_CONTROL,0x06);
515
516 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300517 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 cx_clear(MO_VID_INTMSK, 0x0f0011);
519 return 0;
520}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300521#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523static int restart_video_queue(struct cx8800_dev *dev,
524 struct cx88_dmaqueue *q)
525{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700526 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct cx88_buffer *buf, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800530 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
532 buf, buf->vb.i);
533 start_video_dma(dev, q, buf);
Trent Piepho8bb629e22007-10-10 05:37:40 -0300534 list_for_each_entry(buf, &q->active, vb.queue)
535 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
537 return 0;
538 }
539
540 prev = NULL;
541 for (;;) {
542 if (list_empty(&q->queued))
543 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800544 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700546 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300548 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 buf->count = q->count++;
550 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
551 dprintk(2,"[%p/%d] restart_queue - first active\n",
552 buf,buf->vb.i);
553
554 } else if (prev->vb.width == buf->vb.width &&
555 prev->vb.height == buf->vb.height &&
556 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700557 list_move_tail(&buf->vb.queue, &q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300558 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 buf->count = q->count++;
560 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
561 dprintk(2,"[%p/%d] restart_queue - move to active\n",
562 buf,buf->vb.i);
563 } else {
564 return 0;
565 }
566 prev = buf;
567 }
568}
569
570/* ------------------------------------------------------------------ */
571
572static int
573buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
574{
575 struct cx8800_fh *fh = q->priv_data;
576
577 *size = fh->fmt->depth*fh->width*fh->height >> 3;
578 if (0 == *count)
579 *count = 32;
580 while (*size * *count > vid_limit * 1024 * 1024)
581 (*count)--;
582 return 0;
583}
584
585static int
586buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
587 enum v4l2_field field)
588{
589 struct cx8800_fh *fh = q->priv_data;
590 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700591 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300593 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int rc, init_buffer = 0;
595
596 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700597 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
598 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -EINVAL;
600 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
601 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
602 return -EINVAL;
603
604 if (buf->fmt != fh->fmt ||
605 buf->vb.width != fh->width ||
606 buf->vb.height != fh->height ||
607 buf->vb.field != field) {
608 buf->fmt = fh->fmt;
609 buf->vb.width = fh->width;
610 buf->vb.height = fh->height;
611 buf->vb.field = field;
612 init_buffer = 1;
613 }
614
Brandon Philips0fc06862007-11-06 20:02:36 -0300615 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300617 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto fail;
619 }
620
621 if (init_buffer) {
622 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
623 switch (buf->vb.field) {
624 case V4L2_FIELD_TOP:
625 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300626 dma->sglist, 0, UNSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 buf->bpl, 0, buf->vb.height);
628 break;
629 case V4L2_FIELD_BOTTOM:
630 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300631 dma->sglist, UNSET, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 buf->bpl, 0, buf->vb.height);
633 break;
634 case V4L2_FIELD_INTERLACED:
635 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300636 dma->sglist, 0, buf->bpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 buf->bpl, buf->bpl,
638 buf->vb.height >> 1);
639 break;
640 case V4L2_FIELD_SEQ_TB:
641 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300642 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 0, buf->bpl * (buf->vb.height >> 1),
644 buf->bpl, 0,
645 buf->vb.height >> 1);
646 break;
647 case V4L2_FIELD_SEQ_BT:
648 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300649 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 buf->bpl * (buf->vb.height >> 1), 0,
651 buf->bpl, 0,
652 buf->vb.height >> 1);
653 break;
654 default:
655 BUG();
656 }
657 }
658 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
659 buf, buf->vb.i,
660 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
661 (unsigned long)buf->risc.dma);
662
Brandon Philips0fc06862007-11-06 20:02:36 -0300663 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665
666 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300667 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return rc;
669}
670
671static void
672buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
673{
674 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
675 struct cx88_buffer *prev;
676 struct cx8800_fh *fh = vq->priv_data;
677 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700678 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct cx88_dmaqueue *q = &dev->vidq;
680
681 /* add jump to stopper */
682 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
683 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
684
685 if (!list_empty(&q->queued)) {
686 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300687 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
689 buf, buf->vb.i);
690
691 } else if (list_empty(&q->active)) {
692 list_add_tail(&buf->vb.queue,&q->active);
693 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300694 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 buf->count = q->count++;
696 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
697 dprintk(2,"[%p/%d] buffer_queue - first active\n",
698 buf, buf->vb.i);
699
700 } else {
701 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
702 if (prev->vb.width == buf->vb.width &&
703 prev->vb.height == buf->vb.height &&
704 prev->fmt == buf->fmt) {
705 list_add_tail(&buf->vb.queue,&q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300706 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 buf->count = q->count++;
708 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
709 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
710 buf, buf->vb.i);
711
712 } else {
713 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300714 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
716 buf, buf->vb.i);
717 }
718 }
719}
720
721static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
722{
723 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300725 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Adrian Bunk408b6642005-05-01 08:59:29 -0700728static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 .buf_setup = buffer_setup,
730 .buf_prepare = buffer_prepare,
731 .buf_queue = buffer_queue,
732 .buf_release = buffer_release,
733};
734
735/* ------------------------------------------------------------------ */
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738/* ------------------------------------------------------------------ */
739
740static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
741{
742 switch (fh->type) {
743 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
744 return &fh->vidq;
745 case V4L2_BUF_TYPE_VBI_CAPTURE:
746 return &fh->vbiq;
747 default:
748 BUG();
749 return NULL;
750 }
751}
752
753static int get_ressource(struct cx8800_fh *fh)
754{
755 switch (fh->type) {
756 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
757 return RESOURCE_VIDEO;
758 case V4L2_BUF_TYPE_VBI_CAPTURE:
759 return RESOURCE_VBI;
760 default:
761 BUG();
762 return 0;
763 }
764}
765
766static int video_open(struct inode *inode, struct file *file)
767{
768 int minor = iminor(inode);
769 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700770 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct cx8800_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 enum v4l2_buf_type type = 0;
773 int radio = 0;
774
Trent Piepho8bb629e22007-10-10 05:37:40 -0300775 list_for_each_entry(h, &cx8800_devlist, devlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (h->video_dev->minor == minor) {
777 dev = h;
778 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
779 }
780 if (h->vbi_dev->minor == minor) {
781 dev = h;
782 type = V4L2_BUF_TYPE_VBI_CAPTURE;
783 }
784 if (h->radio_dev &&
785 h->radio_dev->minor == minor) {
786 radio = 1;
787 dev = h;
788 }
789 }
790 if (NULL == dev)
791 return -ENODEV;
792
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700793 core = dev->core;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dprintk(1,"open minor=%d radio=%d type=%s\n",
796 minor,radio,v4l2_type_names[type]);
797
798 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200799 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (NULL == fh)
801 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 file->private_data = fh;
803 fh->dev = dev;
804 fh->radio = radio;
805 fh->type = type;
806 fh->width = 320;
807 fh->height = 240;
808 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
809
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300810 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
811 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 V4L2_BUF_TYPE_VIDEO_CAPTURE,
813 V4L2_FIELD_INTERLACED,
814 sizeof(struct cx88_buffer),
815 fh);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300816 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
817 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 V4L2_BUF_TYPE_VBI_CAPTURE,
819 V4L2_FIELD_SEQ_TB,
820 sizeof(struct cx88_buffer),
821 fh);
822
823 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 dprintk(1,"video_open: setting radio device\n");
Trent Piepho6a59d642007-08-15 14:41:57 -0300825 cx_write(MO_GP3_IO, core->board.radio.gpio3);
826 cx_write(MO_GP0_IO, core->board.radio.gpio0);
827 cx_write(MO_GP1_IO, core->board.radio.gpio1);
828 cx_write(MO_GP2_IO, core->board.radio.gpio2);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700829 core->tvaudio = WW_FM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 cx88_set_tvaudio(core);
831 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700832 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800839video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct cx8800_fh *fh = file->private_data;
842
843 switch (fh->type) {
844 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
845 if (res_locked(fh->dev,RESOURCE_VIDEO))
846 return -EBUSY;
847 return videobuf_read_one(&fh->vidq, data, count, ppos,
848 file->f_flags & O_NONBLOCK);
849 case V4L2_BUF_TYPE_VBI_CAPTURE:
850 if (!res_get(fh->dev,fh,RESOURCE_VBI))
851 return -EBUSY;
852 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
853 file->f_flags & O_NONBLOCK);
854 default:
855 BUG();
856 return 0;
857 }
858}
859
860static unsigned int
861video_poll(struct file *file, struct poll_table_struct *wait)
862{
863 struct cx8800_fh *fh = file->private_data;
864 struct cx88_buffer *buf;
865
866 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
867 if (!res_get(fh->dev,fh,RESOURCE_VBI))
868 return POLLERR;
869 return videobuf_poll_stream(file, &fh->vbiq, wait);
870 }
871
872 if (res_check(fh,RESOURCE_VIDEO)) {
873 /* streaming capture */
874 if (list_empty(&fh->vidq.stream))
875 return POLLERR;
876 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
877 } else {
878 /* read() capture */
879 buf = (struct cx88_buffer*)fh->vidq.read_buf;
880 if (NULL == buf)
881 return POLLERR;
882 }
883 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300884 if (buf->vb.state == VIDEOBUF_DONE ||
885 buf->vb.state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return POLLIN|POLLRDNORM;
887 return 0;
888}
889
890static int video_release(struct inode *inode, struct file *file)
891{
892 struct cx8800_fh *fh = file->private_data;
893 struct cx8800_dev *dev = fh->dev;
894
895 /* turn off overlay */
896 if (res_check(fh, RESOURCE_OVERLAY)) {
897 /* FIXME */
898 res_free(dev,fh,RESOURCE_OVERLAY);
899 }
900
901 /* stop video capture */
902 if (res_check(fh, RESOURCE_VIDEO)) {
903 videobuf_queue_cancel(&fh->vidq);
904 res_free(dev,fh,RESOURCE_VIDEO);
905 }
906 if (fh->vidq.read_buf) {
907 buffer_release(&fh->vidq,fh->vidq.read_buf);
908 kfree(fh->vidq.read_buf);
909 }
910
911 /* stop vbi capture */
912 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -0300913 videobuf_stop(&fh->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 res_free(dev,fh,RESOURCE_VBI);
915 }
916
917 videobuf_mmap_free(&fh->vidq);
918 videobuf_mmap_free(&fh->vbiq);
919 file->private_data = NULL;
920 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700921
922 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925}
926
927static int
928video_mmap(struct file *file, struct vm_area_struct * vma)
929{
930 struct cx8800_fh *fh = file->private_data;
931
932 return videobuf_mmap_mapper(get_queue(fh), vma);
933}
934
935/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300936/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300938int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300940 struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 u32 value;
942 int i;
943
944 for (i = 0; i < CX8800_CTLS; i++)
945 if (cx8800_ctls[i].v.id == ctl->id)
946 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300947 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EINVAL;
949
950 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
951 switch (ctl->id) {
952 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300953 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
954 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 case V4L2_CID_AUDIO_VOLUME:
957 ctl->value = 0x3f - (value & 0x3f);
958 break;
959 default:
960 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
961 break;
962 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300963 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
964 ctl->id, c->v.name, ctl->value, c->reg,
965 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
967}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300968EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300970int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200973 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300975
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200976 for (i = 0; i < CX8800_CTLS; i++) {
977 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200979 }
980 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300981 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return -EINVAL;
983
984 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700985 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700987 ctl->value = c->v.maximum;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200988 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 switch (ctl->id) {
990 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300991 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
993 case V4L2_CID_AUDIO_VOLUME:
994 value = 0x3f - (ctl->value & 0x3f);
995 break;
996 case V4L2_CID_SATURATION:
997 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200998
999 value = ((ctl->value - c->off) << c->shift) & c->mask;
1000
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001001 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001002 /* For SECAM, both U and V sat should be equal */
1003 value=value<<8|value;
1004 } else {
1005 /* Keeps U Saturation proportional to V Sat */
1006 value=(value*0x5a)/0x7f<<8|value;
1007 }
1008 mask=0xffff;
1009 break;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001010 case V4L2_CID_CHROMA_AGC:
1011 /* Do not allow chroma AGC to be enabled for SECAM */
1012 value = ((ctl->value - c->off) << c->shift) & c->mask;
1013 if (core->tvnorm & V4L2_STD_SECAM && value)
1014 return -EINVAL;
1015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 default:
1017 value = ((ctl->value - c->off) << c->shift) & c->mask;
1018 break;
1019 }
Ian Pickworth6457af52006-02-27 00:09:45 -03001020 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1021 ctl->id, c->v.name, ctl->value, c->reg, value,
1022 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001024 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001026 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028 return 0;
1029}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001030EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001032static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001034 struct v4l2_control ctrl;
1035 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001037 for (i = 0; i < CX8800_CTLS; i++) {
1038 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -03001039 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001040
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001041 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001046/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Hans Verkuil78b526a2008-05-28 12:16:41 -03001048static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001049 struct v4l2_format *f)
1050{
1051 struct cx8800_fh *fh = priv;
1052
1053 f->fmt.pix.width = fh->width;
1054 f->fmt.pix.height = fh->height;
1055 f->fmt.pix.field = fh->vidq.field;
1056 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1057 f->fmt.pix.bytesperline =
1058 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1059 f->fmt.pix.sizeimage =
1060 f->fmt.pix.height * f->fmt.pix.bytesperline;
1061 return 0;
1062}
1063
Hans Verkuil78b526a2008-05-28 12:16:41 -03001064static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct v4l2_format *f)
1066{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001067 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1068 struct cx8800_fmt *fmt;
1069 enum v4l2_field field;
1070 unsigned int maxw, maxh;
1071
1072 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1073 if (NULL == fmt)
1074 return -EINVAL;
1075
1076 field = f->fmt.pix.field;
1077 maxw = norm_maxw(core->tvnorm);
1078 maxh = norm_maxh(core->tvnorm);
1079
1080 if (V4L2_FIELD_ANY == field) {
1081 field = (f->fmt.pix.height > maxh/2)
1082 ? V4L2_FIELD_INTERLACED
1083 : V4L2_FIELD_BOTTOM;
1084 }
1085
1086 switch (field) {
1087 case V4L2_FIELD_TOP:
1088 case V4L2_FIELD_BOTTOM:
1089 maxh = maxh / 2;
1090 break;
1091 case V4L2_FIELD_INTERLACED:
1092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 default:
1094 return -EINVAL;
1095 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001096
1097 f->fmt.pix.field = field;
1098 if (f->fmt.pix.height < 32)
1099 f->fmt.pix.height = 32;
1100 if (f->fmt.pix.height > maxh)
1101 f->fmt.pix.height = maxh;
1102 if (f->fmt.pix.width < 48)
1103 f->fmt.pix.width = 48;
1104 if (f->fmt.pix.width > maxw)
1105 f->fmt.pix.width = maxw;
1106 f->fmt.pix.width &= ~0x03;
1107 f->fmt.pix.bytesperline =
1108 (f->fmt.pix.width * fmt->depth) >> 3;
1109 f->fmt.pix.sizeimage =
1110 f->fmt.pix.height * f->fmt.pix.bytesperline;
1111
1112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Hans Verkuil78b526a2008-05-28 12:16:41 -03001115static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001116 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001118 struct cx8800_fh *fh = priv;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001119 int err = vidioc_try_fmt_vid_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001120
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001121 if (0 != err)
1122 return err;
1123 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1124 fh->width = f->fmt.pix.width;
1125 fh->height = f->fmt.pix.height;
1126 fh->vidq.field = f->fmt.pix.field;
1127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001130static int vidioc_querycap (struct file *file, void *priv,
1131 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001133 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001136 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001137 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001138 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1139 cap->version = CX88_VERSION_CODE;
1140 cap->capabilities =
1141 V4L2_CAP_VIDEO_CAPTURE |
1142 V4L2_CAP_READWRITE |
1143 V4L2_CAP_STREAMING |
1144 V4L2_CAP_VBI_CAPTURE;
Trent Piepho6a59d642007-08-15 14:41:57 -03001145 if (UNSET != core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001146 cap->capabilities |= V4L2_CAP_TUNER;
1147 return 0;
1148}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001149
Hans Verkuil78b526a2008-05-28 12:16:41 -03001150static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001151 struct v4l2_fmtdesc *f)
1152{
1153 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1154 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001156 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1157 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001159 return 0;
1160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001162#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001163static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001164{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001165 struct cx8800_fh *fh = priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001166
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001167 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001171static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001173 struct cx8800_fh *fh = priv;
1174 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001177static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1178{
1179 struct cx8800_fh *fh = priv;
1180 return (videobuf_querybuf(get_queue(fh), p));
1181}
1182
1183static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1184{
1185 struct cx8800_fh *fh = priv;
1186 return (videobuf_qbuf(get_queue(fh), p));
1187}
1188
1189static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1190{
1191 struct cx8800_fh *fh = priv;
1192 return (videobuf_dqbuf(get_queue(fh), p,
1193 file->f_flags & O_NONBLOCK));
1194}
1195
1196static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1197{
1198 struct cx8800_fh *fh = priv;
1199 struct cx8800_dev *dev = fh->dev;
1200
1201 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1202 return -EINVAL;
1203 if (unlikely(i != fh->type))
1204 return -EINVAL;
1205
1206 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1207 return -EBUSY;
1208 return videobuf_streamon(get_queue(fh));
1209}
1210
1211static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1212{
1213 struct cx8800_fh *fh = priv;
1214 struct cx8800_dev *dev = fh->dev;
1215 int err, res;
1216
1217 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1218 return -EINVAL;
1219 if (i != fh->type)
1220 return -EINVAL;
1221
1222 res = get_ressource(fh);
1223 err = videobuf_streamoff(get_queue(fh));
1224 if (err < 0)
1225 return err;
1226 res_free(dev,fh,res);
1227 return 0;
1228}
1229
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001230static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001231{
1232 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1233
1234 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001235 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001236 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001237
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001238 return 0;
1239}
1240
1241/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001242int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001243{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001244 static const char *iname[] = {
1245 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1246 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1247 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1248 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1249 [ CX88_VMUX_SVIDEO ] = "S-Video",
1250 [ CX88_VMUX_TELEVISION ] = "Television",
1251 [ CX88_VMUX_CABLE ] = "Cable TV",
1252 [ CX88_VMUX_DVB ] = "DVB",
1253 [ CX88_VMUX_DEBUG ] = "for debug only",
1254 };
1255 unsigned int n;
1256
1257 n = i->index;
1258 if (n >= 4)
1259 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001260 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001261 return -EINVAL;
1262 memset(i,0,sizeof(*i));
1263 i->index = n;
1264 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001265 strcpy(i->name,iname[INPUT(n).type]);
1266 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1267 (CX88_VMUX_CABLE == INPUT(n).type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001268 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001269 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001270 return 0;
1271}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001272EXPORT_SYMBOL(cx88_enum_input);
1273
1274static int vidioc_enum_input (struct file *file, void *priv,
1275 struct v4l2_input *i)
1276{
1277 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1278 return cx88_enum_input (core,i);
1279}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001280
1281static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1282{
1283 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1284
1285 *i = core->input;
1286 return 0;
1287}
1288
1289static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1290{
1291 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1292
1293 if (i >= 4)
1294 return -EINVAL;
1295
1296 mutex_lock(&core->lock);
1297 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001298 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001299 mutex_unlock(&core->lock);
1300 return 0;
1301}
1302
1303
1304
1305static int vidioc_queryctrl (struct file *file, void *priv,
1306 struct v4l2_queryctrl *qctrl)
1307{
Frej Drejhammar6d042032008-03-23 22:43:21 -03001308 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1309
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001310 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1311 if (unlikely(qctrl->id == 0))
1312 return -EINVAL;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001313 return cx8800_ctrl_query(core, qctrl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001314}
1315
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001316static int vidioc_g_ctrl (struct file *file, void *priv,
1317 struct v4l2_control *ctl)
1318{
1319 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1320 return
1321 cx88_get_control(core,ctl);
1322}
1323
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001324static int vidioc_s_ctrl (struct file *file, void *priv,
1325 struct v4l2_control *ctl)
1326{
1327 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001328 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001329 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001330}
1331
1332static int vidioc_g_tuner (struct file *file, void *priv,
1333 struct v4l2_tuner *t)
1334{
1335 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1336 u32 reg;
1337
Trent Piepho6a59d642007-08-15 14:41:57 -03001338 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001339 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001340 if (0 != t->index)
1341 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001342
1343 strcpy(t->name, "Television");
1344 t->type = V4L2_TUNER_ANALOG_TV;
1345 t->capability = V4L2_TUNER_CAP_NORM;
1346 t->rangehigh = 0xffffffffUL;
1347
1348 cx88_get_stereo(core ,t);
1349 reg = cx_read(MO_DEVICE_STATUS);
1350 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1351 return 0;
1352}
1353
1354static int vidioc_s_tuner (struct file *file, void *priv,
1355 struct v4l2_tuner *t)
1356{
1357 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1358
Trent Piepho6a59d642007-08-15 14:41:57 -03001359 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001360 return -EINVAL;
1361 if (0 != t->index)
1362 return -EINVAL;
1363
1364 cx88_set_stereo(core, t->audmode, 1);
1365 return 0;
1366}
1367
1368static int vidioc_g_frequency (struct file *file, void *priv,
1369 struct v4l2_frequency *f)
1370{
1371 struct cx8800_fh *fh = priv;
1372 struct cx88_core *core = fh->dev->core;
1373
Trent Piepho6a59d642007-08-15 14:41:57 -03001374 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001375 return -EINVAL;
1376
1377 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1378 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1379 f->frequency = core->freq;
1380
1381 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1382
1383 return 0;
1384}
1385
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001386int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001387 struct v4l2_frequency *f)
1388{
Trent Piepho6a59d642007-08-15 14:41:57 -03001389 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001390 return -EINVAL;
1391 if (unlikely(f->tuner != 0))
1392 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001393
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001394 mutex_lock(&core->lock);
1395 core->freq = f->frequency;
1396 cx88_newstation(core);
1397 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1398
1399 /* When changing channels it is required to reset TVAUDIO */
1400 msleep (10);
1401 cx88_set_tvaudio(core);
1402
1403 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001404
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001405 return 0;
1406}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001407EXPORT_SYMBOL(cx88_set_freq);
1408
1409static int vidioc_s_frequency (struct file *file, void *priv,
1410 struct v4l2_frequency *f)
1411{
1412 struct cx8800_fh *fh = priv;
1413 struct cx88_core *core = fh->dev->core;
1414
1415 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1416 return -EINVAL;
1417 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1418 return -EINVAL;
1419
1420 return
1421 cx88_set_freq (core,f);
1422}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001423
Trent Piephodbbff482007-01-22 23:31:53 -03001424#ifdef CONFIG_VIDEO_ADV_DEBUG
1425static int vidioc_g_register (struct file *file, void *fh,
1426 struct v4l2_register *reg)
1427{
1428 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1429
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001430 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001431 return -EINVAL;
1432 /* cx2388x has a 24-bit register space */
1433 reg->val = cx_read(reg->reg&0xffffff);
1434 return 0;
1435}
1436
1437static int vidioc_s_register (struct file *file, void *fh,
1438 struct v4l2_register *reg)
1439{
1440 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1441
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001442 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001443 return -EINVAL;
Trent Piephodbbff482007-01-22 23:31:53 -03001444 cx_write(reg->reg&0xffffff, reg->val);
1445 return 0;
1446}
1447#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001450/* RADIO ESPECIFIC IOCTLS */
1451/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001453static int radio_querycap (struct file *file, void *priv,
1454 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001456 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct cx88_core *core = dev->core;
1458
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001459 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001460 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001461 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1462 cap->version = CX88_VERSION_CODE;
1463 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001467static int radio_g_tuner (struct file *file, void *priv,
1468 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001470 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1471
1472 if (unlikely(t->index > 0))
1473 return -EINVAL;
1474
1475 strcpy(t->name, "Radio");
1476 t->type = V4L2_TUNER_RADIO;
1477
1478 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1479 return 0;
1480}
1481
1482static int radio_enum_input (struct file *file, void *priv,
1483 struct v4l2_input *i)
1484{
1485 if (i->index != 0)
1486 return -EINVAL;
1487 strcpy(i->name,"Radio");
1488 i->type = V4L2_INPUT_TYPE_TUNER;
1489
1490 return 0;
1491}
1492
1493static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1494{
1495 if (unlikely(a->index))
1496 return -EINVAL;
1497
1498 memset(a,0,sizeof(*a));
1499 strcpy(a->name,"Radio");
1500 return 0;
1501}
1502
1503/* FIXME: Should add a standard for radio */
1504
1505static int radio_s_tuner (struct file *file, void *priv,
1506 struct v4l2_tuner *t)
1507{
1508 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1509
1510 if (0 != t->index)
1511 return -EINVAL;
1512
1513 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1514
1515 return 0;
1516}
1517
1518static int radio_s_audio (struct file *file, void *fh,
1519 struct v4l2_audio *a)
1520{
1521 return 0;
1522}
1523
1524static int radio_s_input (struct file *file, void *fh, unsigned int i)
1525{
1526 return 0;
1527}
1528
1529static int radio_queryctrl (struct file *file, void *priv,
1530 struct v4l2_queryctrl *c)
1531{
1532 int i;
1533
1534 if (c->id < V4L2_CID_BASE ||
1535 c->id >= V4L2_CID_LASTP1)
1536 return -EINVAL;
1537 if (c->id == V4L2_CID_AUDIO_MUTE) {
1538 for (i = 0; i < CX8800_CTLS; i++)
1539 if (cx8800_ctls[i].v.id == c->id)
1540 break;
1541 *c = cx8800_ctls[i].v;
1542 } else
1543 *c = no_ctl;
1544 return 0;
1545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547/* ----------------------------------------------------------- */
1548
1549static void cx8800_vid_timeout(unsigned long data)
1550{
1551 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1552 struct cx88_core *core = dev->core;
1553 struct cx88_dmaqueue *q = &dev->vidq;
1554 struct cx88_buffer *buf;
1555 unsigned long flags;
1556
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001557 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 cx_clear(MO_VID_DMACNTRL, 0x11);
1560 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1561
1562 spin_lock_irqsave(&dev->slock,flags);
1563 while (!list_empty(&q->active)) {
1564 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1565 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03001566 buf->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 wake_up(&buf->vb.done);
1568 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1569 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1570 }
1571 restart_video_queue(dev,q);
1572 spin_unlock_irqrestore(&dev->slock,flags);
1573}
1574
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001575static char *cx88_vid_irqs[32] = {
1576 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1577 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1578 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1579 "y_sync", "u_sync", "v_sync", "vbi_sync",
1580 "opc_err", "par_err", "rip_err", "pci_abort",
1581};
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583static void cx8800_vid_irq(struct cx8800_dev *dev)
1584{
1585 struct cx88_core *core = dev->core;
1586 u32 status, mask, count;
1587
1588 status = cx_read(MO_VID_INTSTAT);
1589 mask = cx_read(MO_VID_INTMSK);
1590 if (0 == (status & mask))
1591 return;
1592 cx_write(MO_VID_INTSTAT, status);
1593 if (irq_debug || (status & mask & ~0xff))
1594 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001595 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1596 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /* risc op code error */
1599 if (status & (1 << 16)) {
1600 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1601 cx_clear(MO_VID_DMACNTRL, 0x11);
1602 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001603 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605
1606 /* risc1 y */
1607 if (status & 0x01) {
1608 spin_lock(&dev->slock);
1609 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001610 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 spin_unlock(&dev->slock);
1612 }
1613
1614 /* risc1 vbi */
1615 if (status & 0x08) {
1616 spin_lock(&dev->slock);
1617 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001618 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 spin_unlock(&dev->slock);
1620 }
1621
1622 /* risc2 y */
1623 if (status & 0x10) {
1624 dprintk(2,"stopper video\n");
1625 spin_lock(&dev->slock);
1626 restart_video_queue(dev,&dev->vidq);
1627 spin_unlock(&dev->slock);
1628 }
1629
1630 /* risc2 vbi */
1631 if (status & 0x80) {
1632 dprintk(2,"stopper vbi\n");
1633 spin_lock(&dev->slock);
1634 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1635 spin_unlock(&dev->slock);
1636 }
1637}
1638
David Howells7d12e782006-10-05 14:55:46 +01001639static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct cx8800_dev *dev = dev_id;
1642 struct cx88_core *core = dev->core;
1643 u32 status;
1644 int loop, handled = 0;
1645
1646 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001647 status = cx_read(MO_PCI_INTSTAT) &
1648 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if (0 == status)
1650 goto out;
1651 cx_write(MO_PCI_INTSTAT, status);
1652 handled = 1;
1653
1654 if (status & core->pci_irqmask)
1655 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001656 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 cx8800_vid_irq(dev);
1658 };
1659 if (10 == loop) {
1660 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1661 core->name);
1662 cx_write(MO_PCI_INTMSK,0);
1663 }
1664
1665 out:
1666 return IRQ_RETVAL(handled);
1667}
1668
1669/* ----------------------------------------------------------- */
1670/* exported stuff */
1671
Arjan van de Venfa027c22007-02-12 00:55:33 -08001672static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 .owner = THIS_MODULE,
1675 .open = video_open,
1676 .release = video_release,
1677 .read = video_read,
1678 .poll = video_poll,
1679 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001680 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001681 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 .llseek = no_llseek,
1683};
1684
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001685static struct video_device cx8800_vbi_template;
Adrian Bunk408b6642005-05-01 08:59:29 -07001686static struct video_device cx8800_video_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001688 .name = "cx8800-video",
1689 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1690 .fops = &video_fops,
1691 .minor = -1,
1692 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001693 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1694 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1695 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1696 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1697 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1698 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1699 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001700 .vidioc_reqbufs = vidioc_reqbufs,
1701 .vidioc_querybuf = vidioc_querybuf,
1702 .vidioc_qbuf = vidioc_qbuf,
1703 .vidioc_dqbuf = vidioc_dqbuf,
1704 .vidioc_s_std = vidioc_s_std,
1705 .vidioc_enum_input = vidioc_enum_input,
1706 .vidioc_g_input = vidioc_g_input,
1707 .vidioc_s_input = vidioc_s_input,
1708 .vidioc_queryctrl = vidioc_queryctrl,
1709 .vidioc_g_ctrl = vidioc_g_ctrl,
1710 .vidioc_s_ctrl = vidioc_s_ctrl,
1711 .vidioc_streamon = vidioc_streamon,
1712 .vidioc_streamoff = vidioc_streamoff,
1713#ifdef CONFIG_VIDEO_V4L1_COMPAT
1714 .vidiocgmbuf = vidiocgmbuf,
1715#endif
1716 .vidioc_g_tuner = vidioc_g_tuner,
1717 .vidioc_s_tuner = vidioc_s_tuner,
1718 .vidioc_g_frequency = vidioc_g_frequency,
1719 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001720#ifdef CONFIG_VIDEO_ADV_DEBUG
1721 .vidioc_g_register = vidioc_g_register,
1722 .vidioc_s_register = vidioc_s_register,
1723#endif
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001724 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001725 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726};
1727
Arjan van de Venfa027c22007-02-12 00:55:33 -08001728static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 .owner = THIS_MODULE,
1731 .open = video_open,
1732 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001733 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001734 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 .llseek = no_llseek,
1736};
1737
Adrian Bunk408b6642005-05-01 08:59:29 -07001738static struct video_device cx8800_radio_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001740 .name = "cx8800-radio",
1741 .type = VID_TYPE_TUNER,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001742 .fops = &radio_fops,
1743 .minor = -1,
1744 .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
1762/* ----------------------------------------------------------- */
1763
1764static void cx8800_unregister_video(struct cx8800_dev *dev)
1765{
1766 if (dev->radio_dev) {
1767 if (-1 != dev->radio_dev->minor)
1768 video_unregister_device(dev->radio_dev);
1769 else
1770 video_device_release(dev->radio_dev);
1771 dev->radio_dev = NULL;
1772 }
1773 if (dev->vbi_dev) {
1774 if (-1 != dev->vbi_dev->minor)
1775 video_unregister_device(dev->vbi_dev);
1776 else
1777 video_device_release(dev->vbi_dev);
1778 dev->vbi_dev = NULL;
1779 }
1780 if (dev->video_dev) {
1781 if (-1 != dev->video_dev->minor)
1782 video_unregister_device(dev->video_dev);
1783 else
1784 video_device_release(dev->video_dev);
1785 dev->video_dev = NULL;
1786 }
1787}
1788
1789static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1790 const struct pci_device_id *pci_id)
1791{
1792 struct cx8800_dev *dev;
1793 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 int err;
1796
Panagiotis Issaris74081872006-01-11 19:40:56 -02001797 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (NULL == dev)
1799 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* pci init */
1802 dev->pci = pci_dev;
1803 if (pci_enable_device(pci_dev)) {
1804 err = -EIO;
1805 goto fail_free;
1806 }
1807 core = cx88_core_get(dev->pci);
1808 if (NULL == core) {
1809 err = -EINVAL;
1810 goto fail_free;
1811 }
1812 dev->core = core;
1813
1814 /* print pci info */
1815 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001816 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1817 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001818 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001820 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 pci_set_master(pci_dev);
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -03001823 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1825 err = -EIO;
1826 goto fail_core;
1827 }
1828
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001829 /* Initialize VBI template */
1830 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1831 sizeof(cx8800_vbi_template) );
1832 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1833 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001837 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 /* init video dma queues */
1840 INIT_LIST_HEAD(&dev->vidq.active);
1841 INIT_LIST_HEAD(&dev->vidq.queued);
1842 dev->vidq.timeout.function = cx8800_vid_timeout;
1843 dev->vidq.timeout.data = (unsigned long)dev;
1844 init_timer(&dev->vidq.timeout);
1845 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1846 MO_VID_DMACNTRL,0x11,0x00);
1847
1848 /* init vbi dma queues */
1849 INIT_LIST_HEAD(&dev->vbiq.active);
1850 INIT_LIST_HEAD(&dev->vbiq.queued);
1851 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1852 dev->vbiq.timeout.data = (unsigned long)dev;
1853 init_timer(&dev->vbiq.timeout);
1854 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1855 MO_VID_DMACNTRL,0x88,0x00);
1856
1857 /* get irq */
1858 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001859 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001861 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 core->name,pci_dev->irq);
1863 goto fail_core;
1864 }
1865 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1866
1867 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001868
Trent Piepho6a59d642007-08-15 14:41:57 -03001869 if (core->board.audio_chip == AUDIO_CHIP_WM8775)
Steven Toth30579062006-09-25 12:43:42 -03001870 request_module("wm8775");
1871
Michael Krufky6fcecce2007-08-24 01:32:31 -03001872 switch (core->boardnr) {
1873 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Michael Krufky3c66e4e2008-04-22 14:45:35 -03001874 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
Michael Krufky6fcecce2007-08-24 01:32:31 -03001875 request_module("rtc-isl1208");
Michael Krufky8efd2e22008-04-22 14:45:14 -03001876 /* break intentionally omitted */
1877 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1878 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001879 }
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* register v4l devices */
1882 dev->video_dev = cx88_vdev_init(core,dev->pci,
1883 &cx8800_video_template,"video");
1884 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1885 video_nr[core->nr]);
1886 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001887 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 core->name);
1889 goto fail_unreg;
1890 }
1891 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1892 core->name,dev->video_dev->minor & 0x1f);
1893
1894 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1895 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1896 vbi_nr[core->nr]);
1897 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001898 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 core->name);
1900 goto fail_unreg;
1901 }
1902 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1903 core->name,dev->vbi_dev->minor & 0x1f);
1904
Trent Piepho6a59d642007-08-15 14:41:57 -03001905 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1907 &cx8800_radio_template,"radio");
1908 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1909 radio_nr[core->nr]);
1910 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001911 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 core->name);
1913 goto fail_unreg;
1914 }
1915 printk(KERN_INFO "%s/0: registered device radio%d\n",
1916 core->name,dev->radio_dev->minor & 0x1f);
1917 }
1918
1919 /* everything worked */
1920 list_add_tail(&dev->devlist,&cx8800_devlist);
1921 pci_set_drvdata(pci_dev,dev);
1922
1923 /* initial device configuration */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001924 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001925 cx88_set_tvnorm(core,core->tvnorm);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001926 init_controls(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001927 cx88_video_mux(core,0);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001928 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001931 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001933 if (IS_ERR(core->kthread)) {
1934 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001935 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1936 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001937 }
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return 0;
1940
1941fail_unreg:
1942 cx8800_unregister_video(dev);
1943 free_irq(pci_dev->irq, dev);
1944fail_core:
1945 cx88_core_put(core,dev->pci);
1946fail_free:
1947 kfree(dev);
1948 return err;
1949}
1950
1951static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1952{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001953 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001954 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001957 if (core->kthread) {
1958 kthread_stop(core->kthread);
1959 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
Marton Balintb12203d2008-03-26 02:07:35 -03001962 if (core->ir)
1963 cx88_ir_stop(core, core->ir);
1964
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001965 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 pci_disable_device(pci_dev);
1967
1968 /* unregister stuff */
1969
1970 free_irq(pci_dev->irq, dev);
1971 cx8800_unregister_video(dev);
1972 pci_set_drvdata(pci_dev, NULL);
1973
1974 /* free memory */
1975 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1976 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001977 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 kfree(dev);
1979}
1980
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001981#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1983{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001984 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct cx88_core *core = dev->core;
1986
1987 /* stop video+vbi capture */
1988 spin_lock(&dev->slock);
1989 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001990 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 stop_video_dma(dev);
1992 del_timer(&dev->vidq.timeout);
1993 }
1994 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001995 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 cx8800_stop_vbi_dma(dev);
1997 del_timer(&dev->vbiq.timeout);
1998 }
1999 spin_unlock(&dev->slock);
2000
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002001 if (core->ir)
2002 cx88_ir_stop(core, core->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002004 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 pci_save_state(pci_dev);
2007 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2008 pci_disable_device(pci_dev);
2009 dev->state.disabled = 1;
2010 }
2011 return 0;
2012}
2013
2014static int cx8800_resume(struct pci_dev *pci_dev)
2015{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002016 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002018 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002021 err=pci_enable_device(pci_dev);
2022 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002023 printk(KERN_ERR "%s/0: can't enable device\n",
2024 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002025 return err;
2026 }
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 dev->state.disabled = 0;
2029 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002030 err= pci_set_power_state(pci_dev, PCI_D0);
2031 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002032 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002033 pci_disable_device(pci_dev);
2034 dev->state.disabled = 1;
2035
2036 return err;
2037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 pci_restore_state(pci_dev);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002041 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002042 if (core->ir)
2043 cx88_ir_start(core, core->ir);
2044
2045 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 /* restart video+vbi capture */
2048 spin_lock(&dev->slock);
2049 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002050 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 restart_video_queue(dev,&dev->vidq);
2052 }
2053 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002054 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2056 }
2057 spin_unlock(&dev->slock);
2058
2059 return 0;
2060}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002061#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063/* ----------------------------------------------------------- */
2064
Adrian Bunk408b6642005-05-01 08:59:29 -07002065static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 {
2067 .vendor = 0x14f1,
2068 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002069 .subvendor = PCI_ANY_ID,
2070 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 },{
2072 /* --- end of list --- */
2073 }
2074};
2075MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2076
2077static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002078 .name = "cx8800",
2079 .id_table = cx8800_pci_tbl,
2080 .probe = cx8800_initdev,
2081 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002082#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 .suspend = cx8800_suspend,
2084 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002085#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086};
2087
2088static int cx8800_init(void)
2089{
Trent Piepho5772f812007-08-15 14:41:59 -03002090 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 (CX88_VERSION_CODE >> 16) & 0xff,
2092 (CX88_VERSION_CODE >> 8) & 0xff,
2093 CX88_VERSION_CODE & 0xff);
2094#ifdef SNAPSHOT
2095 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2096 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2097#endif
2098 return pci_register_driver(&cx8800_pci_driver);
2099}
2100
2101static void cx8800_fini(void)
2102{
2103 pci_unregister_driver(&cx8800_pci_driver);
2104}
2105
2106module_init(cx8800_init);
2107module_exit(cx8800_fini);
2108
2109/* ----------------------------------------------------------- */
2110/*
2111 * Local variables:
2112 * c-basic-offset: 8
2113 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002114 * 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 -07002115 */