blob: 1db97f3a1975dc42d506267dd73020dc542004e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
5 *
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03008 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * - Multituner support
10 * - video_ioctl2 conversion
11 * - PAL/M fixes
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070035#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/kthread.h>
38#include <asm/div64.h>
39
40#include "cx88.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Lawrence Rust69518032011-02-06 17:46:12 -030043#include <media/wm8775.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
46MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
47MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030048MODULE_VERSION(CX88_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* ------------------------------------------------------------------ */
51
52static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
53static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
54static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55
56module_param_array(video_nr, int, NULL, 0444);
57module_param_array(vbi_nr, int, NULL, 0444);
58module_param_array(radio_nr, int, NULL, 0444);
59
60MODULE_PARM_DESC(video_nr,"video device numbers");
61MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
62MODULE_PARM_DESC(radio_nr,"radio device numbers");
63
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030064static unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065module_param(video_debug,int,0644);
66MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
67
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030068static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069module_param(irq_debug,int,0644);
70MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
71
72static unsigned int vid_limit = 16;
73module_param(vid_limit,int,0644);
74MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
75
76#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070077 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* ------------------------------------------------------------------- */
80/* static data */
81
lawrence rust2e4e98e2010-08-25 09:50:20 -030082static const struct cx8800_fmt formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 {
84 .name = "8 bpp, gray",
85 .fourcc = V4L2_PIX_FMT_GREY,
86 .cxformat = ColorFormatY8,
87 .depth = 8,
88 .flags = FORMAT_FLAGS_PACKED,
89 },{
90 .name = "15 bpp RGB, le",
91 .fourcc = V4L2_PIX_FMT_RGB555,
92 .cxformat = ColorFormatRGB15,
93 .depth = 16,
94 .flags = FORMAT_FLAGS_PACKED,
95 },{
96 .name = "15 bpp RGB, be",
97 .fourcc = V4L2_PIX_FMT_RGB555X,
98 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
99 .depth = 16,
100 .flags = FORMAT_FLAGS_PACKED,
101 },{
102 .name = "16 bpp RGB, le",
103 .fourcc = V4L2_PIX_FMT_RGB565,
104 .cxformat = ColorFormatRGB16,
105 .depth = 16,
106 .flags = FORMAT_FLAGS_PACKED,
107 },{
108 .name = "16 bpp RGB, be",
109 .fourcc = V4L2_PIX_FMT_RGB565X,
110 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
111 .depth = 16,
112 .flags = FORMAT_FLAGS_PACKED,
113 },{
114 .name = "24 bpp RGB, le",
115 .fourcc = V4L2_PIX_FMT_BGR24,
116 .cxformat = ColorFormatRGB24,
117 .depth = 24,
118 .flags = FORMAT_FLAGS_PACKED,
119 },{
120 .name = "32 bpp RGB, le",
121 .fourcc = V4L2_PIX_FMT_BGR32,
122 .cxformat = ColorFormatRGB32,
123 .depth = 32,
124 .flags = FORMAT_FLAGS_PACKED,
125 },{
126 .name = "32 bpp RGB, be",
127 .fourcc = V4L2_PIX_FMT_RGB32,
128 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
129 .depth = 32,
130 .flags = FORMAT_FLAGS_PACKED,
131 },{
132 .name = "4:2:2, packed, YUYV",
133 .fourcc = V4L2_PIX_FMT_YUYV,
134 .cxformat = ColorFormatYUY2,
135 .depth = 16,
136 .flags = FORMAT_FLAGS_PACKED,
137 },{
138 .name = "4:2:2, packed, UYVY",
139 .fourcc = V4L2_PIX_FMT_UYVY,
140 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
141 .depth = 16,
142 .flags = FORMAT_FLAGS_PACKED,
143 },
144};
145
lawrence rust2e4e98e2010-08-25 09:50:20 -0300146static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 unsigned int i;
149
150 for (i = 0; i < ARRAY_SIZE(formats); i++)
151 if (formats[i].fourcc == fourcc)
152 return formats+i;
153 return NULL;
154}
155
156/* ------------------------------------------------------------------- */
157
158static const struct v4l2_queryctrl no_ctl = {
159 .name = "42",
160 .flags = V4L2_CTRL_FLAG_DISABLED,
161};
162
lawrence rust2e4e98e2010-08-25 09:50:20 -0300163static const struct cx88_ctrl cx8800_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* --- video --- */
165 {
166 .v = {
167 .id = V4L2_CID_BRIGHTNESS,
168 .name = "Brightness",
169 .minimum = 0x00,
170 .maximum = 0xff,
171 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300172 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .type = V4L2_CTRL_TYPE_INTEGER,
174 },
175 .off = 128,
176 .reg = MO_CONTR_BRIGHT,
177 .mask = 0x00ff,
178 .shift = 0,
179 },{
180 .v = {
181 .id = V4L2_CID_CONTRAST,
182 .name = "Contrast",
183 .minimum = 0,
184 .maximum = 0xff,
185 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200186 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .type = V4L2_CTRL_TYPE_INTEGER,
188 },
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -0700189 .off = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .reg = MO_CONTR_BRIGHT,
191 .mask = 0xff00,
192 .shift = 8,
193 },{
194 .v = {
195 .id = V4L2_CID_HUE,
196 .name = "Hue",
197 .minimum = 0,
198 .maximum = 0xff,
199 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300200 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .type = V4L2_CTRL_TYPE_INTEGER,
202 },
Michael Krufky9ac4c1582005-07-07 17:58:38 -0700203 .off = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 .reg = MO_HUE,
205 .mask = 0x00ff,
206 .shift = 0,
207 },{
208 /* strictly, this only describes only U saturation.
209 * V saturation is handled specially through code.
210 */
211 .v = {
212 .id = V4L2_CID_SATURATION,
213 .name = "Saturation",
214 .minimum = 0,
215 .maximum = 0xff,
216 .step = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200217 .default_value = 0x7f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .type = V4L2_CTRL_TYPE_INTEGER,
219 },
220 .off = 0,
221 .reg = MO_UV_SATURATION,
222 .mask = 0x00ff,
223 .shift = 0,
224 },{
Frej Drejhammar6d042032008-03-23 22:43:21 -0300225 .v = {
226 .id = V4L2_CID_CHROMA_AGC,
227 .name = "Chroma AGC",
228 .minimum = 0,
229 .maximum = 1,
Frej Drejhammar87a17382008-03-23 22:43:22 -0300230 .default_value = 0x1,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300231 .type = V4L2_CTRL_TYPE_BOOLEAN,
232 },
233 .reg = MO_INPUT_FORMAT,
234 .mask = 1 << 10,
235 .shift = 10,
236 }, {
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300237 .v = {
238 .id = V4L2_CID_COLOR_KILLER,
239 .name = "Color killer",
240 .minimum = 0,
241 .maximum = 1,
Frej Drejhammar0b5afdd2008-03-23 22:43:25 -0300242 .default_value = 0x1,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300243 .type = V4L2_CTRL_TYPE_BOOLEAN,
244 },
245 .reg = MO_INPUT_FORMAT,
246 .mask = 1 << 9,
247 .shift = 9,
248 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* --- audio --- */
250 .v = {
251 .id = V4L2_CID_AUDIO_MUTE,
252 .name = "Mute",
253 .minimum = 0,
254 .maximum = 1,
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200255 .default_value = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .type = V4L2_CTRL_TYPE_BOOLEAN,
257 },
258 .reg = AUD_VOL_CTL,
259 .sreg = SHADOW_AUD_VOL_CTL,
260 .mask = (1 << 6),
261 .shift = 6,
262 },{
263 .v = {
264 .id = V4L2_CID_AUDIO_VOLUME,
265 .name = "Volume",
266 .minimum = 0,
267 .maximum = 0x3f,
268 .step = 1,
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300269 .default_value = 0x3f,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .type = V4L2_CTRL_TYPE_INTEGER,
271 },
272 .reg = AUD_VOL_CTL,
273 .sreg = SHADOW_AUD_VOL_CTL,
274 .mask = 0x3f,
275 .shift = 0,
276 },{
277 .v = {
278 .id = V4L2_CID_AUDIO_BALANCE,
279 .name = "Balance",
280 .minimum = 0,
281 .maximum = 0x7f,
282 .step = 1,
283 .default_value = 0x40,
284 .type = V4L2_CTRL_TYPE_INTEGER,
285 },
286 .reg = AUD_BAL_CTL,
287 .sreg = SHADOW_AUD_BAL_CTL,
288 .mask = 0x7f,
289 .shift = 0,
290 }
291};
lawrence rust2e4e98e2010-08-25 09:50:20 -0300292enum { CX8800_CTLS = ARRAY_SIZE(cx8800_ctls) };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Hans Verkuil2ba58892009-02-13 10:57:48 -0300294/* Must be sorted from low to high control ID! */
Michael Krufky38a27132006-06-26 23:42:39 -0300295const u32 cx88_user_ctrls[] = {
296 V4L2_CID_USER_CLASS,
297 V4L2_CID_BRIGHTNESS,
298 V4L2_CID_CONTRAST,
299 V4L2_CID_SATURATION,
300 V4L2_CID_HUE,
301 V4L2_CID_AUDIO_VOLUME,
302 V4L2_CID_AUDIO_BALANCE,
303 V4L2_CID_AUDIO_MUTE,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300304 V4L2_CID_CHROMA_AGC,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300305 V4L2_CID_COLOR_KILLER,
Michael Krufky38a27132006-06-26 23:42:39 -0300306 0
307};
308EXPORT_SYMBOL(cx88_user_ctrls);
309
lawrence rust2e4e98e2010-08-25 09:50:20 -0300310static const u32 * const ctrl_classes[] = {
Michael Krufky38a27132006-06-26 23:42:39 -0300311 cx88_user_ctrls,
312 NULL
313};
314
Frej Drejhammar6d042032008-03-23 22:43:21 -0300315int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
Michael Krufky38a27132006-06-26 23:42:39 -0300316{
317 int i;
318
319 if (qctrl->id < V4L2_CID_BASE ||
320 qctrl->id >= V4L2_CID_LASTP1)
321 return -EINVAL;
322 for (i = 0; i < CX8800_CTLS; i++)
323 if (cx8800_ctls[i].v.id == qctrl->id)
324 break;
325 if (i == CX8800_CTLS) {
326 *qctrl = no_ctl;
327 return 0;
328 }
329 *qctrl = cx8800_ctls[i].v;
Frej Drejhammar6d042032008-03-23 22:43:21 -0300330 /* Report chroma AGC as inactive when SECAM is selected */
331 if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
332 core->tvnorm & V4L2_STD_SECAM)
333 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
334
Michael Krufky38a27132006-06-26 23:42:39 -0300335 return 0;
336}
337EXPORT_SYMBOL(cx8800_ctrl_query);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/* ------------------------------------------------------------------- */
340/* resource management */
341
342static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
343{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700344 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (fh->resources & bit)
346 /* have it already allocated */
347 return 1;
348
349 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200350 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (dev->resources & bit) {
352 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200353 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355 }
356 /* it's free, grab it */
357 fh->resources |= bit;
358 dev->resources |= bit;
359 dprintk(1,"res: get %d\n",bit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200360 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 1;
362}
363
364static
365int res_check(struct cx8800_fh *fh, unsigned int bit)
366{
367 return (fh->resources & bit);
368}
369
370static
371int res_locked(struct cx8800_dev *dev, unsigned int bit)
372{
373 return (dev->resources & bit);
374}
375
376static
377void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
378{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700379 struct cx88_core *core = dev->core;
Eric Sesterhennae246012006-03-13 13:17:11 -0300380 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Ingo Molnar3593cab2006-02-07 06:49:14 -0200382 mutex_lock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 fh->resources &= ~bits;
384 dev->resources &= ~bits;
385 dprintk(1,"res: put %d\n",bits);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200386 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/* ------------------------------------------------------------------ */
390
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300391int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700393 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300396 input, INPUT(input).vmux,
397 INPUT(input).gpio0,INPUT(input).gpio1,
398 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700399 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300400 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
401 cx_write(MO_GP3_IO, INPUT(input).gpio3);
402 cx_write(MO_GP0_IO, INPUT(input).gpio0);
403 cx_write(MO_GP1_IO, INPUT(input).gpio1);
404 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Trent Piepho6a59d642007-08-15 14:41:57 -0300406 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 case CX88_VMUX_SVIDEO:
408 cx_set(MO_AFECFG_IO, 0x00000001);
409 cx_set(MO_INPUT_FORMAT, 0x00010010);
410 cx_set(MO_FILTER_EVEN, 0x00002020);
411 cx_set(MO_FILTER_ODD, 0x00002020);
412 break;
413 default:
414 cx_clear(MO_AFECFG_IO, 0x00000001);
415 cx_clear(MO_INPUT_FORMAT, 0x00010010);
416 cx_clear(MO_FILTER_EVEN, 0x00002020);
417 cx_clear(MO_FILTER_ODD, 0x00002020);
418 break;
419 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300420
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300421 /* if there are audioroutes defined, we have an external
422 ADC to deal with audio */
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300423 if (INPUT(input).audioroute) {
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300424 /* The wm8775 module has the "2" route hardwired into
425 the initialization. Some boards may use different
426 routes for different inputs. HVR-1300 surely does */
427 if (core->board.audio_chip &&
Hans Verkuil38f9d302008-07-23 05:09:15 -0300428 core->board.audio_chip == V4L2_IDENT_WM8775) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300429 call_all(core, audio, s_routing,
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300430 INPUT(input).audioroute, 0, 0);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300431 }
Darron Broad430189d2008-10-15 14:18:42 -0300432 /* cx2388's C-ADC is connected to the tuner only.
433 When used with S-Video, that ADC is busy dealing with
434 chroma, so an external must be used for baseband audio */
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300435 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
436 INPUT(input).type != CX88_VMUX_CABLE) {
Darron Broad430189d2008-10-15 14:18:42 -0300437 /* "I2S ADC mode" */
438 core->tvaudio = WW_I2SADC;
439 cx88_set_tvaudio(core);
440 } else {
441 /* Normal mode */
442 cx_write(AUD_I2SCNTL, 0x0);
443 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
444 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300445 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300449EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451/* ------------------------------------------------------------------ */
452
453static int start_video_dma(struct cx8800_dev *dev,
454 struct cx88_dmaqueue *q,
455 struct cx88_buffer *buf)
456{
457 struct cx88_core *core = dev->core;
458
459 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700460 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700462 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
464
465 /* reset counter */
466 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
467 q->count = 1;
468
469 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300470 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700471
472 /* Enables corresponding bits at PCI_INT_STAT:
473 bits 0 to 4: video, audio, transport stream, VIP, Host
474 bit 7: timer
475 bits 8 and 9: DMA complete for: SRC, DST
476 bits 10 and 11: BERR signal asserted for RISC: RD, WR
477 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 cx_set(MO_VID_INTMSK, 0x0f0011);
480
481 /* enable capture */
482 cx_set(VID_CAPTURE_CONTROL,0x06);
483
484 /* start dma */
485 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700486 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 return 0;
489}
490
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300491#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static int stop_video_dma(struct cx8800_dev *dev)
493{
494 struct cx88_core *core = dev->core;
495
496 /* stop dma */
497 cx_clear(MO_VID_DMACNTRL, 0x11);
498
499 /* disable capture */
500 cx_clear(VID_CAPTURE_CONTROL,0x06);
501
502 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300503 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 cx_clear(MO_VID_INTMSK, 0x0f0011);
505 return 0;
506}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509static int restart_video_queue(struct cx8800_dev *dev,
510 struct cx88_dmaqueue *q)
511{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700512 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct cx88_buffer *buf, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (!list_empty(&q->active)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800516 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
518 buf, buf->vb.i);
519 start_video_dma(dev, q, buf);
Trent Piepho8bb629e22007-10-10 05:37:40 -0300520 list_for_each_entry(buf, &q->active, vb.queue)
521 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
523 return 0;
524 }
525
526 prev = NULL;
527 for (;;) {
528 if (list_empty(&q->queued))
529 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800530 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (NULL == prev) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700532 list_move_tail(&buf->vb.queue, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300534 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 buf->count = q->count++;
536 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
537 dprintk(2,"[%p/%d] restart_queue - first active\n",
538 buf,buf->vb.i);
539
540 } else if (prev->vb.width == buf->vb.width &&
541 prev->vb.height == buf->vb.height &&
542 prev->fmt == buf->fmt) {
Akinobu Mita179e0912006-06-26 00:24:41 -0700543 list_move_tail(&buf->vb.queue, &q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300544 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 buf->count = q->count++;
546 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
547 dprintk(2,"[%p/%d] restart_queue - move to active\n",
548 buf,buf->vb.i);
549 } else {
550 return 0;
551 }
552 prev = buf;
553 }
554}
555
556/* ------------------------------------------------------------------ */
557
558static int
559buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
560{
561 struct cx8800_fh *fh = q->priv_data;
562
563 *size = fh->fmt->depth*fh->width*fh->height >> 3;
564 if (0 == *count)
565 *count = 32;
Andreas Bombedab7e312010-03-21 16:02:45 -0300566 if (*size * *count > vid_limit * 1024 * 1024)
567 *count = (vid_limit * 1024 * 1024) / *size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
569}
570
571static int
572buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
573 enum v4l2_field field)
574{
575 struct cx8800_fh *fh = q->priv_data;
576 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700577 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300579 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int rc, init_buffer = 0;
581
582 BUG_ON(NULL == fh->fmt);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700583 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
584 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -EINVAL;
586 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
587 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
588 return -EINVAL;
589
590 if (buf->fmt != fh->fmt ||
591 buf->vb.width != fh->width ||
592 buf->vb.height != fh->height ||
593 buf->vb.field != field) {
594 buf->fmt = fh->fmt;
595 buf->vb.width = fh->width;
596 buf->vb.height = fh->height;
597 buf->vb.field = field;
598 init_buffer = 1;
599 }
600
Brandon Philips0fc06862007-11-06 20:02:36 -0300601 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 init_buffer = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300603 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto fail;
605 }
606
607 if (init_buffer) {
608 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
609 switch (buf->vb.field) {
610 case V4L2_FIELD_TOP:
611 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300612 dma->sglist, 0, UNSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 buf->bpl, 0, buf->vb.height);
614 break;
615 case V4L2_FIELD_BOTTOM:
616 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300617 dma->sglist, UNSET, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 buf->bpl, 0, buf->vb.height);
619 break;
620 case V4L2_FIELD_INTERLACED:
621 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300622 dma->sglist, 0, buf->bpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 buf->bpl, buf->bpl,
624 buf->vb.height >> 1);
625 break;
626 case V4L2_FIELD_SEQ_TB:
627 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300628 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 0, buf->bpl * (buf->vb.height >> 1),
630 buf->bpl, 0,
631 buf->vb.height >> 1);
632 break;
633 case V4L2_FIELD_SEQ_BT:
634 cx88_risc_buffer(dev->pci, &buf->risc,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300635 dma->sglist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 buf->bpl * (buf->vb.height >> 1), 0,
637 buf->bpl, 0,
638 buf->vb.height >> 1);
639 break;
640 default:
641 BUG();
642 }
643 }
644 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
645 buf, buf->vb.i,
646 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
647 (unsigned long)buf->risc.dma);
648
Brandon Philips0fc06862007-11-06 20:02:36 -0300649 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651
652 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300653 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return rc;
655}
656
657static void
658buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
659{
660 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
661 struct cx88_buffer *prev;
662 struct cx8800_fh *fh = vq->priv_data;
663 struct cx8800_dev *dev = fh->dev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700664 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 struct cx88_dmaqueue *q = &dev->vidq;
666
667 /* add jump to stopper */
668 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
669 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
670
671 if (!list_empty(&q->queued)) {
672 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300673 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
675 buf, buf->vb.i);
676
677 } else if (list_empty(&q->active)) {
678 list_add_tail(&buf->vb.queue,&q->active);
679 start_video_dma(dev, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300680 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 buf->count = q->count++;
682 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
683 dprintk(2,"[%p/%d] buffer_queue - first active\n",
684 buf, buf->vb.i);
685
686 } else {
687 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
688 if (prev->vb.width == buf->vb.width &&
689 prev->vb.height == buf->vb.height &&
690 prev->fmt == buf->fmt) {
691 list_add_tail(&buf->vb.queue,&q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300692 buf->vb.state = VIDEOBUF_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 buf->count = q->count++;
694 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
695 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
696 buf, buf->vb.i);
697
698 } else {
699 list_add_tail(&buf->vb.queue,&q->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300700 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
702 buf, buf->vb.i);
703 }
704 }
705}
706
707static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
708{
709 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300711 cx88_free_buffer(q,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
lawrence rust2e4e98e2010-08-25 09:50:20 -0300714static const struct videobuf_queue_ops cx8800_video_qops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 .buf_setup = buffer_setup,
716 .buf_prepare = buffer_prepare,
717 .buf_queue = buffer_queue,
718 .buf_release = buffer_release,
719};
720
721/* ------------------------------------------------------------------ */
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724/* ------------------------------------------------------------------ */
725
726static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
727{
728 switch (fh->type) {
729 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
730 return &fh->vidq;
731 case V4L2_BUF_TYPE_VBI_CAPTURE:
732 return &fh->vbiq;
733 default:
734 BUG();
735 return NULL;
736 }
737}
738
739static int get_ressource(struct cx8800_fh *fh)
740{
741 switch (fh->type) {
742 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
743 return RESOURCE_VIDEO;
744 case V4L2_BUF_TYPE_VBI_CAPTURE:
745 return RESOURCE_VBI;
746 default:
747 BUG();
748 return 0;
749 }
750}
751
Hans Verkuilbec43662008-12-30 06:58:20 -0300752static int video_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200754 struct video_device *vdev = video_devdata(file);
755 struct cx8800_dev *dev = video_drvdata(file);
Dan Carpenter5401c2d2010-10-21 16:21:45 -0300756 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct cx8800_fh *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 enum v4l2_buf_type type = 0;
759 int radio = 0;
760
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200761 switch (vdev->vfl_type) {
762 case VFL_TYPE_GRABBER:
763 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
764 break;
765 case VFL_TYPE_VBI:
766 type = V4L2_BUF_TYPE_VBI_CAPTURE;
767 break;
768 case VFL_TYPE_RADIO:
769 radio = 1;
770 break;
771 }
772
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200773 dprintk(1, "open dev=%s radio=%d type=%s\n",
774 video_device_node_name(vdev), radio, v4l2_type_names[type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /* allocate + initialize per filehandle data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200777 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Mauro Carvalho Chehabda497e32010-09-15 09:23:20 -0300778 if (unlikely(!fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -ENOMEM;
Mauro Carvalho Chehabda497e32010-09-15 09:23:20 -0300780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 file->private_data = fh;
782 fh->dev = dev;
783 fh->radio = radio;
784 fh->type = type;
785 fh->width = 320;
786 fh->height = 240;
787 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
788
Mauro Carvalho Chehabda497e32010-09-15 09:23:20 -0300789 mutex_lock(&core->lock);
Mauro Carvalho Chehabda497e32010-09-15 09:23:20 -0300790
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300791 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
792 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 V4L2_BUF_TYPE_VIDEO_CAPTURE,
794 V4L2_FIELD_INTERLACED,
795 sizeof(struct cx88_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -0300796 fh, NULL);
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300797 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
798 &dev->pci->dev, &dev->slock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 V4L2_BUF_TYPE_VBI_CAPTURE,
800 V4L2_FIELD_SEQ_TB,
801 sizeof(struct cx88_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -0300802 fh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (fh->radio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 dprintk(1,"video_open: setting radio device\n");
Trent Piepho6a59d642007-08-15 14:41:57 -0300806 cx_write(MO_GP3_IO, core->board.radio.gpio3);
807 cx_write(MO_GP0_IO, core->board.radio.gpio0);
808 cx_write(MO_GP1_IO, core->board.radio.gpio1);
809 cx_write(MO_GP2_IO, core->board.radio.gpio2);
Darron Broad430189d2008-10-15 14:18:42 -0300810 if (core->board.radio.audioroute) {
811 if(core->board.audio_chip &&
812 core->board.audio_chip == V4L2_IDENT_WM8775) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300813 call_all(core, audio, s_routing,
814 core->board.radio.audioroute, 0, 0);
Darron Broad430189d2008-10-15 14:18:42 -0300815 }
816 /* "I2S ADC mode" */
817 core->tvaudio = WW_I2SADC;
818 cx88_set_tvaudio(core);
819 } else {
820 /* FM Mode */
821 core->tvaudio = WW_FM;
822 cx88_set_tvaudio(core);
823 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
824 }
Hans Verkuilb8341e12009-03-29 08:26:01 -0300825 call_all(core, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Jonathan Nieder8d115932011-05-01 06:31:40 -0300828 core->users++;
Mauro Carvalho Chehabda497e32010-09-15 09:23:20 -0300829 mutex_unlock(&core->lock);
Darron Broad3e010842008-09-25 16:51:11 -0300830
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834static ssize_t
Peter Hagervallf9e7a022005-11-08 21:36:29 -0800835video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct cx8800_fh *fh = file->private_data;
838
839 switch (fh->type) {
840 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
841 if (res_locked(fh->dev,RESOURCE_VIDEO))
842 return -EBUSY;
843 return videobuf_read_one(&fh->vidq, data, count, ppos,
844 file->f_flags & O_NONBLOCK);
845 case V4L2_BUF_TYPE_VBI_CAPTURE:
846 if (!res_get(fh->dev,fh,RESOURCE_VBI))
847 return -EBUSY;
848 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
849 file->f_flags & O_NONBLOCK);
850 default:
851 BUG();
852 return 0;
853 }
854}
855
856static unsigned int
857video_poll(struct file *file, struct poll_table_struct *wait)
858{
859 struct cx8800_fh *fh = file->private_data;
860 struct cx88_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300861 unsigned int rc = POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
864 if (!res_get(fh->dev,fh,RESOURCE_VBI))
865 return POLLERR;
866 return videobuf_poll_stream(file, &fh->vbiq, wait);
867 }
868
Figo.zhang9fd64182009-06-16 13:31:29 -0300869 mutex_lock(&fh->vidq.vb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (res_check(fh,RESOURCE_VIDEO)) {
871 /* streaming capture */
872 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300873 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
875 } else {
876 /* read() capture */
877 buf = (struct cx88_buffer*)fh->vidq.read_buf;
878 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300879 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 poll_wait(file, &buf->vb.done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300882 if (buf->vb.state == VIDEOBUF_DONE ||
883 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300884 rc = POLLIN|POLLRDNORM;
885 else
886 rc = 0;
887done:
888 mutex_unlock(&fh->vidq.vb_lock);
889 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Hans Verkuilbec43662008-12-30 06:58:20 -0300892static int video_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 struct cx8800_fh *fh = file->private_data;
895 struct cx8800_dev *dev = fh->dev;
896
897 /* turn off overlay */
898 if (res_check(fh, RESOURCE_OVERLAY)) {
899 /* FIXME */
900 res_free(dev,fh,RESOURCE_OVERLAY);
901 }
902
903 /* stop video capture */
904 if (res_check(fh, RESOURCE_VIDEO)) {
905 videobuf_queue_cancel(&fh->vidq);
906 res_free(dev,fh,RESOURCE_VIDEO);
907 }
908 if (fh->vidq.read_buf) {
909 buffer_release(&fh->vidq,fh->vidq.read_buf);
910 kfree(fh->vidq.read_buf);
911 }
912
913 /* stop vbi capture */
914 if (res_check(fh, RESOURCE_VBI)) {
Brandon Philips053fcb62007-11-13 20:11:26 -0300915 videobuf_stop(&fh->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 res_free(dev,fh,RESOURCE_VBI);
917 }
918
919 videobuf_mmap_free(&fh->vidq);
920 videobuf_mmap_free(&fh->vbiq);
Mauro Carvalho Chehabda497e32010-09-15 09:23:20 -0300921
922 mutex_lock(&dev->core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 file->private_data = NULL;
924 kfree(fh);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700925
Jonathan Nieder8d115932011-05-01 06:31:40 -0300926 dev->core->users--;
927 if (!dev->core->users)
Laurent Pinchart622b8282009-10-05 10:48:17 -0300928 call_all(dev->core, core, s_power, 0);
Devin Heitmueller06f837c2009-04-28 14:35:27 -0300929 mutex_unlock(&dev->core->lock);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932}
933
934static int
935video_mmap(struct file *file, struct vm_area_struct * vma)
936{
937 struct cx8800_fh *fh = file->private_data;
938
939 return videobuf_mmap_mapper(get_queue(fh), vma);
940}
941
942/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300943/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300945int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
lawrence rust2e4e98e2010-08-25 09:50:20 -0300947 const struct cx88_ctrl *c = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 u32 value;
949 int i;
950
951 for (i = 0; i < CX8800_CTLS; i++)
952 if (cx8800_ctls[i].v.id == ctl->id)
953 c = &cx8800_ctls[i];
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300954 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -EINVAL;
956
957 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
958 switch (ctl->id) {
959 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -0300960 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
961 : (0x7f - (value & 0x7f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
963 case V4L2_CID_AUDIO_VOLUME:
964 ctl->value = 0x3f - (value & 0x3f);
965 break;
966 default:
967 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
968 break;
969 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300970 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
971 ctl->id, c->v.name, ctl->value, c->reg,
972 value,c->mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
974}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300975EXPORT_SYMBOL(cx88_get_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300977int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
lawrence rust2e4e98e2010-08-25 09:50:20 -0300979 const struct cx88_ctrl *c = NULL;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200980 u32 value,mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int i;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300982
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200983 for (i = 0; i < CX8800_CTLS; i++) {
984 if (cx8800_ctls[i].v.id == ctl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 c = &cx8800_ctls[i];
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200986 }
987 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300988 if (unlikely(NULL == c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return -EINVAL;
990
991 if (ctl->value < c->v.minimum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700992 ctl->value = c->v.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (ctl->value > c->v.maximum)
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700994 ctl->value = c->v.maximum;
Lawrence Rust69518032011-02-06 17:46:12 -0300995
996 /* Pass changes onto any WM8775 */
997 if (core->board.audio_chip == V4L2_IDENT_WM8775) {
998 struct v4l2_control client_ctl;
999 memset(&client_ctl, 0, sizeof(client_ctl));
1000 client_ctl.id = ctl->id;
1001
1002 switch (ctl->id) {
1003 case V4L2_CID_AUDIO_MUTE:
1004 client_ctl.value = ctl->value;
1005 break;
1006 case V4L2_CID_AUDIO_VOLUME:
1007 client_ctl.value = (ctl->value) ?
1008 (0x90 + ctl->value) << 8 : 0;
1009 break;
1010 case V4L2_CID_AUDIO_BALANCE:
1011 client_ctl.value = ctl->value << 9;
1012 break;
1013 default:
1014 client_ctl.id = 0;
1015 break;
1016 }
1017 if (client_ctl.id)
1018 call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
1019 }
1020
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001021 mask=c->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 switch (ctl->id) {
1023 case V4L2_CID_AUDIO_BALANCE:
Marcin Rudowski9f9c9072006-03-12 00:03:47 -03001024 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 break;
1026 case V4L2_CID_AUDIO_VOLUME:
1027 value = 0x3f - (ctl->value & 0x3f);
1028 break;
1029 case V4L2_CID_SATURATION:
1030 /* special v_sat handling */
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001031
1032 value = ((ctl->value - c->off) << c->shift) & c->mask;
1033
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001034 if (core->tvnorm & V4L2_STD_SECAM) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001035 /* For SECAM, both U and V sat should be equal */
1036 value=value<<8|value;
1037 } else {
1038 /* Keeps U Saturation proportional to V Sat */
1039 value=(value*0x5a)/0x7f<<8|value;
1040 }
1041 mask=0xffff;
1042 break;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001043 case V4L2_CID_CHROMA_AGC:
1044 /* Do not allow chroma AGC to be enabled for SECAM */
1045 value = ((ctl->value - c->off) << c->shift) & c->mask;
1046 if (core->tvnorm & V4L2_STD_SECAM && value)
1047 return -EINVAL;
1048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 default:
1050 value = ((ctl->value - c->off) << c->shift) & c->mask;
1051 break;
1052 }
Ian Pickworth6457af52006-02-27 00:09:45 -03001053 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1054 ctl->id, c->v.name, ctl->value, c->reg, value,
1055 mask, c->sreg ? " [shadowed]" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (c->sreg) {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001057 cx_sandor(c->sreg, c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 } else {
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001059 cx_andor(c->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 return 0;
1062}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001063EXPORT_SYMBOL(cx88_set_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001065static void init_controls(struct cx88_core *core)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001067 struct v4l2_control ctrl;
1068 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001070 for (i = 0; i < CX8800_CTLS; i++) {
1071 ctrl.id=cx8800_ctls[i].v.id;
Marcin Rudowski9f9c9072006-03-12 00:03:47 -03001072 ctrl.value=cx8800_ctls[i].v.default_value;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001073
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001074 cx88_set_control(core, &ctrl);
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -02001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001079/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Hans Verkuil78b526a2008-05-28 12:16:41 -03001081static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001082 struct v4l2_format *f)
1083{
1084 struct cx8800_fh *fh = priv;
1085
1086 f->fmt.pix.width = fh->width;
1087 f->fmt.pix.height = fh->height;
1088 f->fmt.pix.field = fh->vidq.field;
1089 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1090 f->fmt.pix.bytesperline =
1091 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1092 f->fmt.pix.sizeimage =
1093 f->fmt.pix.height * f->fmt.pix.bytesperline;
1094 return 0;
1095}
1096
Hans Verkuil78b526a2008-05-28 12:16:41 -03001097static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct v4l2_format *f)
1099{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001100 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
lawrence rust2e4e98e2010-08-25 09:50:20 -03001101 const struct cx8800_fmt *fmt;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001102 enum v4l2_field field;
1103 unsigned int maxw, maxh;
1104
1105 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1106 if (NULL == fmt)
1107 return -EINVAL;
1108
1109 field = f->fmt.pix.field;
1110 maxw = norm_maxw(core->tvnorm);
1111 maxh = norm_maxh(core->tvnorm);
1112
1113 if (V4L2_FIELD_ANY == field) {
1114 field = (f->fmt.pix.height > maxh/2)
1115 ? V4L2_FIELD_INTERLACED
1116 : V4L2_FIELD_BOTTOM;
1117 }
1118
1119 switch (field) {
1120 case V4L2_FIELD_TOP:
1121 case V4L2_FIELD_BOTTOM:
1122 maxh = maxh / 2;
1123 break;
1124 case V4L2_FIELD_INTERLACED:
1125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 default:
1127 return -EINVAL;
1128 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001129
1130 f->fmt.pix.field = field;
Trent Piepho4b899452009-05-30 21:45:46 -03001131 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1132 &f->fmt.pix.height, 32, maxh, 0, 0);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001133 f->fmt.pix.bytesperline =
1134 (f->fmt.pix.width * fmt->depth) >> 3;
1135 f->fmt.pix.sizeimage =
1136 f->fmt.pix.height * f->fmt.pix.bytesperline;
1137
1138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Hans Verkuil78b526a2008-05-28 12:16:41 -03001141static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001142 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001144 struct cx8800_fh *fh = priv;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001145 int err = vidioc_try_fmt_vid_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001146
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001147 if (0 != err)
1148 return err;
1149 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1150 fh->width = f->fmt.pix.width;
1151 fh->height = f->fmt.pix.height;
1152 fh->vidq.field = f->fmt.pix.field;
1153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001156static int vidioc_querycap (struct file *file, void *priv,
1157 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001159 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001162 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001163 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001164 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001165 cap->capabilities =
1166 V4L2_CAP_VIDEO_CAPTURE |
1167 V4L2_CAP_READWRITE |
1168 V4L2_CAP_STREAMING |
1169 V4L2_CAP_VBI_CAPTURE;
Trent Piepho6a59d642007-08-15 14:41:57 -03001170 if (UNSET != core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001171 cap->capabilities |= V4L2_CAP_TUNER;
1172 return 0;
1173}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001174
Hans Verkuil78b526a2008-05-28 12:16:41 -03001175static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001176 struct v4l2_fmtdesc *f)
1177{
1178 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001181 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1182 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001184 return 0;
1185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001187static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001189 struct cx8800_fh *fh = priv;
1190 return (videobuf_reqbufs(get_queue(fh), p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001193static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1194{
1195 struct cx8800_fh *fh = priv;
1196 return (videobuf_querybuf(get_queue(fh), p));
1197}
1198
1199static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1200{
1201 struct cx8800_fh *fh = priv;
1202 return (videobuf_qbuf(get_queue(fh), p));
1203}
1204
1205static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1206{
1207 struct cx8800_fh *fh = priv;
1208 return (videobuf_dqbuf(get_queue(fh), p,
1209 file->f_flags & O_NONBLOCK));
1210}
1211
1212static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1213{
1214 struct cx8800_fh *fh = priv;
1215 struct cx8800_dev *dev = fh->dev;
1216
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001217 /* We should remember that this driver also supports teletext, */
1218 /* so we have to test if the v4l2_buf_type is VBI capture data. */
1219 if (unlikely((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1220 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001221 return -EINVAL;
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001222
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001223 if (unlikely(i != fh->type))
1224 return -EINVAL;
1225
1226 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1227 return -EBUSY;
1228 return videobuf_streamon(get_queue(fh));
1229}
1230
1231static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1232{
1233 struct cx8800_fh *fh = priv;
1234 struct cx8800_dev *dev = fh->dev;
1235 int err, res;
1236
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001237 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1238 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001239 return -EINVAL;
Rafael Dinizb058e3f2008-10-24 23:07:57 -03001240
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001241 if (i != fh->type)
1242 return -EINVAL;
1243
1244 res = get_ressource(fh);
1245 err = videobuf_streamoff(get_queue(fh));
1246 if (err < 0)
1247 return err;
1248 res_free(dev,fh,res);
1249 return 0;
1250}
1251
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001252static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001253{
1254 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1255
1256 mutex_lock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001257 cx88_set_tvnorm(core,*tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001258 mutex_unlock(&core->lock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001259
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001260 return 0;
1261}
1262
1263/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001264int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001265{
lawrence rust2e4e98e2010-08-25 09:50:20 -03001266 static const char * const iname[] = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001267 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1268 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1269 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1270 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1271 [ CX88_VMUX_SVIDEO ] = "S-Video",
1272 [ CX88_VMUX_TELEVISION ] = "Television",
1273 [ CX88_VMUX_CABLE ] = "Cable TV",
1274 [ CX88_VMUX_DVB ] = "DVB",
1275 [ CX88_VMUX_DEBUG ] = "for debug only",
1276 };
Trent Piephof3334bc2009-03-04 01:21:03 -03001277 unsigned int n = i->index;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001278
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001279 if (n >= 4)
1280 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -03001281 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001282 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001283 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -03001284 strcpy(i->name,iname[INPUT(n).type]);
1285 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
Julia Lawall473d8022010-08-05 17:22:44 -03001286 (CX88_VMUX_CABLE == INPUT(n).type)) {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001287 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001288 i->std = CX88_NORMS;
Julia Lawall473d8022010-08-05 17:22:44 -03001289 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001290 return 0;
1291}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001292EXPORT_SYMBOL(cx88_enum_input);
1293
1294static int vidioc_enum_input (struct file *file, void *priv,
1295 struct v4l2_input *i)
1296{
1297 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1298 return cx88_enum_input (core,i);
1299}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001300
1301static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1302{
1303 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1304
1305 *i = core->input;
1306 return 0;
1307}
1308
1309static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1310{
1311 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1312
1313 if (i >= 4)
1314 return -EINVAL;
1315
1316 mutex_lock(&core->lock);
1317 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -03001318 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001319 mutex_unlock(&core->lock);
1320 return 0;
1321}
1322
1323
1324
1325static int vidioc_queryctrl (struct file *file, void *priv,
1326 struct v4l2_queryctrl *qctrl)
1327{
Frej Drejhammar6d042032008-03-23 22:43:21 -03001328 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1329
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001330 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1331 if (unlikely(qctrl->id == 0))
1332 return -EINVAL;
Frej Drejhammar6d042032008-03-23 22:43:21 -03001333 return cx8800_ctrl_query(core, qctrl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001334}
1335
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001336static int vidioc_g_ctrl (struct file *file, void *priv,
1337 struct v4l2_control *ctl)
1338{
1339 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1340 return
1341 cx88_get_control(core,ctl);
1342}
1343
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001344static int vidioc_s_ctrl (struct file *file, void *priv,
1345 struct v4l2_control *ctl)
1346{
1347 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001348 return
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001349 cx88_set_control(core,ctl);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001350}
1351
1352static int vidioc_g_tuner (struct file *file, void *priv,
1353 struct v4l2_tuner *t)
1354{
1355 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1356 u32 reg;
1357
Trent Piepho6a59d642007-08-15 14:41:57 -03001358 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001359 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -03001360 if (0 != t->index)
1361 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001362
1363 strcpy(t->name, "Television");
1364 t->type = V4L2_TUNER_ANALOG_TV;
1365 t->capability = V4L2_TUNER_CAP_NORM;
1366 t->rangehigh = 0xffffffffUL;
1367
1368 cx88_get_stereo(core ,t);
1369 reg = cx_read(MO_DEVICE_STATUS);
1370 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1371 return 0;
1372}
1373
1374static int vidioc_s_tuner (struct file *file, void *priv,
1375 struct v4l2_tuner *t)
1376{
1377 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1378
Trent Piepho6a59d642007-08-15 14:41:57 -03001379 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001380 return -EINVAL;
1381 if (0 != t->index)
1382 return -EINVAL;
1383
1384 cx88_set_stereo(core, t->audmode, 1);
1385 return 0;
1386}
1387
1388static int vidioc_g_frequency (struct file *file, void *priv,
1389 struct v4l2_frequency *f)
1390{
1391 struct cx8800_fh *fh = priv;
1392 struct cx88_core *core = fh->dev->core;
1393
Trent Piepho6a59d642007-08-15 14:41:57 -03001394 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001395 return -EINVAL;
1396
1397 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1398 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1399 f->frequency = core->freq;
1400
Hans Verkuilb8341e12009-03-29 08:26:01 -03001401 call_all(core, tuner, g_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001402
1403 return 0;
1404}
1405
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001406int cx88_set_freq (struct cx88_core *core,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001407 struct v4l2_frequency *f)
1408{
Trent Piepho6a59d642007-08-15 14:41:57 -03001409 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001410 return -EINVAL;
1411 if (unlikely(f->tuner != 0))
1412 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001413
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001414 mutex_lock(&core->lock);
1415 core->freq = f->frequency;
1416 cx88_newstation(core);
Hans Verkuilb8341e12009-03-29 08:26:01 -03001417 call_all(core, tuner, s_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001418
1419 /* When changing channels it is required to reset TVAUDIO */
1420 msleep (10);
1421 cx88_set_tvaudio(core);
1422
1423 mutex_unlock(&core->lock);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001424
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001425 return 0;
1426}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001427EXPORT_SYMBOL(cx88_set_freq);
1428
1429static int vidioc_s_frequency (struct file *file, void *priv,
1430 struct v4l2_frequency *f)
1431{
1432 struct cx8800_fh *fh = priv;
1433 struct cx88_core *core = fh->dev->core;
1434
1435 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1436 return -EINVAL;
1437 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1438 return -EINVAL;
1439
1440 return
1441 cx88_set_freq (core,f);
1442}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001443
Trent Piephodbbff482007-01-22 23:31:53 -03001444#ifdef CONFIG_VIDEO_ADV_DEBUG
1445static int vidioc_g_register (struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001446 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001447{
1448 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1449
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001450 if (!v4l2_chip_match_host(&reg->match))
Trent Piephodbbff482007-01-22 23:31:53 -03001451 return -EINVAL;
1452 /* cx2388x has a 24-bit register space */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001453 reg->val = cx_read(reg->reg & 0xffffff);
1454 reg->size = 4;
Trent Piephodbbff482007-01-22 23:31:53 -03001455 return 0;
1456}
1457
1458static int vidioc_s_register (struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001459 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001460{
1461 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1462
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001463 if (!v4l2_chip_match_host(&reg->match))
Trent Piephodbbff482007-01-22 23:31:53 -03001464 return -EINVAL;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001465 cx_write(reg->reg & 0xffffff, reg->val);
Trent Piephodbbff482007-01-22 23:31:53 -03001466 return 0;
1467}
1468#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001471/* RADIO ESPECIFIC IOCTLS */
1472/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001474static int radio_querycap (struct file *file, void *priv,
1475 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001477 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct cx88_core *core = dev->core;
1479
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001480 strcpy(cap->driver, "cx8800");
Trent Piepho6a59d642007-08-15 14:41:57 -03001481 strlcpy(cap->card, core->board.name, sizeof(cap->card));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001482 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001483 cap->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return 0;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001487static int radio_g_tuner (struct file *file, void *priv,
1488 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001490 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1491
1492 if (unlikely(t->index > 0))
1493 return -EINVAL;
1494
1495 strcpy(t->name, "Radio");
1496 t->type = V4L2_TUNER_RADIO;
1497
Hans Verkuilb8341e12009-03-29 08:26:01 -03001498 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001499 return 0;
1500}
1501
1502static int radio_enum_input (struct file *file, void *priv,
1503 struct v4l2_input *i)
1504{
1505 if (i->index != 0)
1506 return -EINVAL;
1507 strcpy(i->name,"Radio");
1508 i->type = V4L2_INPUT_TYPE_TUNER;
1509
1510 return 0;
1511}
1512
1513static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1514{
1515 if (unlikely(a->index))
1516 return -EINVAL;
1517
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001518 strcpy(a->name,"Radio");
1519 return 0;
1520}
1521
1522/* FIXME: Should add a standard for radio */
1523
1524static int radio_s_tuner (struct file *file, void *priv,
1525 struct v4l2_tuner *t)
1526{
1527 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1528
1529 if (0 != t->index)
1530 return -EINVAL;
1531
Hans Verkuilb8341e12009-03-29 08:26:01 -03001532 call_all(core, tuner, s_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001533
1534 return 0;
1535}
1536
1537static int radio_s_audio (struct file *file, void *fh,
1538 struct v4l2_audio *a)
1539{
1540 return 0;
1541}
1542
1543static int radio_s_input (struct file *file, void *fh, unsigned int i)
1544{
1545 return 0;
1546}
1547
1548static int radio_queryctrl (struct file *file, void *priv,
1549 struct v4l2_queryctrl *c)
1550{
1551 int i;
1552
1553 if (c->id < V4L2_CID_BASE ||
1554 c->id >= V4L2_CID_LASTP1)
1555 return -EINVAL;
Lawrence Rust69518032011-02-06 17:46:12 -03001556 if (c->id == V4L2_CID_AUDIO_MUTE ||
1557 c->id == V4L2_CID_AUDIO_VOLUME ||
1558 c->id == V4L2_CID_AUDIO_BALANCE) {
Dan Carpentera41b2ea2010-04-02 08:31:50 -03001559 for (i = 0; i < CX8800_CTLS; i++) {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001560 if (cx8800_ctls[i].v.id == c->id)
1561 break;
Dan Carpentera41b2ea2010-04-02 08:31:50 -03001562 }
1563 if (i == CX8800_CTLS)
1564 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001565 *c = cx8800_ctls[i].v;
1566 } else
1567 *c = no_ctl;
1568 return 0;
1569}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571/* ----------------------------------------------------------- */
1572
1573static void cx8800_vid_timeout(unsigned long data)
1574{
1575 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1576 struct cx88_core *core = dev->core;
1577 struct cx88_dmaqueue *q = &dev->vidq;
1578 struct cx88_buffer *buf;
1579 unsigned long flags;
1580
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001581 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 cx_clear(MO_VID_DMACNTRL, 0x11);
1584 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1585
1586 spin_lock_irqsave(&dev->slock,flags);
1587 while (!list_empty(&q->active)) {
1588 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1589 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -03001590 buf->vb.state = VIDEOBUF_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 wake_up(&buf->vb.done);
1592 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1593 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1594 }
1595 restart_video_queue(dev,q);
1596 spin_unlock_irqrestore(&dev->slock,flags);
1597}
1598
lawrence rust2e4e98e2010-08-25 09:50:20 -03001599static const char *cx88_vid_irqs[32] = {
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001600 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1601 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1602 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1603 "y_sync", "u_sync", "v_sync", "vbi_sync",
1604 "opc_err", "par_err", "rip_err", "pci_abort",
1605};
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607static void cx8800_vid_irq(struct cx8800_dev *dev)
1608{
1609 struct cx88_core *core = dev->core;
1610 u32 status, mask, count;
1611
1612 status = cx_read(MO_VID_INTSTAT);
1613 mask = cx_read(MO_VID_INTMSK);
1614 if (0 == (status & mask))
1615 return;
1616 cx_write(MO_VID_INTSTAT, status);
1617 if (irq_debug || (status & mask & ~0xff))
1618 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001619 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1620 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /* risc op code error */
1623 if (status & (1 << 16)) {
1624 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1625 cx_clear(MO_VID_DMACNTRL, 0x11);
1626 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001627 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629
1630 /* risc1 y */
1631 if (status & 0x01) {
1632 spin_lock(&dev->slock);
1633 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001634 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 spin_unlock(&dev->slock);
1636 }
1637
1638 /* risc1 vbi */
1639 if (status & 0x08) {
1640 spin_lock(&dev->slock);
1641 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001642 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 spin_unlock(&dev->slock);
1644 }
1645
1646 /* risc2 y */
1647 if (status & 0x10) {
1648 dprintk(2,"stopper video\n");
1649 spin_lock(&dev->slock);
1650 restart_video_queue(dev,&dev->vidq);
1651 spin_unlock(&dev->slock);
1652 }
1653
1654 /* risc2 vbi */
1655 if (status & 0x80) {
1656 dprintk(2,"stopper vbi\n");
1657 spin_lock(&dev->slock);
1658 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1659 spin_unlock(&dev->slock);
1660 }
1661}
1662
David Howells7d12e782006-10-05 14:55:46 +01001663static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 struct cx8800_dev *dev = dev_id;
1666 struct cx88_core *core = dev->core;
1667 u32 status;
1668 int loop, handled = 0;
1669
1670 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001671 status = cx_read(MO_PCI_INTSTAT) &
1672 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (0 == status)
1674 goto out;
1675 cx_write(MO_PCI_INTSTAT, status);
1676 handled = 1;
1677
1678 if (status & core->pci_irqmask)
1679 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001680 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 cx8800_vid_irq(dev);
1682 };
1683 if (10 == loop) {
1684 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1685 core->name);
1686 cx_write(MO_PCI_INTMSK,0);
1687 }
1688
1689 out:
1690 return IRQ_RETVAL(handled);
1691}
1692
1693/* ----------------------------------------------------------- */
1694/* exported stuff */
1695
Hans Verkuilbec43662008-12-30 06:58:20 -03001696static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 .owner = THIS_MODULE,
1699 .open = video_open,
1700 .release = video_release,
1701 .read = video_read,
1702 .poll = video_poll,
1703 .mmap = video_mmap,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001704 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705};
1706
Hans Verkuila3998102008-07-21 02:57:38 -03001707static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001708 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001709 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1710 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1711 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1712 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1713 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1714 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1715 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001716 .vidioc_reqbufs = vidioc_reqbufs,
1717 .vidioc_querybuf = vidioc_querybuf,
1718 .vidioc_qbuf = vidioc_qbuf,
1719 .vidioc_dqbuf = vidioc_dqbuf,
1720 .vidioc_s_std = vidioc_s_std,
1721 .vidioc_enum_input = vidioc_enum_input,
1722 .vidioc_g_input = vidioc_g_input,
1723 .vidioc_s_input = vidioc_s_input,
1724 .vidioc_queryctrl = vidioc_queryctrl,
1725 .vidioc_g_ctrl = vidioc_g_ctrl,
1726 .vidioc_s_ctrl = vidioc_s_ctrl,
1727 .vidioc_streamon = vidioc_streamon,
1728 .vidioc_streamoff = vidioc_streamoff,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001729 .vidioc_g_tuner = vidioc_g_tuner,
1730 .vidioc_s_tuner = vidioc_s_tuner,
1731 .vidioc_g_frequency = vidioc_g_frequency,
1732 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001733#ifdef CONFIG_VIDEO_ADV_DEBUG
1734 .vidioc_g_register = vidioc_g_register,
1735 .vidioc_s_register = vidioc_s_register,
1736#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001737};
1738
1739static struct video_device cx8800_vbi_template;
1740
lawrence rust2e4e98e2010-08-25 09:50:20 -03001741static const struct video_device cx8800_video_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001742 .name = "cx8800-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001743 .fops = &video_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001744 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001745 .tvnorms = CX88_NORMS,
Trent Piephodbbff482007-01-22 23:31:53 -03001746 .current_norm = V4L2_STD_NTSC_M,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747};
1748
Hans Verkuilbec43662008-12-30 06:58:20 -03001749static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 .owner = THIS_MODULE,
1752 .open = video_open,
1753 .release = video_release,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001754 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755};
1756
Hans Verkuila3998102008-07-21 02:57:38 -03001757static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001758 .vidioc_querycap = radio_querycap,
1759 .vidioc_g_tuner = radio_g_tuner,
1760 .vidioc_enum_input = radio_enum_input,
1761 .vidioc_g_audio = radio_g_audio,
1762 .vidioc_s_tuner = radio_s_tuner,
1763 .vidioc_s_audio = radio_s_audio,
1764 .vidioc_s_input = radio_s_input,
1765 .vidioc_queryctrl = radio_queryctrl,
1766 .vidioc_g_ctrl = vidioc_g_ctrl,
1767 .vidioc_s_ctrl = vidioc_s_ctrl,
1768 .vidioc_g_frequency = vidioc_g_frequency,
1769 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephoa75d2042007-08-01 00:13:28 -03001770#ifdef CONFIG_VIDEO_ADV_DEBUG
1771 .vidioc_g_register = vidioc_g_register,
1772 .vidioc_s_register = vidioc_s_register,
1773#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774};
1775
lawrence rust2e4e98e2010-08-25 09:50:20 -03001776static const struct video_device cx8800_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001777 .name = "cx8800-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001778 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001779 .ioctl_ops = &radio_ioctl_ops,
1780};
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/* ----------------------------------------------------------- */
1783
1784static void cx8800_unregister_video(struct cx8800_dev *dev)
1785{
1786 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001787 if (video_is_registered(dev->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 video_unregister_device(dev->radio_dev);
1789 else
1790 video_device_release(dev->radio_dev);
1791 dev->radio_dev = NULL;
1792 }
1793 if (dev->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001794 if (video_is_registered(dev->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 video_unregister_device(dev->vbi_dev);
1796 else
1797 video_device_release(dev->vbi_dev);
1798 dev->vbi_dev = NULL;
1799 }
1800 if (dev->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001801 if (video_is_registered(dev->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 video_unregister_device(dev->video_dev);
1803 else
1804 video_device_release(dev->video_dev);
1805 dev->video_dev = NULL;
1806 }
1807}
1808
1809static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1810 const struct pci_device_id *pci_id)
1811{
1812 struct cx8800_dev *dev;
1813 struct cx88_core *core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 int err;
1816
Panagiotis Issaris74081872006-01-11 19:40:56 -02001817 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (NULL == dev)
1819 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 /* pci init */
1822 dev->pci = pci_dev;
1823 if (pci_enable_device(pci_dev)) {
1824 err = -EIO;
1825 goto fail_free;
1826 }
1827 core = cx88_core_get(dev->pci);
1828 if (NULL == core) {
1829 err = -EINVAL;
1830 goto fail_free;
1831 }
1832 dev->core = core;
1833
1834 /* print pci info */
Bjørn Morkabd34d82011-03-21 11:35:56 -03001835 dev->pci_rev = pci_dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001836 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1837 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001838 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001840 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 pci_set_master(pci_dev);
Yang Hongyang284901a2009-04-06 19:01:15 -07001843 if (!pci_dma_supported(pci_dev,DMA_BIT_MASK(32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1845 err = -EIO;
1846 goto fail_core;
1847 }
1848
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001849 /* Initialize VBI template */
1850 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1851 sizeof(cx8800_vbi_template) );
1852 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 spin_lock_init(&dev->slock);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001856 core->tvnorm = cx8800_video_template.current_norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* init video dma queues */
1859 INIT_LIST_HEAD(&dev->vidq.active);
1860 INIT_LIST_HEAD(&dev->vidq.queued);
1861 dev->vidq.timeout.function = cx8800_vid_timeout;
1862 dev->vidq.timeout.data = (unsigned long)dev;
1863 init_timer(&dev->vidq.timeout);
1864 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1865 MO_VID_DMACNTRL,0x11,0x00);
1866
1867 /* init vbi dma queues */
1868 INIT_LIST_HEAD(&dev->vbiq.active);
1869 INIT_LIST_HEAD(&dev->vbiq.queued);
1870 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1871 dev->vbiq.timeout.data = (unsigned long)dev;
1872 init_timer(&dev->vbiq.timeout);
1873 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1874 MO_VID_DMACNTRL,0x88,0x00);
1875
1876 /* get irq */
1877 err = request_irq(pci_dev->irq, cx8800_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -07001878 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001880 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 core->name,pci_dev->irq);
1882 goto fail_core;
1883 }
1884 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1885
1886 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001887
Lawrence Rust69518032011-02-06 17:46:12 -03001888 if (core->board.audio_chip == V4L2_IDENT_WM8775) {
1889 struct i2c_board_info wm8775_info = {
1890 .type = "wm8775",
1891 .addr = 0x36 >> 1,
1892 .platform_data = &core->wm8775_data,
1893 };
1894 struct v4l2_subdev *sd;
1895
1896 if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
1897 core->wm8775_data.is_nova_s = true;
1898 else
1899 core->wm8775_data.is_nova_s = false;
1900
1901 sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
1902 &wm8775_info, NULL);
1903 if (sd != NULL)
1904 sd->grp_id = WM8775_GID;
1905 }
Hans Verkuilb8341e12009-03-29 08:26:01 -03001906
1907 if (core->board.audio_chip == V4L2_IDENT_TVAUDIO) {
1908 /* This probes for a tda9874 as is used on some
1909 Pixelview Ultra boards. */
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001910 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
1911 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
Hans Verkuilb8341e12009-03-29 08:26:01 -03001912 }
Steven Toth30579062006-09-25 12:43:42 -03001913
Michael Krufky6fcecce2007-08-24 01:32:31 -03001914 switch (core->boardnr) {
1915 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Hans Verkuilb8341e12009-03-29 08:26:01 -03001916 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
lawrence rust2e4e98e2010-08-25 09:50:20 -03001917 static const struct i2c_board_info rtc_info = {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001918 I2C_BOARD_INFO("isl1208", 0x6f)
1919 };
1920
Michael Krufky6fcecce2007-08-24 01:32:31 -03001921 request_module("rtc-isl1208");
Hans Verkuilb8341e12009-03-29 08:26:01 -03001922 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1923 }
Michael Krufky8efd2e22008-04-22 14:45:14 -03001924 /* break intentionally omitted */
1925 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1926 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001927 }
1928
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001929 /* Sets device info at pci_dev */
1930 pci_set_drvdata(pci_dev, dev);
1931
1932 /* initial device configuration */
1933 mutex_lock(&core->lock);
1934 cx88_set_tvnorm(core, core->tvnorm);
1935 init_controls(core);
1936 cx88_video_mux(core, 0);
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /* register v4l devices */
1939 dev->video_dev = cx88_vdev_init(core,dev->pci,
1940 &cx8800_video_template,"video");
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001941 video_set_drvdata(dev->video_dev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1943 video_nr[core->nr]);
1944 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001945 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 core->name);
1947 goto fail_unreg;
1948 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001949 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1950 core->name, video_device_node_name(dev->video_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001953 video_set_drvdata(dev->vbi_dev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1955 vbi_nr[core->nr]);
1956 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001957 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 core->name);
1959 goto fail_unreg;
1960 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001961 printk(KERN_INFO "%s/0: registered device %s\n",
1962 core->name, video_device_node_name(dev->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Trent Piepho6a59d642007-08-15 14:41:57 -03001964 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1966 &cx8800_radio_template,"radio");
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001967 video_set_drvdata(dev->radio_dev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1969 radio_nr[core->nr]);
1970 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001971 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 core->name);
1973 goto fail_unreg;
1974 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001975 printk(KERN_INFO "%s/0: registered device %s\n",
1976 core->name, video_device_node_name(dev->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001980 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001982 if (IS_ERR(core->kthread)) {
1983 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001984 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1985 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001986 }
1987 }
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001988 mutex_unlock(&core->lock);
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return 0;
1991
1992fail_unreg:
1993 cx8800_unregister_video(dev);
1994 free_irq(pci_dev->irq, dev);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001995 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996fail_core:
1997 cx88_core_put(core,dev->pci);
1998fail_free:
1999 kfree(dev);
2000 return err;
2001}
2002
2003static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
2004{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08002005 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002006 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002009 if (core->kthread) {
2010 kthread_stop(core->kthread);
2011 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
Marton Balintb12203d2008-03-26 02:07:35 -03002014 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03002015 cx88_ir_stop(core);
Marton Balintb12203d2008-03-26 02:07:35 -03002016
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002017 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 pci_disable_device(pci_dev);
2019
2020 /* unregister stuff */
2021
2022 free_irq(pci_dev->irq, dev);
2023 cx8800_unregister_video(dev);
2024 pci_set_drvdata(pci_dev, NULL);
2025
2026 /* free memory */
2027 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002028 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 kfree(dev);
2030}
2031
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002032#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2034{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002035 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 struct cx88_core *core = dev->core;
2037
2038 /* stop video+vbi capture */
2039 spin_lock(&dev->slock);
2040 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002041 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 stop_video_dma(dev);
2043 del_timer(&dev->vidq.timeout);
2044 }
2045 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002046 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 cx8800_stop_vbi_dma(dev);
2048 del_timer(&dev->vbiq.timeout);
2049 }
2050 spin_unlock(&dev->slock);
2051
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002052 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03002053 cx88_ir_stop(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002055 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 pci_save_state(pci_dev);
2058 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2059 pci_disable_device(pci_dev);
2060 dev->state.disabled = 1;
2061 }
2062 return 0;
2063}
2064
2065static int cx8800_resume(struct pci_dev *pci_dev)
2066{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002067 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002069 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002072 err=pci_enable_device(pci_dev);
2073 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002074 printk(KERN_ERR "%s/0: can't enable device\n",
2075 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002076 return err;
2077 }
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 dev->state.disabled = 0;
2080 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002081 err= pci_set_power_state(pci_dev, PCI_D0);
2082 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03002083 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07002084 pci_disable_device(pci_dev);
2085 dev->state.disabled = 1;
2086
2087 return err;
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 pci_restore_state(pci_dev);
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07002092 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002093 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03002094 cx88_ir_start(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03002095
2096 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 /* restart video+vbi capture */
2099 spin_lock(&dev->slock);
2100 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002101 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 restart_video_queue(dev,&dev->vidq);
2103 }
2104 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03002105 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2107 }
2108 spin_unlock(&dev->slock);
2109
2110 return 0;
2111}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114/* ----------------------------------------------------------- */
2115
lawrence rust2e4e98e2010-08-25 09:50:20 -03002116static const struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 {
2118 .vendor = 0x14f1,
2119 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002120 .subvendor = PCI_ANY_ID,
2121 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 },{
2123 /* --- end of list --- */
2124 }
2125};
2126MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2127
2128static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07002129 .name = "cx8800",
2130 .id_table = cx8800_pci_tbl,
2131 .probe = cx8800_initdev,
2132 .remove = __devexit_p(cx8800_finidev),
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002133#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 .suspend = cx8800_suspend,
2135 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03002136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137};
2138
Peter Huewe31d0f842009-07-17 01:00:01 +02002139static int __init cx8800_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002141 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %s loaded\n",
2142 CX88_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return pci_register_driver(&cx8800_pci_driver);
2144}
2145
Peter Huewe31d0f842009-07-17 01:00:01 +02002146static void __exit cx8800_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 pci_unregister_driver(&cx8800_pci_driver);
2149}
2150
2151module_init(cx8800_init);
2152module_exit(cx8800_fini);