blob: 3f1d07e6490a5873e2f46724ffc5a5441da0dee2 [file] [log] [blame]
Steven Tothe47f30b2008-01-10 04:25:59 -03001/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
Steven Tothe47f30b2008-01-10 04:25:59 -03005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/init.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kmod.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040029#include <linux/smp_lock.h>
Steven Tothe47f30b2008-01-10 04:25:59 -030030#include <linux/interrupt.h>
31#include <linux/delay.h>
32#include <linux/kthread.h>
33#include <asm/div64.h>
34
35#include "cx23885.h"
36#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030037#include <media/v4l2-ioctl.h>
Andy Walls74618242009-09-26 22:50:44 -030038#include "cx23885-ioctl.h"
Steven Tothe47f30b2008-01-10 04:25:59 -030039
Steven Tothe47f30b2008-01-10 04:25:59 -030040MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
Steven Toth6d897612008-09-03 17:12:12 -030041MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Tothe47f30b2008-01-10 04:25:59 -030042MODULE_LICENSE("GPL");
43
44/* ------------------------------------------------------------------ */
45
46static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
47static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
48static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49
50module_param_array(video_nr, int, NULL, 0444);
51module_param_array(vbi_nr, int, NULL, 0444);
52module_param_array(radio_nr, int, NULL, 0444);
53
54MODULE_PARM_DESC(video_nr, "video device numbers");
55MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
56MODULE_PARM_DESC(radio_nr, "radio device numbers");
57
Steven Toth4513fc692008-01-12 11:36:36 -030058static unsigned int video_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030059module_param(video_debug, int, 0644);
60MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
61
Steven Toth4513fc692008-01-12 11:36:36 -030062static unsigned int irq_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030063module_param(irq_debug, int, 0644);
64MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
65
66static unsigned int vid_limit = 16;
67module_param(vid_limit, int, 0644);
68MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
69
70#define dprintk(level, fmt, arg...)\
Steven Toth4513fc692008-01-12 11:36:36 -030071 do { if (video_debug >= level)\
72 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
73 } while (0)
Steven Tothe47f30b2008-01-10 04:25:59 -030074
75/* ------------------------------------------------------------------- */
76/* static data */
77
78#define FORMAT_FLAGS_PACKED 0x01
79
80static struct cx23885_fmt formats[] = {
81 {
82 .name = "8 bpp, gray",
83 .fourcc = V4L2_PIX_FMT_GREY,
84 .depth = 8,
85 .flags = FORMAT_FLAGS_PACKED,
86 }, {
87 .name = "15 bpp RGB, le",
88 .fourcc = V4L2_PIX_FMT_RGB555,
89 .depth = 16,
90 .flags = FORMAT_FLAGS_PACKED,
91 }, {
92 .name = "15 bpp RGB, be",
93 .fourcc = V4L2_PIX_FMT_RGB555X,
94 .depth = 16,
95 .flags = FORMAT_FLAGS_PACKED,
96 }, {
97 .name = "16 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_RGB565,
99 .depth = 16,
100 .flags = FORMAT_FLAGS_PACKED,
101 }, {
102 .name = "16 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB565X,
104 .depth = 16,
105 .flags = FORMAT_FLAGS_PACKED,
106 }, {
107 .name = "24 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_BGR24,
109 .depth = 24,
110 .flags = FORMAT_FLAGS_PACKED,
111 }, {
112 .name = "32 bpp RGB, le",
113 .fourcc = V4L2_PIX_FMT_BGR32,
114 .depth = 32,
115 .flags = FORMAT_FLAGS_PACKED,
116 }, {
117 .name = "32 bpp RGB, be",
118 .fourcc = V4L2_PIX_FMT_RGB32,
119 .depth = 32,
120 .flags = FORMAT_FLAGS_PACKED,
121 }, {
122 .name = "4:2:2, packed, YUYV",
123 .fourcc = V4L2_PIX_FMT_YUYV,
124 .depth = 16,
125 .flags = FORMAT_FLAGS_PACKED,
126 }, {
127 .name = "4:2:2, packed, UYVY",
128 .fourcc = V4L2_PIX_FMT_UYVY,
129 .depth = 16,
130 .flags = FORMAT_FLAGS_PACKED,
131 },
132};
133
134static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
135{
136 unsigned int i;
137
138 for (i = 0; i < ARRAY_SIZE(formats); i++)
139 if (formats[i].fourcc == fourcc)
140 return formats+i;
141
Harvey Harrison22b4e642008-04-08 23:20:00 -0300142 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300143 return NULL;
144}
145
146/* ------------------------------------------------------------------- */
147
148static const struct v4l2_queryctrl no_ctl = {
149 .name = "42",
150 .flags = V4L2_CTRL_FLAG_DISABLED,
151};
152
153static struct cx23885_ctrl cx23885_ctls[] = {
154 /* --- video --- */
155 {
156 .v = {
157 .id = V4L2_CID_BRIGHTNESS,
158 .name = "Brightness",
159 .minimum = 0x00,
160 .maximum = 0xff,
161 .step = 1,
162 .default_value = 0x7f,
163 .type = V4L2_CTRL_TYPE_INTEGER,
164 },
165 .off = 128,
166 .reg = LUMA_CTRL,
167 .mask = 0x00ff,
168 .shift = 0,
169 }, {
170 .v = {
171 .id = V4L2_CID_CONTRAST,
172 .name = "Contrast",
173 .minimum = 0,
174 .maximum = 0xff,
175 .step = 1,
176 .default_value = 0x3f,
177 .type = V4L2_CTRL_TYPE_INTEGER,
178 },
179 .off = 0,
180 .reg = LUMA_CTRL,
181 .mask = 0xff00,
182 .shift = 8,
183 }, {
184 .v = {
185 .id = V4L2_CID_HUE,
186 .name = "Hue",
187 .minimum = 0,
188 .maximum = 0xff,
189 .step = 1,
190 .default_value = 0x7f,
191 .type = V4L2_CTRL_TYPE_INTEGER,
192 },
193 .off = 128,
194 .reg = CHROMA_CTRL,
195 .mask = 0xff0000,
196 .shift = 16,
197 }, {
198 /* strictly, this only describes only U saturation.
199 * V saturation is handled specially through code.
200 */
201 .v = {
202 .id = V4L2_CID_SATURATION,
203 .name = "Saturation",
204 .minimum = 0,
205 .maximum = 0xff,
206 .step = 1,
207 .default_value = 0x7f,
208 .type = V4L2_CTRL_TYPE_INTEGER,
209 },
210 .off = 0,
211 .reg = CHROMA_CTRL,
212 .mask = 0x00ff,
213 .shift = 0,
214 }, {
215 /* --- audio --- */
216 .v = {
217 .id = V4L2_CID_AUDIO_MUTE,
218 .name = "Mute",
219 .minimum = 0,
220 .maximum = 1,
221 .default_value = 1,
222 .type = V4L2_CTRL_TYPE_BOOLEAN,
223 },
224 .reg = PATH1_CTL1,
225 .mask = (0x1f << 24),
226 .shift = 24,
227 }, {
228 .v = {
229 .id = V4L2_CID_AUDIO_VOLUME,
230 .name = "Volume",
231 .minimum = 0,
232 .maximum = 0x3f,
233 .step = 1,
234 .default_value = 0x3f,
235 .type = V4L2_CTRL_TYPE_INTEGER,
236 },
237 .reg = PATH1_VOL_CTL,
238 .mask = 0xff,
239 .shift = 0,
240 }
241};
242static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
243
Hans Verkuil2ba58892009-02-13 10:57:48 -0300244/* Must be sorted from low to high control ID! */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300245static const u32 cx23885_user_ctrls[] = {
Steven Tothe47f30b2008-01-10 04:25:59 -0300246 V4L2_CID_USER_CLASS,
247 V4L2_CID_BRIGHTNESS,
248 V4L2_CID_CONTRAST,
249 V4L2_CID_SATURATION,
250 V4L2_CID_HUE,
251 V4L2_CID_AUDIO_VOLUME,
252 V4L2_CID_AUDIO_MUTE,
253 0
254};
Steven Tothe47f30b2008-01-10 04:25:59 -0300255
256static const u32 *ctrl_classes[] = {
257 cx23885_user_ctrls,
258 NULL
259};
260
Hans Verkuild45b9b82008-09-04 03:33:43 -0300261static void cx23885_video_wakeup(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300262 struct cx23885_dmaqueue *q, u32 count)
263{
264 struct cx23885_buffer *buf;
265 int bc;
266
267 for (bc = 0;; bc++) {
268 if (list_empty(&q->active))
269 break;
270 buf = list_entry(q->active.next,
271 struct cx23885_buffer, vb.queue);
Steven Totha19602f22008-01-10 11:43:18 -0300272
Steven Tothe47f30b2008-01-10 04:25:59 -0300273 /* count comes from the hw and is is 16bit wide --
274 * this trick handles wrap-arounds correctly for
275 * up to 32767 buffers in flight... */
276 if ((s16) (count - buf->count) < 0)
277 break;
Steven Totha19602f22008-01-10 11:43:18 -0300278
Steven Tothe47f30b2008-01-10 04:25:59 -0300279 do_gettimeofday(&buf->vb.ts);
280 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
281 count, buf->count);
282 buf->vb.state = VIDEOBUF_DONE;
283 list_del(&buf->vb.queue);
284 wake_up(&buf->vb.done);
285 }
Steven Toth9c8ced52008-10-16 20:18:44 -0300286 if (list_empty(&q->active))
Steven Tothe47f30b2008-01-10 04:25:59 -0300287 del_timer(&q->timeout);
Steven Toth9c8ced52008-10-16 20:18:44 -0300288 else
Steven Tothe47f30b2008-01-10 04:25:59 -0300289 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
Steven Tothe47f30b2008-01-10 04:25:59 -0300290 if (bc != 1)
Steven Toth21a78092008-01-10 04:38:59 -0300291 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300292 __func__, bc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300293}
294
Hans Verkuild45b9b82008-09-04 03:33:43 -0300295static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
Steven Tothe47f30b2008-01-10 04:25:59 -0300296{
297 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300298 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300299 (unsigned int)norm,
300 v4l2_norm_to_name(norm));
301
302 dev->tvnorm = norm;
303
Hans Verkuilf41737e2009-04-01 03:52:39 -0300304 call_all(dev, core, s_std, norm);
Steven Tothe47f30b2008-01-10 04:25:59 -0300305
306 return 0;
307}
308
Hans Verkuild45b9b82008-09-04 03:33:43 -0300309static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300310 struct pci_dev *pci,
311 struct video_device *template,
312 char *type)
313{
314 struct video_device *vfd;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300315 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300316
317 vfd = video_device_alloc();
318 if (NULL == vfd)
319 return NULL;
320 *vfd = *template;
Hans Verkuilc0714f62009-03-13 08:02:43 -0300321 vfd->minor = -1;
322 vfd->v4l2_dev = &dev->v4l2_dev;
Steven Tothe47f30b2008-01-10 04:25:59 -0300323 vfd->release = video_device_release;
324 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
325 dev->name, type, cx23885_boards[dev->board].name);
326 return vfd;
327}
328
Hans Verkuild45b9b82008-09-04 03:33:43 -0300329static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300330{
331 int i;
332
333 if (qctrl->id < V4L2_CID_BASE ||
334 qctrl->id >= V4L2_CID_LASTP1)
335 return -EINVAL;
336 for (i = 0; i < CX23885_CTLS; i++)
337 if (cx23885_ctls[i].v.id == qctrl->id)
338 break;
339 if (i == CX23885_CTLS) {
340 *qctrl = no_ctl;
341 return 0;
342 }
343 *qctrl = cx23885_ctls[i].v;
344 return 0;
345}
Steven Tothe47f30b2008-01-10 04:25:59 -0300346
347/* ------------------------------------------------------------------- */
348/* resource management */
349
350static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
351 unsigned int bit)
352{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300353 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300354 if (fh->resources & bit)
355 /* have it already allocated */
356 return 1;
357
358 /* is it free? */
359 mutex_lock(&dev->lock);
360 if (dev->resources & bit) {
361 /* no, someone else uses it */
362 mutex_unlock(&dev->lock);
363 return 0;
364 }
365 /* it's free, grab it */
366 fh->resources |= bit;
367 dev->resources |= bit;
368 dprintk(1, "res: get %d\n", bit);
369 mutex_unlock(&dev->lock);
370 return 1;
371}
372
373static int res_check(struct cx23885_fh *fh, unsigned int bit)
374{
Steven Toth9c8ced52008-10-16 20:18:44 -0300375 return fh->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300376}
377
378static int res_locked(struct cx23885_dev *dev, unsigned int bit)
379{
Steven Toth9c8ced52008-10-16 20:18:44 -0300380 return dev->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300381}
382
383static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
384 unsigned int bits)
385{
386 BUG_ON((fh->resources & bits) != bits);
Harvey Harrison22b4e642008-04-08 23:20:00 -0300387 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300388
389 mutex_lock(&dev->lock);
390 fh->resources &= ~bits;
391 dev->resources &= ~bits;
392 dprintk(1, "res: put %d\n", bits);
393 mutex_unlock(&dev->lock);
394}
395
Hans Verkuild45b9b82008-09-04 03:33:43 -0300396static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
Steven Tothe47f30b2008-01-10 04:25:59 -0300397{
Steven Tothe47f30b2008-01-10 04:25:59 -0300398 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300399 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300400 input, INPUT(input)->vmux,
401 INPUT(input)->gpio0, INPUT(input)->gpio1,
402 INPUT(input)->gpio2, INPUT(input)->gpio3);
403 dev->input = input;
404
Steven Tothe47f30b2008-01-10 04:25:59 -0300405 /* Tell the internal A/V decoder */
Hans Verkuil5325b422009-04-02 11:26:22 -0300406 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
407 INPUT(input)->vmux, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300408
409 return 0;
410}
Steven Tothe47f30b2008-01-10 04:25:59 -0300411
412/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300413static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300414 unsigned int height, enum v4l2_field field)
415{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300416 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300417 return 0;
418}
419
420static int cx23885_start_video_dma(struct cx23885_dev *dev,
421 struct cx23885_dmaqueue *q,
422 struct cx23885_buffer *buf)
423{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300424 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300425
426 /* setup fifo + format */
427 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
428 buf->bpl, buf->risc.dma);
429 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
430
431 /* reset counter */
432 cx_write(VID_A_GPCNT_CTL, 3);
433 q->count = 1;
434
435 /* enable irq */
436 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
437 cx_set(VID_A_INT_MSK, 0x000011);
438
439 /* start dma */
440 cx_set(DEV_CNTRL2, (1<<5));
441 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
442
443 return 0;
444}
445
Steven Tothe47f30b2008-01-10 04:25:59 -0300446
447static int cx23885_restart_video_queue(struct cx23885_dev *dev,
448 struct cx23885_dmaqueue *q)
449{
450 struct cx23885_buffer *buf, *prev;
451 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300452 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300453
454 if (!list_empty(&q->active)) {
455 buf = list_entry(q->active.next, struct cx23885_buffer,
456 vb.queue);
457 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
458 buf, buf->vb.i);
459 cx23885_start_video_dma(dev, q, buf);
460 list_for_each(item, &q->active) {
461 buf = list_entry(item, struct cx23885_buffer,
462 vb.queue);
463 buf->count = q->count++;
464 }
465 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
466 return 0;
467 }
468
469 prev = NULL;
470 for (;;) {
471 if (list_empty(&q->queued))
472 return 0;
473 buf = list_entry(q->queued.next, struct cx23885_buffer,
474 vb.queue);
475 if (NULL == prev) {
476 list_move_tail(&buf->vb.queue, &q->active);
477 cx23885_start_video_dma(dev, q, buf);
478 buf->vb.state = VIDEOBUF_ACTIVE;
479 buf->count = q->count++;
480 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
481 dprintk(2, "[%p/%d] restart_queue - first active\n",
482 buf, buf->vb.i);
483
484 } else if (prev->vb.width == buf->vb.width &&
485 prev->vb.height == buf->vb.height &&
486 prev->fmt == buf->fmt) {
487 list_move_tail(&buf->vb.queue, &q->active);
488 buf->vb.state = VIDEOBUF_ACTIVE;
489 buf->count = q->count++;
490 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
491 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
492 dprintk(2, "[%p/%d] restart_queue - move to active\n",
493 buf, buf->vb.i);
494 } else {
495 return 0;
496 }
497 prev = buf;
498 }
499}
500
501static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
502 unsigned int *size)
503{
504 struct cx23885_fh *fh = q->priv_data;
505
506 *size = fh->fmt->depth*fh->width*fh->height >> 3;
507 if (0 == *count)
508 *count = 32;
509 while (*size * *count > vid_limit * 1024 * 1024)
510 (*count)--;
511 return 0;
512}
513
514static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
515 enum v4l2_field field)
516{
517 struct cx23885_fh *fh = q->priv_data;
518 struct cx23885_dev *dev = fh->dev;
519 struct cx23885_buffer *buf =
520 container_of(vb, struct cx23885_buffer, vb);
521 int rc, init_buffer = 0;
522 u32 line0_offset, line1_offset;
523 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
524
525 BUG_ON(NULL == fh->fmt);
526 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
527 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
528 return -EINVAL;
529 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
530 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
531 return -EINVAL;
532
533 if (buf->fmt != fh->fmt ||
534 buf->vb.width != fh->width ||
535 buf->vb.height != fh->height ||
536 buf->vb.field != field) {
537 buf->fmt = fh->fmt;
538 buf->vb.width = fh->width;
539 buf->vb.height = fh->height;
540 buf->vb.field = field;
541 init_buffer = 1;
542 }
543
544 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
545 init_buffer = 1;
546 rc = videobuf_iolock(q, &buf->vb, NULL);
547 if (0 != rc)
548 goto fail;
549 }
550
551 if (init_buffer) {
552 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
553 switch (buf->vb.field) {
554 case V4L2_FIELD_TOP:
555 cx23885_risc_buffer(dev->pci, &buf->risc,
556 dma->sglist, 0, UNSET,
557 buf->bpl, 0, buf->vb.height);
558 break;
559 case V4L2_FIELD_BOTTOM:
560 cx23885_risc_buffer(dev->pci, &buf->risc,
561 dma->sglist, UNSET, 0,
562 buf->bpl, 0, buf->vb.height);
563 break;
564 case V4L2_FIELD_INTERLACED:
565 if (dev->tvnorm & V4L2_STD_NTSC) {
566 /* cx25840 transmits NTSC bottom field first */
567 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300568 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300569 line0_offset = buf->bpl;
570 line1_offset = 0;
571 } else {
572 /* All other formats are top field first */
573 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300574 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300575 line0_offset = 0;
576 line1_offset = buf->bpl;
577 }
578 cx23885_risc_buffer(dev->pci, &buf->risc,
579 dma->sglist, line0_offset,
580 line1_offset,
581 buf->bpl, buf->bpl,
582 buf->vb.height >> 1);
583 break;
584 case V4L2_FIELD_SEQ_TB:
585 cx23885_risc_buffer(dev->pci, &buf->risc,
586 dma->sglist,
587 0, buf->bpl * (buf->vb.height >> 1),
588 buf->bpl, 0,
589 buf->vb.height >> 1);
590 break;
591 case V4L2_FIELD_SEQ_BT:
592 cx23885_risc_buffer(dev->pci, &buf->risc,
593 dma->sglist,
594 buf->bpl * (buf->vb.height >> 1), 0,
595 buf->bpl, 0,
596 buf->vb.height >> 1);
597 break;
598 default:
599 BUG();
600 }
601 }
602 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
603 buf, buf->vb.i,
604 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
605 (unsigned long)buf->risc.dma);
606
607 buf->vb.state = VIDEOBUF_PREPARED;
608 return 0;
609
610 fail:
611 cx23885_free_buffer(q, buf);
612 return rc;
613}
614
615static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
616{
617 struct cx23885_buffer *buf = container_of(vb,
618 struct cx23885_buffer, vb);
619 struct cx23885_buffer *prev;
620 struct cx23885_fh *fh = vq->priv_data;
621 struct cx23885_dev *dev = fh->dev;
622 struct cx23885_dmaqueue *q = &dev->vidq;
623
624 /* add jump to stopper */
625 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
626 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
627 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
628
629 if (!list_empty(&q->queued)) {
630 list_add_tail(&buf->vb.queue, &q->queued);
631 buf->vb.state = VIDEOBUF_QUEUED;
632 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
633 buf, buf->vb.i);
634
635 } else if (list_empty(&q->active)) {
636 list_add_tail(&buf->vb.queue, &q->active);
637 cx23885_start_video_dma(dev, q, buf);
638 buf->vb.state = VIDEOBUF_ACTIVE;
639 buf->count = q->count++;
640 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
641 dprintk(2, "[%p/%d] buffer_queue - first active\n",
642 buf, buf->vb.i);
643
644 } else {
645 prev = list_entry(q->active.prev, struct cx23885_buffer,
646 vb.queue);
647 if (prev->vb.width == buf->vb.width &&
648 prev->vb.height == buf->vb.height &&
649 prev->fmt == buf->fmt) {
650 list_add_tail(&buf->vb.queue, &q->active);
651 buf->vb.state = VIDEOBUF_ACTIVE;
652 buf->count = q->count++;
653 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
654 /* 64 bit bits 63-32 */
655 prev->risc.jmp[2] = cpu_to_le32(0);
656 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
657 buf, buf->vb.i);
658
659 } else {
660 list_add_tail(&buf->vb.queue, &q->queued);
661 buf->vb.state = VIDEOBUF_QUEUED;
662 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
663 buf, buf->vb.i);
664 }
665 }
666}
667
668static void buffer_release(struct videobuf_queue *q,
669 struct videobuf_buffer *vb)
670{
671 struct cx23885_buffer *buf = container_of(vb,
672 struct cx23885_buffer, vb);
673
674 cx23885_free_buffer(q, buf);
675}
676
677static struct videobuf_queue_ops cx23885_video_qops = {
678 .buf_setup = buffer_setup,
679 .buf_prepare = buffer_prepare,
680 .buf_queue = buffer_queue,
681 .buf_release = buffer_release,
682};
683
684static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
685{
686 switch (fh->type) {
687 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
688 return &fh->vidq;
689 case V4L2_BUF_TYPE_VBI_CAPTURE:
690 return &fh->vbiq;
691 default:
692 BUG();
693 return NULL;
694 }
695}
696
697static int get_resource(struct cx23885_fh *fh)
698{
699 switch (fh->type) {
700 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
701 return RESOURCE_VIDEO;
702 case V4L2_BUF_TYPE_VBI_CAPTURE:
703 return RESOURCE_VBI;
704 default:
705 BUG();
706 return 0;
707 }
708}
709
Hans Verkuilbec43662008-12-30 06:58:20 -0300710static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300711{
Hans Verkuilbec43662008-12-30 06:58:20 -0300712 int minor = video_devdata(file)->minor;
Steven Tothe47f30b2008-01-10 04:25:59 -0300713 struct cx23885_dev *h, *dev = NULL;
714 struct cx23885_fh *fh;
715 struct list_head *list;
716 enum v4l2_buf_type type = 0;
717 int radio = 0;
718
Hans Verkuild56dc612008-07-30 08:43:36 -0300719 lock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300720 list_for_each(list, &cx23885_devlist) {
721 h = list_entry(list, struct cx23885_dev, devlist);
Andy Wallscd8f8942009-01-09 22:59:27 -0300722 if (h->video_dev &&
723 h->video_dev->minor == minor) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300724 dev = h;
725 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
726 }
727 if (h->vbi_dev &&
Andy Wallscd8f8942009-01-09 22:59:27 -0300728 h->vbi_dev->minor == minor) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300729 dev = h;
730 type = V4L2_BUF_TYPE_VBI_CAPTURE;
731 }
732 if (h->radio_dev &&
733 h->radio_dev->minor == minor) {
734 radio = 1;
735 dev = h;
736 }
737 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300738 if (NULL == dev) {
739 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300740 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -0300741 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300742
743 dprintk(1, "open minor=%d radio=%d type=%s\n",
744 minor, radio, v4l2_type_names[type]);
745
746 /* allocate + initialize per filehandle data */
747 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -0300748 if (NULL == fh) {
749 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300750 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -0300751 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300752 file->private_data = fh;
753 fh->dev = dev;
754 fh->radio = radio;
755 fh->type = type;
756 fh->width = 320;
757 fh->height = 240;
758 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
759
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300760 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
761 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300762 V4L2_BUF_TYPE_VIDEO_CAPTURE,
763 V4L2_FIELD_INTERLACED,
764 sizeof(struct cx23885_buffer),
765 fh);
766
767 dprintk(1, "post videobuf_queue_init()\n");
768
Hans Verkuild56dc612008-07-30 08:43:36 -0300769 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300770
771 return 0;
772}
773
774static ssize_t video_read(struct file *file, char __user *data,
775 size_t count, loff_t *ppos)
776{
777 struct cx23885_fh *fh = file->private_data;
778
779 switch (fh->type) {
780 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
781 if (res_locked(fh->dev, RESOURCE_VIDEO))
782 return -EBUSY;
783 return videobuf_read_one(&fh->vidq, data, count, ppos,
784 file->f_flags & O_NONBLOCK);
785 case V4L2_BUF_TYPE_VBI_CAPTURE:
786 if (!res_get(fh->dev, fh, RESOURCE_VBI))
787 return -EBUSY;
788 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
789 file->f_flags & O_NONBLOCK);
790 default:
791 BUG();
792 return 0;
793 }
794}
795
796static unsigned int video_poll(struct file *file,
797 struct poll_table_struct *wait)
798{
799 struct cx23885_fh *fh = file->private_data;
800 struct cx23885_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300801 unsigned int rc = POLLERR;
Steven Tothe47f30b2008-01-10 04:25:59 -0300802
803 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
804 if (!res_get(fh->dev, fh, RESOURCE_VBI))
805 return POLLERR;
806 return videobuf_poll_stream(file, &fh->vbiq, wait);
807 }
808
Figo.zhang9fd64182009-06-16 13:31:29 -0300809 mutex_lock(&fh->vidq.vb_lock);
Steven Tothe47f30b2008-01-10 04:25:59 -0300810 if (res_check(fh, RESOURCE_VIDEO)) {
811 /* streaming capture */
812 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300813 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300814 buf = list_entry(fh->vidq.stream.next,
815 struct cx23885_buffer, vb.stream);
816 } else {
817 /* read() capture */
818 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
819 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300820 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300821 }
822 poll_wait(file, &buf->vb.done, wait);
823 if (buf->vb.state == VIDEOBUF_DONE ||
824 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300825 rc = POLLIN|POLLRDNORM;
826 else
827 rc = 0;
828done:
829 mutex_unlock(&fh->vidq.vb_lock);
830 return rc;
Steven Tothe47f30b2008-01-10 04:25:59 -0300831}
832
Hans Verkuilbec43662008-12-30 06:58:20 -0300833static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300834{
835 struct cx23885_fh *fh = file->private_data;
836 struct cx23885_dev *dev = fh->dev;
837
838 /* turn off overlay */
839 if (res_check(fh, RESOURCE_OVERLAY)) {
840 /* FIXME */
841 res_free(dev, fh, RESOURCE_OVERLAY);
842 }
843
844 /* stop video capture */
845 if (res_check(fh, RESOURCE_VIDEO)) {
846 videobuf_queue_cancel(&fh->vidq);
847 res_free(dev, fh, RESOURCE_VIDEO);
848 }
849 if (fh->vidq.read_buf) {
850 buffer_release(&fh->vidq, fh->vidq.read_buf);
851 kfree(fh->vidq.read_buf);
852 }
853
854 /* stop vbi capture */
855 if (res_check(fh, RESOURCE_VBI)) {
856 if (fh->vbiq.streaming)
857 videobuf_streamoff(&fh->vbiq);
858 if (fh->vbiq.reading)
859 videobuf_read_stop(&fh->vbiq);
860 res_free(dev, fh, RESOURCE_VBI);
861 }
862
863 videobuf_mmap_free(&fh->vidq);
864 file->private_data = NULL;
865 kfree(fh);
866
Steven Totha19602f22008-01-10 11:43:18 -0300867 /* We are not putting the tuner to sleep here on exit, because
868 * we want to use the mpeg encoder in another session to capture
869 * tuner video. Closing this will result in no video to the encoder.
870 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300871
872 return 0;
873}
874
875static int video_mmap(struct file *file, struct vm_area_struct *vma)
876{
877 struct cx23885_fh *fh = file->private_data;
878
879 return videobuf_mmap_mapper(get_queue(fh), vma);
880}
881
882/* ------------------------------------------------------------------ */
883/* VIDEO CTRL IOCTLS */
884
Steven Toth9c8ced52008-10-16 20:18:44 -0300885static int cx23885_get_control(struct cx23885_dev *dev,
886 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300887{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300888 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300889 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -0300890 return 0;
891}
Steven Tothe47f30b2008-01-10 04:25:59 -0300892
Steven Toth9c8ced52008-10-16 20:18:44 -0300893static int cx23885_set_control(struct cx23885_dev *dev,
894 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300895{
896 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
Harvey Harrison22b4e642008-04-08 23:20:00 -0300897 " (disabled - no action)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300898 return 0;
899}
Steven Tothe47f30b2008-01-10 04:25:59 -0300900
901static void init_controls(struct cx23885_dev *dev)
902{
903 struct v4l2_control ctrl;
904 int i;
905
906 for (i = 0; i < CX23885_CTLS; i++) {
907 ctrl.id = cx23885_ctls[i].v.id;
908 ctrl.value = cx23885_ctls[i].v.default_value;
909
910 cx23885_set_control(dev, &ctrl);
911 }
912}
913
914/* ------------------------------------------------------------------ */
915/* VIDEO IOCTLS */
916
Hans Verkuil78b526a2008-05-28 12:16:41 -0300917static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300918 struct v4l2_format *f)
919{
920 struct cx23885_fh *fh = priv;
921
922 f->fmt.pix.width = fh->width;
923 f->fmt.pix.height = fh->height;
924 f->fmt.pix.field = fh->vidq.field;
925 f->fmt.pix.pixelformat = fh->fmt->fourcc;
926 f->fmt.pix.bytesperline =
927 (f->fmt.pix.width * fh->fmt->depth) >> 3;
928 f->fmt.pix.sizeimage =
929 f->fmt.pix.height * f->fmt.pix.bytesperline;
930
931 return 0;
932}
933
Hans Verkuil78b526a2008-05-28 12:16:41 -0300934static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300935 struct v4l2_format *f)
936{
937 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
938 struct cx23885_fmt *fmt;
939 enum v4l2_field field;
940 unsigned int maxw, maxh;
941
942 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
943 if (NULL == fmt)
944 return -EINVAL;
945
946 field = f->fmt.pix.field;
947 maxw = norm_maxw(dev->tvnorm);
948 maxh = norm_maxh(dev->tvnorm);
949
950 if (V4L2_FIELD_ANY == field) {
951 field = (f->fmt.pix.height > maxh/2)
952 ? V4L2_FIELD_INTERLACED
953 : V4L2_FIELD_BOTTOM;
954 }
955
956 switch (field) {
957 case V4L2_FIELD_TOP:
958 case V4L2_FIELD_BOTTOM:
959 maxh = maxh / 2;
960 break;
961 case V4L2_FIELD_INTERLACED:
962 break;
963 default:
964 return -EINVAL;
965 }
966
967 f->fmt.pix.field = field;
Trent Piepho2449afc2009-05-30 21:45:46 -0300968 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
969 &f->fmt.pix.height, 32, maxh, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300970 f->fmt.pix.bytesperline =
971 (f->fmt.pix.width * fmt->depth) >> 3;
972 f->fmt.pix.sizeimage =
973 f->fmt.pix.height * f->fmt.pix.bytesperline;
974
975 return 0;
976}
977
Hans Verkuil78b526a2008-05-28 12:16:41 -0300978static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300979 struct v4l2_format *f)
980{
981 struct cx23885_fh *fh = priv;
982 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
983 int err;
984
Harvey Harrison22b4e642008-04-08 23:20:00 -0300985 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -0300986 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -0300987
988 if (0 != err)
989 return err;
990 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
991 fh->width = f->fmt.pix.width;
992 fh->height = f->fmt.pix.height;
993 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300994 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300995 fh->width, fh->height, fh->vidq.field);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300996 call_all(dev, video, s_fmt, f);
Steven Tothe47f30b2008-01-10 04:25:59 -0300997 return 0;
998}
999
1000static int vidioc_querycap(struct file *file, void *priv,
1001 struct v4l2_capability *cap)
1002{
1003 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1004
1005 strcpy(cap->driver, "cx23885");
1006 strlcpy(cap->card, cx23885_boards[dev->board].name,
1007 sizeof(cap->card));
1008 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1009 cap->version = CX23885_VERSION_CODE;
1010 cap->capabilities =
1011 V4L2_CAP_VIDEO_CAPTURE |
1012 V4L2_CAP_READWRITE |
1013 V4L2_CAP_STREAMING |
1014 V4L2_CAP_VBI_CAPTURE;
1015 if (UNSET != dev->tuner_type)
1016 cap->capabilities |= V4L2_CAP_TUNER;
1017 return 0;
1018}
1019
Hans Verkuil78b526a2008-05-28 12:16:41 -03001020static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001021 struct v4l2_fmtdesc *f)
1022{
1023 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1024 return -EINVAL;
1025
1026 strlcpy(f->description, formats[f->index].name,
1027 sizeof(f->description));
1028 f->pixelformat = formats[f->index].fourcc;
1029
1030 return 0;
1031}
1032
1033#ifdef CONFIG_VIDEO_V4L1_COMPAT
1034static int vidiocgmbuf(struct file *file, void *priv,
1035 struct video_mbuf *mbuf)
1036{
1037 struct cx23885_fh *fh = priv;
1038 struct videobuf_queue *q;
1039 struct v4l2_requestbuffers req;
1040 unsigned int i;
1041 int err;
1042
1043 q = get_queue(fh);
1044 memset(&req, 0, sizeof(req));
1045 req.type = q->type;
1046 req.count = 8;
1047 req.memory = V4L2_MEMORY_MMAP;
1048 err = videobuf_reqbufs(q, &req);
1049 if (err < 0)
1050 return err;
1051
1052 mbuf->frames = req.count;
1053 mbuf->size = 0;
1054 for (i = 0; i < mbuf->frames; i++) {
1055 mbuf->offsets[i] = q->bufs[i]->boff;
1056 mbuf->size += q->bufs[i]->bsize;
1057 }
1058 return 0;
1059}
1060#endif
1061
1062static int vidioc_reqbufs(struct file *file, void *priv,
1063 struct v4l2_requestbuffers *p)
1064{
1065 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001066 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001067}
1068
1069static int vidioc_querybuf(struct file *file, void *priv,
1070 struct v4l2_buffer *p)
1071{
1072 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001073 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001074}
1075
1076static int vidioc_qbuf(struct file *file, void *priv,
1077 struct v4l2_buffer *p)
1078{
1079 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001080 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001081}
1082
1083static int vidioc_dqbuf(struct file *file, void *priv,
1084 struct v4l2_buffer *p)
1085{
1086 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001087 return videobuf_dqbuf(get_queue(fh), p,
1088 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001089}
1090
1091static int vidioc_streamon(struct file *file, void *priv,
1092 enum v4l2_buf_type i)
1093{
1094 struct cx23885_fh *fh = priv;
1095 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001096 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001097
1098 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1099 return -EINVAL;
1100 if (unlikely(i != fh->type))
1101 return -EINVAL;
1102
1103 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1104 return -EBUSY;
1105 return videobuf_streamon(get_queue(fh));
1106}
1107
1108static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1109{
1110 struct cx23885_fh *fh = priv;
1111 struct cx23885_dev *dev = fh->dev;
1112 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001113 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001114
1115 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1116 return -EINVAL;
1117 if (i != fh->type)
1118 return -EINVAL;
1119
1120 res = get_resource(fh);
1121 err = videobuf_streamoff(get_queue(fh));
1122 if (err < 0)
1123 return err;
1124 res_free(dev, fh, res);
1125 return 0;
1126}
1127
1128static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1129{
1130 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001131 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001132
1133 mutex_lock(&dev->lock);
1134 cx23885_set_tvnorm(dev, *tvnorms);
1135 mutex_unlock(&dev->lock);
1136
1137 return 0;
1138}
1139
Hans Verkuild45b9b82008-09-04 03:33:43 -03001140static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001141{
1142 static const char *iname[] = {
1143 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1144 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1145 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1146 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1147 [CX23885_VMUX_SVIDEO] = "S-Video",
1148 [CX23885_VMUX_TELEVISION] = "Television",
1149 [CX23885_VMUX_CABLE] = "Cable TV",
1150 [CX23885_VMUX_DVB] = "DVB",
1151 [CX23885_VMUX_DEBUG] = "for debug only",
1152 };
1153 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001154 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001155
1156 n = i->index;
1157 if (n >= 4)
1158 return -EINVAL;
1159
1160 if (0 == INPUT(n)->type)
1161 return -EINVAL;
1162
1163 memset(i, 0, sizeof(*i));
1164 i->index = n;
1165 i->type = V4L2_INPUT_TYPE_CAMERA;
1166 strcpy(i->name, iname[INPUT(n)->type]);
1167 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1168 (CX23885_VMUX_CABLE == INPUT(n)->type))
1169 i->type = V4L2_INPUT_TYPE_TUNER;
1170 i->std = CX23885_NORMS;
1171 return 0;
1172}
Steven Tothe47f30b2008-01-10 04:25:59 -03001173
1174static int vidioc_enum_input(struct file *file, void *priv,
1175 struct v4l2_input *i)
1176{
1177 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001178 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001179 return cx23885_enum_input(dev, i);
1180}
1181
1182static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1183{
1184 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1185
1186 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001187 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001188 return 0;
1189}
1190
1191static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1192{
1193 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1194
Harvey Harrison22b4e642008-04-08 23:20:00 -03001195 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001196
1197 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001198 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001199 return -EINVAL;
1200 }
1201
1202 mutex_lock(&dev->lock);
1203 cx23885_video_mux(dev, i);
1204 mutex_unlock(&dev->lock);
1205 return 0;
1206}
1207
1208static int vidioc_queryctrl(struct file *file, void *priv,
1209 struct v4l2_queryctrl *qctrl)
1210{
1211 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1212 if (unlikely(qctrl->id == 0))
1213 return -EINVAL;
1214 return cx23885_ctrl_query(qctrl);
1215}
1216
1217static int vidioc_g_ctrl(struct file *file, void *priv,
1218 struct v4l2_control *ctl)
1219{
1220 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1221
1222 return cx23885_get_control(dev, ctl);
1223}
1224
1225static int vidioc_s_ctrl(struct file *file, void *priv,
1226 struct v4l2_control *ctl)
1227{
1228 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1229
1230 return cx23885_set_control(dev, ctl);
1231}
1232
1233static int vidioc_g_tuner(struct file *file, void *priv,
1234 struct v4l2_tuner *t)
1235{
1236 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1237
1238 if (unlikely(UNSET == dev->tuner_type))
1239 return -EINVAL;
1240 if (0 != t->index)
1241 return -EINVAL;
1242
1243 strcpy(t->name, "Television");
1244 t->type = V4L2_TUNER_ANALOG_TV;
1245 t->capability = V4L2_TUNER_CAP_NORM;
1246 t->rangehigh = 0xffffffffUL;
1247 t->signal = 0xffff ; /* LOCKED */
1248 return 0;
1249}
1250
1251static int vidioc_s_tuner(struct file *file, void *priv,
1252 struct v4l2_tuner *t)
1253{
1254 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1255
1256 if (UNSET == dev->tuner_type)
1257 return -EINVAL;
1258 if (0 != t->index)
1259 return -EINVAL;
1260 return 0;
1261}
1262
1263static int vidioc_g_frequency(struct file *file, void *priv,
1264 struct v4l2_frequency *f)
1265{
1266 struct cx23885_fh *fh = priv;
1267 struct cx23885_dev *dev = fh->dev;
1268
1269 if (unlikely(UNSET == dev->tuner_type))
1270 return -EINVAL;
1271
1272 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1273 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1274 f->frequency = dev->freq;
1275
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001276 call_all(dev, tuner, g_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001277
1278 return 0;
1279}
1280
Hans Verkuild45b9b82008-09-04 03:33:43 -03001281static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001282{
1283 if (unlikely(UNSET == dev->tuner_type))
1284 return -EINVAL;
1285 if (unlikely(f->tuner != 0))
1286 return -EINVAL;
1287
1288 mutex_lock(&dev->lock);
1289 dev->freq = f->frequency;
1290
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001291 call_all(dev, tuner, s_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001292
1293 /* When changing channels it is required to reset TVAUDIO */
1294 msleep(10);
1295
1296 mutex_unlock(&dev->lock);
1297
1298 return 0;
1299}
Steven Tothe47f30b2008-01-10 04:25:59 -03001300
1301static int vidioc_s_frequency(struct file *file, void *priv,
1302 struct v4l2_frequency *f)
1303{
1304 struct cx23885_fh *fh = priv;
1305 struct cx23885_dev *dev = fh->dev;
1306
1307 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1308 return -EINVAL;
1309 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1310 return -EINVAL;
1311
1312 return
1313 cx23885_set_freq(dev, f);
1314}
1315
Steven Tothe47f30b2008-01-10 04:25:59 -03001316/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001317
1318static void cx23885_vid_timeout(unsigned long data)
1319{
1320 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1321 struct cx23885_dmaqueue *q = &dev->vidq;
1322 struct cx23885_buffer *buf;
1323 unsigned long flags;
1324
1325 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1326
1327 cx_clear(VID_A_DMA_CTL, 0x11);
1328
1329 spin_lock_irqsave(&dev->slock, flags);
1330 while (!list_empty(&q->active)) {
1331 buf = list_entry(q->active.next,
1332 struct cx23885_buffer, vb.queue);
1333 list_del(&buf->vb.queue);
1334 buf->vb.state = VIDEOBUF_ERROR;
1335 wake_up(&buf->vb.done);
1336 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1337 dev->name, buf, buf->vb.i,
1338 (unsigned long)buf->risc.dma);
1339 }
1340 cx23885_restart_video_queue(dev, q);
1341 spin_unlock_irqrestore(&dev->slock, flags);
1342}
1343
1344int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1345{
1346 u32 mask, count;
1347 int handled = 0;
1348
1349 mask = cx_read(VID_A_INT_MSK);
1350 if (0 == (status & mask))
1351 return handled;
1352 cx_write(VID_A_INT_STAT, status);
1353
Harvey Harrison22b4e642008-04-08 23:20:00 -03001354 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
Steven Tothe47f30b2008-01-10 04:25:59 -03001355 /* risc op code error */
1356 if (status & (1 << 16)) {
1357 printk(KERN_WARNING "%s/0: video risc op code error\n",
1358 dev->name);
1359 cx_clear(VID_A_DMA_CTL, 0x11);
1360 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1361 }
1362
1363 /* risc1 y */
1364 if (status & 0x01) {
1365 spin_lock(&dev->slock);
1366 count = cx_read(VID_A_GPCNT);
1367 cx23885_video_wakeup(dev, &dev->vidq, count);
1368 spin_unlock(&dev->slock);
1369 handled++;
1370 }
1371 /* risc2 y */
1372 if (status & 0x10) {
1373 dprintk(2, "stopper video\n");
1374 spin_lock(&dev->slock);
1375 cx23885_restart_video_queue(dev, &dev->vidq);
1376 spin_unlock(&dev->slock);
1377 handled++;
1378 }
1379
1380 return handled;
1381}
1382
Steven Tothe47f30b2008-01-10 04:25:59 -03001383/* ----------------------------------------------------------- */
1384/* exported stuff */
1385
Hans Verkuilbec43662008-12-30 06:58:20 -03001386static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001387 .owner = THIS_MODULE,
1388 .open = video_open,
1389 .release = video_release,
1390 .read = video_read,
1391 .poll = video_poll,
1392 .mmap = video_mmap,
1393 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001394};
1395
Hans Verkuila3998102008-07-21 02:57:38 -03001396static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001397 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001398 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1399 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1400 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1401 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1402 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1403 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1404 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001405 .vidioc_reqbufs = vidioc_reqbufs,
1406 .vidioc_querybuf = vidioc_querybuf,
1407 .vidioc_qbuf = vidioc_qbuf,
1408 .vidioc_dqbuf = vidioc_dqbuf,
1409 .vidioc_s_std = vidioc_s_std,
1410 .vidioc_enum_input = vidioc_enum_input,
1411 .vidioc_g_input = vidioc_g_input,
1412 .vidioc_s_input = vidioc_s_input,
1413 .vidioc_queryctrl = vidioc_queryctrl,
1414 .vidioc_g_ctrl = vidioc_g_ctrl,
1415 .vidioc_s_ctrl = vidioc_s_ctrl,
1416 .vidioc_streamon = vidioc_streamon,
1417 .vidioc_streamoff = vidioc_streamoff,
1418#ifdef CONFIG_VIDEO_V4L1_COMPAT
1419 .vidiocgmbuf = vidiocgmbuf,
1420#endif
1421 .vidioc_g_tuner = vidioc_g_tuner,
1422 .vidioc_s_tuner = vidioc_s_tuner,
1423 .vidioc_g_frequency = vidioc_g_frequency,
1424 .vidioc_s_frequency = vidioc_s_frequency,
Andy Walls74618242009-09-26 22:50:44 -03001425 .vidioc_g_chip_ident = cx23885_g_chip_ident,
Steven Tothe47f30b2008-01-10 04:25:59 -03001426#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls74618242009-09-26 22:50:44 -03001427 .vidioc_g_register = cx23885_g_register,
1428 .vidioc_s_register = cx23885_s_register,
Steven Tothe47f30b2008-01-10 04:25:59 -03001429#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001430};
1431
1432static struct video_device cx23885_vbi_template;
1433static struct video_device cx23885_video_template = {
1434 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001435 .fops = &video_fops,
1436 .minor = -1,
1437 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001438 .tvnorms = CX23885_NORMS,
1439 .current_norm = V4L2_STD_NTSC_M,
1440};
1441
Hans Verkuilbec43662008-12-30 06:58:20 -03001442static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001443 .owner = THIS_MODULE,
1444 .open = video_open,
1445 .release = video_release,
1446 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001447};
1448
1449
1450void cx23885_video_unregister(struct cx23885_dev *dev)
1451{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001452 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001453 cx_clear(PCI_INT_MSK, 1);
1454
1455 if (dev->video_dev) {
1456 if (-1 != dev->video_dev->minor)
1457 video_unregister_device(dev->video_dev);
1458 else
1459 video_device_release(dev->video_dev);
1460 dev->video_dev = NULL;
1461
1462 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1463 }
1464}
1465
1466int cx23885_video_register(struct cx23885_dev *dev)
1467{
1468 int err;
1469
Harvey Harrison22b4e642008-04-08 23:20:00 -03001470 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001471 spin_lock_init(&dev->slock);
1472
1473 /* Initialize VBI template */
1474 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1475 sizeof(cx23885_vbi_template));
1476 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001477
1478 dev->tvnorm = cx23885_video_template.current_norm;
1479
1480 /* init video dma queues */
1481 INIT_LIST_HEAD(&dev->vidq.active);
1482 INIT_LIST_HEAD(&dev->vidq.queued);
1483 dev->vidq.timeout.function = cx23885_vid_timeout;
1484 dev->vidq.timeout.data = (unsigned long)dev;
1485 init_timer(&dev->vidq.timeout);
1486 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1487 VID_A_DMA_CTL, 0x11, 0x00);
1488
Steven Totha19602f22008-01-10 11:43:18 -03001489 /* Don't enable VBI yet */
Steven Tothe47f30b2008-01-10 04:25:59 -03001490 cx_set(PCI_INT_MSK, 1);
1491
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001492 if (TUNER_ABSENT != dev->tuner_type) {
1493 struct v4l2_subdev *sd = NULL;
1494
1495 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001496 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1497 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -03001498 "tuner", "tuner", dev->tuner_addr, NULL);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001499 else
Hans Verkuil53dacb12009-08-10 02:49:08 -03001500 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Hans Verkuile6574f22009-04-01 03:57:53 -03001501 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -03001502 "tuner", "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001503 if (sd) {
1504 struct tuner_setup tun_setup;
1505
1506 tun_setup.mode_mask = T_ANALOG_TV;
1507 tun_setup.type = dev->tuner_type;
1508 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1509
1510 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1511 }
1512 }
1513
Steven Tothe47f30b2008-01-10 04:25:59 -03001514
1515 /* register v4l devices */
1516 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1517 &cx23885_video_template, "video");
1518 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1519 video_nr[dev->nr]);
1520 if (err < 0) {
1521 printk(KERN_INFO "%s: can't register video device\n",
1522 dev->name);
1523 goto fail_unreg;
1524 }
1525 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001526 dev->name, dev->video_dev->num);
Steven Tothe47f30b2008-01-10 04:25:59 -03001527 /* initial device configuration */
1528 mutex_lock(&dev->lock);
1529 cx23885_set_tvnorm(dev, dev->tvnorm);
1530 init_controls(dev);
1531 cx23885_video_mux(dev, 0);
1532 mutex_unlock(&dev->lock);
1533
Steven Tothe47f30b2008-01-10 04:25:59 -03001534 return 0;
1535
1536fail_unreg:
1537 cx23885_video_unregister(dev);
1538 return err;
1539}
1540