1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for the Conexant CX23885 PCIe bridge
4 *
5 * Copyright (c) 2007 Steven Toth <[email protected]>
6 */
7
8 #include "cx23885.h"
9 #include "cx23885-video.h"
10
11 #include <linux/init.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kmod.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/kthread.h>
21 #include <asm/div64.h>
22
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-event.h>
26 #include "cx23885-ioctl.h"
27 #include "xc2028.h"
28
29 #include <media/drv-intf/cx25840.h>
30
31 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
32 MODULE_AUTHOR("Steven Toth <[email protected]>");
33 MODULE_LICENSE("GPL");
34
35 /* ------------------------------------------------------------------ */
36
37 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
38 static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
39
40 module_param_array(video_nr, int, NULL, 0444);
41 module_param_array(vbi_nr, int, NULL, 0444);
42
43 MODULE_PARM_DESC(video_nr, "video device numbers");
44 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
45
46 static unsigned int video_debug;
47 module_param(video_debug, int, 0644);
48 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
49
50 static unsigned int irq_debug;
51 module_param(irq_debug, int, 0644);
52 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
53
54 static unsigned int vid_limit = 16;
55 module_param(vid_limit, int, 0644);
56 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
57
58 #define dprintk(level, fmt, arg...)\
59 do { if (video_debug >= level)\
60 printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
61 __func__, ##arg); \
62 } while (0)
63
64 /* ------------------------------------------------------------------- */
65 /* static data */
66
67 #define FORMAT_FLAGS_PACKED 0x01
68 static struct cx23885_fmt formats[] = {
69 {
70 .fourcc = V4L2_PIX_FMT_YUYV,
71 .depth = 16,
72 .flags = FORMAT_FLAGS_PACKED,
73 }
74 };
75
format_by_fourcc(unsigned int fourcc)76 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
77 {
78 unsigned int i;
79
80 for (i = 0; i < ARRAY_SIZE(formats); i++)
81 if (formats[i].fourcc == fourcc)
82 return formats+i;
83 return NULL;
84 }
85
86 /* ------------------------------------------------------------------- */
87
cx23885_video_wakeup(struct cx23885_dev * dev,struct cx23885_dmaqueue * q,u32 count)88 void cx23885_video_wakeup(struct cx23885_dev *dev,
89 struct cx23885_dmaqueue *q, u32 count)
90 {
91 struct cx23885_buffer *buf;
92
93 if (list_empty(&q->active))
94 return;
95 buf = list_entry(q->active.next,
96 struct cx23885_buffer, queue);
97
98 buf->vb.sequence = q->count++;
99 buf->vb.vb2_buf.timestamp = ktime_get_ns();
100 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
101 buf->vb.vb2_buf.index, count, q->count);
102 list_del(&buf->queue);
103 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
104 }
105
cx23885_set_tvnorm(struct cx23885_dev * dev,v4l2_std_id norm)106 int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
107 {
108 struct v4l2_subdev_format format = {
109 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
110 .format.code = MEDIA_BUS_FMT_FIXED,
111 };
112
113 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
114 __func__,
115 (unsigned int)norm,
116 v4l2_norm_to_name(norm));
117
118 if (dev->tvnorm == norm)
119 return 0;
120
121 if (dev->tvnorm != norm) {
122 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
123 vb2_is_busy(&dev->vb2_mpegq))
124 return -EBUSY;
125 }
126
127 dev->tvnorm = norm;
128 dev->width = 720;
129 dev->height = norm_maxh(norm);
130 dev->field = V4L2_FIELD_INTERLACED;
131
132 call_all(dev, video, s_std, norm);
133
134 format.format.width = dev->width;
135 format.format.height = dev->height;
136 format.format.field = dev->field;
137 call_all(dev, pad, set_fmt, NULL, &format);
138
139 return 0;
140 }
141
cx23885_vdev_init(struct cx23885_dev * dev,struct pci_dev * pci,struct video_device * template,char * type)142 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
143 struct pci_dev *pci,
144 struct video_device *template,
145 char *type)
146 {
147 struct video_device *vfd;
148 dprintk(1, "%s()\n", __func__);
149
150 vfd = video_device_alloc();
151 if (NULL == vfd)
152 return NULL;
153 *vfd = *template;
154 vfd->v4l2_dev = &dev->v4l2_dev;
155 vfd->release = video_device_release;
156 vfd->lock = &dev->lock;
157 snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
158 cx23885_boards[dev->board].name, type);
159 video_set_drvdata(vfd, dev);
160 return vfd;
161 }
162
cx23885_flatiron_write(struct cx23885_dev * dev,u8 reg,u8 data)163 int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
164 {
165 /* 8 bit registers, 8 bit values */
166 u8 buf[] = { reg, data };
167
168 struct i2c_msg msg = { .addr = 0x98 >> 1,
169 .flags = 0, .buf = buf, .len = 2 };
170
171 return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
172 }
173
cx23885_flatiron_read(struct cx23885_dev * dev,u8 reg)174 u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
175 {
176 /* 8 bit registers, 8 bit values */
177 int ret;
178 u8 b0[] = { reg };
179 u8 b1[] = { 0 };
180
181 struct i2c_msg msg[] = {
182 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
183 { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
184 };
185
186 ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
187 if (ret != 2)
188 pr_err("%s() error\n", __func__);
189
190 return b1[0];
191 }
192
cx23885_flatiron_dump(struct cx23885_dev * dev)193 static void cx23885_flatiron_dump(struct cx23885_dev *dev)
194 {
195 int i;
196 dprintk(1, "Flatiron dump\n");
197 for (i = 0; i < 0x24; i++) {
198 dprintk(1, "FI[%02x] = %02x\n", i,
199 cx23885_flatiron_read(dev, i));
200 }
201 }
202
cx23885_flatiron_mux(struct cx23885_dev * dev,int input)203 static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
204 {
205 u8 val;
206 dprintk(1, "%s(input = %d)\n", __func__, input);
207
208 if (input == 1)
209 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
210 else if (input == 2)
211 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
212 else
213 return -EINVAL;
214
215 val |= 0x20; /* Enable clock to delta-sigma and dec filter */
216
217 cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
218
219 /* Wake up */
220 cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
221
222 if (video_debug)
223 cx23885_flatiron_dump(dev);
224
225 return 0;
226 }
227
cx23885_video_mux(struct cx23885_dev * dev,unsigned int input)228 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
229 {
230 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
231 __func__,
232 input, INPUT(input)->vmux,
233 INPUT(input)->gpio0, INPUT(input)->gpio1,
234 INPUT(input)->gpio2, INPUT(input)->gpio3);
235 dev->input = input;
236
237 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
238 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
239 dev->board == CX23885_BOARD_MYGICA_X8507) {
240 /* Select Analog TV */
241 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
242 cx23885_gpio_clear(dev, GPIO_0);
243 }
244
245 /* Tell the internal A/V decoder */
246 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
247 INPUT(input)->vmux, 0, 0);
248
249 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
250 (dev->board == CX23885_BOARD_MPX885) ||
251 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
252 (dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
253 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
254 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
255 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4) ||
256 (dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC) ||
257 (dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_DVB) ||
258 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
259 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR5525) ||
260 (dev->board == CX23885_BOARD_MYGICA_X8507) ||
261 (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
262 (dev->board == CX23885_BOARD_VIEWCAST_260E) ||
263 (dev->board == CX23885_BOARD_VIEWCAST_460E) ||
264 (dev->board == CX23885_BOARD_AVERMEDIA_CE310B)) {
265 /* Configure audio routing */
266 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
267 INPUT(input)->amux, 0, 0);
268
269 if (INPUT(input)->amux == CX25840_AUDIO7)
270 cx23885_flatiron_mux(dev, 1);
271 else if (INPUT(input)->amux == CX25840_AUDIO6)
272 cx23885_flatiron_mux(dev, 2);
273 }
274
275 return 0;
276 }
277
cx23885_audio_mux(struct cx23885_dev * dev,unsigned int input)278 static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
279 {
280 dprintk(1, "%s(input=%d)\n", __func__, input);
281
282 /* The baseband video core of the cx23885 has two audio inputs.
283 * LR1 and LR2. In almost every single case so far only HVR1xxx
284 * cards we've only ever supported LR1. Time to support LR2,
285 * which is available via the optional white breakout header on
286 * the board.
287 * We'll use a could of existing enums in the card struct to allow
288 * devs to specify which baseband input they need, or just default
289 * to what we've always used.
290 */
291 if (INPUT(input)->amux == CX25840_AUDIO7)
292 cx23885_flatiron_mux(dev, 1);
293 else if (INPUT(input)->amux == CX25840_AUDIO6)
294 cx23885_flatiron_mux(dev, 2);
295 else {
296 /* Not specifically defined, assume the default. */
297 cx23885_flatiron_mux(dev, 1);
298 }
299
300 return 0;
301 }
302
303 /* ------------------------------------------------------------------ */
cx23885_start_video_dma(struct cx23885_dev * dev,struct cx23885_dmaqueue * q,struct cx23885_buffer * buf)304 static int cx23885_start_video_dma(struct cx23885_dev *dev,
305 struct cx23885_dmaqueue *q,
306 struct cx23885_buffer *buf)
307 {
308 dprintk(1, "%s()\n", __func__);
309
310 /* Stop the dma/fifo before we tamper with it's risc programs */
311 cx_clear(VID_A_DMA_CTL, 0x11);
312
313 /* setup fifo + format */
314 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
315 buf->bpl, buf->risc.dma);
316
317 /* reset counter */
318 cx_write(VID_A_GPCNT_CTL, 3);
319 q->count = 0;
320
321 /* enable irq */
322 cx23885_irq_add_enable(dev, 0x01);
323 cx_set(VID_A_INT_MSK, 0x000011);
324
325 /* start dma */
326 cx_set(DEV_CNTRL2, (1<<5));
327 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
328
329 return 0;
330 }
331
queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])332 static int queue_setup(struct vb2_queue *q,
333 unsigned int *num_buffers, unsigned int *num_planes,
334 unsigned int sizes[], struct device *alloc_devs[])
335 {
336 struct cx23885_dev *dev = q->drv_priv;
337
338 *num_planes = 1;
339 sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
340 return 0;
341 }
342
buffer_prepare(struct vb2_buffer * vb)343 static int buffer_prepare(struct vb2_buffer *vb)
344 {
345 int ret;
346 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
347 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
348 struct cx23885_buffer *buf =
349 container_of(vbuf, struct cx23885_buffer, vb);
350 u32 line0_offset, line1_offset;
351 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
352 int field_tff;
353
354 buf->bpl = (dev->width * dev->fmt->depth) >> 3;
355
356 if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
357 return -EINVAL;
358 vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
359
360 switch (dev->field) {
361 case V4L2_FIELD_TOP:
362 ret = cx23885_risc_buffer(dev->pci, &buf->risc,
363 sgt->sgl, 0, UNSET,
364 buf->bpl, 0, dev->height);
365 break;
366 case V4L2_FIELD_BOTTOM:
367 ret = cx23885_risc_buffer(dev->pci, &buf->risc,
368 sgt->sgl, UNSET, 0,
369 buf->bpl, 0, dev->height);
370 break;
371 case V4L2_FIELD_INTERLACED:
372 if (dev->tvnorm & V4L2_STD_525_60)
373 /* NTSC or */
374 field_tff = 1;
375 else
376 field_tff = 0;
377
378 if (cx23885_boards[dev->board].force_bff)
379 /* PAL / SECAM OR 888 in NTSC MODE */
380 field_tff = 0;
381
382 if (field_tff) {
383 /* cx25840 transmits NTSC bottom field first */
384 dprintk(1, "%s() Creating TFF/NTSC risc\n",
385 __func__);
386 line0_offset = buf->bpl;
387 line1_offset = 0;
388 } else {
389 /* All other formats are top field first */
390 dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
391 __func__);
392 line0_offset = 0;
393 line1_offset = buf->bpl;
394 }
395 ret = cx23885_risc_buffer(dev->pci, &buf->risc,
396 sgt->sgl, line0_offset,
397 line1_offset,
398 buf->bpl, buf->bpl,
399 dev->height >> 1);
400 break;
401 case V4L2_FIELD_SEQ_TB:
402 ret = cx23885_risc_buffer(dev->pci, &buf->risc,
403 sgt->sgl,
404 0, buf->bpl * (dev->height >> 1),
405 buf->bpl, 0,
406 dev->height >> 1);
407 break;
408 case V4L2_FIELD_SEQ_BT:
409 ret = cx23885_risc_buffer(dev->pci, &buf->risc,
410 sgt->sgl,
411 buf->bpl * (dev->height >> 1), 0,
412 buf->bpl, 0,
413 dev->height >> 1);
414 break;
415 default:
416 return -EINVAL; /* should not happen */
417 }
418 dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp 0x%08x - dma=0x%08lx\n",
419 buf, buf->vb.vb2_buf.index,
420 dev->width, dev->height, dev->fmt->depth, dev->fmt->fourcc,
421 (unsigned long)buf->risc.dma);
422 return ret;
423 }
424
buffer_finish(struct vb2_buffer * vb)425 static void buffer_finish(struct vb2_buffer *vb)
426 {
427 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
428 struct cx23885_buffer *buf = container_of(vbuf,
429 struct cx23885_buffer, vb);
430
431 cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
432 }
433
434 /*
435 * The risc program for each buffer works as follows: it starts with a simple
436 * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
437 * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
438 * the initial JUMP).
439 *
440 * This is the risc program of the first buffer to be queued if the active list
441 * is empty and it just keeps DMAing this buffer without generating any
442 * interrupts.
443 *
444 * If a new buffer is added then the initial JUMP in the code for that buffer
445 * will generate an interrupt which signals that the previous buffer has been
446 * DMAed successfully and that it can be returned to userspace.
447 *
448 * It also sets the final jump of the previous buffer to the start of the new
449 * buffer, thus chaining the new buffer into the DMA chain. This is a single
450 * atomic u32 write, so there is no race condition.
451 *
452 * The end-result of all this that you only get an interrupt when a buffer
453 * is ready, so the control flow is very easy.
454 */
buffer_queue(struct vb2_buffer * vb)455 static void buffer_queue(struct vb2_buffer *vb)
456 {
457 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
458 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
459 struct cx23885_buffer *buf = container_of(vbuf,
460 struct cx23885_buffer, vb);
461 struct cx23885_buffer *prev;
462 struct cx23885_dmaqueue *q = &dev->vidq;
463 unsigned long flags;
464
465 /* add jump to start */
466 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
467 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
468 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
469 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
470
471 spin_lock_irqsave(&dev->slock, flags);
472 if (list_empty(&q->active)) {
473 list_add_tail(&buf->queue, &q->active);
474 dprintk(2, "[%p/%d] buffer_queue - first active\n",
475 buf, buf->vb.vb2_buf.index);
476 } else {
477 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
478 prev = list_entry(q->active.prev, struct cx23885_buffer,
479 queue);
480 list_add_tail(&buf->queue, &q->active);
481 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
482 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
483 buf, buf->vb.vb2_buf.index);
484 }
485 spin_unlock_irqrestore(&dev->slock, flags);
486 }
487
cx23885_start_streaming(struct vb2_queue * q,unsigned int count)488 static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
489 {
490 struct cx23885_dev *dev = q->drv_priv;
491 struct cx23885_dmaqueue *dmaq = &dev->vidq;
492 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
493 struct cx23885_buffer, queue);
494
495 cx23885_start_video_dma(dev, dmaq, buf);
496 return 0;
497 }
498
cx23885_stop_streaming(struct vb2_queue * q)499 static void cx23885_stop_streaming(struct vb2_queue *q)
500 {
501 struct cx23885_dev *dev = q->drv_priv;
502 struct cx23885_dmaqueue *dmaq = &dev->vidq;
503 unsigned long flags;
504
505 cx_clear(VID_A_DMA_CTL, 0x11);
506 spin_lock_irqsave(&dev->slock, flags);
507 while (!list_empty(&dmaq->active)) {
508 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
509 struct cx23885_buffer, queue);
510
511 list_del(&buf->queue);
512 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
513 }
514 spin_unlock_irqrestore(&dev->slock, flags);
515 }
516
517 static const struct vb2_ops cx23885_video_qops = {
518 .queue_setup = queue_setup,
519 .buf_prepare = buffer_prepare,
520 .buf_finish = buffer_finish,
521 .buf_queue = buffer_queue,
522 .start_streaming = cx23885_start_streaming,
523 .stop_streaming = cx23885_stop_streaming,
524 };
525
526 /* ------------------------------------------------------------------ */
527 /* VIDEO IOCTLS */
528
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)529 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
530 struct v4l2_format *f)
531 {
532 struct cx23885_dev *dev = video_drvdata(file);
533
534 f->fmt.pix.width = dev->width;
535 f->fmt.pix.height = dev->height;
536 f->fmt.pix.field = dev->field;
537 f->fmt.pix.pixelformat = dev->fmt->fourcc;
538 f->fmt.pix.bytesperline =
539 (f->fmt.pix.width * dev->fmt->depth) >> 3;
540 f->fmt.pix.sizeimage =
541 f->fmt.pix.height * f->fmt.pix.bytesperline;
542 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
543
544 return 0;
545 }
546
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)547 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
548 struct v4l2_format *f)
549 {
550 struct cx23885_dev *dev = video_drvdata(file);
551 struct cx23885_fmt *fmt;
552 enum v4l2_field field;
553 unsigned int maxw, maxh;
554
555 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
556 if (NULL == fmt)
557 return -EINVAL;
558
559 field = f->fmt.pix.field;
560 maxw = 720;
561 maxh = norm_maxh(dev->tvnorm);
562
563 if (V4L2_FIELD_ANY == field) {
564 field = (f->fmt.pix.height > maxh/2)
565 ? V4L2_FIELD_INTERLACED
566 : V4L2_FIELD_BOTTOM;
567 }
568
569 switch (field) {
570 case V4L2_FIELD_TOP:
571 case V4L2_FIELD_BOTTOM:
572 maxh = maxh / 2;
573 break;
574 case V4L2_FIELD_INTERLACED:
575 case V4L2_FIELD_SEQ_TB:
576 case V4L2_FIELD_SEQ_BT:
577 break;
578 default:
579 field = V4L2_FIELD_INTERLACED;
580 break;
581 }
582
583 f->fmt.pix.field = field;
584 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
585 &f->fmt.pix.height, 32, maxh, 0, 0);
586 f->fmt.pix.bytesperline =
587 (f->fmt.pix.width * fmt->depth) >> 3;
588 f->fmt.pix.sizeimage =
589 f->fmt.pix.height * f->fmt.pix.bytesperline;
590 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
591
592 return 0;
593 }
594
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)595 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
596 struct v4l2_format *f)
597 {
598 struct cx23885_dev *dev = video_drvdata(file);
599 struct v4l2_subdev_format format = {
600 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
601 };
602 int err;
603
604 dprintk(2, "%s()\n", __func__);
605 err = vidioc_try_fmt_vid_cap(file, priv, f);
606
607 if (0 != err)
608 return err;
609
610 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
611 vb2_is_busy(&dev->vb2_mpegq))
612 return -EBUSY;
613
614 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
615 dev->width = f->fmt.pix.width;
616 dev->height = f->fmt.pix.height;
617 dev->field = f->fmt.pix.field;
618 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
619 dev->width, dev->height, dev->field);
620 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
621 call_all(dev, pad, set_fmt, NULL, &format);
622 v4l2_fill_pix_format(&f->fmt.pix, &format.format);
623 /* set_fmt overwrites f->fmt.pix.field, restore it */
624 f->fmt.pix.field = dev->field;
625 return 0;
626 }
627
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)628 static int vidioc_querycap(struct file *file, void *priv,
629 struct v4l2_capability *cap)
630 {
631 struct cx23885_dev *dev = video_drvdata(file);
632
633 strscpy(cap->driver, "cx23885", sizeof(cap->driver));
634 strscpy(cap->card, cx23885_boards[dev->board].name,
635 sizeof(cap->card));
636 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
637 cap->capabilities = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
638 V4L2_CAP_AUDIO | V4L2_CAP_VBI_CAPTURE |
639 V4L2_CAP_VIDEO_CAPTURE |
640 V4L2_CAP_DEVICE_CAPS;
641 switch (dev->board) { /* i2c device tuners */
642 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
643 case CX23885_BOARD_HAUPPAUGE_HVR5525:
644 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
645 case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
646 cap->capabilities |= V4L2_CAP_TUNER;
647 break;
648 default:
649 if (dev->tuner_type != TUNER_ABSENT)
650 cap->capabilities |= V4L2_CAP_TUNER;
651 break;
652 }
653 return 0;
654 }
655
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)656 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
657 struct v4l2_fmtdesc *f)
658 {
659 if (unlikely(f->index >= ARRAY_SIZE(formats)))
660 return -EINVAL;
661
662 f->pixelformat = formats[f->index].fourcc;
663
664 return 0;
665 }
666
vidioc_g_pixelaspect(struct file * file,void * priv,int type,struct v4l2_fract * f)667 static int vidioc_g_pixelaspect(struct file *file, void *priv,
668 int type, struct v4l2_fract *f)
669 {
670 struct cx23885_dev *dev = video_drvdata(file);
671 bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
672
673 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
674 return -EINVAL;
675
676 f->numerator = is_50hz ? 54 : 11;
677 f->denominator = is_50hz ? 59 : 10;
678
679 return 0;
680 }
681
vidioc_g_selection(struct file * file,void * fh,struct v4l2_selection * sel)682 static int vidioc_g_selection(struct file *file, void *fh,
683 struct v4l2_selection *sel)
684 {
685 struct cx23885_dev *dev = video_drvdata(file);
686
687 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
688 return -EINVAL;
689
690 switch (sel->target) {
691 case V4L2_SEL_TGT_CROP_BOUNDS:
692 case V4L2_SEL_TGT_CROP_DEFAULT:
693 sel->r.top = 0;
694 sel->r.left = 0;
695 sel->r.width = 720;
696 sel->r.height = norm_maxh(dev->tvnorm);
697 break;
698 default:
699 return -EINVAL;
700 }
701 return 0;
702 }
703
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * id)704 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
705 {
706 struct cx23885_dev *dev = video_drvdata(file);
707 dprintk(1, "%s()\n", __func__);
708
709 *id = dev->tvnorm;
710 return 0;
711 }
712
vidioc_s_std(struct file * file,void * priv,v4l2_std_id tvnorms)713 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
714 {
715 struct cx23885_dev *dev = video_drvdata(file);
716 dprintk(1, "%s()\n", __func__);
717
718 return cx23885_set_tvnorm(dev, tvnorms);
719 }
720
cx23885_enum_input(struct cx23885_dev * dev,struct v4l2_input * i)721 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
722 {
723 static const char *iname[] = {
724 [CX23885_VMUX_COMPOSITE1] = "Composite1",
725 [CX23885_VMUX_COMPOSITE2] = "Composite2",
726 [CX23885_VMUX_COMPOSITE3] = "Composite3",
727 [CX23885_VMUX_COMPOSITE4] = "Composite4",
728 [CX23885_VMUX_SVIDEO] = "S-Video",
729 [CX23885_VMUX_COMPONENT] = "Component",
730 [CX23885_VMUX_TELEVISION] = "Television",
731 [CX23885_VMUX_CABLE] = "Cable TV",
732 [CX23885_VMUX_DVB] = "DVB",
733 [CX23885_VMUX_DEBUG] = "for debug only",
734 };
735 unsigned int n;
736 dprintk(1, "%s()\n", __func__);
737
738 n = i->index;
739 if (n >= MAX_CX23885_INPUT)
740 return -EINVAL;
741
742 if (0 == INPUT(n)->type)
743 return -EINVAL;
744
745 i->index = n;
746 i->type = V4L2_INPUT_TYPE_CAMERA;
747 strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
748 i->std = CX23885_NORMS;
749 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
750 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
751 i->type = V4L2_INPUT_TYPE_TUNER;
752 i->audioset = 4;
753 } else {
754 /* Two selectable audio inputs for non-tv inputs */
755 i->audioset = 3;
756 }
757
758 if (dev->input == n) {
759 /* enum'd input matches our configured input.
760 * Ask the video decoder to process the call
761 * and give it an oppertunity to update the
762 * status field.
763 */
764 call_all(dev, video, g_input_status, &i->status);
765 }
766
767 return 0;
768 }
769
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)770 static int vidioc_enum_input(struct file *file, void *priv,
771 struct v4l2_input *i)
772 {
773 struct cx23885_dev *dev = video_drvdata(file);
774 dprintk(1, "%s()\n", __func__);
775 return cx23885_enum_input(dev, i);
776 }
777
cx23885_get_input(struct file * file,void * priv,unsigned int * i)778 int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
779 {
780 struct cx23885_dev *dev = video_drvdata(file);
781
782 *i = dev->input;
783 dprintk(1, "%s() returns %d\n", __func__, *i);
784 return 0;
785 }
786
vidioc_g_input(struct file * file,void * priv,unsigned int * i)787 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
788 {
789 return cx23885_get_input(file, priv, i);
790 }
791
cx23885_set_input(struct file * file,void * priv,unsigned int i)792 int cx23885_set_input(struct file *file, void *priv, unsigned int i)
793 {
794 struct cx23885_dev *dev = video_drvdata(file);
795
796 dprintk(1, "%s(%d)\n", __func__, i);
797
798 if (i >= MAX_CX23885_INPUT) {
799 dprintk(1, "%s() -EINVAL\n", __func__);
800 return -EINVAL;
801 }
802
803 if (INPUT(i)->type == 0)
804 return -EINVAL;
805
806 cx23885_video_mux(dev, i);
807
808 /* By default establish the default audio input for the card also */
809 /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
810 cx23885_audio_mux(dev, i);
811 return 0;
812 }
813
vidioc_s_input(struct file * file,void * priv,unsigned int i)814 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
815 {
816 return cx23885_set_input(file, priv, i);
817 }
818
vidioc_log_status(struct file * file,void * priv)819 static int vidioc_log_status(struct file *file, void *priv)
820 {
821 struct cx23885_dev *dev = video_drvdata(file);
822
823 call_all(dev, core, log_status);
824 return 0;
825 }
826
cx23885_query_audinput(struct file * file,void * priv,struct v4l2_audio * i)827 static int cx23885_query_audinput(struct file *file, void *priv,
828 struct v4l2_audio *i)
829 {
830 static const char *iname[] = {
831 [0] = "Baseband L/R 1",
832 [1] = "Baseband L/R 2",
833 [2] = "TV",
834 };
835 unsigned int n;
836 dprintk(1, "%s()\n", __func__);
837
838 n = i->index;
839 if (n >= 3)
840 return -EINVAL;
841
842 memset(i, 0, sizeof(*i));
843 i->index = n;
844 strscpy(i->name, iname[n], sizeof(i->name));
845 i->capability = V4L2_AUDCAP_STEREO;
846 return 0;
847
848 }
849
vidioc_enum_audinput(struct file * file,void * priv,struct v4l2_audio * i)850 static int vidioc_enum_audinput(struct file *file, void *priv,
851 struct v4l2_audio *i)
852 {
853 return cx23885_query_audinput(file, priv, i);
854 }
855
vidioc_g_audinput(struct file * file,void * priv,struct v4l2_audio * i)856 static int vidioc_g_audinput(struct file *file, void *priv,
857 struct v4l2_audio *i)
858 {
859 struct cx23885_dev *dev = video_drvdata(file);
860
861 if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
862 (CX23885_VMUX_CABLE == INPUT(dev->input)->type))
863 i->index = 2;
864 else
865 i->index = dev->audinput;
866 dprintk(1, "%s(input=%d)\n", __func__, i->index);
867
868 return cx23885_query_audinput(file, priv, i);
869 }
870
vidioc_s_audinput(struct file * file,void * priv,const struct v4l2_audio * i)871 static int vidioc_s_audinput(struct file *file, void *priv,
872 const struct v4l2_audio *i)
873 {
874 struct cx23885_dev *dev = video_drvdata(file);
875
876 if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
877 (CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
878 return i->index != 2 ? -EINVAL : 0;
879 }
880 if (i->index > 1)
881 return -EINVAL;
882
883 dprintk(1, "%s(%d)\n", __func__, i->index);
884
885 dev->audinput = i->index;
886
887 /* Skip the audio defaults from the cards struct, caller wants
888 * directly touch the audio mux hardware. */
889 cx23885_flatiron_mux(dev, dev->audinput + 1);
890 return 0;
891 }
892
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)893 static int vidioc_g_tuner(struct file *file, void *priv,
894 struct v4l2_tuner *t)
895 {
896 struct cx23885_dev *dev = video_drvdata(file);
897
898 switch (dev->board) { /* i2c device tuners */
899 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
900 case CX23885_BOARD_HAUPPAUGE_HVR5525:
901 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
902 case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
903 break;
904 default:
905 if (dev->tuner_type == TUNER_ABSENT)
906 return -EINVAL;
907 break;
908 }
909 if (0 != t->index)
910 return -EINVAL;
911
912 strscpy(t->name, "Television", sizeof(t->name));
913
914 call_all(dev, tuner, g_tuner, t);
915 return 0;
916 }
917
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)918 static int vidioc_s_tuner(struct file *file, void *priv,
919 const struct v4l2_tuner *t)
920 {
921 struct cx23885_dev *dev = video_drvdata(file);
922
923 switch (dev->board) { /* i2c device tuners */
924 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
925 case CX23885_BOARD_HAUPPAUGE_HVR5525:
926 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
927 case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
928 break;
929 default:
930 if (dev->tuner_type == TUNER_ABSENT)
931 return -EINVAL;
932 break;
933 }
934 if (0 != t->index)
935 return -EINVAL;
936 /* Update the A/V core */
937 call_all(dev, tuner, s_tuner, t);
938
939 return 0;
940 }
941
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)942 static int vidioc_g_frequency(struct file *file, void *priv,
943 struct v4l2_frequency *f)
944 {
945 struct cx23885_dev *dev = video_drvdata(file);
946
947 switch (dev->board) { /* i2c device tuners */
948 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
949 case CX23885_BOARD_HAUPPAUGE_HVR5525:
950 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
951 case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
952 break;
953 default:
954 if (dev->tuner_type == TUNER_ABSENT)
955 return -EINVAL;
956 break;
957 }
958 f->type = V4L2_TUNER_ANALOG_TV;
959 f->frequency = dev->freq;
960
961 call_all(dev, tuner, g_frequency, f);
962
963 return 0;
964 }
965
cx23885_set_freq(struct cx23885_dev * dev,const struct v4l2_frequency * f)966 static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
967 {
968 struct v4l2_ctrl *mute;
969 int old_mute_val = 1;
970
971 switch (dev->board) { /* i2c device tuners */
972 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
973 case CX23885_BOARD_HAUPPAUGE_HVR5525:
974 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
975 case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
976 break;
977 default:
978 if (dev->tuner_type == TUNER_ABSENT)
979 return -EINVAL;
980 break;
981 }
982 if (unlikely(f->tuner != 0))
983 return -EINVAL;
984
985 dev->freq = f->frequency;
986
987 /* I need to mute audio here */
988 mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
989 if (mute) {
990 old_mute_val = v4l2_ctrl_g_ctrl(mute);
991 if (!old_mute_val)
992 v4l2_ctrl_s_ctrl(mute, 1);
993 }
994
995 call_all(dev, tuner, s_frequency, f);
996
997 /* When changing channels it is required to reset TVAUDIO */
998 msleep(100);
999
1000 /* I need to unmute audio here */
1001 if (old_mute_val == 0)
1002 v4l2_ctrl_s_ctrl(mute, old_mute_val);
1003
1004 return 0;
1005 }
1006
cx23885_set_freq_via_ops(struct cx23885_dev * dev,const struct v4l2_frequency * f)1007 static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
1008 const struct v4l2_frequency *f)
1009 {
1010 struct v4l2_ctrl *mute;
1011 int old_mute_val = 1;
1012 struct vb2_dvb_frontend *vfe;
1013 struct dvb_frontend *fe;
1014
1015 struct analog_parameters params = {
1016 .mode = V4L2_TUNER_ANALOG_TV,
1017 .audmode = V4L2_TUNER_MODE_STEREO,
1018 .std = dev->tvnorm,
1019 .frequency = f->frequency
1020 };
1021
1022 dev->freq = f->frequency;
1023
1024 /* I need to mute audio here */
1025 mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1026 if (mute) {
1027 old_mute_val = v4l2_ctrl_g_ctrl(mute);
1028 if (!old_mute_val)
1029 v4l2_ctrl_s_ctrl(mute, 1);
1030 }
1031
1032 /* If HVR1850 */
1033 dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1034 params.frequency, f->tuner, params.std);
1035
1036 vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
1037 if (!vfe) {
1038 return -EINVAL;
1039 }
1040
1041 fe = vfe->dvb.frontend;
1042
1043 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
1044 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
1045 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
1046 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4) ||
1047 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR5525) ||
1048 (dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_DVB) ||
1049 (dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC))
1050 fe = &dev->ts1.analog_fe;
1051
1052 if (fe && fe->ops.tuner_ops.set_analog_params) {
1053 call_all(dev, video, s_std, dev->tvnorm);
1054 fe->ops.tuner_ops.set_analog_params(fe, ¶ms);
1055 }
1056 else
1057 pr_err("%s() No analog tuner, aborting\n", __func__);
1058
1059 /* When changing channels it is required to reset TVAUDIO */
1060 msleep(100);
1061
1062 /* I need to unmute audio here */
1063 if (old_mute_val == 0)
1064 v4l2_ctrl_s_ctrl(mute, old_mute_val);
1065
1066 return 0;
1067 }
1068
cx23885_set_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)1069 int cx23885_set_frequency(struct file *file, void *priv,
1070 const struct v4l2_frequency *f)
1071 {
1072 struct cx23885_dev *dev = video_drvdata(file);
1073 int ret;
1074
1075 switch (dev->board) {
1076 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1077 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1078 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1079 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1080 case CX23885_BOARD_HAUPPAUGE_HVR5525:
1081 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1082 case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1083 ret = cx23885_set_freq_via_ops(dev, f);
1084 break;
1085 default:
1086 ret = cx23885_set_freq(dev, f);
1087 }
1088
1089 return ret;
1090 }
1091
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)1092 static int vidioc_s_frequency(struct file *file, void *priv,
1093 const struct v4l2_frequency *f)
1094 {
1095 return cx23885_set_frequency(file, priv, f);
1096 }
1097
1098 /* ----------------------------------------------------------- */
1099
cx23885_video_irq(struct cx23885_dev * dev,u32 status)1100 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1101 {
1102 u32 mask, count;
1103 int handled = 0;
1104
1105 mask = cx_read(VID_A_INT_MSK);
1106 if (0 == (status & mask))
1107 return handled;
1108
1109 cx_write(VID_A_INT_STAT, status);
1110
1111 /* risc op code error, fifo overflow or line sync detection error */
1112 if ((status & VID_BC_MSK_OPC_ERR) ||
1113 (status & VID_BC_MSK_SYNC) ||
1114 (status & VID_BC_MSK_OF)) {
1115
1116 if (status & VID_BC_MSK_OPC_ERR) {
1117 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1118 VID_BC_MSK_OPC_ERR);
1119 pr_warn("%s: video risc op code error\n",
1120 dev->name);
1121 cx23885_sram_channel_dump(dev,
1122 &dev->sram_channels[SRAM_CH01]);
1123 }
1124
1125 if (status & VID_BC_MSK_SYNC)
1126 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) video lines miss-match\n",
1127 VID_BC_MSK_SYNC);
1128
1129 if (status & VID_BC_MSK_OF)
1130 dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1131 VID_BC_MSK_OF);
1132
1133 }
1134
1135 /* Video */
1136 if (status & VID_BC_MSK_RISCI1) {
1137 spin_lock(&dev->slock);
1138 count = cx_read(VID_A_GPCNT);
1139 cx23885_video_wakeup(dev, &dev->vidq, count);
1140 spin_unlock(&dev->slock);
1141 handled++;
1142 }
1143
1144 /* Allow the VBI framework to process it's payload */
1145 handled += cx23885_vbi_irq(dev, status);
1146
1147 return handled;
1148 }
1149
1150 /* ----------------------------------------------------------- */
1151 /* exported stuff */
1152
1153 static const struct v4l2_file_operations video_fops = {
1154 .owner = THIS_MODULE,
1155 .open = v4l2_fh_open,
1156 .release = vb2_fop_release,
1157 .read = vb2_fop_read,
1158 .poll = vb2_fop_poll,
1159 .unlocked_ioctl = video_ioctl2,
1160 .mmap = vb2_fop_mmap,
1161 };
1162
1163 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1164 .vidioc_querycap = vidioc_querycap,
1165 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1166 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1167 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1168 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1169 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1170 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1171 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
1172 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1173 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1174 .vidioc_querybuf = vb2_ioctl_querybuf,
1175 .vidioc_qbuf = vb2_ioctl_qbuf,
1176 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1177 .vidioc_streamon = vb2_ioctl_streamon,
1178 .vidioc_streamoff = vb2_ioctl_streamoff,
1179 .vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1180 .vidioc_g_selection = vidioc_g_selection,
1181 .vidioc_s_std = vidioc_s_std,
1182 .vidioc_g_std = vidioc_g_std,
1183 .vidioc_enum_input = vidioc_enum_input,
1184 .vidioc_g_input = vidioc_g_input,
1185 .vidioc_s_input = vidioc_s_input,
1186 .vidioc_log_status = vidioc_log_status,
1187 .vidioc_g_tuner = vidioc_g_tuner,
1188 .vidioc_s_tuner = vidioc_s_tuner,
1189 .vidioc_g_frequency = vidioc_g_frequency,
1190 .vidioc_s_frequency = vidioc_s_frequency,
1191 #ifdef CONFIG_VIDEO_ADV_DEBUG
1192 .vidioc_g_chip_info = cx23885_g_chip_info,
1193 .vidioc_g_register = cx23885_g_register,
1194 .vidioc_s_register = cx23885_s_register,
1195 #endif
1196 .vidioc_enumaudio = vidioc_enum_audinput,
1197 .vidioc_g_audio = vidioc_g_audinput,
1198 .vidioc_s_audio = vidioc_s_audinput,
1199 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1200 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1201 };
1202
1203 static struct video_device cx23885_vbi_template;
1204 static struct video_device cx23885_video_template = {
1205 .name = "cx23885-video",
1206 .fops = &video_fops,
1207 .ioctl_ops = &video_ioctl_ops,
1208 .tvnorms = CX23885_NORMS,
1209 };
1210
cx23885_video_unregister(struct cx23885_dev * dev)1211 void cx23885_video_unregister(struct cx23885_dev *dev)
1212 {
1213 dprintk(1, "%s()\n", __func__);
1214 cx23885_irq_remove(dev, 0x01);
1215
1216 if (dev->vbi_dev) {
1217 if (video_is_registered(dev->vbi_dev))
1218 video_unregister_device(dev->vbi_dev);
1219 else
1220 video_device_release(dev->vbi_dev);
1221 dev->vbi_dev = NULL;
1222 }
1223 if (dev->video_dev) {
1224 if (video_is_registered(dev->video_dev))
1225 video_unregister_device(dev->video_dev);
1226 else
1227 video_device_release(dev->video_dev);
1228 dev->video_dev = NULL;
1229 }
1230
1231 if (dev->audio_dev)
1232 cx23885_audio_unregister(dev);
1233 }
1234
cx23885_video_register(struct cx23885_dev * dev)1235 int cx23885_video_register(struct cx23885_dev *dev)
1236 {
1237 struct vb2_queue *q;
1238 int err;
1239
1240 dprintk(1, "%s()\n", __func__);
1241
1242 /* Initialize VBI template */
1243 cx23885_vbi_template = cx23885_video_template;
1244 strscpy(cx23885_vbi_template.name, "cx23885-vbi",
1245 sizeof(cx23885_vbi_template.name));
1246
1247 dev->tvnorm = V4L2_STD_NTSC_M;
1248 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
1249 dev->field = V4L2_FIELD_INTERLACED;
1250 dev->width = 720;
1251 dev->height = norm_maxh(dev->tvnorm);
1252
1253 /* init video dma queues */
1254 INIT_LIST_HEAD(&dev->vidq.active);
1255
1256 /* init vbi dma queues */
1257 INIT_LIST_HEAD(&dev->vbiq.active);
1258
1259 cx23885_irq_add_enable(dev, 0x01);
1260
1261 if ((TUNER_ABSENT != dev->tuner_type) &&
1262 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1263 struct v4l2_subdev *sd = NULL;
1264
1265 if (dev->tuner_addr)
1266 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1267 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1268 "tuner", dev->tuner_addr, NULL);
1269 else
1270 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1271 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1272 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1273 if (sd) {
1274 struct tuner_setup tun_setup;
1275
1276 memset(&tun_setup, 0, sizeof(tun_setup));
1277 tun_setup.mode_mask = T_ANALOG_TV;
1278 tun_setup.type = dev->tuner_type;
1279 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1280 tun_setup.tuner_callback = cx23885_tuner_callback;
1281
1282 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1283
1284 if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1285 (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1286 struct xc2028_ctrl ctrl = {
1287 .fname = XC2028_DEFAULT_FIRMWARE,
1288 .max_len = 64
1289 };
1290 struct v4l2_priv_tun_config cfg = {
1291 .tuner = dev->tuner_type,
1292 .priv = &ctrl
1293 };
1294 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1295 }
1296
1297 if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1298 struct xc2028_ctrl ctrl = {
1299 .fname = "xc3028L-v36.fw",
1300 .max_len = 64
1301 };
1302 struct v4l2_priv_tun_config cfg = {
1303 .tuner = dev->tuner_type,
1304 .priv = &ctrl
1305 };
1306 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1307 }
1308 }
1309 }
1310
1311 /* initial device configuration */
1312 mutex_lock(&dev->lock);
1313 cx23885_set_tvnorm(dev, dev->tvnorm);
1314 cx23885_video_mux(dev, 0);
1315 cx23885_audio_mux(dev, 0);
1316 mutex_unlock(&dev->lock);
1317
1318 q = &dev->vb2_vidq;
1319 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1320 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1321 q->gfp_flags = GFP_DMA32;
1322 q->min_queued_buffers = 2;
1323 q->drv_priv = dev;
1324 q->buf_struct_size = sizeof(struct cx23885_buffer);
1325 q->ops = &cx23885_video_qops;
1326 q->mem_ops = &vb2_dma_sg_memops;
1327 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1328 q->lock = &dev->lock;
1329 q->dev = &dev->pci->dev;
1330
1331 err = vb2_queue_init(q);
1332 if (err < 0)
1333 goto fail_unreg;
1334
1335 q = &dev->vb2_vbiq;
1336 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1337 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1338 q->gfp_flags = GFP_DMA32;
1339 q->min_queued_buffers = 2;
1340 q->drv_priv = dev;
1341 q->buf_struct_size = sizeof(struct cx23885_buffer);
1342 q->ops = &cx23885_vbi_qops;
1343 q->mem_ops = &vb2_dma_sg_memops;
1344 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1345 q->lock = &dev->lock;
1346 q->dev = &dev->pci->dev;
1347
1348 err = vb2_queue_init(q);
1349 if (err < 0)
1350 goto fail_unreg;
1351
1352 /* register Video device */
1353 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1354 &cx23885_video_template, "video");
1355 if (!dev->video_dev) {
1356 err = -ENOMEM;
1357 goto fail_unreg;
1358 }
1359 dev->video_dev->queue = &dev->vb2_vidq;
1360 dev->video_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1361 V4L2_CAP_AUDIO | V4L2_CAP_VIDEO_CAPTURE;
1362 switch (dev->board) { /* i2c device tuners */
1363 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1364 case CX23885_BOARD_HAUPPAUGE_HVR5525:
1365 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1366 case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1367 dev->video_dev->device_caps |= V4L2_CAP_TUNER;
1368 break;
1369 default:
1370 if (dev->tuner_type != TUNER_ABSENT)
1371 dev->video_dev->device_caps |= V4L2_CAP_TUNER;
1372 }
1373
1374 err = video_register_device(dev->video_dev, VFL_TYPE_VIDEO,
1375 video_nr[dev->nr]);
1376 if (err < 0) {
1377 pr_info("%s: can't register video device\n",
1378 dev->name);
1379 goto fail_unreg;
1380 }
1381 pr_info("%s: registered device %s [v4l2]\n",
1382 dev->name, video_device_node_name(dev->video_dev));
1383
1384 /* register VBI device */
1385 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1386 &cx23885_vbi_template, "vbi");
1387 if (!dev->vbi_dev) {
1388 err = -ENOMEM;
1389 goto fail_unreg;
1390 }
1391 dev->vbi_dev->queue = &dev->vb2_vbiq;
1392 dev->vbi_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1393 V4L2_CAP_AUDIO | V4L2_CAP_VBI_CAPTURE;
1394 switch (dev->board) { /* i2c device tuners */
1395 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1396 case CX23885_BOARD_HAUPPAUGE_HVR5525:
1397 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1398 case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1399 dev->vbi_dev->device_caps |= V4L2_CAP_TUNER;
1400 break;
1401 default:
1402 if (dev->tuner_type != TUNER_ABSENT)
1403 dev->vbi_dev->device_caps |= V4L2_CAP_TUNER;
1404 }
1405 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1406 vbi_nr[dev->nr]);
1407 if (err < 0) {
1408 pr_info("%s: can't register vbi device\n",
1409 dev->name);
1410 goto fail_unreg;
1411 }
1412 pr_info("%s: registered device %s\n",
1413 dev->name, video_device_node_name(dev->vbi_dev));
1414
1415 /* Register ALSA audio device */
1416 dev->audio_dev = cx23885_audio_register(dev);
1417
1418 return 0;
1419
1420 fail_unreg:
1421 cx23885_video_unregister(dev);
1422 return err;
1423 }
1424