]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/pci/cx88/cx88-video.c
[media] vb2: replace void *alloc_ctxs by struct device *alloc_devs
[mirror_ubuntu-artful-kernel.git] / drivers / media / pci / cx88 / cx88-video.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 *
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 *
8d87cb9f
MCC
8 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * - Multituner support
10 * - video_ioctl2 conversion
11 * - PAL/M fixes
12 *
1da177e4
LT
13 * 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>
1da177e4
LT
31#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
c24228da 35#include <linux/dma-mapping.h>
1da177e4
LT
36#include <linux/delay.h>
37#include <linux/kthread.h>
38#include <asm/div64.h>
39
40#include "cx88.h"
5e453dc7 41#include <media/v4l2-common.h>
35ea11ff 42#include <media/v4l2-ioctl.h>
1a3c60a0 43#include <media/v4l2-event.h>
b5dcee22 44#include <media/i2c/wm8775.h>
1da177e4
LT
45
46MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
47MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
48MODULE_LICENSE("GPL");
1990d50b 49MODULE_VERSION(CX88_VERSION);
1da177e4
LT
50
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
ff699e6b 65static unsigned int video_debug;
1da177e4
LT
66module_param(video_debug,int,0644);
67MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
68
ff699e6b 69static unsigned int irq_debug;
1da177e4
LT
70module_param(irq_debug,int,0644);
71MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
72
1da177e4 73#define dprintk(level,fmt, arg...) if (video_debug >= level) \
e52e98a7 74 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
1da177e4 75
1da177e4
LT
76/* ------------------------------------------------------------------- */
77/* static data */
78
2e4e98e7 79static const struct cx8800_fmt formats[] = {
1da177e4
LT
80 {
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
2e4e98e7 143static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
1da177e4
LT
144{
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
bac63981
HV
155struct 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
8c7cb12a 171static const struct cx88_ctrl cx8800_vid_ctls[] = {
1da177e4
LT
172 /* --- video --- */
173 {
bac63981
HV
174 .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,
1da177e4 183 },{
bac63981
HV
184 .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,
1da177e4 193 },{
bac63981
HV
194 .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,
1da177e4
LT
203 },{
204 /* strictly, this only describes only U saturation.
205 * V saturation is handled specially through code.
206 */
bac63981
HV
207 .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,
eea16e36 216 }, {
bac63981
HV
217 .id = V4L2_CID_SHARPNESS,
218 .minimum = 0,
219 .maximum = 4,
220 .step = 1,
221 .default_value = 0x0,
222 .off = 0,
eea16e36 223 /* NOTE: the value is converted and written to both even
224 and odd registers in the code */
bac63981
HV
225 .reg = MO_FILTER_ODD,
226 .mask = 7 << 7,
227 .shift = 7,
eea16e36 228 }, {
bac63981
HV
229 .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,
1b879c43 236 }, {
bac63981
HV
237 .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,
bded70d2 244 }, {
bac63981
HV
245 .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,
8c7cb12a
HV
254 }
255};
256
257static const struct cx88_ctrl cx8800_aud_ctls[] = {
258 {
bac63981
HV
259 /* --- 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,
1da177e4 268 },{
bac63981
HV
269 .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,
1da177e4 278 },{
bac63981
HV
279 .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,
1da177e4
LT
288 }
289};
1da177e4 290
8c7cb12a
HV
291enum {
292 CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
293 CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
294};
38a2713a 295
1da177e4
LT
296/* ------------------------------------------------------------------ */
297
e90311a1 298int cx88_video_mux(struct cx88_core *core, unsigned int input)
1da177e4 299{
e52e98a7 300 /* struct cx88_core *core = dev->core; */
1da177e4
LT
301
302 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
6a59d64c
TP
303 input, INPUT(input).vmux,
304 INPUT(input).gpio0,INPUT(input).gpio1,
305 INPUT(input).gpio2,INPUT(input).gpio3);
e52e98a7 306 core->input = input;
6a59d64c
TP
307 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);
1da177e4 312
6a59d64c 313 switch (INPUT(input).type) {
1da177e4
LT
314 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 }
f24546a9 327
66e6fbdf
RC
328 /* if there are audioroutes defined, we have an external
329 ADC to deal with audio */
66e6fbdf 330 if (INPUT(input).audioroute) {
66e6fbdf
RC
331 /* 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 */
609c4c12 334 if (core->sd_wm8775) {
5325b427 335 call_all(core, audio, s_routing,
6e1f4df7 336 INPUT(input).audioroute, 0, 0);
66e6fbdf 337 }
430189da
DB
338 /* 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 */
6e1f4df7 341 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
342 INPUT(input).type != CX88_VMUX_CABLE) {
430189da
DB
343 /* "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 }
f24546a9 351 }
66e6fbdf 352
1da177e4
LT
353 return 0;
354}
e90311a1 355EXPORT_SYMBOL(cx88_video_mux);
1da177e4
LT
356
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 */
e52e98a7 366 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
1da177e4 367 buf->bpl, buf->risc.dma);
ccd6f1d4 368 cx88_set_scale(core, core->width, core->height, core->field);
637bc207 369 cx_write(MO_COLOR_CTRL, dev->fmt->cxformat | ColorFormatGamma);
1da177e4
LT
370
371 /* reset counter */
372 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
9450684b 373 q->count = 0;
1da177e4
LT
374
375 /* enable irqs */
8ddac9ee 376 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
e52e98a7
MCC
377
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 */
1da177e4
LT
385 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));
e52e98a7 392 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
1da177e4
LT
393
394 return 0;
395}
396
17bc98a4 397#ifdef CONFIG_PM
1da177e4
LT
398static 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 */
8ddac9ee 409 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
1da177e4
LT
410 cx_clear(MO_VID_INTMSK, 0x0f0011);
411 return 0;
412}
413
414static int restart_video_queue(struct cx8800_dev *dev,
415 struct cx88_dmaqueue *q)
416{
e52e98a7 417 struct cx88_core *core = dev->core;
6f11adc6 418 struct cx88_buffer *buf;
1da177e4
LT
419
420 if (!list_empty(&q->active)) {
0b6b6302 421 buf = list_entry(q->active.next, struct cx88_buffer, list);
1da177e4 422 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
2d700715 423 buf, buf->vb.vb2_buf.index);
1da177e4 424 start_video_dma(dev, q, buf);
1da177e4 425 }
6f11adc6 426 return 0;
1da177e4 427}
bc5e66bd 428#endif
1da177e4
LT
429
430/* ------------------------------------------------------------------ */
431
df9ecb0c 432static int queue_setup(struct vb2_queue *q,
0b6b6302 433 unsigned int *num_buffers, unsigned int *num_planes,
36c0f8b3 434 unsigned int sizes[], struct device *alloc_devs[])
1da177e4 435{
0b6b6302 436 struct cx8800_dev *dev = q->drv_priv;
ccd6f1d4 437 struct cx88_core *core = dev->core;
0b6b6302
HV
438
439 *num_planes = 1;
ccd6f1d4 440 sizes[0] = (dev->fmt->depth * core->width * core->height) >> 3;
1da177e4
LT
441 return 0;
442}
443
0b6b6302 444static int buffer_prepare(struct vb2_buffer *vb)
1da177e4 445{
2d700715 446 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0b6b6302 447 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
e52e98a7 448 struct cx88_core *core = dev->core;
2d700715 449 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
0b6b6302 450 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
1da177e4 451
ccd6f1d4 452 buf->bpl = core->width * dev->fmt->depth >> 3;
0b6b6302 453
ccd6f1d4 454 if (vb2_plane_size(vb, 0) < core->height * buf->bpl)
1da177e4 455 return -EINVAL;
ccd6f1d4 456 vb2_set_plane_payload(vb, 0, core->height * buf->bpl);
1da177e4 457
ccd6f1d4 458 switch (core->field) {
637bc207
HV
459 case V4L2_FIELD_TOP:
460 cx88_risc_buffer(dev->pci, &buf->risc,
0b6b6302 461 sgt->sgl, 0, UNSET,
ccd6f1d4 462 buf->bpl, 0, core->height);
637bc207
HV
463 break;
464 case V4L2_FIELD_BOTTOM:
465 cx88_risc_buffer(dev->pci, &buf->risc,
0b6b6302 466 sgt->sgl, UNSET, 0,
ccd6f1d4 467 buf->bpl, 0, core->height);
637bc207
HV
468 break;
469 case V4L2_FIELD_SEQ_TB:
470 cx88_risc_buffer(dev->pci, &buf->risc,
0b6b6302 471 sgt->sgl,
ccd6f1d4 472 0, buf->bpl * (core->height >> 1),
637bc207 473 buf->bpl, 0,
ccd6f1d4 474 core->height >> 1);
637bc207
HV
475 break;
476 case V4L2_FIELD_SEQ_BT:
477 cx88_risc_buffer(dev->pci, &buf->risc,
0b6b6302 478 sgt->sgl,
ccd6f1d4 479 buf->bpl * (core->height >> 1), 0,
637bc207 480 buf->bpl, 0,
ccd6f1d4 481 core->height >> 1);
637bc207
HV
482 break;
483 case V4L2_FIELD_INTERLACED:
484 default:
485 cx88_risc_buffer(dev->pci, &buf->risc,
0b6b6302 486 sgt->sgl, 0, buf->bpl,
637bc207 487 buf->bpl, buf->bpl,
ccd6f1d4 488 core->height >> 1);
637bc207 489 break;
1da177e4
LT
490 }
491 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
2d700715 492 buf, buf->vb.vb2_buf.index,
ccd6f1d4 493 core->width, core->height, dev->fmt->depth, dev->fmt->name,
1da177e4 494 (unsigned long)buf->risc.dma);
1da177e4 495 return 0;
0b6b6302
HV
496}
497
498static void buffer_finish(struct vb2_buffer *vb)
499{
2d700715 500 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0b6b6302 501 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
2d700715 502 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
5e7045e3 503 struct cx88_riscmem *risc = &buf->risc;
1da177e4 504
5e7045e3
HV
505 if (risc->cpu)
506 pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
507 memset(risc, 0, sizeof(*risc));
1da177e4
LT
508}
509
0b6b6302 510static void buffer_queue(struct vb2_buffer *vb)
1da177e4 511{
2d700715 512 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0b6b6302 513 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
2d700715 514 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
1da177e4 515 struct cx88_buffer *prev;
e52e98a7 516 struct cx88_core *core = dev->core;
1da177e4
LT
517 struct cx88_dmaqueue *q = &dev->vidq;
518
0b6b6302
HV
519 /* add jump to start */
520 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
521 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
522 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
1da177e4 523
6f11adc6 524 if (list_empty(&q->active)) {
0b6b6302 525 list_add_tail(&buf->list, &q->active);
1da177e4 526 dprintk(2,"[%p/%d] buffer_queue - first active\n",
2d700715 527 buf, buf->vb.vb2_buf.index);
1da177e4
LT
528
529 } else {
0b6b6302
HV
530 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
531 prev = list_entry(q->active.prev, struct cx88_buffer, list);
532 list_add_tail(&buf->list, &q->active);
637bc207
HV
533 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
534 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
2d700715 535 buf, buf->vb.vb2_buf.index);
1da177e4
LT
536 }
537}
538
0b6b6302 539static int start_streaming(struct vb2_queue *q, unsigned int count)
1da177e4 540{
0b6b6302
HV
541 struct cx8800_dev *dev = q->drv_priv;
542 struct cx88_dmaqueue *dmaq = &dev->vidq;
543 struct cx88_buffer *buf = list_entry(dmaq->active.next,
544 struct cx88_buffer, list);
1da177e4 545
0b6b6302
HV
546 start_video_dma(dev, dmaq, buf);
547 return 0;
1da177e4
LT
548}
549
0b6b6302
HV
550static void stop_streaming(struct vb2_queue *q)
551{
552 struct cx8800_dev *dev = q->drv_priv;
553 struct cx88_core *core = dev->core;
554 struct cx88_dmaqueue *dmaq = &dev->vidq;
555 unsigned long flags;
1da177e4 556
0b6b6302
HV
557 cx_clear(MO_VID_DMACNTRL, 0x11);
558 cx_clear(VID_CAPTURE_CONTROL, 0x06);
559 spin_lock_irqsave(&dev->slock, flags);
560 while (!list_empty(&dmaq->active)) {
561 struct cx88_buffer *buf = list_entry(dmaq->active.next,
562 struct cx88_buffer, list);
edbd138e 563
0b6b6302 564 list_del(&buf->list);
2d700715 565 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1da177e4 566 }
0b6b6302 567 spin_unlock_irqrestore(&dev->slock, flags);
1da177e4
LT
568}
569
0b6b6302
HV
570static struct vb2_ops cx8800_video_qops = {
571 .queue_setup = queue_setup,
572 .buf_prepare = buffer_prepare,
573 .buf_finish = buffer_finish,
574 .buf_queue = buffer_queue,
575 .wait_prepare = vb2_ops_wait_prepare,
576 .wait_finish = vb2_ops_wait_finish,
577 .start_streaming = start_streaming,
578 .stop_streaming = stop_streaming,
579};
edbd138e 580
0b6b6302 581/* ------------------------------------------------------------------ */
1da177e4 582
0b6b6302 583static int radio_open(struct file *file)
1da177e4 584{
63b0d5ad 585 struct cx8800_dev *dev = video_drvdata(file);
5401c2db 586 struct cx88_core *core = dev->core;
0b6b6302 587 int ret = v4l2_fh_open(file);
1da177e4 588
0b6b6302
HV
589 if (ret)
590 return ret;
1da177e4 591
0b6b6302
HV
592 cx_write(MO_GP3_IO, core->board.radio.gpio3);
593 cx_write(MO_GP0_IO, core->board.radio.gpio0);
594 cx_write(MO_GP1_IO, core->board.radio.gpio1);
595 cx_write(MO_GP2_IO, core->board.radio.gpio2);
596 if (core->board.radio.audioroute) {
597 if (core->sd_wm8775) {
598 call_all(core, audio, s_routing,
5325b427 599 core->board.radio.audioroute, 0, 0);
430189da 600 }
0b6b6302
HV
601 /* "I2S ADC mode" */
602 core->tvaudio = WW_I2SADC;
603 cx88_set_tvaudio(core);
1da177e4 604 } else {
0b6b6302
HV
605 /* FM Mode */
606 core->tvaudio = WW_FM;
607 cx88_set_tvaudio(core);
608 cx88_set_stereo(core, V4L2_TUNER_MODE_STEREO, 1);
1da177e4 609 }
0b6b6302 610 call_all(core, tuner, s_radio);
1da177e4
LT
611 return 0;
612}
613
1da177e4 614/* ------------------------------------------------------------------ */
8d87cb9f 615/* VIDEO CTRL IOCTLS */
1da177e4 616
8c7cb12a
HV
617static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
618{
619 struct cx88_core *core =
620 container_of(ctrl->handler, struct cx88_core, video_hdl);
621 const struct cx88_ctrl *cc = ctrl->priv;
622 u32 value, mask;
623
624 mask = cc->mask;
625 switch (ctrl->id) {
626 case V4L2_CID_SATURATION:
627 /* special v_sat handling */
628
629 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
630
631 if (core->tvnorm & V4L2_STD_SECAM) {
632 /* For SECAM, both U and V sat should be equal */
633 value = value << 8 | value;
634 } else {
635 /* Keeps U Saturation proportional to V Sat */
636 value = (value * 0x5a) / 0x7f << 8 | value;
637 }
638 mask = 0xffff;
639 break;
640 case V4L2_CID_SHARPNESS:
641 /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
642 value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
643 /* needs to be set for both fields */
644 cx_andor(MO_FILTER_EVEN, mask, value);
645 break;
646 case V4L2_CID_CHROMA_AGC:
647 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
648 break;
649 default:
650 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
651 break;
652 }
653 dprintk(1, "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
654 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
655 mask, cc->sreg ? " [shadowed]" : "");
656 if (cc->sreg)
657 cx_sandor(cc->sreg, cc->reg, mask, value);
658 else
659 cx_andor(cc->reg, mask, value);
660 return 0;
661}
662
663static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
1da177e4 664{
bac63981 665 struct cx88_core *core =
8c7cb12a 666 container_of(ctrl->handler, struct cx88_core, audio_hdl);
bac63981 667 const struct cx88_ctrl *cc = ctrl->priv;
70f00044 668 u32 value,mask;
6951803c
LR
669
670 /* Pass changes onto any WM8775 */
609c4c12 671 if (core->sd_wm8775) {
bac63981 672 switch (ctrl->id) {
6951803c 673 case V4L2_CID_AUDIO_MUTE:
bac63981 674 wm8775_s_ctrl(core, ctrl->id, ctrl->val);
6951803c
LR
675 break;
676 case V4L2_CID_AUDIO_VOLUME:
bac63981
HV
677 wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
678 (0x90 + ctrl->val) << 8 : 0);
6951803c
LR
679 break;
680 case V4L2_CID_AUDIO_BALANCE:
bac63981 681 wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
6951803c
LR
682 break;
683 default:
6951803c
LR
684 break;
685 }
6951803c
LR
686 }
687
bac63981
HV
688 mask = cc->mask;
689 switch (ctrl->id) {
1da177e4 690 case V4L2_CID_AUDIO_BALANCE:
bac63981 691 value = (ctrl->val < 0x40) ? (0x7f - ctrl->val) : (ctrl->val - 0x40);
1da177e4
LT
692 break;
693 case V4L2_CID_AUDIO_VOLUME:
bac63981 694 value = 0x3f - (ctrl->val & 0x3f);
1da177e4 695 break;
1da177e4 696 default:
bac63981 697 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
1da177e4
LT
698 break;
699 }
6457af5f 700 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
bac63981
HV
701 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
702 mask, cc->sreg ? " [shadowed]" : "");
703 if (cc->sreg)
704 cx_sandor(cc->sreg, cc->reg, mask, value);
705 else
706 cx_andor(cc->reg, mask, value);
1da177e4
LT
707 return 0;
708}
1da177e4
LT
709
710/* ------------------------------------------------------------------ */
8d87cb9f 711/* VIDEO IOCTLS */
1da177e4 712
78b526a4 713static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
8d87cb9f 714 struct v4l2_format *f)
1da177e4 715{
0b6b6302 716 struct cx8800_dev *dev = video_drvdata(file);
ccd6f1d4 717 struct cx88_core *core = dev->core;
8d87cb9f 718
ccd6f1d4
HV
719 f->fmt.pix.width = core->width;
720 f->fmt.pix.height = core->height;
721 f->fmt.pix.field = core->field;
c5a86144 722 f->fmt.pix.pixelformat = dev->fmt->fourcc;
8d87cb9f 723 f->fmt.pix.bytesperline =
c5a86144 724 (f->fmt.pix.width * dev->fmt->depth) >> 3;
8d87cb9f
MCC
725 f->fmt.pix.sizeimage =
726 f->fmt.pix.height * f->fmt.pix.bytesperline;
c5a86144 727 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
8d87cb9f 728 return 0;
1da177e4
LT
729}
730
78b526a4 731static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
8d87cb9f 732 struct v4l2_format *f)
1da177e4 733{
0b6b6302
HV
734 struct cx8800_dev *dev = video_drvdata(file);
735 struct cx88_core *core = dev->core;
2e4e98e7 736 const struct cx8800_fmt *fmt;
8d87cb9f
MCC
737 enum v4l2_field field;
738 unsigned int maxw, maxh;
e52e98a7 739
8d87cb9f
MCC
740 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
741 if (NULL == fmt)
742 return -EINVAL;
1da177e4 743
ccd6f1d4
HV
744 maxw = norm_maxw(core->tvnorm);
745 maxh = norm_maxh(core->tvnorm);
1da177e4 746
ccd6f1d4 747 field = f->fmt.pix.field;
8d87cb9f
MCC
748
749 switch (field) {
750 case V4L2_FIELD_TOP:
751 case V4L2_FIELD_BOTTOM:
8d87cb9f 752 case V4L2_FIELD_INTERLACED:
ccd6f1d4
HV
753 case V4L2_FIELD_SEQ_BT:
754 case V4L2_FIELD_SEQ_TB:
8d87cb9f 755 break;
1da177e4 756 default:
ccd6f1d4
HV
757 field = (f->fmt.pix.height > maxh / 2)
758 ? V4L2_FIELD_INTERLACED
759 : V4L2_FIELD_BOTTOM;
760 break;
1da177e4 761 }
ccd6f1d4
HV
762 if (V4L2_FIELD_HAS_T_OR_B(field))
763 maxh /= 2;
8d87cb9f 764
4b89945e
TP
765 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
766 &f->fmt.pix.height, 32, maxh, 0, 0);
ccd6f1d4 767 f->fmt.pix.field = field;
8d87cb9f
MCC
768 f->fmt.pix.bytesperline =
769 (f->fmt.pix.width * fmt->depth) >> 3;
770 f->fmt.pix.sizeimage =
771 f->fmt.pix.height * f->fmt.pix.bytesperline;
9450684b 772 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
8d87cb9f
MCC
773
774 return 0;
1da177e4
LT
775}
776
78b526a4 777static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
8d87cb9f 778 struct v4l2_format *f)
1da177e4 779{
0b6b6302 780 struct cx8800_dev *dev = video_drvdata(file);
ccd6f1d4 781 struct cx88_core *core = dev->core;
78b526a4 782 int err = vidioc_try_fmt_vid_cap (file,priv,f);
8d87cb9f
MCC
783
784 if (0 != err)
785 return err;
078859a3
HV
786 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq))
787 return -EBUSY;
788 if (core->dvbdev && vb2_is_busy(&core->dvbdev->vb2_mpegq))
789 return -EBUSY;
ccd6f1d4
HV
790 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
791 core->width = f->fmt.pix.width;
792 core->height = f->fmt.pix.height;
793 core->field = f->fmt.pix.field;
8d87cb9f 794 return 0;
1da177e4
LT
795}
796
902e197d
HV
797void cx88_querycap(struct file *file, struct cx88_core *core,
798 struct v4l2_capability *cap)
799{
800 struct video_device *vdev = video_devdata(file);
801
802 strlcpy(cap->card, core->board.name, sizeof(cap->card));
803 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
804 if (UNSET != core->board.tuner_type)
805 cap->device_caps |= V4L2_CAP_TUNER;
806 switch (vdev->vfl_type) {
807 case VFL_TYPE_RADIO:
808 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
809 break;
810 case VFL_TYPE_GRABBER:
811 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
812 break;
813 case VFL_TYPE_VBI:
814 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
815 break;
816 }
817 cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
818 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
819 if (core->board.radio.type == CX88_RADIO)
820 cap->capabilities |= V4L2_CAP_RADIO;
821}
822EXPORT_SYMBOL(cx88_querycap);
823
824static int vidioc_querycap(struct file *file, void *priv,
8d87cb9f 825 struct v4l2_capability *cap)
1da177e4 826{
0b6b6302
HV
827 struct cx8800_dev *dev = video_drvdata(file);
828 struct cx88_core *core = dev->core;
1da177e4 829
8d87cb9f 830 strcpy(cap->driver, "cx8800");
902e197d
HV
831 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
832 cx88_querycap(file, core, cap);
8d87cb9f
MCC
833 return 0;
834}
e52e98a7 835
78b526a4 836static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
8d87cb9f
MCC
837 struct v4l2_fmtdesc *f)
838{
839 if (unlikely(f->index >= ARRAY_SIZE(formats)))
840 return -EINVAL;
841
842 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
843 f->pixelformat = formats[f->index].fourcc;
844
845 return 0;
846}
1da177e4 847
48d68801
HV
848static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
849{
0b6b6302
HV
850 struct cx8800_dev *dev = video_drvdata(file);
851 struct cx88_core *core = dev->core;
48d68801
HV
852
853 *tvnorm = core->tvnorm;
854 return 0;
855}
856
314527ac 857static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
e52e98a7 858{
0b6b6302
HV
859 struct cx8800_dev *dev = video_drvdata(file);
860 struct cx88_core *core = dev->core;
e52e98a7 861
078859a3 862 return cx88_set_tvnorm(core, tvnorms);
8d87cb9f 863}
1da177e4 864
8d87cb9f 865/* only one input in this sample driver */
54da49f5 866int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
8d87cb9f 867{
2e4e98e7 868 static const char * const iname[] = {
8d87cb9f
MCC
869 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
870 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
871 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
872 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
873 [ CX88_VMUX_SVIDEO ] = "S-Video",
874 [ CX88_VMUX_TELEVISION ] = "Television",
875 [ CX88_VMUX_CABLE ] = "Cable TV",
876 [ CX88_VMUX_DVB ] = "DVB",
877 [ CX88_VMUX_DEBUG ] = "for debug only",
878 };
f3334bcb 879 unsigned int n = i->index;
1da177e4 880
8d87cb9f
MCC
881 if (n >= 4)
882 return -EINVAL;
6a59d64c 883 if (0 == INPUT(n).type)
8d87cb9f 884 return -EINVAL;
8d87cb9f 885 i->type = V4L2_INPUT_TYPE_CAMERA;
6a59d64c
TP
886 strcpy(i->name,iname[INPUT(n).type]);
887 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
473d8024 888 (CX88_VMUX_CABLE == INPUT(n).type)) {
8d87cb9f 889 i->type = V4L2_INPUT_TYPE_TUNER;
473d8024 890 }
f33e9868 891 i->std = CX88_NORMS;
8d87cb9f
MCC
892 return 0;
893}
54da49f5
MCC
894EXPORT_SYMBOL(cx88_enum_input);
895
896static int vidioc_enum_input (struct file *file, void *priv,
897 struct v4l2_input *i)
898{
0b6b6302
HV
899 struct cx8800_dev *dev = video_drvdata(file);
900 struct cx88_core *core = dev->core;
54da49f5
MCC
901 return cx88_enum_input (core,i);
902}
1da177e4 903
8d87cb9f
MCC
904static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
905{
0b6b6302
HV
906 struct cx8800_dev *dev = video_drvdata(file);
907 struct cx88_core *core = dev->core;
1da177e4 908
8d87cb9f
MCC
909 *i = core->input;
910 return 0;
911}
1da177e4 912
8d87cb9f
MCC
913static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
914{
0b6b6302
HV
915 struct cx8800_dev *dev = video_drvdata(file);
916 struct cx88_core *core = dev->core;
1da177e4 917
8d87cb9f
MCC
918 if (i >= 4)
919 return -EINVAL;
f33e9868
HV
920 if (0 == INPUT(i).type)
921 return -EINVAL;
1da177e4 922
8d87cb9f 923 cx88_newstation(core);
e90311a1 924 cx88_video_mux(core,i);
8d87cb9f
MCC
925 return 0;
926}
1da177e4 927
8d87cb9f
MCC
928static int vidioc_g_tuner (struct file *file, void *priv,
929 struct v4l2_tuner *t)
930{
0b6b6302
HV
931 struct cx8800_dev *dev = video_drvdata(file);
932 struct cx88_core *core = dev->core;
8d87cb9f 933 u32 reg;
1da177e4 934
6a59d64c 935 if (unlikely(UNSET == core->board.tuner_type))
8d87cb9f 936 return -EINVAL;
243d8c0f
MCC
937 if (0 != t->index)
938 return -EINVAL;
a82decf6 939
8d87cb9f 940 strcpy(t->name, "Television");
8d87cb9f
MCC
941 t->capability = V4L2_TUNER_CAP_NORM;
942 t->rangehigh = 0xffffffffUL;
f33e9868 943 call_all(core, tuner, g_tuner, t);
a82decf6 944
8d87cb9f
MCC
945 cx88_get_stereo(core ,t);
946 reg = cx_read(MO_DEVICE_STATUS);
947 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
948 return 0;
949}
41ef7c1e 950
8d87cb9f 951static int vidioc_s_tuner (struct file *file, void *priv,
2f73c7c5 952 const struct v4l2_tuner *t)
8d87cb9f 953{
0b6b6302
HV
954 struct cx8800_dev *dev = video_drvdata(file);
955 struct cx88_core *core = dev->core;
41ef7c1e 956
6a59d64c 957 if (UNSET == core->board.tuner_type)
8d87cb9f
MCC
958 return -EINVAL;
959 if (0 != t->index)
960 return -EINVAL;
c5287ba1 961
8d87cb9f
MCC
962 cx88_set_stereo(core, t->audmode, 1);
963 return 0;
964}
902fc997 965
8d87cb9f
MCC
966static int vidioc_g_frequency (struct file *file, void *priv,
967 struct v4l2_frequency *f)
968{
0b6b6302
HV
969 struct cx8800_dev *dev = video_drvdata(file);
970 struct cx88_core *core = dev->core;
902fc997 971
6a59d64c 972 if (unlikely(UNSET == core->board.tuner_type))
8d87cb9f 973 return -EINVAL;
f33e9868
HV
974 if (f->tuner)
975 return -EINVAL;
8d87cb9f 976
8d87cb9f
MCC
977 f->frequency = core->freq;
978
b8341e1d 979 call_all(core, tuner, g_frequency, f);
1da177e4 980
1da177e4
LT
981 return 0;
982}
983
54da49f5 984int cx88_set_freq (struct cx88_core *core,
b530a447 985 const struct v4l2_frequency *f)
1da177e4 986{
b530a447
HV
987 struct v4l2_frequency new_freq = *f;
988
6a59d64c 989 if (unlikely(UNSET == core->board.tuner_type))
8d87cb9f
MCC
990 return -EINVAL;
991 if (unlikely(f->tuner != 0))
992 return -EINVAL;
54da49f5 993
8d87cb9f 994 cx88_newstation(core);
b8341e1d 995 call_all(core, tuner, s_frequency, f);
b530a447
HV
996 call_all(core, tuner, g_frequency, &new_freq);
997 core->freq = new_freq.frequency;
c7b0ac05 998
8d87cb9f
MCC
999 /* When changing channels it is required to reset TVAUDIO */
1000 msleep (10);
1001 cx88_set_tvaudio(core);
c7b0ac05 1002
8d87cb9f 1003 return 0;
1da177e4 1004}
54da49f5
MCC
1005EXPORT_SYMBOL(cx88_set_freq);
1006
1007static int vidioc_s_frequency (struct file *file, void *priv,
b530a447 1008 const struct v4l2_frequency *f)
54da49f5 1009{
0b6b6302
HV
1010 struct cx8800_dev *dev = video_drvdata(file);
1011 struct cx88_core *core = dev->core;
54da49f5 1012
edbd138e 1013 return cx88_set_freq(core, f);
54da49f5 1014}
1da177e4 1015
dbbff48f
TP
1016#ifdef CONFIG_VIDEO_ADV_DEBUG
1017static int vidioc_g_register (struct file *file, void *fh,
aecde8b5 1018 struct v4l2_dbg_register *reg)
dbbff48f 1019{
0b6b6302
HV
1020 struct cx8800_dev *dev = video_drvdata(file);
1021 struct cx88_core *core = dev->core;
dbbff48f 1022
dbbff48f 1023 /* cx2388x has a 24-bit register space */
7feeb148 1024 reg->val = cx_read(reg->reg & 0xfffffc);
aecde8b5 1025 reg->size = 4;
dbbff48f
TP
1026 return 0;
1027}
1028
1029static int vidioc_s_register (struct file *file, void *fh,
977ba3b1 1030 const struct v4l2_dbg_register *reg)
dbbff48f 1031{
0b6b6302
HV
1032 struct cx8800_dev *dev = video_drvdata(file);
1033 struct cx88_core *core = dev->core;
dbbff48f 1034
7feeb148 1035 cx_write(reg->reg & 0xfffffc, reg->val);
dbbff48f
TP
1036 return 0;
1037}
1038#endif
8d87cb9f
MCC
1039
1040/* ----------------------------------------------------------- */
1041/* RADIO ESPECIFIC IOCTLS */
1da177e4
LT
1042/* ----------------------------------------------------------- */
1043
8d87cb9f
MCC
1044static int radio_g_tuner (struct file *file, void *priv,
1045 struct v4l2_tuner *t)
1046{
0b6b6302
HV
1047 struct cx8800_dev *dev = video_drvdata(file);
1048 struct cx88_core *core = dev->core;
1da177e4 1049
8d87cb9f
MCC
1050 if (unlikely(t->index > 0))
1051 return -EINVAL;
1da177e4 1052
8d87cb9f 1053 strcpy(t->name, "Radio");
1da177e4 1054
b8341e1d 1055 call_all(core, tuner, g_tuner, t);
8d87cb9f
MCC
1056 return 0;
1057}
1da177e4 1058
8d87cb9f 1059static int radio_s_tuner (struct file *file, void *priv,
2f73c7c5 1060 const struct v4l2_tuner *t)
8d87cb9f 1061{
0b6b6302
HV
1062 struct cx8800_dev *dev = video_drvdata(file);
1063 struct cx88_core *core = dev->core;
a82decf6 1064
8d87cb9f
MCC
1065 if (0 != t->index)
1066 return -EINVAL;
1da177e4 1067
b8341e1d 1068 call_all(core, tuner, s_tuner, t);
8d87cb9f
MCC
1069 return 0;
1070}
1da177e4 1071
1da177e4
LT
1072/* ----------------------------------------------------------- */
1073
2e4e98e7 1074static const char *cx88_vid_irqs[32] = {
41ef7c1e
MCC
1075 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1076 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1077 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1078 "y_sync", "u_sync", "v_sync", "vbi_sync",
1079 "opc_err", "par_err", "rip_err", "pci_abort",
1080};
1081
1da177e4
LT
1082static void cx8800_vid_irq(struct cx8800_dev *dev)
1083{
1084 struct cx88_core *core = dev->core;
1085 u32 status, mask, count;
1086
1087 status = cx_read(MO_VID_INTSTAT);
1088 mask = cx_read(MO_VID_INTMSK);
1089 if (0 == (status & mask))
1090 return;
1091 cx_write(MO_VID_INTSTAT, status);
1092 if (irq_debug || (status & mask & ~0xff))
1093 cx88_print_irqbits(core->name, "irq vid",
66623a04
MCC
1094 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1095 status, mask);
1da177e4
LT
1096
1097 /* risc op code error */
1098 if (status & (1 << 16)) {
1099 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1100 cx_clear(MO_VID_DMACNTRL, 0x11);
1101 cx_clear(VID_CAPTURE_CONTROL, 0x06);
e52e98a7 1102 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1da177e4
LT
1103 }
1104
1105 /* risc1 y */
1106 if (status & 0x01) {
1107 spin_lock(&dev->slock);
1108 count = cx_read(MO_VIDY_GPCNT);
e52e98a7 1109 cx88_wakeup(core, &dev->vidq, count);
1da177e4
LT
1110 spin_unlock(&dev->slock);
1111 }
1112
1113 /* risc1 vbi */
1114 if (status & 0x08) {
1115 spin_lock(&dev->slock);
1116 count = cx_read(MO_VBI_GPCNT);
e52e98a7 1117 cx88_wakeup(core, &dev->vbiq, count);
1da177e4
LT
1118 spin_unlock(&dev->slock);
1119 }
1da177e4
LT
1120}
1121
7d12e780 1122static irqreturn_t cx8800_irq(int irq, void *dev_id)
1da177e4
LT
1123{
1124 struct cx8800_dev *dev = dev_id;
1125 struct cx88_core *core = dev->core;
1126 u32 status;
1127 int loop, handled = 0;
1128
1129 for (loop = 0; loop < 10; loop++) {
8ddac9ee
TP
1130 status = cx_read(MO_PCI_INTSTAT) &
1131 (core->pci_irqmask | PCI_INT_VIDINT);
1da177e4
LT
1132 if (0 == status)
1133 goto out;
1134 cx_write(MO_PCI_INTSTAT, status);
1135 handled = 1;
1136
1137 if (status & core->pci_irqmask)
1138 cx88_core_irq(core,status);
8ddac9ee 1139 if (status & PCI_INT_VIDINT)
1da177e4 1140 cx8800_vid_irq(dev);
c2c1b415 1141 }
1da177e4
LT
1142 if (10 == loop) {
1143 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1144 core->name);
1145 cx_write(MO_PCI_INTMSK,0);
1146 }
1147
1148 out:
1149 return IRQ_RETVAL(handled);
1150}
1151
1152/* ----------------------------------------------------------- */
1153/* exported stuff */
1154
bec43661 1155static const struct v4l2_file_operations video_fops =
1da177e4
LT
1156{
1157 .owner = THIS_MODULE,
0b6b6302
HV
1158 .open = v4l2_fh_open,
1159 .release = vb2_fop_release,
1160 .read = vb2_fop_read,
1161 .poll = vb2_fop_poll,
1162 .mmap = vb2_fop_mmap,
b6187264 1163 .unlocked_ioctl = video_ioctl2,
1da177e4
LT
1164};
1165
a399810c 1166static const struct v4l2_ioctl_ops video_ioctl_ops = {
8d87cb9f 1167 .vidioc_querycap = vidioc_querycap,
78b526a4
HV
1168 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1169 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1170 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1171 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
0b6b6302
HV
1172 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1173 .vidioc_querybuf = vb2_ioctl_querybuf,
1174 .vidioc_qbuf = vb2_ioctl_qbuf,
1175 .vidioc_dqbuf = vb2_ioctl_dqbuf,
48d68801 1176 .vidioc_g_std = vidioc_g_std,
8d87cb9f
MCC
1177 .vidioc_s_std = vidioc_s_std,
1178 .vidioc_enum_input = vidioc_enum_input,
1179 .vidioc_g_input = vidioc_g_input,
1180 .vidioc_s_input = vidioc_s_input,
0b6b6302
HV
1181 .vidioc_streamon = vb2_ioctl_streamon,
1182 .vidioc_streamoff = vb2_ioctl_streamoff,
8d87cb9f
MCC
1183 .vidioc_g_tuner = vidioc_g_tuner,
1184 .vidioc_s_tuner = vidioc_s_tuner,
1185 .vidioc_g_frequency = vidioc_g_frequency,
1186 .vidioc_s_frequency = vidioc_s_frequency,
1a3c60a0
HV
1187 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1188 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
dbbff48f
TP
1189#ifdef CONFIG_VIDEO_ADV_DEBUG
1190 .vidioc_g_register = vidioc_g_register,
1191 .vidioc_s_register = vidioc_s_register,
1192#endif
a399810c
HV
1193};
1194
2e4e98e7 1195static const struct video_device cx8800_video_template = {
a399810c 1196 .name = "cx8800-video",
a399810c 1197 .fops = &video_fops,
a399810c 1198 .ioctl_ops = &video_ioctl_ops,
63ab1bdc 1199 .tvnorms = CX88_NORMS,
1da177e4
LT
1200};
1201
f33e9868
HV
1202static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1203 .vidioc_querycap = vidioc_querycap,
1204 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1205 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1206 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
0b6b6302
HV
1207 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1208 .vidioc_querybuf = vb2_ioctl_querybuf,
1209 .vidioc_qbuf = vb2_ioctl_qbuf,
1210 .vidioc_dqbuf = vb2_ioctl_dqbuf,
48d68801 1211 .vidioc_g_std = vidioc_g_std,
f33e9868
HV
1212 .vidioc_s_std = vidioc_s_std,
1213 .vidioc_enum_input = vidioc_enum_input,
1214 .vidioc_g_input = vidioc_g_input,
1215 .vidioc_s_input = vidioc_s_input,
0b6b6302
HV
1216 .vidioc_streamon = vb2_ioctl_streamon,
1217 .vidioc_streamoff = vb2_ioctl_streamoff,
f33e9868
HV
1218 .vidioc_g_tuner = vidioc_g_tuner,
1219 .vidioc_s_tuner = vidioc_s_tuner,
1220 .vidioc_g_frequency = vidioc_g_frequency,
1221 .vidioc_s_frequency = vidioc_s_frequency,
f33e9868
HV
1222#ifdef CONFIG_VIDEO_ADV_DEBUG
1223 .vidioc_g_register = vidioc_g_register,
1224 .vidioc_s_register = vidioc_s_register,
1225#endif
1226};
1227
1228static const struct video_device cx8800_vbi_template = {
1229 .name = "cx8800-vbi",
1230 .fops = &video_fops,
1231 .ioctl_ops = &vbi_ioctl_ops,
1232 .tvnorms = CX88_NORMS,
f33e9868
HV
1233};
1234
bec43661 1235static const struct v4l2_file_operations radio_fops =
1da177e4
LT
1236{
1237 .owner = THIS_MODULE,
0b6b6302 1238 .open = radio_open,
1a3c60a0 1239 .poll = v4l2_ctrl_poll,
0b6b6302 1240 .release = v4l2_fh_release,
b6187264 1241 .unlocked_ioctl = video_ioctl2,
1da177e4
LT
1242};
1243
a399810c 1244static const struct v4l2_ioctl_ops radio_ioctl_ops = {
902e197d 1245 .vidioc_querycap = vidioc_querycap,
8d87cb9f 1246 .vidioc_g_tuner = radio_g_tuner,
8d87cb9f 1247 .vidioc_s_tuner = radio_s_tuner,
8d87cb9f
MCC
1248 .vidioc_g_frequency = vidioc_g_frequency,
1249 .vidioc_s_frequency = vidioc_s_frequency,
1a3c60a0
HV
1250 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1251 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
a75d2048
TP
1252#ifdef CONFIG_VIDEO_ADV_DEBUG
1253 .vidioc_g_register = vidioc_g_register,
1254 .vidioc_s_register = vidioc_s_register,
1255#endif
1da177e4
LT
1256};
1257
2e4e98e7 1258static const struct video_device cx8800_radio_template = {
a399810c 1259 .name = "cx8800-radio",
a399810c 1260 .fops = &radio_fops,
a399810c
HV
1261 .ioctl_ops = &radio_ioctl_ops,
1262};
1263
8c7cb12a
HV
1264static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
1265 .s_ctrl = cx8800_s_vid_ctrl,
1266};
1267
1268static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
1269 .s_ctrl = cx8800_s_aud_ctrl,
bac63981
HV
1270};
1271
1da177e4
LT
1272/* ----------------------------------------------------------- */
1273
1274static void cx8800_unregister_video(struct cx8800_dev *dev)
1275{
34080bc2
HV
1276 video_unregister_device(&dev->radio_dev);
1277 video_unregister_device(&dev->vbi_dev);
1278 video_unregister_device(&dev->video_dev);
1da177e4
LT
1279}
1280
4c62e976
GKH
1281static int cx8800_initdev(struct pci_dev *pci_dev,
1282 const struct pci_device_id *pci_id)
1da177e4
LT
1283{
1284 struct cx8800_dev *dev;
1285 struct cx88_core *core;
0b6b6302 1286 struct vb2_queue *q;
1da177e4 1287 int err;
bac63981 1288 int i;
1da177e4 1289
7408187d 1290 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1da177e4
LT
1291 if (NULL == dev)
1292 return -ENOMEM;
1da177e4
LT
1293
1294 /* pci init */
1295 dev->pci = pci_dev;
1296 if (pci_enable_device(pci_dev)) {
1297 err = -EIO;
1298 goto fail_free;
1299 }
1300 core = cx88_core_get(dev->pci);
1301 if (NULL == core) {
1302 err = -EINVAL;
1303 goto fail_free;
1304 }
1305 dev->core = core;
1306
1307 /* print pci info */
abd34d8d 1308 dev->pci_rev = pci_dev->revision;
4ac97914
MCC
1309 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1310 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
228aef63 1311 "latency: %d, mmio: 0x%llx\n", core->name,
1da177e4 1312 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
228aef63 1313 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1da177e4
LT
1314
1315 pci_set_master(pci_dev);
1a47de6e
CH
1316 err = pci_set_dma_mask(pci_dev,DMA_BIT_MASK(32));
1317 if (err) {
1da177e4 1318 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1da177e4
LT
1319 goto fail_core;
1320 }
1321
1322 /* initialize driver struct */
1da177e4 1323 spin_lock_init(&dev->slock);
1da177e4
LT
1324
1325 /* init video dma queues */
1326 INIT_LIST_HEAD(&dev->vidq.active);
1da177e4
LT
1327
1328 /* init vbi dma queues */
1329 INIT_LIST_HEAD(&dev->vbiq.active);
1da177e4
LT
1330
1331 /* get irq */
1332 err = request_irq(pci_dev->irq, cx8800_irq,
3e018fe4 1333 IRQF_SHARED, core->name, dev);
1da177e4 1334 if (err < 0) {
5772f813 1335 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
1da177e4
LT
1336 core->name,pci_dev->irq);
1337 goto fail_core;
1338 }
1339 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1340
8c7cb12a
HV
1341 for (i = 0; i < CX8800_AUD_CTLS; i++) {
1342 const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
1343 struct v4l2_ctrl *vc;
1344
1345 vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
1346 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1347 if (vc == NULL) {
1348 err = core->audio_hdl.error;
1349 goto fail_core;
1350 }
1351 vc->priv = (void *)cc;
1352 }
1353
1354 for (i = 0; i < CX8800_VID_CTLS; i++) {
1355 const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
bac63981
HV
1356 struct v4l2_ctrl *vc;
1357
8c7cb12a 1358 vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
bac63981
HV
1359 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1360 if (vc == NULL) {
8c7cb12a 1361 err = core->video_hdl.error;
bac63981
HV
1362 goto fail_core;
1363 }
1364 vc->priv = (void *)cc;
8c7cb12a
HV
1365 if (vc->id == V4L2_CID_CHROMA_AGC)
1366 core->chroma_agc = vc;
bac63981 1367 }
34a6b7d0 1368 v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL);
bac63981 1369
1da177e4 1370 /* load and configure helper modules */
e52e98a7 1371
facd2366 1372 if (core->board.audio_chip == CX88_AUDIO_WM8775) {
6951803c
LR
1373 struct i2c_board_info wm8775_info = {
1374 .type = "wm8775",
1375 .addr = 0x36 >> 1,
1376 .platform_data = &core->wm8775_data,
1377 };
1378 struct v4l2_subdev *sd;
1379
1380 if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
1381 core->wm8775_data.is_nova_s = true;
1382 else
1383 core->wm8775_data.is_nova_s = false;
1384
1385 sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
1386 &wm8775_info, NULL);
bac63981
HV
1387 if (sd != NULL) {
1388 core->sd_wm8775 = sd;
6951803c 1389 sd->grp_id = WM8775_GID;
bac63981 1390 }
6951803c 1391 }
b8341e1d 1392
facd2366 1393 if (core->board.audio_chip == CX88_AUDIO_TVAUDIO) {
b8341e1d
HV
1394 /* This probes for a tda9874 as is used on some
1395 Pixelview Ultra boards. */
9a1f8b34
LP
1396 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
1397 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
b8341e1d 1398 }
3057906d 1399
6fcecce7
MK
1400 switch (core->boardnr) {
1401 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
b8341e1d 1402 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
2e4e98e7 1403 static const struct i2c_board_info rtc_info = {
b8341e1d
HV
1404 I2C_BOARD_INFO("isl1208", 0x6f)
1405 };
1406
6fcecce7 1407 request_module("rtc-isl1208");
b8341e1d
HV
1408 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1409 }
8efd2e28
MK
1410 /* break intentionally omitted */
1411 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1412 request_module("ir-kbd-i2c");
6fcecce7
MK
1413 }
1414
121ec132
MCC
1415 /* Sets device info at pci_dev */
1416 pci_set_drvdata(pci_dev, dev);
1417
ccd6f1d4 1418 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
c5a86144 1419
078859a3
HV
1420 /* Maintain a reference so cx88-blackbird can query the 8800 device. */
1421 core->v4ldev = dev;
1422
121ec132
MCC
1423 /* initial device configuration */
1424 mutex_lock(&core->lock);
1425 cx88_set_tvnorm(core, core->tvnorm);
8c7cb12a
HV
1426 v4l2_ctrl_handler_setup(&core->video_hdl);
1427 v4l2_ctrl_handler_setup(&core->audio_hdl);
121ec132
MCC
1428 cx88_video_mux(core, 0);
1429
0b6b6302
HV
1430 q = &dev->vb2_vidq;
1431 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1432 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1433 q->gfp_flags = GFP_DMA32;
1434 q->min_buffers_needed = 2;
1435 q->drv_priv = dev;
1436 q->buf_struct_size = sizeof(struct cx88_buffer);
1437 q->ops = &cx8800_video_qops;
1438 q->mem_ops = &vb2_dma_sg_memops;
1439 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1440 q->lock = &core->lock;
2bc46b3a 1441 q->dev = &dev->pci->dev;
0b6b6302
HV
1442
1443 err = vb2_queue_init(q);
1444 if (err < 0)
1445 goto fail_unreg;
1446
1447 q = &dev->vb2_vbiq;
1448 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1449 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1450 q->gfp_flags = GFP_DMA32;
1451 q->min_buffers_needed = 2;
1452 q->drv_priv = dev;
1453 q->buf_struct_size = sizeof(struct cx88_buffer);
1454 q->ops = &cx8800_vbi_qops;
1455 q->mem_ops = &vb2_dma_sg_memops;
1456 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1457 q->lock = &core->lock;
2bc46b3a 1458 q->dev = &dev->pci->dev;
0b6b6302
HV
1459
1460 err = vb2_queue_init(q);
1461 if (err < 0)
1462 goto fail_unreg;
1463
1da177e4 1464 /* register v4l devices */
34080bc2
HV
1465 cx88_vdev_init(core, dev->pci, &dev->video_dev,
1466 &cx8800_video_template, "video");
1467 video_set_drvdata(&dev->video_dev, dev);
1468 dev->video_dev.ctrl_handler = &core->video_hdl;
1469 dev->video_dev.queue = &dev->vb2_vidq;
1470 err = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER,
1da177e4
LT
1471 video_nr[core->nr]);
1472 if (err < 0) {
5772f813 1473 printk(KERN_ERR "%s/0: can't register video device\n",
1da177e4
LT
1474 core->name);
1475 goto fail_unreg;
1476 }
38c7c036 1477 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
34080bc2 1478 core->name, video_device_node_name(&dev->video_dev));
1da177e4 1479
34080bc2
HV
1480 cx88_vdev_init(core, dev->pci, &dev->vbi_dev,
1481 &cx8800_vbi_template, "vbi");
1482 video_set_drvdata(&dev->vbi_dev, dev);
1483 dev->vbi_dev.queue = &dev->vb2_vbiq;
1484 err = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
1da177e4
LT
1485 vbi_nr[core->nr]);
1486 if (err < 0) {
5772f813 1487 printk(KERN_ERR "%s/0: can't register vbi device\n",
1da177e4
LT
1488 core->name);
1489 goto fail_unreg;
1490 }
38c7c036 1491 printk(KERN_INFO "%s/0: registered device %s\n",
34080bc2 1492 core->name, video_device_node_name(&dev->vbi_dev));
1da177e4 1493
6a59d64c 1494 if (core->board.radio.type == CX88_RADIO) {
34080bc2
HV
1495 cx88_vdev_init(core, dev->pci, &dev->radio_dev,
1496 &cx8800_radio_template, "radio");
1497 video_set_drvdata(&dev->radio_dev, dev);
1498 dev->radio_dev.ctrl_handler = &core->audio_hdl;
1499 err = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
1da177e4
LT
1500 radio_nr[core->nr]);
1501 if (err < 0) {
5772f813 1502 printk(KERN_ERR "%s/0: can't register radio device\n",
1da177e4
LT
1503 core->name);
1504 goto fail_unreg;
1505 }
38c7c036 1506 printk(KERN_INFO "%s/0: registered device %s\n",
34080bc2 1507 core->name, video_device_node_name(&dev->radio_dev));
1da177e4
LT
1508 }
1509
1da177e4 1510 /* start tvaudio thread */
c39ba330 1511 if (core->board.tuner_type != UNSET) {
1da177e4 1512 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
32b78de7
CG
1513 if (IS_ERR(core->kthread)) {
1514 err = PTR_ERR(core->kthread);
5772f813
TP
1515 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1516 core->name, err);
32b78de7
CG
1517 }
1518 }
121ec132
MCC
1519 mutex_unlock(&core->lock);
1520
1da177e4
LT
1521 return 0;
1522
1523fail_unreg:
1524 cx8800_unregister_video(dev);
1525 free_irq(pci_dev->irq, dev);
121ec132 1526 mutex_unlock(&core->lock);
1da177e4 1527fail_core:
078859a3 1528 core->v4ldev = NULL;
1da177e4
LT
1529 cx88_core_put(core,dev->pci);
1530fail_free:
1531 kfree(dev);
1532 return err;
1533}
1534
4c62e976 1535static void cx8800_finidev(struct pci_dev *pci_dev)
1da177e4 1536{
4ac97914 1537 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
e52e98a7 1538 struct cx88_core *core = dev->core;
1da177e4
LT
1539
1540 /* stop thread */
e52e98a7
MCC
1541 if (core->kthread) {
1542 kthread_stop(core->kthread);
1543 core->kthread = NULL;
1da177e4
LT
1544 }
1545
b12203d2 1546 if (core->ir)
92f4fc10 1547 cx88_ir_stop(core);
b12203d2 1548
e52e98a7 1549 cx88_shutdown(core); /* FIXME */
1da177e4
LT
1550
1551 /* unregister stuff */
1552
1553 free_irq(pci_dev->irq, dev);
1554 cx8800_unregister_video(dev);
98822de9 1555 pci_disable_device(pci_dev);
1da177e4 1556
078859a3
HV
1557 core->v4ldev = NULL;
1558
1da177e4 1559 /* free memory */
e52e98a7 1560 cx88_core_put(core,dev->pci);
1da177e4
LT
1561 kfree(dev);
1562}
1563
17bc98a4 1564#ifdef CONFIG_PM
1da177e4
LT
1565static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1566{
b45009b0 1567 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1da177e4 1568 struct cx88_core *core = dev->core;
5ddfbbb9 1569 unsigned long flags;
1da177e4
LT
1570
1571 /* stop video+vbi capture */
5ddfbbb9 1572 spin_lock_irqsave(&dev->slock, flags);
1da177e4 1573 if (!list_empty(&dev->vidq.active)) {
5772f813 1574 printk("%s/0: suspend video\n", core->name);
1da177e4 1575 stop_video_dma(dev);
1da177e4
LT
1576 }
1577 if (!list_empty(&dev->vbiq.active)) {
5772f813 1578 printk("%s/0: suspend vbi\n", core->name);
1da177e4 1579 cx8800_stop_vbi_dma(dev);
1da177e4 1580 }
5ddfbbb9 1581 spin_unlock_irqrestore(&dev->slock, flags);
1da177e4 1582
13595a51 1583 if (core->ir)
92f4fc10 1584 cx88_ir_stop(core);
1da177e4 1585 /* FIXME -- shutdown device */
e52e98a7 1586 cx88_shutdown(core);
1da177e4
LT
1587
1588 pci_save_state(pci_dev);
1589 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1590 pci_disable_device(pci_dev);
1591 dev->state.disabled = 1;
1592 }
1593 return 0;
1594}
1595
1596static int cx8800_resume(struct pci_dev *pci_dev)
1597{
b45009b0 1598 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1da177e4 1599 struct cx88_core *core = dev->core;
5ddfbbb9 1600 unsigned long flags;
08adb9e2 1601 int err;
1da177e4
LT
1602
1603 if (dev->state.disabled) {
08adb9e2
MCC
1604 err=pci_enable_device(pci_dev);
1605 if (err) {
5772f813
TP
1606 printk(KERN_ERR "%s/0: can't enable device\n",
1607 core->name);
08adb9e2
MCC
1608 return err;
1609 }
1610
1da177e4
LT
1611 dev->state.disabled = 0;
1612 }
08adb9e2
MCC
1613 err= pci_set_power_state(pci_dev, PCI_D0);
1614 if (err) {
5772f813 1615 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
08adb9e2
MCC
1616 pci_disable_device(pci_dev);
1617 dev->state.disabled = 1;
1618
1619 return err;
1620 }
1da177e4
LT
1621 pci_restore_state(pci_dev);
1622
1da177e4 1623 /* FIXME: re-initialize hardware */
e52e98a7 1624 cx88_reset(core);
13595a51 1625 if (core->ir)
92f4fc10 1626 cx88_ir_start(core);
13595a51
MCC
1627
1628 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1da177e4
LT
1629
1630 /* restart video+vbi capture */
5ddfbbb9 1631 spin_lock_irqsave(&dev->slock, flags);
1da177e4 1632 if (!list_empty(&dev->vidq.active)) {
5772f813 1633 printk("%s/0: resume video\n", core->name);
1da177e4
LT
1634 restart_video_queue(dev,&dev->vidq);
1635 }
1636 if (!list_empty(&dev->vbiq.active)) {
5772f813 1637 printk("%s/0: resume vbi\n", core->name);
1da177e4
LT
1638 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1639 }
5ddfbbb9 1640 spin_unlock_irqrestore(&dev->slock, flags);
1da177e4
LT
1641
1642 return 0;
1643}
17bc98a4 1644#endif
1da177e4
LT
1645
1646/* ----------------------------------------------------------- */
1647
2e4e98e7 1648static const struct pci_device_id cx8800_pci_tbl[] = {
1da177e4
LT
1649 {
1650 .vendor = 0x14f1,
1651 .device = 0x8800,
b45009b0
MCC
1652 .subvendor = PCI_ANY_ID,
1653 .subdevice = PCI_ANY_ID,
1da177e4
LT
1654 },{
1655 /* --- end of list --- */
1656 }
1657};
1658MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
1659
1660static struct pci_driver cx8800_pci_driver = {
b45009b0
MCC
1661 .name = "cx8800",
1662 .id_table = cx8800_pci_tbl,
1663 .probe = cx8800_initdev,
4c62e976 1664 .remove = cx8800_finidev,
17bc98a4 1665#ifdef CONFIG_PM
1da177e4
LT
1666 .suspend = cx8800_suspend,
1667 .resume = cx8800_resume,
17bc98a4 1668#endif
1da177e4
LT
1669};
1670
06333e0a 1671module_pci_driver(cx8800_pci_driver);