blob: 9887da52c4550e7dd1075b8e2842cfda5821ca38 [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>
Hans Verkuil1a3c60a2012-05-11 11:25:03 -030043#include <media/v4l2-event.h>
Lawrence Rust69518032011-02-06 17:46:12 -030044#include <media/wm8775.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
47MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
48MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030049MODULE_VERSION(CX88_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* ------------------------------------------------------------------ */
52
53static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
54static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56
57module_param_array(video_nr, int, NULL, 0444);
58module_param_array(vbi_nr, int, NULL, 0444);
59module_param_array(radio_nr, int, NULL, 0444);
60
61MODULE_PARM_DESC(video_nr,"video device numbers");
62MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
63MODULE_PARM_DESC(radio_nr,"radio device numbers");
64
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030065static unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066module_param(video_debug,int,0644);
67MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
68
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030069static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070module_param(irq_debug,int,0644);
71MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070074 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* ------------------------------------------------------------------- */
77/* static data */
78
lawrence rust2e4e98e2010-08-25 09:50:20 -030079static const struct cx8800_fmt formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 {
81 .name = "8 bpp, gray",
82 .fourcc = V4L2_PIX_FMT_GREY,
83 .cxformat = ColorFormatY8,
84 .depth = 8,
85 .flags = FORMAT_FLAGS_PACKED,
86 },{
87 .name = "15 bpp RGB, le",
88 .fourcc = V4L2_PIX_FMT_RGB555,
89 .cxformat = ColorFormatRGB15,
90 .depth = 16,
91 .flags = FORMAT_FLAGS_PACKED,
92 },{
93 .name = "15 bpp RGB, be",
94 .fourcc = V4L2_PIX_FMT_RGB555X,
95 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
96 .depth = 16,
97 .flags = FORMAT_FLAGS_PACKED,
98 },{
99 .name = "16 bpp RGB, le",
100 .fourcc = V4L2_PIX_FMT_RGB565,
101 .cxformat = ColorFormatRGB16,
102 .depth = 16,
103 .flags = FORMAT_FLAGS_PACKED,
104 },{
105 .name = "16 bpp RGB, be",
106 .fourcc = V4L2_PIX_FMT_RGB565X,
107 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
108 .depth = 16,
109 .flags = FORMAT_FLAGS_PACKED,
110 },{
111 .name = "24 bpp RGB, le",
112 .fourcc = V4L2_PIX_FMT_BGR24,
113 .cxformat = ColorFormatRGB24,
114 .depth = 24,
115 .flags = FORMAT_FLAGS_PACKED,
116 },{
117 .name = "32 bpp RGB, le",
118 .fourcc = V4L2_PIX_FMT_BGR32,
119 .cxformat = ColorFormatRGB32,
120 .depth = 32,
121 .flags = FORMAT_FLAGS_PACKED,
122 },{
123 .name = "32 bpp RGB, be",
124 .fourcc = V4L2_PIX_FMT_RGB32,
125 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
126 .depth = 32,
127 .flags = FORMAT_FLAGS_PACKED,
128 },{
129 .name = "4:2:2, packed, YUYV",
130 .fourcc = V4L2_PIX_FMT_YUYV,
131 .cxformat = ColorFormatYUY2,
132 .depth = 16,
133 .flags = FORMAT_FLAGS_PACKED,
134 },{
135 .name = "4:2:2, packed, UYVY",
136 .fourcc = V4L2_PIX_FMT_UYVY,
137 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
138 .depth = 16,
139 .flags = FORMAT_FLAGS_PACKED,
140 },
141};
142
lawrence rust2e4e98e2010-08-25 09:50:20 -0300143static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 unsigned int i;
146
147 for (i = 0; i < ARRAY_SIZE(formats); i++)
148 if (formats[i].fourcc == fourcc)
149 return formats+i;
150 return NULL;
151}
152
153/* ------------------------------------------------------------------- */
154
Hans Verkuilbac63982012-06-10 07:39:52 -0300155struct cx88_ctrl {
156 /* control information */
157 u32 id;
158 s32 minimum;
159 s32 maximum;
160 u32 step;
161 s32 default_value;
162
163 /* control register information */
164 u32 off;
165 u32 reg;
166 u32 sreg;
167 u32 mask;
168 u32 shift;
169};
170
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300171static const struct cx88_ctrl cx8800_vid_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* --- video --- */
173 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300174 .id = V4L2_CID_BRIGHTNESS,
175 .minimum = 0x00,
176 .maximum = 0xff,
177 .step = 1,
178 .default_value = 0x7f,
179 .off = 128,
180 .reg = MO_CONTR_BRIGHT,
181 .mask = 0x00ff,
182 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300184 .id = V4L2_CID_CONTRAST,
185 .minimum = 0,
186 .maximum = 0xff,
187 .step = 1,
188 .default_value = 0x3f,
189 .off = 0,
190 .reg = MO_CONTR_BRIGHT,
191 .mask = 0xff00,
192 .shift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300194 .id = V4L2_CID_HUE,
195 .minimum = 0,
196 .maximum = 0xff,
197 .step = 1,
198 .default_value = 0x7f,
199 .off = 128,
200 .reg = MO_HUE,
201 .mask = 0x00ff,
202 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 },{
204 /* strictly, this only describes only U saturation.
205 * V saturation is handled specially through code.
206 */
Hans Verkuilbac63982012-06-10 07:39:52 -0300207 .id = V4L2_CID_SATURATION,
208 .minimum = 0,
209 .maximum = 0xff,
210 .step = 1,
211 .default_value = 0x7f,
212 .off = 0,
213 .reg = MO_UV_SATURATION,
214 .mask = 0x00ff,
215 .shift = 0,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300216 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300217 .id = V4L2_CID_SHARPNESS,
218 .minimum = 0,
219 .maximum = 4,
220 .step = 1,
221 .default_value = 0x0,
222 .off = 0,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300223 /* NOTE: the value is converted and written to both even
224 and odd registers in the code */
Hans Verkuilbac63982012-06-10 07:39:52 -0300225 .reg = MO_FILTER_ODD,
226 .mask = 7 << 7,
227 .shift = 7,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300228 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300229 .id = V4L2_CID_CHROMA_AGC,
230 .minimum = 0,
231 .maximum = 1,
232 .default_value = 0x1,
233 .reg = MO_INPUT_FORMAT,
234 .mask = 1 << 10,
235 .shift = 10,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300236 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300237 .id = V4L2_CID_COLOR_KILLER,
238 .minimum = 0,
239 .maximum = 1,
240 .default_value = 0x1,
241 .reg = MO_INPUT_FORMAT,
242 .mask = 1 << 9,
243 .shift = 9,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300244 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300245 .id = V4L2_CID_BAND_STOP_FILTER,
246 .minimum = 0,
247 .maximum = 1,
248 .step = 1,
249 .default_value = 0x0,
250 .off = 0,
251 .reg = MO_HTOTAL,
252 .mask = 3 << 11,
253 .shift = 11,
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300254 }
255};
256
257static const struct cx88_ctrl cx8800_aud_ctls[] = {
258 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300259 /* --- audio --- */
260 .id = V4L2_CID_AUDIO_MUTE,
261 .minimum = 0,
262 .maximum = 1,
263 .default_value = 1,
264 .reg = AUD_VOL_CTL,
265 .sreg = SHADOW_AUD_VOL_CTL,
266 .mask = (1 << 6),
267 .shift = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300269 .id = V4L2_CID_AUDIO_VOLUME,
270 .minimum = 0,
271 .maximum = 0x3f,
272 .step = 1,
273 .default_value = 0x3f,
274 .reg = AUD_VOL_CTL,
275 .sreg = SHADOW_AUD_VOL_CTL,
276 .mask = 0x3f,
277 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300279 .id = V4L2_CID_AUDIO_BALANCE,
280 .minimum = 0,
281 .maximum = 0x7f,
282 .step = 1,
283 .default_value = 0x40,
284 .reg = AUD_BAL_CTL,
285 .sreg = SHADOW_AUD_BAL_CTL,
286 .mask = 0x7f,
287 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289};
Hans Verkuilbac63982012-06-10 07:39:52 -0300290
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300291enum {
292 CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
293 CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
294};
Michael Krufky38a27132006-06-26 23:42:39 -0300295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/* ------------------------------------------------------------------ */
297
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300298int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700300 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300303 input, INPUT(input).vmux,
304 INPUT(input).gpio0,INPUT(input).gpio1,
305 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700306 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300307 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
308 cx_write(MO_GP3_IO, INPUT(input).gpio3);
309 cx_write(MO_GP0_IO, INPUT(input).gpio0);
310 cx_write(MO_GP1_IO, INPUT(input).gpio1);
311 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Trent Piepho6a59d642007-08-15 14:41:57 -0300313 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case CX88_VMUX_SVIDEO:
315 cx_set(MO_AFECFG_IO, 0x00000001);
316 cx_set(MO_INPUT_FORMAT, 0x00010010);
317 cx_set(MO_FILTER_EVEN, 0x00002020);
318 cx_set(MO_FILTER_ODD, 0x00002020);
319 break;
320 default:
321 cx_clear(MO_AFECFG_IO, 0x00000001);
322 cx_clear(MO_INPUT_FORMAT, 0x00010010);
323 cx_clear(MO_FILTER_EVEN, 0x00002020);
324 cx_clear(MO_FILTER_ODD, 0x00002020);
325 break;
326 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300327
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300328 /* if there are audioroutes defined, we have an external
329 ADC to deal with audio */
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300330 if (INPUT(input).audioroute) {
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300331 /* The wm8775 module has the "2" route hardwired into
332 the initialization. Some boards may use different
333 routes for different inputs. HVR-1300 surely does */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300334 if (core->sd_wm8775) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300335 call_all(core, audio, s_routing,
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300336 INPUT(input).audioroute, 0, 0);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300337 }
Darron Broad430189d2008-10-15 14:18:42 -0300338 /* cx2388's C-ADC is connected to the tuner only.
339 When used with S-Video, that ADC is busy dealing with
340 chroma, so an external must be used for baseband audio */
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300341 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
342 INPUT(input).type != CX88_VMUX_CABLE) {
Darron Broad430189d2008-10-15 14:18:42 -0300343 /* "I2S ADC mode" */
344 core->tvaudio = WW_I2SADC;
345 cx88_set_tvaudio(core);
346 } else {
347 /* Normal mode */
348 cx_write(AUD_I2SCNTL, 0x0);
349 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
350 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300351 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300355EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357/* ------------------------------------------------------------------ */
358
359static int start_video_dma(struct cx8800_dev *dev,
360 struct cx88_dmaqueue *q,
361 struct cx88_buffer *buf)
362{
363 struct cx88_core *core = dev->core;
364
365 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700366 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 buf->bpl, buf->risc.dma);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300368 cx88_set_scale(core, dev->width, dev->height, dev->field);
Hans Verkuil637bc202014-08-29 03:46:05 -0300369 cx_write(MO_COLOR_CTRL, dev->fmt->cxformat | ColorFormatGamma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* reset counter */
372 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
373 q->count = 1;
374
375 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300376 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700377
378 /* Enables corresponding bits at PCI_INT_STAT:
379 bits 0 to 4: video, audio, transport stream, VIP, Host
380 bit 7: timer
381 bits 8 and 9: DMA complete for: SRC, DST
382 bits 10 and 11: BERR signal asserted for RISC: RD, WR
383 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 cx_set(MO_VID_INTMSK, 0x0f0011);
386
387 /* enable capture */
388 cx_set(VID_CAPTURE_CONTROL,0x06);
389
390 /* start dma */
391 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700392 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 return 0;
395}
396
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300397#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static int stop_video_dma(struct cx8800_dev *dev)
399{
400 struct cx88_core *core = dev->core;
401
402 /* stop dma */
403 cx_clear(MO_VID_DMACNTRL, 0x11);
404
405 /* disable capture */
406 cx_clear(VID_CAPTURE_CONTROL,0x06);
407
408 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300409 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 cx_clear(MO_VID_INTMSK, 0x0f0011);
411 return 0;
412}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300413#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415static int restart_video_queue(struct cx8800_dev *dev,
416 struct cx88_dmaqueue *q)
417{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700418 struct cx88_core *core = dev->core;
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300419 struct cx88_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (!list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300422 buf = list_entry(q->active.next, struct cx88_buffer, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300424 buf, buf->vb.v4l2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 start_video_dma(dev, q, buf);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300426 list_for_each_entry(buf, &q->active, list)
Trent Piepho8bb629e22007-10-10 05:37:40 -0300427 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/* ------------------------------------------------------------------ */
433
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300434static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
435 unsigned int *num_buffers, unsigned int *num_planes,
436 unsigned int sizes[], void *alloc_ctxs[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300438 struct cx8800_dev *dev = q->drv_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300440 *num_planes = 1;
441 sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
443}
444
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300445static int buffer_prepare(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300447 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700448 struct cx88_core *core = dev->core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300449 struct cx88_buffer *buf = container_of(vb, struct cx88_buffer, vb);
450 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
Hans Verkuil637bc202014-08-29 03:46:05 -0300451 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300453 buf->bpl = dev->width * dev->fmt->depth >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300455 if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
456 return -EINVAL;
457 vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300459 rc = dma_map_sg(&dev->pci->dev, sgt->sgl, sgt->nents, DMA_FROM_DEVICE);
460 if (!rc)
461 return -EIO;
462
463 switch (dev->field) {
Hans Verkuil637bc202014-08-29 03:46:05 -0300464 case V4L2_FIELD_TOP:
465 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300466 sgt->sgl, 0, UNSET,
467 buf->bpl, 0, dev->height);
Hans Verkuil637bc202014-08-29 03:46:05 -0300468 break;
469 case V4L2_FIELD_BOTTOM:
470 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300471 sgt->sgl, UNSET, 0,
472 buf->bpl, 0, dev->height);
Hans Verkuil637bc202014-08-29 03:46:05 -0300473 break;
474 case V4L2_FIELD_SEQ_TB:
475 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300476 sgt->sgl,
477 0, buf->bpl * (dev->height >> 1),
Hans Verkuil637bc202014-08-29 03:46:05 -0300478 buf->bpl, 0,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300479 dev->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300480 break;
481 case V4L2_FIELD_SEQ_BT:
482 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300483 sgt->sgl,
484 buf->bpl * (dev->height >> 1), 0,
Hans Verkuil637bc202014-08-29 03:46:05 -0300485 buf->bpl, 0,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300486 dev->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300487 break;
488 case V4L2_FIELD_INTERLACED:
489 default:
490 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300491 sgt->sgl, 0, buf->bpl,
Hans Verkuil637bc202014-08-29 03:46:05 -0300492 buf->bpl, buf->bpl,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300493 dev->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300497 buf, buf->vb.v4l2_buf.index,
Hans Verkuilc5a86142012-05-11 10:45:18 -0300498 dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 (unsigned long)buf->risc.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300503static void buffer_finish(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300505 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
506 struct cx88_buffer *buf = container_of(vb, struct cx88_buffer, vb);
507 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
508
509 cx88_free_buffer(vb->vb2_queue, buf);
510
511 dma_unmap_sg(&dev->pci->dev, sgt->sgl, sgt->nents, DMA_FROM_DEVICE);
512}
513
514static void buffer_queue(struct vb2_buffer *vb)
515{
516 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
517 struct cx88_buffer *buf = container_of(vb, struct cx88_buffer, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct cx88_buffer *prev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700519 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct cx88_dmaqueue *q = &dev->vidq;
521
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300522 /* add jump to start */
523 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
524 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
525 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300527 if (list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300528 list_add_tail(&buf->list, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 start_video_dma(dev, q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 buf->count = q->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 dprintk(2,"[%p/%d] buffer_queue - first active\n",
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300532 buf, buf->vb.v4l2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300535 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
536 prev = list_entry(q->active.prev, struct cx88_buffer, list);
537 list_add_tail(&buf->list, &q->active);
Hans Verkuil637bc202014-08-29 03:46:05 -0300538 buf->count = q->count++;
539 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
540 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300541 buf, buf->vb.v4l2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543}
544
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300545static int start_streaming(struct vb2_queue *q, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300547 struct cx8800_dev *dev = q->drv_priv;
548 struct cx88_dmaqueue *dmaq = &dev->vidq;
549 struct cx88_buffer *buf = list_entry(dmaq->active.next,
550 struct cx88_buffer, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300552 start_video_dma(dev, dmaq, buf);
553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300556static void stop_streaming(struct vb2_queue *q)
557{
558 struct cx8800_dev *dev = q->drv_priv;
559 struct cx88_core *core = dev->core;
560 struct cx88_dmaqueue *dmaq = &dev->vidq;
561 unsigned long flags;
562
563 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
564
565 cx_clear(MO_VID_DMACNTRL, 0x11);
566 cx_clear(VID_CAPTURE_CONTROL, 0x06);
567 spin_lock_irqsave(&dev->slock, flags);
568 while (!list_empty(&dmaq->active)) {
569 struct cx88_buffer *buf = list_entry(dmaq->active.next,
570 struct cx88_buffer, list);
571
572 list_del(&buf->list);
573 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
574 }
575 spin_unlock_irqrestore(&dev->slock, flags);
576}
577
578static struct vb2_ops cx8800_video_qops = {
579 .queue_setup = queue_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 .buf_prepare = buffer_prepare,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300581 .buf_finish = buffer_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .buf_queue = buffer_queue,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300583 .wait_prepare = vb2_ops_wait_prepare,
584 .wait_finish = vb2_ops_wait_finish,
585 .start_streaming = start_streaming,
586 .stop_streaming = stop_streaming,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587};
588
589/* ------------------------------------------------------------------ */
590
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300591static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200593 struct cx8800_dev *dev = video_drvdata(file);
Dan Carpenter5401c2d2010-10-21 16:21:45 -0300594 struct cx88_core *core = dev->core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300595 int ret = v4l2_fh_open(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300597 if (ret)
598 return ret;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200599
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300600 cx_write(MO_GP3_IO, core->board.radio.gpio3);
601 cx_write(MO_GP0_IO, core->board.radio.gpio0);
602 cx_write(MO_GP1_IO, core->board.radio.gpio1);
603 cx_write(MO_GP2_IO, core->board.radio.gpio2);
604 if (core->board.radio.audioroute) {
605 if (core->sd_wm8775) {
606 call_all(core, audio, s_routing,
Hans Verkuil5325b422009-04-02 11:26:22 -0300607 core->board.radio.audioroute, 0, 0);
Darron Broad430189d2008-10-15 14:18:42 -0300608 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300609 /* "I2S ADC mode" */
610 core->tvaudio = WW_I2SADC;
611 cx88_set_tvaudio(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300613 /* FM Mode */
614 core->tvaudio = WW_FM;
615 cx88_set_tvaudio(core);
616 cx88_set_stereo(core, V4L2_TUNER_MODE_STEREO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300618 call_all(core, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300623/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300625static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Hans Verkuilbac63982012-06-10 07:39:52 -0300627 struct cx88_core *core =
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300628 container_of(ctrl->handler, struct cx88_core, video_hdl);
629 const struct cx88_ctrl *cc = ctrl->priv;
630 u32 value, mask;
631
632 mask = cc->mask;
633 switch (ctrl->id) {
634 case V4L2_CID_SATURATION:
635 /* special v_sat handling */
636
637 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
638
639 if (core->tvnorm & V4L2_STD_SECAM) {
640 /* For SECAM, both U and V sat should be equal */
641 value = value << 8 | value;
642 } else {
643 /* Keeps U Saturation proportional to V Sat */
644 value = (value * 0x5a) / 0x7f << 8 | value;
645 }
646 mask = 0xffff;
647 break;
648 case V4L2_CID_SHARPNESS:
649 /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
650 value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
651 /* needs to be set for both fields */
652 cx_andor(MO_FILTER_EVEN, mask, value);
653 break;
654 case V4L2_CID_CHROMA_AGC:
655 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
656 break;
657 default:
658 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
659 break;
660 }
661 dprintk(1, "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
662 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
663 mask, cc->sreg ? " [shadowed]" : "");
664 if (cc->sreg)
665 cx_sandor(cc->sreg, cc->reg, mask, value);
666 else
667 cx_andor(cc->reg, mask, value);
668 return 0;
669}
670
671static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
672{
673 struct cx88_core *core =
674 container_of(ctrl->handler, struct cx88_core, audio_hdl);
Hans Verkuilbac63982012-06-10 07:39:52 -0300675 const struct cx88_ctrl *cc = ctrl->priv;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200676 u32 value,mask;
Lawrence Rust69518032011-02-06 17:46:12 -0300677
678 /* Pass changes onto any WM8775 */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300679 if (core->sd_wm8775) {
Hans Verkuilbac63982012-06-10 07:39:52 -0300680 switch (ctrl->id) {
Lawrence Rust69518032011-02-06 17:46:12 -0300681 case V4L2_CID_AUDIO_MUTE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300682 wm8775_s_ctrl(core, ctrl->id, ctrl->val);
Lawrence Rust69518032011-02-06 17:46:12 -0300683 break;
684 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300685 wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
686 (0x90 + ctrl->val) << 8 : 0);
Lawrence Rust69518032011-02-06 17:46:12 -0300687 break;
688 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300689 wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
Lawrence Rust69518032011-02-06 17:46:12 -0300690 break;
691 default:
Lawrence Rust69518032011-02-06 17:46:12 -0300692 break;
693 }
Lawrence Rust69518032011-02-06 17:46:12 -0300694 }
695
Hans Verkuilbac63982012-06-10 07:39:52 -0300696 mask = cc->mask;
697 switch (ctrl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300699 value = (ctrl->val < 0x40) ? (0x7f - ctrl->val) : (ctrl->val - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
701 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300702 value = 0x3f - (ctrl->val & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 default:
Hans Verkuilbac63982012-06-10 07:39:52 -0300705 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300708 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
Hans Verkuilbac63982012-06-10 07:39:52 -0300709 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
710 mask, cc->sreg ? " [shadowed]" : "");
711 if (cc->sreg)
712 cx_sandor(cc->sreg, cc->reg, mask, value);
713 else
714 cx_andor(cc->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return 0;
716}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300719/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Hans Verkuil78b526a2008-05-28 12:16:41 -0300721static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300722 struct v4l2_format *f)
723{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300724 struct cx8800_dev *dev = video_drvdata(file);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300725
Hans Verkuilc5a86142012-05-11 10:45:18 -0300726 f->fmt.pix.width = dev->width;
727 f->fmt.pix.height = dev->height;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300728 f->fmt.pix.field = dev->field;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300729 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300730 f->fmt.pix.bytesperline =
Hans Verkuilc5a86142012-05-11 10:45:18 -0300731 (f->fmt.pix.width * dev->fmt->depth) >> 3;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300732 f->fmt.pix.sizeimage =
733 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300734 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300735 return 0;
736}
737
Hans Verkuil78b526a2008-05-28 12:16:41 -0300738static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct v4l2_format *f)
740{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300741 struct cx8800_dev *dev = video_drvdata(file);
742 struct cx88_core *core = dev->core;
lawrence rust2e4e98e2010-08-25 09:50:20 -0300743 const struct cx8800_fmt *fmt;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300744 enum v4l2_field field;
745 unsigned int maxw, maxh;
746
747 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
748 if (NULL == fmt)
749 return -EINVAL;
750
751 field = f->fmt.pix.field;
752 maxw = norm_maxw(core->tvnorm);
753 maxh = norm_maxh(core->tvnorm);
754
755 if (V4L2_FIELD_ANY == field) {
756 field = (f->fmt.pix.height > maxh/2)
757 ? V4L2_FIELD_INTERLACED
758 : V4L2_FIELD_BOTTOM;
759 }
760
761 switch (field) {
762 case V4L2_FIELD_TOP:
763 case V4L2_FIELD_BOTTOM:
764 maxh = maxh / 2;
765 break;
766 case V4L2_FIELD_INTERLACED:
767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 default:
769 return -EINVAL;
770 }
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300771
772 f->fmt.pix.field = field;
Trent Piepho4b899452009-05-30 21:45:46 -0300773 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
774 &f->fmt.pix.height, 32, maxh, 0, 0);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300775 f->fmt.pix.bytesperline =
776 (f->fmt.pix.width * fmt->depth) >> 3;
777 f->fmt.pix.sizeimage =
778 f->fmt.pix.height * f->fmt.pix.bytesperline;
779
780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Hans Verkuil78b526a2008-05-28 12:16:41 -0300783static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300784 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300786 struct cx8800_dev *dev = video_drvdata(file);
Hans Verkuil78b526a2008-05-28 12:16:41 -0300787 int err = vidioc_try_fmt_vid_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700788
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300789 if (0 != err)
790 return err;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300791 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
792 dev->width = f->fmt.pix.width;
793 dev->height = f->fmt.pix.height;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300794 dev->field = f->fmt.pix.field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Hans Verkuil902e1972012-05-09 16:23:07 -0300798void cx88_querycap(struct file *file, struct cx88_core *core,
799 struct v4l2_capability *cap)
800{
801 struct video_device *vdev = video_devdata(file);
802
803 strlcpy(cap->card, core->board.name, sizeof(cap->card));
804 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
805 if (UNSET != core->board.tuner_type)
806 cap->device_caps |= V4L2_CAP_TUNER;
807 switch (vdev->vfl_type) {
808 case VFL_TYPE_RADIO:
809 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
810 break;
811 case VFL_TYPE_GRABBER:
812 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
813 break;
814 case VFL_TYPE_VBI:
815 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
816 break;
817 }
818 cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
819 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
820 if (core->board.radio.type == CX88_RADIO)
821 cap->capabilities |= V4L2_CAP_RADIO;
822}
823EXPORT_SYMBOL(cx88_querycap);
824
825static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300826 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300828 struct cx8800_dev *dev = video_drvdata(file);
829 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300831 strcpy(cap->driver, "cx8800");
Hans Verkuil902e1972012-05-09 16:23:07 -0300832 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
833 cx88_querycap(file, core, cap);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300834 return 0;
835}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700836
Hans Verkuil78b526a2008-05-28 12:16:41 -0300837static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300838 struct v4l2_fmtdesc *f)
839{
840 if (unlikely(f->index >= ARRAY_SIZE(formats)))
841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300843 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
844 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300846 return 0;
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Hans Verkuil48d68802012-05-25 12:15:30 -0300849static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
850{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300851 struct cx8800_dev *dev = video_drvdata(file);
852 struct cx88_core *core = dev->core;
Hans Verkuil48d68802012-05-25 12:15:30 -0300853
854 *tvnorm = core->tvnorm;
855 return 0;
856}
857
Hans Verkuil314527a2013-03-15 06:10:40 -0300858static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300859{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300860 struct cx8800_dev *dev = video_drvdata(file);
861 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300862
Hans Verkuil314527a2013-03-15 06:10:40 -0300863 cx88_set_tvnorm(core, tvnorms);
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -0300864
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300865 return 0;
866}
867
868/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300869int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300870{
lawrence rust2e4e98e2010-08-25 09:50:20 -0300871 static const char * const iname[] = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300872 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
873 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
874 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
875 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
876 [ CX88_VMUX_SVIDEO ] = "S-Video",
877 [ CX88_VMUX_TELEVISION ] = "Television",
878 [ CX88_VMUX_CABLE ] = "Cable TV",
879 [ CX88_VMUX_DVB ] = "DVB",
880 [ CX88_VMUX_DEBUG ] = "for debug only",
881 };
Trent Piephof3334bc2009-03-04 01:21:03 -0300882 unsigned int n = i->index;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300883
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300884 if (n >= 4)
885 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -0300886 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300887 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300888 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -0300889 strcpy(i->name,iname[INPUT(n).type]);
890 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
Julia Lawall473d8022010-08-05 17:22:44 -0300891 (CX88_VMUX_CABLE == INPUT(n).type)) {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300892 i->type = V4L2_INPUT_TYPE_TUNER;
Julia Lawall473d8022010-08-05 17:22:44 -0300893 }
Hans Verkuilf33e9862012-05-25 12:04:10 -0300894 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300895 return 0;
896}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300897EXPORT_SYMBOL(cx88_enum_input);
898
899static int vidioc_enum_input (struct file *file, void *priv,
900 struct v4l2_input *i)
901{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300902 struct cx8800_dev *dev = video_drvdata(file);
903 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300904 return cx88_enum_input (core,i);
905}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300906
907static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
908{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300909 struct cx8800_dev *dev = video_drvdata(file);
910 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300911
912 *i = core->input;
913 return 0;
914}
915
916static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
917{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300918 struct cx8800_dev *dev = video_drvdata(file);
919 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300920
921 if (i >= 4)
922 return -EINVAL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300923 if (0 == INPUT(i).type)
924 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300925
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300926 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300927 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300928 return 0;
929}
930
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300931static int vidioc_g_tuner (struct file *file, void *priv,
932 struct v4l2_tuner *t)
933{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300934 struct cx8800_dev *dev = video_drvdata(file);
935 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300936 u32 reg;
937
Trent Piepho6a59d642007-08-15 14:41:57 -0300938 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300939 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -0300940 if (0 != t->index)
941 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300942
943 strcpy(t->name, "Television");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300944 t->capability = V4L2_TUNER_CAP_NORM;
945 t->rangehigh = 0xffffffffUL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300946 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300947
948 cx88_get_stereo(core ,t);
949 reg = cx_read(MO_DEVICE_STATUS);
950 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
951 return 0;
952}
953
954static int vidioc_s_tuner (struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300955 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300956{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300957 struct cx8800_dev *dev = video_drvdata(file);
958 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300959
Trent Piepho6a59d642007-08-15 14:41:57 -0300960 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300961 return -EINVAL;
962 if (0 != t->index)
963 return -EINVAL;
964
965 cx88_set_stereo(core, t->audmode, 1);
966 return 0;
967}
968
969static int vidioc_g_frequency (struct file *file, void *priv,
970 struct v4l2_frequency *f)
971{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300972 struct cx8800_dev *dev = video_drvdata(file);
973 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300974
Trent Piepho6a59d642007-08-15 14:41:57 -0300975 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300976 return -EINVAL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300977 if (f->tuner)
978 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300979
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300980 f->frequency = core->freq;
981
Hans Verkuilb8341e12009-03-29 08:26:01 -0300982 call_all(core, tuner, g_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300983
984 return 0;
985}
986
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300987int cx88_set_freq (struct cx88_core *core,
Hans Verkuilb530a442013-03-19 04:09:26 -0300988 const struct v4l2_frequency *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300989{
Hans Verkuilb530a442013-03-19 04:09:26 -0300990 struct v4l2_frequency new_freq = *f;
991
Trent Piepho6a59d642007-08-15 14:41:57 -0300992 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300993 return -EINVAL;
994 if (unlikely(f->tuner != 0))
995 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300996
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300997 cx88_newstation(core);
Hans Verkuilb8341e12009-03-29 08:26:01 -0300998 call_all(core, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -0300999 call_all(core, tuner, g_frequency, &new_freq);
1000 core->freq = new_freq.frequency;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001001
1002 /* When changing channels it is required to reset TVAUDIO */
1003 msleep (10);
1004 cx88_set_tvaudio(core);
1005
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001006 return 0;
1007}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001008EXPORT_SYMBOL(cx88_set_freq);
1009
1010static int vidioc_s_frequency (struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001011 const struct v4l2_frequency *f)
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001012{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001013 struct cx8800_dev *dev = video_drvdata(file);
1014 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001015
Hans Verkuiledbd1382012-05-11 10:33:25 -03001016 return cx88_set_freq(core, f);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001017}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001018
Trent Piephodbbff482007-01-22 23:31:53 -03001019#ifdef CONFIG_VIDEO_ADV_DEBUG
1020static int vidioc_g_register (struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001021 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001022{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001023 struct cx8800_dev *dev = video_drvdata(file);
1024 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001025
Trent Piephodbbff482007-01-22 23:31:53 -03001026 /* cx2388x has a 24-bit register space */
Hans Verkuil7feeb142013-05-29 06:22:02 -03001027 reg->val = cx_read(reg->reg & 0xfffffc);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001028 reg->size = 4;
Trent Piephodbbff482007-01-22 23:31:53 -03001029 return 0;
1030}
1031
1032static int vidioc_s_register (struct file *file, void *fh,
Hans Verkuil977ba3b12013-03-24 08:28:46 -03001033 const struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001034{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001035 struct cx8800_dev *dev = video_drvdata(file);
1036 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001037
Hans Verkuil7feeb142013-05-29 06:22:02 -03001038 cx_write(reg->reg & 0xfffffc, reg->val);
Trent Piephodbbff482007-01-22 23:31:53 -03001039 return 0;
1040}
1041#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001044/* RADIO ESPECIFIC IOCTLS */
1045/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001047static int radio_g_tuner (struct file *file, void *priv,
1048 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001050 struct cx8800_dev *dev = video_drvdata(file);
1051 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001052
1053 if (unlikely(t->index > 0))
1054 return -EINVAL;
1055
1056 strcpy(t->name, "Radio");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001057
Hans Verkuilb8341e12009-03-29 08:26:01 -03001058 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001059 return 0;
1060}
1061
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001062static int radio_s_tuner (struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001063 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001064{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001065 struct cx8800_dev *dev = video_drvdata(file);
1066 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001067
1068 if (0 != t->index)
1069 return -EINVAL;
1070
Hans Verkuilb8341e12009-03-29 08:26:01 -03001071 call_all(core, tuner, s_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001072 return 0;
1073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/* ----------------------------------------------------------- */
1076
lawrence rust2e4e98e2010-08-25 09:50:20 -03001077static const char *cx88_vid_irqs[32] = {
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001078 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1079 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1080 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1081 "y_sync", "u_sync", "v_sync", "vbi_sync",
1082 "opc_err", "par_err", "rip_err", "pci_abort",
1083};
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085static void cx8800_vid_irq(struct cx8800_dev *dev)
1086{
1087 struct cx88_core *core = dev->core;
1088 u32 status, mask, count;
1089
1090 status = cx_read(MO_VID_INTSTAT);
1091 mask = cx_read(MO_VID_INTMSK);
1092 if (0 == (status & mask))
1093 return;
1094 cx_write(MO_VID_INTSTAT, status);
1095 if (irq_debug || (status & mask & ~0xff))
1096 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001097 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1098 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* risc op code error */
1101 if (status & (1 << 16)) {
1102 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1103 cx_clear(MO_VID_DMACNTRL, 0x11);
1104 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001105 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
1108 /* risc1 y */
1109 if (status & 0x01) {
1110 spin_lock(&dev->slock);
1111 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001112 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 spin_unlock(&dev->slock);
1114 }
1115
1116 /* risc1 vbi */
1117 if (status & 0x08) {
1118 spin_lock(&dev->slock);
1119 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001120 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 spin_unlock(&dev->slock);
1122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
David Howells7d12e782006-10-05 14:55:46 +01001125static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct cx8800_dev *dev = dev_id;
1128 struct cx88_core *core = dev->core;
1129 u32 status;
1130 int loop, handled = 0;
1131
1132 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001133 status = cx_read(MO_PCI_INTSTAT) &
1134 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (0 == status)
1136 goto out;
1137 cx_write(MO_PCI_INTSTAT, status);
1138 handled = 1;
1139
1140 if (status & core->pci_irqmask)
1141 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001142 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 cx8800_vid_irq(dev);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03001144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (10 == loop) {
1146 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1147 core->name);
1148 cx_write(MO_PCI_INTMSK,0);
1149 }
1150
1151 out:
1152 return IRQ_RETVAL(handled);
1153}
1154
1155/* ----------------------------------------------------------- */
1156/* exported stuff */
1157
Hans Verkuilbec43662008-12-30 06:58:20 -03001158static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001161 .open = v4l2_fh_open,
1162 .release = vb2_fop_release,
1163 .read = vb2_fop_read,
1164 .poll = vb2_fop_poll,
1165 .mmap = vb2_fop_mmap,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001166 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167};
1168
Hans Verkuila3998102008-07-21 02:57:38 -03001169static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001170 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001171 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1172 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1173 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1174 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001175 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1176 .vidioc_querybuf = vb2_ioctl_querybuf,
1177 .vidioc_qbuf = vb2_ioctl_qbuf,
1178 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001179 .vidioc_g_std = vidioc_g_std,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001180 .vidioc_s_std = vidioc_s_std,
1181 .vidioc_enum_input = vidioc_enum_input,
1182 .vidioc_g_input = vidioc_g_input,
1183 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001184 .vidioc_streamon = vb2_ioctl_streamon,
1185 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001186 .vidioc_g_tuner = vidioc_g_tuner,
1187 .vidioc_s_tuner = vidioc_s_tuner,
1188 .vidioc_g_frequency = vidioc_g_frequency,
1189 .vidioc_s_frequency = vidioc_s_frequency,
1190 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1191 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001192#ifdef CONFIG_VIDEO_ADV_DEBUG
1193 .vidioc_g_register = vidioc_g_register,
1194 .vidioc_s_register = vidioc_s_register,
1195#endif
1196};
1197
1198static const struct video_device cx8800_video_template = {
1199 .name = "cx8800-video",
1200 .fops = &video_fops,
1201 .ioctl_ops = &video_ioctl_ops,
1202 .tvnorms = CX88_NORMS,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001203};
1204
1205static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1206 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001207 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1208 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1209 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001210 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1211 .vidioc_querybuf = vb2_ioctl_querybuf,
1212 .vidioc_qbuf = vb2_ioctl_qbuf,
1213 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001214 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001215 .vidioc_s_std = vidioc_s_std,
1216 .vidioc_enum_input = vidioc_enum_input,
1217 .vidioc_g_input = vidioc_g_input,
1218 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001219 .vidioc_streamon = vb2_ioctl_streamon,
1220 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001221 .vidioc_g_tuner = vidioc_g_tuner,
1222 .vidioc_s_tuner = vidioc_s_tuner,
1223 .vidioc_g_frequency = vidioc_g_frequency,
1224 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001225#ifdef CONFIG_VIDEO_ADV_DEBUG
1226 .vidioc_g_register = vidioc_g_register,
1227 .vidioc_s_register = vidioc_s_register,
1228#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001229};
1230
Hans Verkuilf33e9862012-05-25 12:04:10 -03001231static const struct video_device cx8800_vbi_template = {
1232 .name = "cx8800-vbi",
Hans Verkuila3998102008-07-21 02:57:38 -03001233 .fops = &video_fops,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001234 .ioctl_ops = &vbi_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001235 .tvnorms = CX88_NORMS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236};
1237
Hans Verkuilbec43662008-12-30 06:58:20 -03001238static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001241 .open = radio_open,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001242 .poll = v4l2_ctrl_poll,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001243 .release = v4l2_fh_release,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001244 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245};
1246
Hans Verkuila3998102008-07-21 02:57:38 -03001247static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil902e1972012-05-09 16:23:07 -03001248 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001249 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001250 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001251 .vidioc_g_frequency = vidioc_g_frequency,
1252 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001253 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1254 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Trent Piephoa75d2042007-08-01 00:13:28 -03001255#ifdef CONFIG_VIDEO_ADV_DEBUG
1256 .vidioc_g_register = vidioc_g_register,
1257 .vidioc_s_register = vidioc_s_register,
1258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259};
1260
lawrence rust2e4e98e2010-08-25 09:50:20 -03001261static const struct video_device cx8800_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001262 .name = "cx8800-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001263 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001264 .ioctl_ops = &radio_ioctl_ops,
1265};
1266
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001267static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
1268 .s_ctrl = cx8800_s_vid_ctrl,
1269};
1270
1271static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
1272 .s_ctrl = cx8800_s_aud_ctrl,
Hans Verkuilbac63982012-06-10 07:39:52 -03001273};
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275/* ----------------------------------------------------------- */
1276
1277static void cx8800_unregister_video(struct cx8800_dev *dev)
1278{
1279 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001280 if (video_is_registered(dev->radio_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 video_unregister_device(dev->radio_dev);
1282 else
1283 video_device_release(dev->radio_dev);
1284 dev->radio_dev = NULL;
1285 }
1286 if (dev->vbi_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001287 if (video_is_registered(dev->vbi_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 video_unregister_device(dev->vbi_dev);
1289 else
1290 video_device_release(dev->vbi_dev);
1291 dev->vbi_dev = NULL;
1292 }
1293 if (dev->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001294 if (video_is_registered(dev->video_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 video_unregister_device(dev->video_dev);
1296 else
1297 video_device_release(dev->video_dev);
1298 dev->video_dev = NULL;
1299 }
1300}
1301
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001302static int cx8800_initdev(struct pci_dev *pci_dev,
1303 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct cx8800_dev *dev;
1306 struct cx88_core *core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001307 struct vb2_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int err;
Hans Verkuilbac63982012-06-10 07:39:52 -03001309 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Panagiotis Issaris74081872006-01-11 19:40:56 -02001311 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (NULL == dev)
1313 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* pci init */
1316 dev->pci = pci_dev;
1317 if (pci_enable_device(pci_dev)) {
1318 err = -EIO;
1319 goto fail_free;
1320 }
1321 core = cx88_core_get(dev->pci);
1322 if (NULL == core) {
1323 err = -EINVAL;
1324 goto fail_free;
1325 }
1326 dev->core = core;
1327
1328 /* print pci info */
Bjørn Morkabd34d82011-03-21 11:35:56 -03001329 dev->pci_rev = pci_dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001330 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1331 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001332 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001334 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 pci_set_master(pci_dev);
Yang Hongyang284901a2009-04-06 19:01:15 -07001337 if (!pci_dma_supported(pci_dev,DMA_BIT_MASK(32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1339 err = -EIO;
1340 goto fail_core;
1341 }
1342
1343 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 spin_lock_init(&dev->slock);
Hans Verkuil48d68802012-05-25 12:15:30 -03001345 core->tvnorm = V4L2_STD_NTSC_M;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /* init video dma queues */
1348 INIT_LIST_HEAD(&dev->vidq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 /* init vbi dma queues */
1351 INIT_LIST_HEAD(&dev->vbiq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* get irq */
1354 err = request_irq(pci_dev->irq, cx8800_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -03001355 IRQF_SHARED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001357 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 core->name,pci_dev->irq);
1359 goto fail_core;
1360 }
1361 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1362
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001363 for (i = 0; i < CX8800_AUD_CTLS; i++) {
1364 const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
Hans Verkuilbac63982012-06-10 07:39:52 -03001365 struct v4l2_ctrl *vc;
1366
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001367 vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
Hans Verkuilbac63982012-06-10 07:39:52 -03001368 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1369 if (vc == NULL) {
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001370 err = core->audio_hdl.error;
Hans Verkuilbac63982012-06-10 07:39:52 -03001371 goto fail_core;
1372 }
1373 vc->priv = (void *)cc;
1374 }
1375
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001376 for (i = 0; i < CX8800_VID_CTLS; i++) {
1377 const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
1378 struct v4l2_ctrl *vc;
1379
1380 vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
1381 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1382 if (vc == NULL) {
1383 err = core->video_hdl.error;
1384 goto fail_core;
1385 }
1386 vc->priv = (void *)cc;
1387 if (vc->id == V4L2_CID_CHROMA_AGC)
1388 core->chroma_agc = vc;
1389 }
Hans Verkuil34a6b7d2012-09-14 07:15:03 -03001390 v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001393
Hans Verkuilfacd2362013-05-26 07:56:06 -03001394 if (core->board.audio_chip == CX88_AUDIO_WM8775) {
Lawrence Rust69518032011-02-06 17:46:12 -03001395 struct i2c_board_info wm8775_info = {
1396 .type = "wm8775",
1397 .addr = 0x36 >> 1,
1398 .platform_data = &core->wm8775_data,
1399 };
1400 struct v4l2_subdev *sd;
1401
1402 if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
1403 core->wm8775_data.is_nova_s = true;
1404 else
1405 core->wm8775_data.is_nova_s = false;
1406
1407 sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
1408 &wm8775_info, NULL);
Hans Verkuilbac63982012-06-10 07:39:52 -03001409 if (sd != NULL) {
1410 core->sd_wm8775 = sd;
Lawrence Rust69518032011-02-06 17:46:12 -03001411 sd->grp_id = WM8775_GID;
Hans Verkuilbac63982012-06-10 07:39:52 -03001412 }
Lawrence Rust69518032011-02-06 17:46:12 -03001413 }
Hans Verkuilb8341e12009-03-29 08:26:01 -03001414
Hans Verkuilfacd2362013-05-26 07:56:06 -03001415 if (core->board.audio_chip == CX88_AUDIO_TVAUDIO) {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001416 /* This probes for a tda9874 as is used on some
1417 Pixelview Ultra boards. */
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001418 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
1419 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
Hans Verkuilb8341e12009-03-29 08:26:01 -03001420 }
Steven Toth30579062006-09-25 12:43:42 -03001421
Michael Krufky6fcecce2007-08-24 01:32:31 -03001422 switch (core->boardnr) {
1423 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Hans Verkuilb8341e12009-03-29 08:26:01 -03001424 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
lawrence rust2e4e98e2010-08-25 09:50:20 -03001425 static const struct i2c_board_info rtc_info = {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001426 I2C_BOARD_INFO("isl1208", 0x6f)
1427 };
1428
Michael Krufky6fcecce2007-08-24 01:32:31 -03001429 request_module("rtc-isl1208");
Hans Verkuilb8341e12009-03-29 08:26:01 -03001430 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1431 }
Michael Krufky8efd2e22008-04-22 14:45:14 -03001432 /* break intentionally omitted */
1433 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1434 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001435 }
1436
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001437 /* Sets device info at pci_dev */
1438 pci_set_drvdata(pci_dev, dev);
1439
Hans Verkuilc5a86142012-05-11 10:45:18 -03001440 dev->width = 320;
1441 dev->height = 240;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001442 dev->field = V4L2_FIELD_INTERLACED;
Hans Verkuilc5a86142012-05-11 10:45:18 -03001443 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1444
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001445 /* initial device configuration */
1446 mutex_lock(&core->lock);
1447 cx88_set_tvnorm(core, core->tvnorm);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001448 v4l2_ctrl_handler_setup(&core->video_hdl);
1449 v4l2_ctrl_handler_setup(&core->audio_hdl);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001450 cx88_video_mux(core, 0);
1451
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001452 q = &dev->vb2_vidq;
1453 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1454 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1455 q->gfp_flags = GFP_DMA32;
1456 q->min_buffers_needed = 2;
1457 q->drv_priv = dev;
1458 q->buf_struct_size = sizeof(struct cx88_buffer);
1459 q->ops = &cx8800_video_qops;
1460 q->mem_ops = &vb2_dma_sg_memops;
1461 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1462 q->lock = &core->lock;
1463
1464 err = vb2_queue_init(q);
1465 if (err < 0)
1466 goto fail_unreg;
1467
1468 q = &dev->vb2_vbiq;
1469 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1470 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1471 q->gfp_flags = GFP_DMA32;
1472 q->min_buffers_needed = 2;
1473 q->drv_priv = dev;
1474 q->buf_struct_size = sizeof(struct cx88_buffer);
1475 q->ops = &cx8800_vbi_qops;
1476 q->mem_ops = &vb2_dma_sg_memops;
1477 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1478 q->lock = &core->lock;
1479
1480 err = vb2_queue_init(q);
1481 if (err < 0)
1482 goto fail_unreg;
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* register v4l devices */
1485 dev->video_dev = cx88_vdev_init(core,dev->pci,
1486 &cx8800_video_template,"video");
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001487 video_set_drvdata(dev->video_dev, dev);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001488 dev->video_dev->ctrl_handler = &core->video_hdl;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001489 dev->video_dev->queue = &dev->vb2_vidq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1491 video_nr[core->nr]);
1492 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001493 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 core->name);
1495 goto fail_unreg;
1496 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001497 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1498 core->name, video_device_node_name(dev->video_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001501 video_set_drvdata(dev->vbi_dev, dev);
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001502 dev->vbi_dev->queue = &dev->vb2_vbiq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1504 vbi_nr[core->nr]);
1505 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001506 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 core->name);
1508 goto fail_unreg;
1509 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001510 printk(KERN_INFO "%s/0: registered device %s\n",
1511 core->name, video_device_node_name(dev->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Trent Piepho6a59d642007-08-15 14:41:57 -03001513 if (core->board.radio.type == CX88_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1515 &cx8800_radio_template,"radio");
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001516 video_set_drvdata(dev->radio_dev, dev);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001517 dev->radio_dev->ctrl_handler = &core->audio_hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1519 radio_nr[core->nr]);
1520 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001521 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 core->name);
1523 goto fail_unreg;
1524 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001525 printk(KERN_INFO "%s/0: registered device %s\n",
1526 core->name, video_device_node_name(dev->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* start tvaudio thread */
Trent Piepho6a59d642007-08-15 14:41:57 -03001530 if (core->board.tuner_type != TUNER_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001532 if (IS_ERR(core->kthread)) {
1533 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001534 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1535 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001536 }
1537 }
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001538 mutex_unlock(&core->lock);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return 0;
1541
1542fail_unreg:
1543 cx8800_unregister_video(dev);
1544 free_irq(pci_dev->irq, dev);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001545 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546fail_core:
1547 cx88_core_put(core,dev->pci);
1548fail_free:
1549 kfree(dev);
1550 return err;
1551}
1552
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001553static void cx8800_finidev(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001555 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001556 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001559 if (core->kthread) {
1560 kthread_stop(core->kthread);
1561 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563
Marton Balintb12203d2008-03-26 02:07:35 -03001564 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001565 cx88_ir_stop(core);
Marton Balintb12203d2008-03-26 02:07:35 -03001566
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001567 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 pci_disable_device(pci_dev);
1569
1570 /* unregister stuff */
1571
1572 free_irq(pci_dev->irq, dev);
1573 cx8800_unregister_video(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 /* free memory */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001576 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 kfree(dev);
1578}
1579
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001580#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1582{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001583 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001585 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /* stop video+vbi capture */
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001588 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001590 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 stop_video_dma(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001594 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 cx8800_stop_vbi_dma(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001597 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001599 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001600 cx88_ir_stop(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001602 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 pci_save_state(pci_dev);
1605 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1606 pci_disable_device(pci_dev);
1607 dev->state.disabled = 1;
1608 }
1609 return 0;
1610}
1611
1612static int cx8800_resume(struct pci_dev *pci_dev)
1613{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001614 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001616 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001617 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001620 err=pci_enable_device(pci_dev);
1621 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001622 printk(KERN_ERR "%s/0: can't enable device\n",
1623 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001624 return err;
1625 }
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 dev->state.disabled = 0;
1628 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001629 err= pci_set_power_state(pci_dev, PCI_D0);
1630 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001631 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001632 pci_disable_device(pci_dev);
1633 dev->state.disabled = 1;
1634
1635 return err;
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 pci_restore_state(pci_dev);
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001640 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001641 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001642 cx88_ir_start(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001643
1644 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /* restart video+vbi capture */
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001647 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001649 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 restart_video_queue(dev,&dev->vidq);
1651 }
1652 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001653 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1655 }
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001656 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 return 0;
1659}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001660#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662/* ----------------------------------------------------------- */
1663
lawrence rust2e4e98e2010-08-25 09:50:20 -03001664static const struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 {
1666 .vendor = 0x14f1,
1667 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001668 .subvendor = PCI_ANY_ID,
1669 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 },{
1671 /* --- end of list --- */
1672 }
1673};
1674MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
1675
1676static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001677 .name = "cx8800",
1678 .id_table = cx8800_pci_tbl,
1679 .probe = cx8800_initdev,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001680 .remove = cx8800_finidev,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001681#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 .suspend = cx8800_suspend,
1683 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001684#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685};
1686
Sachin Kamat06333e02013-09-20 05:32:07 -03001687module_pci_driver(cx8800_pci_driver);