blob: 6d5ea8ce9830610d182ed6c5d14b11121da35bff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
5 *
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03008 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * - Multituner support
10 * - video_ioctl2 conversion
11 * - PAL/M fixes
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070035#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/kthread.h>
38#include <asm/div64.h>
39
40#include "cx88.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020041#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030043#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080044/* Include V4L1 specific functions. Should be removed soon */
45#include <linux/videodev.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030046#endif
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
49MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
50MODULE_LICENSE("GPL");
51
52/* ------------------------------------------------------------------ */
53
54static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57
58module_param_array(video_nr, int, NULL, 0444);
59module_param_array(vbi_nr, int, NULL, 0444);
60module_param_array(radio_nr, int, NULL, 0444);
61
62MODULE_PARM_DESC(video_nr,"video device numbers");
63MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
64MODULE_PARM_DESC(radio_nr,"radio device numbers");
65
66static unsigned int video_debug = 0;
67module_param(video_debug,int,0644);
68MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
69
70static unsigned int irq_debug = 0;
71module_param(irq_debug,int,0644);
72MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
73
74static unsigned int vid_limit = 16;
75module_param(vid_limit,int,0644);
76MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
77
78#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070079 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* ------------------------------------------------------------------ */
82
83static LIST_HEAD(cx8800_devlist);
84
85/* ------------------------------------------------------------------- */
86/* static data */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static struct cx8800_fmt formats[] = {
89 {
90 .name = "8 bpp, gray",
91 .fourcc = V4L2_PIX_FMT_GREY,
92 .cxformat = ColorFormatY8,
93 .depth = 8,
94 .flags = FORMAT_FLAGS_PACKED,
95 },{
96 .name = "15 bpp RGB, le",
97 .fourcc = V4L2_PIX_FMT_RGB555,
98 .cxformat = ColorFormatRGB15,
99 .depth = 16,
100 .flags = FORMAT_FLAGS_PACKED,
101 },{
102 .name = "15 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB555X,
104 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
105 .depth = 16,
106 .flags = FORMAT_FLAGS_PACKED,
107 },{
108 .name = "16 bpp RGB, le",
109 .fourcc = V4L2_PIX_FMT_RGB565,
110 .cxformat = ColorFormatRGB16,
111 .depth = 16,
112 .flags = FORMAT_FLAGS_PACKED,
113 },{
114 .name = "16 bpp RGB, be",
115 .fourcc = V4L2_PIX_FMT_RGB565X,
116 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
117 .depth = 16,
118 .flags = FORMAT_FLAGS_PACKED,
119 },{
120 .name = "24 bpp RGB, le",
121 .fourcc = V4L2_PIX_FMT_BGR24,
122 .cxformat = ColorFormatRGB24,
123 .depth = 24,
124 .flags = FORMAT_FLAGS_PACKED,
125 },{
126 .name = "32 bpp RGB, le",
127 .fourcc = V4L2_PIX_FMT_BGR32,
128 .cxformat = ColorFormatRGB32,
129 .depth = 32,
130 .flags = FORMAT_FLAGS_PACKED,
131 },{
132 .name = "32 bpp RGB, be",
133 .fourcc = V4L2_PIX_FMT_RGB32,
134 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
135 .depth = 32,
136 .flags = FORMAT_FLAGS_PACKED,
137 },{
138 .name = "4:2:2, packed, YUYV",
139 .fourcc = V4L2_PIX_FMT_YUYV,
140 .cxformat = ColorFormatYUY2,
141 .depth = 16,
142 .flags = FORMAT_FLAGS_PACKED,
143 },{
144 .name = "4:2:2, packed, UYVY",
145 .fourcc = V4L2_PIX_FMT_UYVY,
146 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
147 .depth = 16,
148 .flags = FORMAT_FLAGS_PACKED,
149 },
150};
151
152static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
153{
154 unsigned int i;
155
156 for (i = 0; i < ARRAY_SIZE(formats); i++)
157 if (formats[i].fourcc == fourcc)
158 return formats+i;
159 return NULL;
160}
161
162/* ------------------------------------------------------------------- */
163
164static const struct v4l2_queryctrl no_ctl = {
165 .name = "42",
166 .flags = V4L2_CTRL_FLAG_DISABLED,
167};
168
169static struct cx88_ctrl cx8800_ctls[] = {
170 /* --- video --- */
171 {
172 .v = {
173 .id = V4L2_CID_BRIGHTNESS,
174 .name = "Brightness",
175 .minimum = 0x00,
176 .maximum = 0xff,
177 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300178 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .type = V4L2_CTRL_TYPE_INTEGER,
180 },
181 .off = 128,
182 .reg = MO_CONTR_BRIGHT,
183 .mask = 0x00ff,
184 .shift = 0,
185 },{
186 .v = {
187 .id = V4L2_CID_CONTRAST,
188 .name = "Contrast",
189 .minimum = 0,
190 .maximum = 0xff,
191 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200192 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .type = V4L2_CTRL_TYPE_INTEGER,
194 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700195 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .reg = MO_CONTR_BRIGHT,
197 .mask = 0xff00,
198 .shift = 8,
199 },{
200 .v = {
201 .id = V4L2_CID_HUE,
202 .name = "Hue",
203 .minimum = 0,
204 .maximum = 0xff,
205 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300206 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .type = V4L2_CTRL_TYPE_INTEGER,
208 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700209 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .reg = MO_HUE,
211 .mask = 0x00ff,
212 .shift = 0,
213 },{
214 /* strictly, this only describes only U saturation.
215 * V saturation is handled specially through code.
216 */
217 .v = {
218 .id = V4L2_CID_SATURATION,
219 .name = "Saturation",
220 .minimum = 0,
221 .maximum = 0xff,
222 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200223 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .type = V4L2_CTRL_TYPE_INTEGER,
225 },
226 .off = 0,
227 .reg = MO_UV_SATURATION,
228 .mask = 0x00ff,
229 .shift = 0,
230 },{
231 /* --- audio --- */
232 .v = {
233 .id = V4L2_CID_AUDIO_MUTE,
234 .name = "Mute",
235 .minimum = 0,
236 .maximum = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200237 .default_value = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .type = V4L2_CTRL_TYPE_BOOLEAN,
239 },
240 .reg = AUD_VOL_CTL,
241 .sreg = SHADOW_AUD_VOL_CTL,
242 .mask = (1 << 6),
243 .shift = 6,
244 },{
245 .v = {
246 .id = V4L2_CID_AUDIO_VOLUME,
247 .name = "Volume",
248 .minimum = 0,
249 .maximum = 0x3f,
250 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300251 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .type = V4L2_CTRL_TYPE_INTEGER,
253 },
254 .reg = AUD_VOL_CTL,
255 .sreg = SHADOW_AUD_VOL_CTL,
256 .mask = 0x3f,
257 .shift = 0,
258 },{
259 .v = {
260 .id = V4L2_CID_AUDIO_BALANCE,
261 .name = "Balance",
262 .minimum = 0,
263 .maximum = 0x7f,
264 .step = 1,
265 .default_value = 0x40,
266 .type = V4L2_CTRL_TYPE_INTEGER,
267 },
268 .reg = AUD_BAL_CTL,
269 .sreg = SHADOW_AUD_BAL_CTL,
270 .mask = 0x7f,
271 .shift = 0,
272 }
273};
Adrian Bunk408b6642005-05-01 08:59:29 -0700274static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Michael Krufky38a27132006-06-26 23:42:39 -0300276const u32 cx88_user_ctrls[] = {
277 V4L2_CID_USER_CLASS,
278 V4L2_CID_BRIGHTNESS,
279 V4L2_CID_CONTRAST,
280 V4L2_CID_SATURATION,
281 V4L2_CID_HUE,
282 V4L2_CID_AUDIO_VOLUME,
283 V4L2_CID_AUDIO_BALANCE,
284 V4L2_CID_AUDIO_MUTE,
285 0
286};
287EXPORT_SYMBOL(cx88_user_ctrls);
288
289static const u32 *ctrl_classes[] = {
290 cx88_user_ctrls,
291 NULL
292};
293
294int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
295{
296 int i;
297
298 if (qctrl->id < V4L2_CID_BASE ||
299 qctrl->id >= V4L2_CID_LASTP1)
300 return -EINVAL;
301 for (i = 0; i < CX8800_CTLS; i++)
302 if (cx8800_ctls[i].v.id == qctrl->id)
303 break;
304 if (i == CX8800_CTLS) {
305 *qctrl = no_ctl;
306 return 0;
307 }
308 *qctrl = cx8800_ctls[i].v;
309 return 0;
310}
311EXPORT_SYMBOL(cx8800_ctrl_query);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/* ------------------------------------------------------------------- */
314/* resource management */
315
316static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
317{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700318 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (fh->resources & bit)
320 /* have it already allocated */
321 return 1;
322
323 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200324 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (dev->resources & bit) {
326 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200327 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329 }
330 /* it's free, grab it */
331 fh->resources |= bit;
332 dev->resources |= bit;
333 dprintk(1,"res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200334 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 1;
336}
337
338static
339int res_check(struct cx8800_fh *fh, unsigned int bit)
340{
341 return (fh->resources & bit);
342}
343
344static
345int res_locked(struct cx8800_dev *dev, unsigned int bit)
346{
347 return (dev->resources & bit);
348}
349
350static
351void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
352{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700353 struct cx88_core *core = dev->core;
Eric Sesterhennae246012006-03-13 13:17:11 -0300354 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Ingo Molnar3593cab2006-02-07 06:49:14 -0200356 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 fh->resources &= ~bits;
358 dev->resources &= ~bits;
359 dprintk(1,"res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200360 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/* ------------------------------------------------------------------ */
364
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300365int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700367 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300370 input, INPUT(input).vmux,
371 INPUT(input).gpio0,INPUT(input).gpio1,
372 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700373 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300374 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
375 cx_write(MO_GP3_IO, INPUT(input).gpio3);
376 cx_write(MO_GP0_IO, INPUT(input).gpio0);
377 cx_write(MO_GP1_IO, INPUT(input).gpio1);
378 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Trent Piepho6a59d642007-08-15 14:41:57 -0300380 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case CX88_VMUX_SVIDEO:
382 cx_set(MO_AFECFG_IO, 0x00000001);
383 cx_set(MO_INPUT_FORMAT, 0x00010010);
384 cx_set(MO_FILTER_EVEN, 0x00002020);
385 cx_set(MO_FILTER_ODD, 0x00002020);
386 break;
387 default:
388 cx_clear(MO_AFECFG_IO, 0x00000001);
389 cx_clear(MO_INPUT_FORMAT, 0x00010010);
390 cx_clear(MO_FILTER_EVEN, 0x00002020);
391 cx_clear(MO_FILTER_ODD, 0x00002020);
392 break;
393 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300394
Trent Piepho6a59d642007-08-15 14:41:57 -0300395 if (core->board.mpeg & CX88_MPEG_BLACKBIRD) {
Michael Krufkyf24546a2006-10-16 16:51:11 -0300396 /* sets sound input from external adc */
Trent Piepho6a59d642007-08-15 14:41:57 -0300397 if (INPUT(input).extadc)
Michael Krufkyf24546a2006-10-16 16:51:11 -0300398 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
399 else
400 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300404EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/* ------------------------------------------------------------------ */
407
408static int start_video_dma(struct cx8800_dev *dev,
409 struct cx88_dmaqueue *q,
410 struct cx88_buffer *buf)
411{
412 struct cx88_core *core = dev->core;
413
414 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700415 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700417 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
419
420 /* reset counter */
421 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
422 q->count = 1;
423
424 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300425 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700426
427 /* Enables corresponding bits at PCI_INT_STAT:
428 bits 0 to 4: video, audio, transport stream, VIP, Host
429 bit 7: timer
430 bits 8 and 9: DMA complete for: SRC, DST
431 bits 10 and 11: BERR signal asserted for RISC: RD, WR
432 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 cx_set(MO_VID_INTMSK, 0x0f0011);
435
436 /* enable capture */
437 cx_set(VID_CAPTURE_CONTROL,0x06);
438
439 /* start dma */
440 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700441 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 return 0;
444}
445
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300446#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static int stop_video_dma(struct cx8800_dev *dev)
448{
449 struct cx88_core *core = dev->core;
450
451 /* stop dma */
452 cx_clear(MO_VID_DMACNTRL, 0x11);
453
454 /* disable capture */
455 cx_clear(VID_CAPTURE_CONTROL,0x06);
456
457 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300458 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 cx_clear(MO_VID_INTMSK, 0x0f0011);
460 return 0;
461}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300462#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464static int restart_video_queue(struct cx8800_dev *dev,
465 struct cx88_dmaqueue *q)
466{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700467 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct cx88_buffer *buf, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800471 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
473 buf, buf->vb.i);
474 start_video_dma(dev, q, buf);
Trent Piepho8bb629e22007-10-10 05:37:40 -0300475 list_for_each_entry(buf, &q->active, vb.queue)
476 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
478 return 0;
479 }
480
481 prev = NULL;
482 for (;;) {
483 if (list_empty(&q->queued))
484 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800485 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700487 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300489 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 buf->count = q->count++;
491 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
492 dprintk(2,"[%p/%d] restart_queue - first active\n",
493 buf,buf->vb.i);
494
495 } else if (prev->vb.width == buf->vb.width &&
496 prev->vb.height == buf->vb.height &&
497 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700498 list_move_tail(&buf->vb.queue, &q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300499 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 buf->count = q->count++;
501 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
502 dprintk(2,"[%p/%d] restart_queue - move to active\n",
503 buf,buf->vb.i);
504 } else {
505 return 0;
506 }
507 prev = buf;
508 }
509}
510
511/* ------------------------------------------------------------------ */
512
513static int
514buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
515{
516 struct cx8800_fh *fh = q->priv_data;
517
518 *size = fh->fmt->depth*fh->width*fh->height >> 3;
519 if (0 == *count)
520 *count = 32;
521 while (*size * *count > vid_limit * 1024 * 1024)
522 (*count)--;
523 return 0;
524}
525
526static int
527buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
528 enum v4l2_field field)
529{
530 struct cx8800_fh *fh = q->priv_data;
531 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700532 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300534 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int rc, init_buffer = 0;
536
537 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700538 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
539 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EINVAL;
541 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
542 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
543 return -EINVAL;
544
545 if (buf->fmt != fh->fmt ||
546 buf->vb.width != fh->width ||
547 buf->vb.height != fh->height ||
548 buf->vb.field != field) {
549 buf->fmt = fh->fmt;
550 buf->vb.width = fh->width;
551 buf->vb.height = fh->height;
552 buf->vb.field = field;
553 init_buffer = 1;
554 }
555
Brandon Philips0fc06862007-11-06 20:02:36 -0300556 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300558 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto fail;
560 }
561
562 if (init_buffer) {
563 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
564 switch (buf->vb.field) {
565 case V4L2_FIELD_TOP:
566 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300567 dma->sglist, 0, UNSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 buf->bpl, 0, buf->vb.height);
569 break;
570 case V4L2_FIELD_BOTTOM:
571 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300572 dma->sglist, UNSET, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 buf->bpl, 0, buf->vb.height);
574 break;
575 case V4L2_FIELD_INTERLACED:
576 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300577 dma->sglist, 0, buf->bpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 buf->bpl, buf->bpl,
579 buf->vb.height >> 1);
580 break;
581 case V4L2_FIELD_SEQ_TB:
582 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300583 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 0, buf->bpl * (buf->vb.height >> 1),
585 buf->bpl, 0,
586 buf->vb.height >> 1);
587 break;
588 case V4L2_FIELD_SEQ_BT:
589 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300590 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 buf->bpl * (buf->vb.height >> 1), 0,
592 buf->bpl, 0,
593 buf->vb.height >> 1);
594 break;
595 default:
596 BUG();
597 }
598 }
599 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
600 buf, buf->vb.i,
601 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
602 (unsigned long)buf->risc.dma);
603
Brandon Philips0fc06862007-11-06 20:02:36 -0300604 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606
607 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300608 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return rc;
610}
611
612static void
613buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
614{
615 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
616 struct cx88_buffer *prev;
617 struct cx8800_fh *fh = vq->priv_data;
618 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700619 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct cx88_dmaqueue *q = &dev->vidq;
621
622 /* add jump to stopper */
623 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
624 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
625
626 if (!list_empty(&q->queued)) {
627 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300628 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
630 buf, buf->vb.i);
631
632 } else if (list_empty(&q->active)) {
633 list_add_tail(&buf->vb.queue,&q->active);
634 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300635 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 buf->count = q->count++;
637 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
638 dprintk(2,"[%p/%d] buffer_queue - first active\n",
639 buf, buf->vb.i);
640
641 } else {
642 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
643 if (prev->vb.width == buf->vb.width &&
644 prev->vb.height == buf->vb.height &&
645 prev->fmt == buf->fmt) {
646 list_add_tail(&buf->vb.queue,&q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300647 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 buf->count = q->count++;
649 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
650 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
651 buf, buf->vb.i);
652
653 } else {
654 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300655 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
657 buf, buf->vb.i);
658 }
659 }
660}
661
662static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
663{
664 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300666 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Adrian Bunk408b6642005-05-01 08:59:29 -0700669static struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 .buf_setup = buffer_setup,
671 .buf_prepare = buffer_prepare,
672 .buf_queue = buffer_queue,
673 .buf_release = buffer_release,
674};
675
676/* ------------------------------------------------------------------ */
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/* ------------------------------------------------------------------ */
680
681static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
682{
683 switch (fh->type) {
684 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
685 return &fh->vidq;
686 case V4L2_BUF_TYPE_VBI_CAPTURE:
687 return &fh->vbiq;
688 default:
689 BUG();
690 return NULL;
691 }
692}
693
694static int get_ressource(struct cx8800_fh *fh)
695{
696 switch (fh->type) {
697 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
698 return RESOURCE_VIDEO;
699 case V4L2_BUF_TYPE_VBI_CAPTURE:
700 return RESOURCE_VBI;
701 default:
702 BUG();
703 return 0;
704 }
705}
706
707static int video_open(struct inode *inode, struct file *file)
708{
709 int minor = iminor(inode);
710 struct cx8800_dev *h,*dev = NULL;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700711 struct cx88_core *core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct cx8800_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 enum v4l2_buf_type type = 0;
714 int radio = 0;
715
Trent Piepho8bb629e22007-10-10 05:37:40 -0300716 list_for_each_entry(h, &cx8800_devlist, devlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (h->video_dev->minor == minor) {
718 dev = h;
719 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
720 }
721 if (h->vbi_dev->minor == minor) {
722 dev = h;
723 type = V4L2_BUF_TYPE_VBI_CAPTURE;
724 }
725 if (h->radio_dev &&
726 h->radio_dev->minor == minor) {
727 radio = 1;
728 dev = h;
729 }
730 }
731 if (NULL == dev)
732 return -ENODEV;
733
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700734 core = dev->core;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 dprintk(1,"open minor=%d radio=%d type=%s\n",
737 minor,radio,v4l2_type_names[type]);
738
739 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200740 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (NULL == fh)
742 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 file->private_data = fh;
744 fh->dev = dev;
745 fh->radio = radio;
746 fh->type = type;
747 fh->width = 320;
748 fh->height = 240;
749 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
750
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300751 videobuf_queue_pci_init(&fh->vidq, &cx8800_video_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 dev->pci, &dev->slock,
753 V4L2_BUF_TYPE_VIDEO_CAPTURE,
754 V4L2_FIELD_INTERLACED,
755 sizeof(struct cx88_buffer),
756 fh);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300757 videobuf_queue_pci_init(&fh->vbiq, &cx8800_vbi_qops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 dev->pci, &dev->slock,
759 V4L2_BUF_TYPE_VBI_CAPTURE,
760 V4L2_FIELD_SEQ_TB,
761 sizeof(struct cx88_buffer),
762 fh);
763
764 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 dprintk(1,"video_open: setting radio device\n");
Trent Piepho6a59d642007-08-15 14:41:57 -0300766 cx_write(MO_GP3_IO, core->board.radio.gpio3);
767 cx_write(MO_GP0_IO, core->board.radio.gpio0);
768 cx_write(MO_GP1_IO, core->board.radio.gpio1);
769 cx_write(MO_GP2_IO, core->board.radio.gpio2);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700770 core->tvaudio = WW_FM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 cx88_set_tvaudio(core);
772 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700773 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800780video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct cx8800_fh *fh = file->private_data;
783
784 switch (fh->type) {
785 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
786 if (res_locked(fh->dev,RESOURCE_VIDEO))
787 return -EBUSY;
788 return videobuf_read_one(&fh->vidq, data, count, ppos,
789 file->f_flags & O_NONBLOCK);
790 case V4L2_BUF_TYPE_VBI_CAPTURE:
791 if (!res_get(fh->dev,fh,RESOURCE_VBI))
792 return -EBUSY;
793 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
794 file->f_flags & O_NONBLOCK);
795 default:
796 BUG();
797 return 0;
798 }
799}
800
801static unsigned int
802video_poll(struct file *file, struct poll_table_struct *wait)
803{
804 struct cx8800_fh *fh = file->private_data;
805 struct cx88_buffer *buf;
806
807 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
808 if (!res_get(fh->dev,fh,RESOURCE_VBI))
809 return POLLERR;
810 return videobuf_poll_stream(file, &fh->vbiq, wait);
811 }
812
813 if (res_check(fh,RESOURCE_VIDEO)) {
814 /* streaming capture */
815 if (list_empty(&fh->vidq.stream))
816 return POLLERR;
817 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
818 } else {
819 /* read() capture */
820 buf = (struct cx88_buffer*)fh->vidq.read_buf;
821 if (NULL == buf)
822 return POLLERR;
823 }
824 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300825 if (buf->vb.state == VIDEOBUF_DONE ||
826 buf->vb.state == VIDEOBUF_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return POLLIN|POLLRDNORM;
828 return 0;
829}
830
831static int video_release(struct inode *inode, struct file *file)
832{
833 struct cx8800_fh *fh = file->private_data;
834 struct cx8800_dev *dev = fh->dev;
835
836 /* turn off overlay */
837 if (res_check(fh, RESOURCE_OVERLAY)) {
838 /* FIXME */
839 res_free(dev,fh,RESOURCE_OVERLAY);
840 }
841
842 /* stop video capture */
843 if (res_check(fh, RESOURCE_VIDEO)) {
844 videobuf_queue_cancel(&fh->vidq);
845 res_free(dev,fh,RESOURCE_VIDEO);
846 }
847 if (fh->vidq.read_buf) {
848 buffer_release(&fh->vidq,fh->vidq.read_buf);
849 kfree(fh->vidq.read_buf);
850 }
851
852 /* stop vbi capture */
853 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -0300854 videobuf_stop(&fh->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 res_free(dev,fh,RESOURCE_VBI);
856 }
857
858 videobuf_mmap_free(&fh->vidq);
859 videobuf_mmap_free(&fh->vbiq);
860 file->private_data = NULL;
861 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700862
863 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
868static int
869video_mmap(struct file *file, struct vm_area_struct * vma)
870{
871 struct cx8800_fh *fh = file->private_data;
872
873 return videobuf_mmap_mapper(get_queue(fh), vma);
874}
875
876/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300877/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300879int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300881 struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 u32 value;
883 int i;
884
885 for (i = 0; i < CX8800_CTLS; i++)
886 if (cx8800_ctls[i].v.id == ctl->id)
887 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300888 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -EINVAL;
890
891 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
892 switch (ctl->id) {
893 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300894 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
895 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
897 case V4L2_CID_AUDIO_VOLUME:
898 ctl->value = 0x3f - (value & 0x3f);
899 break;
900 default:
901 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
902 break;
903 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300904 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
905 ctl->id, c->v.name, ctl->value, c->reg,
906 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300909EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300911int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200914 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300916
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200917 for (i = 0; i < CX8800_CTLS; i++) {
918 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200920 }
921 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300922 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -EINVAL;
924
925 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700926 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700928 ctl->value = c->v.maximum;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200929 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 switch (ctl->id) {
931 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300932 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
934 case V4L2_CID_AUDIO_VOLUME:
935 value = 0x3f - (ctl->value & 0x3f);
936 break;
937 case V4L2_CID_SATURATION:
938 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200939
940 value = ((ctl->value - c->off) << c->shift) & c->mask;
941
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -0300942 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200943 /* For SECAM, both U and V sat should be equal */
944 value=value<<8|value;
945 } else {
946 /* Keeps U Saturation proportional to V Sat */
947 value=(value*0x5a)/0x7f<<8|value;
948 }
949 mask=0xffff;
950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 default:
952 value = ((ctl->value - c->off) << c->shift) & c->mask;
953 break;
954 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300955 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
956 ctl->id, c->v.name, ctl->value, c->reg, value,
957 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200959 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200961 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 return 0;
964}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300965EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700967static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200969 struct v4l2_control ctrl;
970 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200972 for (i = 0; i < CX8800_CTLS; i++) {
973 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300974 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300975
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300976 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300981/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300983static int vidioc_g_fmt_cap (struct file *file, void *priv,
984 struct v4l2_format *f)
985{
986 struct cx8800_fh *fh = priv;
987
988 f->fmt.pix.width = fh->width;
989 f->fmt.pix.height = fh->height;
990 f->fmt.pix.field = fh->vidq.field;
991 f->fmt.pix.pixelformat = fh->fmt->fourcc;
992 f->fmt.pix.bytesperline =
993 (f->fmt.pix.width * fh->fmt->depth) >> 3;
994 f->fmt.pix.sizeimage =
995 f->fmt.pix.height * f->fmt.pix.bytesperline;
996 return 0;
997}
998
999static int vidioc_try_fmt_cap (struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 struct v4l2_format *f)
1001{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001002 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1003 struct cx8800_fmt *fmt;
1004 enum v4l2_field field;
1005 unsigned int maxw, maxh;
1006
1007 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1008 if (NULL == fmt)
1009 return -EINVAL;
1010
1011 field = f->fmt.pix.field;
1012 maxw = norm_maxw(core->tvnorm);
1013 maxh = norm_maxh(core->tvnorm);
1014
1015 if (V4L2_FIELD_ANY == field) {
1016 field = (f->fmt.pix.height > maxh/2)
1017 ? V4L2_FIELD_INTERLACED
1018 : V4L2_FIELD_BOTTOM;
1019 }
1020
1021 switch (field) {
1022 case V4L2_FIELD_TOP:
1023 case V4L2_FIELD_BOTTOM:
1024 maxh = maxh / 2;
1025 break;
1026 case V4L2_FIELD_INTERLACED:
1027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 default:
1029 return -EINVAL;
1030 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001031
1032 f->fmt.pix.field = field;
1033 if (f->fmt.pix.height < 32)
1034 f->fmt.pix.height = 32;
1035 if (f->fmt.pix.height > maxh)
1036 f->fmt.pix.height = maxh;
1037 if (f->fmt.pix.width < 48)
1038 f->fmt.pix.width = 48;
1039 if (f->fmt.pix.width > maxw)
1040 f->fmt.pix.width = maxw;
1041 f->fmt.pix.width &= ~0x03;
1042 f->fmt.pix.bytesperline =
1043 (f->fmt.pix.width * fmt->depth) >> 3;
1044 f->fmt.pix.sizeimage =
1045 f->fmt.pix.height * f->fmt.pix.bytesperline;
1046
1047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001050static int vidioc_s_fmt_cap (struct file *file, void *priv,
1051 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001053 struct cx8800_fh *fh = priv;
1054 int err = vidioc_try_fmt_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001055
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001056 if (0 != err)
1057 return err;
1058 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1059 fh->width = f->fmt.pix.width;
1060 fh->height = f->fmt.pix.height;
1061 fh->vidq.field = f->fmt.pix.field;
1062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001065static int vidioc_querycap (struct file *file, void *priv,
1066 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001068 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001071 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001072 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001073 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1074 cap->version = CX88_VERSION_CODE;
1075 cap->capabilities =
1076 V4L2_CAP_VIDEO_CAPTURE |
1077 V4L2_CAP_READWRITE |
1078 V4L2_CAP_STREAMING |
1079 V4L2_CAP_VBI_CAPTURE;
Trent Piepho6a59d642007-08-15 14:41:57 -03001080 if (UNSET != core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001081 cap->capabilities |= V4L2_CAP_TUNER;
1082 return 0;
1083}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001084
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001085static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1086 struct v4l2_fmtdesc *f)
1087{
1088 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1089 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001091 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1092 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001094 return 0;
1095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001097#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001098static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001099{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001100 struct cx8800_fh *fh = priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001101
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -03001102 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001106static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001108 struct cx8800_fh *fh = priv;
1109 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001112static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1113{
1114 struct cx8800_fh *fh = priv;
1115 return (videobuf_querybuf(get_queue(fh), p));
1116}
1117
1118static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1119{
1120 struct cx8800_fh *fh = priv;
1121 return (videobuf_qbuf(get_queue(fh), p));
1122}
1123
1124static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1125{
1126 struct cx8800_fh *fh = priv;
1127 return (videobuf_dqbuf(get_queue(fh), p,
1128 file->f_flags & O_NONBLOCK));
1129}
1130
1131static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1132{
1133 struct cx8800_fh *fh = priv;
1134 struct cx8800_dev *dev = fh->dev;
1135
1136 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1137 return -EINVAL;
1138 if (unlikely(i != fh->type))
1139 return -EINVAL;
1140
1141 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1142 return -EBUSY;
1143 return videobuf_streamon(get_queue(fh));
1144}
1145
1146static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1147{
1148 struct cx8800_fh *fh = priv;
1149 struct cx8800_dev *dev = fh->dev;
1150 int err, res;
1151
1152 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1153 return -EINVAL;
1154 if (i != fh->type)
1155 return -EINVAL;
1156
1157 res = get_ressource(fh);
1158 err = videobuf_streamoff(get_queue(fh));
1159 if (err < 0)
1160 return err;
1161 res_free(dev,fh,res);
1162 return 0;
1163}
1164
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001165static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001166{
1167 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1168
1169 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001170 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001171 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001172
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001173 return 0;
1174}
1175
1176/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001177int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001178{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001179 static const char *iname[] = {
1180 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1181 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1182 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1183 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1184 [ CX88_VMUX_SVIDEO ] = "S-Video",
1185 [ CX88_VMUX_TELEVISION ] = "Television",
1186 [ CX88_VMUX_CABLE ] = "Cable TV",
1187 [ CX88_VMUX_DVB ] = "DVB",
1188 [ CX88_VMUX_DEBUG ] = "for debug only",
1189 };
1190 unsigned int n;
1191
1192 n = i->index;
1193 if (n >= 4)
1194 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001195 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001196 return -EINVAL;
1197 memset(i,0,sizeof(*i));
1198 i->index = n;
1199 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001200 strcpy(i->name,iname[INPUT(n).type]);
1201 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1202 (CX88_VMUX_CABLE == INPUT(n).type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001203 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001204 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001205 return 0;
1206}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001207EXPORT_SYMBOL(cx88_enum_input);
1208
1209static int vidioc_enum_input (struct file *file, void *priv,
1210 struct v4l2_input *i)
1211{
1212 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1213 return cx88_enum_input (core,i);
1214}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001215
1216static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1217{
1218 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1219
1220 *i = core->input;
1221 return 0;
1222}
1223
1224static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1225{
1226 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1227
1228 if (i >= 4)
1229 return -EINVAL;
1230
1231 mutex_lock(&core->lock);
1232 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001233 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001234 mutex_unlock(&core->lock);
1235 return 0;
1236}
1237
1238
1239
1240static int vidioc_queryctrl (struct file *file, void *priv,
1241 struct v4l2_queryctrl *qctrl)
1242{
1243 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1244 if (unlikely(qctrl->id == 0))
1245 return -EINVAL;
1246 return cx8800_ctrl_query(qctrl);
1247}
1248
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001249static int vidioc_g_ctrl (struct file *file, void *priv,
1250 struct v4l2_control *ctl)
1251{
1252 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1253 return
1254 cx88_get_control(core,ctl);
1255}
1256
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001257static int vidioc_s_ctrl (struct file *file, void *priv,
1258 struct v4l2_control *ctl)
1259{
1260 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001261 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001262 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001263}
1264
1265static int vidioc_g_tuner (struct file *file, void *priv,
1266 struct v4l2_tuner *t)
1267{
1268 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1269 u32 reg;
1270
Trent Piepho6a59d642007-08-15 14:41:57 -03001271 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001272 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001273 if (0 != t->index)
1274 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001275
1276 strcpy(t->name, "Television");
1277 t->type = V4L2_TUNER_ANALOG_TV;
1278 t->capability = V4L2_TUNER_CAP_NORM;
1279 t->rangehigh = 0xffffffffUL;
1280
1281 cx88_get_stereo(core ,t);
1282 reg = cx_read(MO_DEVICE_STATUS);
1283 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1284 return 0;
1285}
1286
1287static int vidioc_s_tuner (struct file *file, void *priv,
1288 struct v4l2_tuner *t)
1289{
1290 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1291
Trent Piepho6a59d642007-08-15 14:41:57 -03001292 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001293 return -EINVAL;
1294 if (0 != t->index)
1295 return -EINVAL;
1296
1297 cx88_set_stereo(core, t->audmode, 1);
1298 return 0;
1299}
1300
1301static int vidioc_g_frequency (struct file *file, void *priv,
1302 struct v4l2_frequency *f)
1303{
1304 struct cx8800_fh *fh = priv;
1305 struct cx88_core *core = fh->dev->core;
1306
Trent Piepho6a59d642007-08-15 14:41:57 -03001307 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001308 return -EINVAL;
1309
1310 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1311 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1312 f->frequency = core->freq;
1313
1314 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1315
1316 return 0;
1317}
1318
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001319int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001320 struct v4l2_frequency *f)
1321{
Trent Piepho6a59d642007-08-15 14:41:57 -03001322 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001323 return -EINVAL;
1324 if (unlikely(f->tuner != 0))
1325 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001326
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001327 mutex_lock(&core->lock);
1328 core->freq = f->frequency;
1329 cx88_newstation(core);
1330 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1331
1332 /* When changing channels it is required to reset TVAUDIO */
1333 msleep (10);
1334 cx88_set_tvaudio(core);
1335
1336 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001337
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001338 return 0;
1339}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001340EXPORT_SYMBOL(cx88_set_freq);
1341
1342static int vidioc_s_frequency (struct file *file, void *priv,
1343 struct v4l2_frequency *f)
1344{
1345 struct cx8800_fh *fh = priv;
1346 struct cx88_core *core = fh->dev->core;
1347
1348 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1349 return -EINVAL;
1350 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1351 return -EINVAL;
1352
1353 return
1354 cx88_set_freq (core,f);
1355}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001356
Trent Piephodbbff482007-01-22 23:31:53 -03001357#ifdef CONFIG_VIDEO_ADV_DEBUG
1358static int vidioc_g_register (struct file *file, void *fh,
1359 struct v4l2_register *reg)
1360{
1361 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1362
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001363 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001364 return -EINVAL;
1365 /* cx2388x has a 24-bit register space */
1366 reg->val = cx_read(reg->reg&0xffffff);
1367 return 0;
1368}
1369
1370static int vidioc_s_register (struct file *file, void *fh,
1371 struct v4l2_register *reg)
1372{
1373 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1374
Hans Verkuilf3d092b2007-02-23 20:55:14 -03001375 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
Trent Piephodbbff482007-01-22 23:31:53 -03001376 return -EINVAL;
Trent Piephodbbff482007-01-22 23:31:53 -03001377 cx_write(reg->reg&0xffffff, reg->val);
1378 return 0;
1379}
1380#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001383/* RADIO ESPECIFIC IOCTLS */
1384/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001386static int radio_querycap (struct file *file, void *priv,
1387 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001389 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct cx88_core *core = dev->core;
1391
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001392 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001393 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001394 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1395 cap->version = CX88_VERSION_CODE;
1396 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001400static int radio_g_tuner (struct file *file, void *priv,
1401 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001403 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1404
1405 if (unlikely(t->index > 0))
1406 return -EINVAL;
1407
1408 strcpy(t->name, "Radio");
1409 t->type = V4L2_TUNER_RADIO;
1410
1411 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1412 return 0;
1413}
1414
1415static int radio_enum_input (struct file *file, void *priv,
1416 struct v4l2_input *i)
1417{
1418 if (i->index != 0)
1419 return -EINVAL;
1420 strcpy(i->name,"Radio");
1421 i->type = V4L2_INPUT_TYPE_TUNER;
1422
1423 return 0;
1424}
1425
1426static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1427{
1428 if (unlikely(a->index))
1429 return -EINVAL;
1430
1431 memset(a,0,sizeof(*a));
1432 strcpy(a->name,"Radio");
1433 return 0;
1434}
1435
1436/* FIXME: Should add a standard for radio */
1437
1438static int radio_s_tuner (struct file *file, void *priv,
1439 struct v4l2_tuner *t)
1440{
1441 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1442
1443 if (0 != t->index)
1444 return -EINVAL;
1445
1446 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1447
1448 return 0;
1449}
1450
1451static int radio_s_audio (struct file *file, void *fh,
1452 struct v4l2_audio *a)
1453{
1454 return 0;
1455}
1456
1457static int radio_s_input (struct file *file, void *fh, unsigned int i)
1458{
1459 return 0;
1460}
1461
1462static int radio_queryctrl (struct file *file, void *priv,
1463 struct v4l2_queryctrl *c)
1464{
1465 int i;
1466
1467 if (c->id < V4L2_CID_BASE ||
1468 c->id >= V4L2_CID_LASTP1)
1469 return -EINVAL;
1470 if (c->id == V4L2_CID_AUDIO_MUTE) {
1471 for (i = 0; i < CX8800_CTLS; i++)
1472 if (cx8800_ctls[i].v.id == c->id)
1473 break;
1474 *c = cx8800_ctls[i].v;
1475 } else
1476 *c = no_ctl;
1477 return 0;
1478}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480/* ----------------------------------------------------------- */
1481
1482static void cx8800_vid_timeout(unsigned long data)
1483{
1484 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1485 struct cx88_core *core = dev->core;
1486 struct cx88_dmaqueue *q = &dev->vidq;
1487 struct cx88_buffer *buf;
1488 unsigned long flags;
1489
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001490 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 cx_clear(MO_VID_DMACNTRL, 0x11);
1493 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1494
1495 spin_lock_irqsave(&dev->slock,flags);
1496 while (!list_empty(&q->active)) {
1497 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1498 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03001499 buf->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 wake_up(&buf->vb.done);
1501 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1502 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1503 }
1504 restart_video_queue(dev,q);
1505 spin_unlock_irqrestore(&dev->slock,flags);
1506}
1507
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001508static char *cx88_vid_irqs[32] = {
1509 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1510 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1511 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1512 "y_sync", "u_sync", "v_sync", "vbi_sync",
1513 "opc_err", "par_err", "rip_err", "pci_abort",
1514};
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516static void cx8800_vid_irq(struct cx8800_dev *dev)
1517{
1518 struct cx88_core *core = dev->core;
1519 u32 status, mask, count;
1520
1521 status = cx_read(MO_VID_INTSTAT);
1522 mask = cx_read(MO_VID_INTMSK);
1523 if (0 == (status & mask))
1524 return;
1525 cx_write(MO_VID_INTSTAT, status);
1526 if (irq_debug || (status & mask & ~0xff))
1527 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001528 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1529 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 /* risc op code error */
1532 if (status & (1 << 16)) {
1533 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1534 cx_clear(MO_VID_DMACNTRL, 0x11);
1535 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001536 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
1539 /* risc1 y */
1540 if (status & 0x01) {
1541 spin_lock(&dev->slock);
1542 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001543 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 spin_unlock(&dev->slock);
1545 }
1546
1547 /* risc1 vbi */
1548 if (status & 0x08) {
1549 spin_lock(&dev->slock);
1550 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001551 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 spin_unlock(&dev->slock);
1553 }
1554
1555 /* risc2 y */
1556 if (status & 0x10) {
1557 dprintk(2,"stopper video\n");
1558 spin_lock(&dev->slock);
1559 restart_video_queue(dev,&dev->vidq);
1560 spin_unlock(&dev->slock);
1561 }
1562
1563 /* risc2 vbi */
1564 if (status & 0x80) {
1565 dprintk(2,"stopper vbi\n");
1566 spin_lock(&dev->slock);
1567 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1568 spin_unlock(&dev->slock);
1569 }
1570}
1571
David Howells7d12e782006-10-05 14:55:46 +01001572static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 struct cx8800_dev *dev = dev_id;
1575 struct cx88_core *core = dev->core;
1576 u32 status;
1577 int loop, handled = 0;
1578
1579 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001580 status = cx_read(MO_PCI_INTSTAT) &
1581 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (0 == status)
1583 goto out;
1584 cx_write(MO_PCI_INTSTAT, status);
1585 handled = 1;
1586
1587 if (status & core->pci_irqmask)
1588 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001589 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 cx8800_vid_irq(dev);
1591 };
1592 if (10 == loop) {
1593 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1594 core->name);
1595 cx_write(MO_PCI_INTMSK,0);
1596 }
1597
1598 out:
1599 return IRQ_RETVAL(handled);
1600}
1601
1602/* ----------------------------------------------------------- */
1603/* exported stuff */
1604
Arjan van de Venfa027c22007-02-12 00:55:33 -08001605static const struct file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 .owner = THIS_MODULE,
1608 .open = video_open,
1609 .release = video_release,
1610 .read = video_read,
1611 .poll = video_poll,
1612 .mmap = video_mmap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001613 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001614 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 .llseek = no_llseek,
1616};
1617
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001618static struct video_device cx8800_vbi_template;
Adrian Bunk408b6642005-05-01 08:59:29 -07001619static struct video_device cx8800_video_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001621 .name = "cx8800-video",
1622 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1623 .fops = &video_fops,
1624 .minor = -1,
1625 .vidioc_querycap = vidioc_querycap,
1626 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1627 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1628 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1629 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1630 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1631 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1632 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1633 .vidioc_reqbufs = vidioc_reqbufs,
1634 .vidioc_querybuf = vidioc_querybuf,
1635 .vidioc_qbuf = vidioc_qbuf,
1636 .vidioc_dqbuf = vidioc_dqbuf,
1637 .vidioc_s_std = vidioc_s_std,
1638 .vidioc_enum_input = vidioc_enum_input,
1639 .vidioc_g_input = vidioc_g_input,
1640 .vidioc_s_input = vidioc_s_input,
1641 .vidioc_queryctrl = vidioc_queryctrl,
1642 .vidioc_g_ctrl = vidioc_g_ctrl,
1643 .vidioc_s_ctrl = vidioc_s_ctrl,
1644 .vidioc_streamon = vidioc_streamon,
1645 .vidioc_streamoff = vidioc_streamoff,
1646#ifdef CONFIG_VIDEO_V4L1_COMPAT
1647 .vidiocgmbuf = vidiocgmbuf,
1648#endif
1649 .vidioc_g_tuner = vidioc_g_tuner,
1650 .vidioc_s_tuner = vidioc_s_tuner,
1651 .vidioc_g_frequency = vidioc_g_frequency,
1652 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001653#ifdef CONFIG_VIDEO_ADV_DEBUG
1654 .vidioc_g_register = vidioc_g_register,
1655 .vidioc_s_register = vidioc_s_register,
1656#endif
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001657 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001658 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659};
1660
Arjan van de Venfa027c22007-02-12 00:55:33 -08001661static const struct file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 .owner = THIS_MODULE,
1664 .open = video_open,
1665 .release = video_release,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001666 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001667 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 .llseek = no_llseek,
1669};
1670
Adrian Bunk408b6642005-05-01 08:59:29 -07001671static struct video_device cx8800_radio_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001673 .name = "cx8800-radio",
1674 .type = VID_TYPE_TUNER,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001675 .fops = &radio_fops,
1676 .minor = -1,
1677 .vidioc_querycap = radio_querycap,
1678 .vidioc_g_tuner = radio_g_tuner,
1679 .vidioc_enum_input = radio_enum_input,
1680 .vidioc_g_audio = radio_g_audio,
1681 .vidioc_s_tuner = radio_s_tuner,
1682 .vidioc_s_audio = radio_s_audio,
1683 .vidioc_s_input = radio_s_input,
1684 .vidioc_queryctrl = radio_queryctrl,
1685 .vidioc_g_ctrl = vidioc_g_ctrl,
1686 .vidioc_s_ctrl = vidioc_s_ctrl,
1687 .vidioc_g_frequency = vidioc_g_frequency,
1688 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephoa75d2042007-08-01 00:13:28 -03001689#ifdef CONFIG_VIDEO_ADV_DEBUG
1690 .vidioc_g_register = vidioc_g_register,
1691 .vidioc_s_register = vidioc_s_register,
1692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693};
1694
1695/* ----------------------------------------------------------- */
1696
1697static void cx8800_unregister_video(struct cx8800_dev *dev)
1698{
1699 if (dev->radio_dev) {
1700 if (-1 != dev->radio_dev->minor)
1701 video_unregister_device(dev->radio_dev);
1702 else
1703 video_device_release(dev->radio_dev);
1704 dev->radio_dev = NULL;
1705 }
1706 if (dev->vbi_dev) {
1707 if (-1 != dev->vbi_dev->minor)
1708 video_unregister_device(dev->vbi_dev);
1709 else
1710 video_device_release(dev->vbi_dev);
1711 dev->vbi_dev = NULL;
1712 }
1713 if (dev->video_dev) {
1714 if (-1 != dev->video_dev->minor)
1715 video_unregister_device(dev->video_dev);
1716 else
1717 video_device_release(dev->video_dev);
1718 dev->video_dev = NULL;
1719 }
1720}
1721
1722static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1723 const struct pci_device_id *pci_id)
1724{
1725 struct cx8800_dev *dev;
1726 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 int err;
1729
Panagiotis Issaris74081872006-01-11 19:40:56 -02001730 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (NULL == dev)
1732 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /* pci init */
1735 dev->pci = pci_dev;
1736 if (pci_enable_device(pci_dev)) {
1737 err = -EIO;
1738 goto fail_free;
1739 }
1740 core = cx88_core_get(dev->pci);
1741 if (NULL == core) {
1742 err = -EINVAL;
1743 goto fail_free;
1744 }
1745 dev->core = core;
1746
1747 /* print pci info */
1748 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001749 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1750 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001751 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001753 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 pci_set_master(pci_dev);
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -03001756 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1758 err = -EIO;
1759 goto fail_core;
1760 }
1761
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001762 /* Initialize VBI template */
1763 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1764 sizeof(cx8800_vbi_template) );
1765 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1766 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001770 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /* init video dma queues */
1773 INIT_LIST_HEAD(&dev->vidq.active);
1774 INIT_LIST_HEAD(&dev->vidq.queued);
1775 dev->vidq.timeout.function = cx8800_vid_timeout;
1776 dev->vidq.timeout.data = (unsigned long)dev;
1777 init_timer(&dev->vidq.timeout);
1778 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1779 MO_VID_DMACNTRL,0x11,0x00);
1780
1781 /* init vbi dma queues */
1782 INIT_LIST_HEAD(&dev->vbiq.active);
1783 INIT_LIST_HEAD(&dev->vbiq.queued);
1784 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1785 dev->vbiq.timeout.data = (unsigned long)dev;
1786 init_timer(&dev->vbiq.timeout);
1787 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1788 MO_VID_DMACNTRL,0x88,0x00);
1789
1790 /* get irq */
1791 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001792 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001794 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 core->name,pci_dev->irq);
1796 goto fail_core;
1797 }
1798 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1799
1800 /* load and configure helper modules */
Trent Piepho6a59d642007-08-15 14:41:57 -03001801 if (TUNER_ABSENT != core->board.tuner_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 request_module("tuner");
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001803
Trent Piepho6a59d642007-08-15 14:41:57 -03001804 if (core->board.audio_chip == AUDIO_CHIP_WM8775)
Steven Toth30579062006-09-25 12:43:42 -03001805 request_module("wm8775");
1806
Michael Krufky6fcecce2007-08-24 01:32:31 -03001807 switch (core->boardnr) {
1808 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1809 request_module("ir-kbd-i2c");
1810 request_module("rtc-isl1208");
1811 }
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 /* register v4l devices */
1814 dev->video_dev = cx88_vdev_init(core,dev->pci,
1815 &cx8800_video_template,"video");
1816 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1817 video_nr[core->nr]);
1818 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001819 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 core->name);
1821 goto fail_unreg;
1822 }
1823 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1824 core->name,dev->video_dev->minor & 0x1f);
1825
1826 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1827 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1828 vbi_nr[core->nr]);
1829 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001830 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 core->name);
1832 goto fail_unreg;
1833 }
1834 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1835 core->name,dev->vbi_dev->minor & 0x1f);
1836
Trent Piepho6a59d642007-08-15 14:41:57 -03001837 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1839 &cx8800_radio_template,"radio");
1840 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1841 radio_nr[core->nr]);
1842 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001843 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 core->name);
1845 goto fail_unreg;
1846 }
1847 printk(KERN_INFO "%s/0: registered device radio%d\n",
1848 core->name,dev->radio_dev->minor & 0x1f);
1849 }
1850
1851 /* everything worked */
1852 list_add_tail(&dev->devlist,&cx8800_devlist);
1853 pci_set_drvdata(pci_dev,dev);
1854
1855 /* initial device configuration */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001856 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001857 cx88_set_tvnorm(core,core->tvnorm);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001858 init_controls(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001859 cx88_video_mux(core,0);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001860 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001863 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001865 if (IS_ERR(core->kthread)) {
1866 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001867 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1868 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001869 }
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return 0;
1872
1873fail_unreg:
1874 cx8800_unregister_video(dev);
1875 free_irq(pci_dev->irq, dev);
1876fail_core:
1877 cx88_core_put(core,dev->pci);
1878fail_free:
1879 kfree(dev);
1880 return err;
1881}
1882
1883static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1884{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001885 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001886 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001889 if (core->kthread) {
1890 kthread_stop(core->kthread);
1891 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001894 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 pci_disable_device(pci_dev);
1896
1897 /* unregister stuff */
1898
1899 free_irq(pci_dev->irq, dev);
1900 cx8800_unregister_video(dev);
1901 pci_set_drvdata(pci_dev, NULL);
1902
1903 /* free memory */
1904 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1905 list_del(&dev->devlist);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001906 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 kfree(dev);
1908}
1909
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001910#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1912{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001913 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct cx88_core *core = dev->core;
1915
1916 /* stop video+vbi capture */
1917 spin_lock(&dev->slock);
1918 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001919 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 stop_video_dma(dev);
1921 del_timer(&dev->vidq.timeout);
1922 }
1923 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001924 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 cx8800_stop_vbi_dma(dev);
1926 del_timer(&dev->vbiq.timeout);
1927 }
1928 spin_unlock(&dev->slock);
1929
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001930 if (core->ir)
1931 cx88_ir_stop(core, core->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001933 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 pci_save_state(pci_dev);
1936 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1937 pci_disable_device(pci_dev);
1938 dev->state.disabled = 1;
1939 }
1940 return 0;
1941}
1942
1943static int cx8800_resume(struct pci_dev *pci_dev)
1944{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001945 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001947 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001950 err=pci_enable_device(pci_dev);
1951 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001952 printk(KERN_ERR "%s/0: can't enable device\n",
1953 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001954 return err;
1955 }
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 dev->state.disabled = 0;
1958 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001959 err= pci_set_power_state(pci_dev, PCI_D0);
1960 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001961 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001962 pci_disable_device(pci_dev);
1963 dev->state.disabled = 1;
1964
1965 return err;
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 pci_restore_state(pci_dev);
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001970 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001971 if (core->ir)
1972 cx88_ir_start(core, core->ir);
1973
1974 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 /* restart video+vbi capture */
1977 spin_lock(&dev->slock);
1978 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001979 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 restart_video_queue(dev,&dev->vidq);
1981 }
1982 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001983 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1985 }
1986 spin_unlock(&dev->slock);
1987
1988 return 0;
1989}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992/* ----------------------------------------------------------- */
1993
Adrian Bunk408b6642005-05-01 08:59:29 -07001994static struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 {
1996 .vendor = 0x14f1,
1997 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001998 .subvendor = PCI_ANY_ID,
1999 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 },{
2001 /* --- end of list --- */
2002 }
2003};
2004MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2005
2006static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002007 .name = "cx8800",
2008 .id_table = cx8800_pci_tbl,
2009 .probe = cx8800_initdev,
2010 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002011#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 .suspend = cx8800_suspend,
2013 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015};
2016
2017static int cx8800_init(void)
2018{
Trent Piepho5772f812007-08-15 14:41:59 -03002019 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 (CX88_VERSION_CODE >> 16) & 0xff,
2021 (CX88_VERSION_CODE >> 8) & 0xff,
2022 CX88_VERSION_CODE & 0xff);
2023#ifdef SNAPSHOT
2024 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2025 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2026#endif
2027 return pci_register_driver(&cx8800_pci_driver);
2028}
2029
2030static void cx8800_fini(void)
2031{
2032 pci_unregister_driver(&cx8800_pci_driver);
2033}
2034
2035module_init(cx8800_init);
2036module_exit(cx8800_fini);
2037
2038/* ----------------------------------------------------------- */
2039/*
2040 * Local variables:
2041 * c-basic-offset: 8
2042 * End:
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002043 * 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 -07002044 */