aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/pci/cx25821/cx25821-video.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/pci/cx25821/cx25821-video.c')
-rw-r--r--drivers/media/pci/cx25821/cx25821-video.c1818
1 files changed, 480 insertions, 1338 deletions
diff --git a/drivers/media/pci/cx25821/cx25821-video.c b/drivers/media/pci/cx25821/cx25821-video.c
index 1465591d000c..b194138961df 100644
--- a/drivers/media/pci/cx25821/cx25821-video.c
+++ b/drivers/media/pci/cx25821/cx25821-video.c
@@ -33,13 +33,10 @@ MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
33MODULE_LICENSE("GPL"); 33MODULE_LICENSE("GPL");
34 34
35static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET }; 35static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
36static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
37 36
38module_param_array(video_nr, int, NULL, 0444); 37module_param_array(video_nr, int, NULL, 0444);
39module_param_array(radio_nr, int, NULL, 0444);
40 38
41MODULE_PARM_DESC(video_nr, "video device numbers"); 39MODULE_PARM_DESC(video_nr, "video device numbers");
42MODULE_PARM_DESC(radio_nr, "radio device numbers");
43 40
44static unsigned int video_debug = VIDEO_DEBUG; 41static unsigned int video_debug = VIDEO_DEBUG;
45module_param(video_debug, int, 0644); 42module_param(video_debug, int, 0644);
@@ -49,24 +46,14 @@ static unsigned int irq_debug;
49module_param(irq_debug, int, 0644); 46module_param(irq_debug, int, 0644);
50MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]"); 47MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
51 48
52unsigned int vid_limit = 16; 49static unsigned int vid_limit = 16;
53module_param(vid_limit, int, 0644); 50module_param(vid_limit, int, 0644);
54MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes"); 51MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
55 52
56static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
57
58static const struct v4l2_file_operations video_fops;
59static const struct v4l2_ioctl_ops video_ioctl_ops;
60
61#define FORMAT_FLAGS_PACKED 0x01 53#define FORMAT_FLAGS_PACKED 0x01
62 54
63struct cx25821_fmt formats[] = { 55static const struct cx25821_fmt formats[] = {
64 { 56 {
65 .name = "8 bpp, gray",
66 .fourcc = V4L2_PIX_FMT_GREY,
67 .depth = 8,
68 .flags = FORMAT_FLAGS_PACKED,
69 }, {
70 .name = "4:1:1, packed, Y41P", 57 .name = "4:1:1, packed, Y41P",
71 .fourcc = V4L2_PIX_FMT_Y41P, 58 .fourcc = V4L2_PIX_FMT_Y41P,
72 .depth = 12, 59 .depth = 12,
@@ -76,36 +63,16 @@ struct cx25821_fmt formats[] = {
76 .fourcc = V4L2_PIX_FMT_YUYV, 63 .fourcc = V4L2_PIX_FMT_YUYV,
77 .depth = 16, 64 .depth = 16,
78 .flags = FORMAT_FLAGS_PACKED, 65 .flags = FORMAT_FLAGS_PACKED,
79 }, {
80 .name = "4:2:2, packed, UYVY",
81 .fourcc = V4L2_PIX_FMT_UYVY,
82 .depth = 16,
83 .flags = FORMAT_FLAGS_PACKED,
84 }, {
85 .name = "4:2:0, YUV",
86 .fourcc = V4L2_PIX_FMT_YUV420,
87 .depth = 12,
88 .flags = FORMAT_FLAGS_PACKED,
89 }, 66 },
90}; 67};
91 68
92int cx25821_get_format_size(void) 69static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
93{
94 return ARRAY_SIZE(formats);
95}
96
97struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
98{ 70{
99 unsigned int i; 71 unsigned int i;
100 72
101 if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
102 return formats + 1;
103
104 for (i = 0; i < ARRAY_SIZE(formats); i++) 73 for (i = 0; i < ARRAY_SIZE(formats); i++)
105 if (formats[i].fourcc == fourcc) 74 if (formats[i].fourcc == fourcc)
106 return formats + i; 75 return formats + i;
107
108 pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
109 return NULL; 76 return NULL;
110} 77}
111 78
@@ -144,129 +111,10 @@ void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
144 pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc); 111 pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
145} 112}
146 113
147#ifdef TUNER_FLAG
148int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
149{
150 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
151 __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
152
153 dev->tvnorm = norm;
154
155 /* Tell the internal A/V decoder */
156 cx25821_call_all(dev, core, s_std, norm);
157
158 return 0;
159}
160#endif
161
162struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
163 struct pci_dev *pci,
164 struct video_device *template,
165 char *type)
166{
167 struct video_device *vfd;
168 dprintk(1, "%s()\n", __func__);
169
170 vfd = video_device_alloc();
171 if (NULL == vfd)
172 return NULL;
173 *vfd = *template;
174 vfd->v4l2_dev = &dev->v4l2_dev;
175 vfd->release = video_device_release;
176 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
177 cx25821_boards[dev->board].name);
178 video_set_drvdata(vfd, dev);
179 return vfd;
180}
181
182/*
183static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
184{
185 int i;
186
187 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
188 return -EINVAL;
189 for (i = 0; i < CX25821_CTLS; i++)
190 if (cx25821_ctls[i].v.id == qctrl->id)
191 break;
192 if (i == CX25821_CTLS) {
193 *qctrl = no_ctl;
194 return 0;
195 }
196 *qctrl = cx25821_ctls[i].v;
197 return 0;
198}
199*/
200
201/* resource management */
202int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
203 unsigned int bit)
204{
205 dprintk(1, "%s()\n", __func__);
206 if (fh->resources & bit)
207 /* have it already allocated */
208 return 1;
209
210 /* is it free? */
211 mutex_lock(&dev->lock);
212 if (dev->channels[fh->channel_id].resources & bit) {
213 /* no, someone else uses it */
214 mutex_unlock(&dev->lock);
215 return 0;
216 }
217 /* it's free, grab it */
218 fh->resources |= bit;
219 dev->channels[fh->channel_id].resources |= bit;
220 dprintk(1, "res: get %d\n", bit);
221 mutex_unlock(&dev->lock);
222 return 1;
223}
224
225int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
226{
227 return fh->resources & bit;
228}
229
230int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
231{
232 return fh->dev->channels[fh->channel_id].resources & bit;
233}
234
235void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
236 unsigned int bits)
237{
238 BUG_ON((fh->resources & bits) != bits);
239 dprintk(1, "%s()\n", __func__);
240
241 mutex_lock(&dev->lock);
242 fh->resources &= ~bits;
243 dev->channels[fh->channel_id].resources &= ~bits;
244 dprintk(1, "res: put %d\n", bits);
245 mutex_unlock(&dev->lock);
246}
247
248int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
249{
250 struct v4l2_routing route;
251 memset(&route, 0, sizeof(route));
252
253 dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
254 __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
255 INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
256 dev->input = input;
257
258 route.input = INPUT(input)->vmux;
259
260 /* Tell the internal A/V decoder */
261 cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
262
263 return 0;
264}
265
266int cx25821_start_video_dma(struct cx25821_dev *dev, 114int cx25821_start_video_dma(struct cx25821_dev *dev,
267 struct cx25821_dmaqueue *q, 115 struct cx25821_dmaqueue *q,
268 struct cx25821_buffer *buf, 116 struct cx25821_buffer *buf,
269 struct sram_channel *channel) 117 const struct sram_channel *channel)
270{ 118{
271 int tmp = 0; 119 int tmp = 0;
272 120
@@ -293,7 +141,7 @@ int cx25821_start_video_dma(struct cx25821_dev *dev,
293 141
294static int cx25821_restart_video_queue(struct cx25821_dev *dev, 142static int cx25821_restart_video_queue(struct cx25821_dev *dev,
295 struct cx25821_dmaqueue *q, 143 struct cx25821_dmaqueue *q,
296 struct sram_channel *channel) 144 const struct sram_channel *channel)
297{ 145{
298 struct cx25821_buffer *buf, *prev; 146 struct cx25821_buffer *buf, *prev;
299 struct list_head *item; 147 struct list_head *item;
@@ -346,8 +194,8 @@ static void cx25821_vid_timeout(unsigned long data)
346{ 194{
347 struct cx25821_data *timeout_data = (struct cx25821_data *)data; 195 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
348 struct cx25821_dev *dev = timeout_data->dev; 196 struct cx25821_dev *dev = timeout_data->dev;
349 struct sram_channel *channel = timeout_data->channel; 197 const struct sram_channel *channel = timeout_data->channel;
350 struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq; 198 struct cx25821_dmaqueue *q = &dev->channels[channel->i].dma_vidq;
351 struct cx25821_buffer *buf; 199 struct cx25821_buffer *buf;
352 unsigned long flags; 200 unsigned long flags;
353 201
@@ -373,7 +221,7 @@ int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
373 u32 count = 0; 221 u32 count = 0;
374 int handled = 0; 222 int handled = 0;
375 u32 mask; 223 u32 mask;
376 struct sram_channel *channel = dev->channels[chan_num].sram_channels; 224 const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
377 225
378 mask = cx_read(channel->int_msk); 226 mask = cx_read(channel->int_msk);
379 if (0 == (status & mask)) 227 if (0 == (status & mask))
@@ -393,7 +241,7 @@ int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
393 if (status & FLD_VID_DST_RISC1) { 241 if (status & FLD_VID_DST_RISC1) {
394 spin_lock(&dev->slock); 242 spin_lock(&dev->slock);
395 count = cx_read(channel->gpcnt); 243 count = cx_read(channel->gpcnt);
396 cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq, 244 cx25821_video_wakeup(dev, &dev->channels[channel->i].dma_vidq,
397 count); 245 count);
398 spin_unlock(&dev->slock); 246 spin_unlock(&dev->slock);
399 handled++; 247 handled++;
@@ -404,122 +252,19 @@ int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
404 dprintk(2, "stopper video\n"); 252 dprintk(2, "stopper video\n");
405 spin_lock(&dev->slock); 253 spin_lock(&dev->slock);
406 cx25821_restart_video_queue(dev, 254 cx25821_restart_video_queue(dev,
407 &dev->channels[channel->i].vidq, channel); 255 &dev->channels[channel->i].dma_vidq, channel);
408 spin_unlock(&dev->slock); 256 spin_unlock(&dev->slock);
409 handled++; 257 handled++;
410 } 258 }
411 return handled; 259 return handled;
412} 260}
413 261
414void cx25821_videoioctl_unregister(struct cx25821_dev *dev) 262static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
415{
416 if (dev->ioctl_dev) {
417 if (video_is_registered(dev->ioctl_dev))
418 video_unregister_device(dev->ioctl_dev);
419 else
420 video_device_release(dev->ioctl_dev);
421
422 dev->ioctl_dev = NULL;
423 }
424}
425
426void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
427{
428 cx_clear(PCI_INT_MSK, 1);
429
430 if (dev->channels[chan_num].video_dev) {
431 if (video_is_registered(dev->channels[chan_num].video_dev))
432 video_unregister_device(
433 dev->channels[chan_num].video_dev);
434 else
435 video_device_release(
436 dev->channels[chan_num].video_dev);
437
438 dev->channels[chan_num].video_dev = NULL;
439
440 btcx_riscmem_free(dev->pci,
441 &dev->channels[chan_num].vidq.stopper);
442
443 pr_warn("device %d released!\n", chan_num);
444 }
445
446}
447
448int cx25821_video_register(struct cx25821_dev *dev)
449{
450 int err;
451 int i;
452
453 struct video_device cx25821_video_device = {
454 .name = "cx25821-video",
455 .fops = &video_fops,
456 .minor = -1,
457 .ioctl_ops = &video_ioctl_ops,
458 .tvnorms = CX25821_NORMS,
459 .current_norm = V4L2_STD_NTSC_M,
460 };
461
462 spin_lock_init(&dev->slock);
463
464 for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
465 cx25821_init_controls(dev, i);
466
467 cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
468 dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
469
470 dev->channels[i].sram_channels = &cx25821_sram_channels[i];
471 dev->channels[i].video_dev = NULL;
472 dev->channels[i].resources = 0;
473
474 cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
475
476 INIT_LIST_HEAD(&dev->channels[i].vidq.active);
477 INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
478
479 dev->channels[i].timeout_data.dev = dev;
480 dev->channels[i].timeout_data.channel =
481 &cx25821_sram_channels[i];
482 dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
483 dev->channels[i].vidq.timeout.data =
484 (unsigned long)&dev->channels[i].timeout_data;
485 init_timer(&dev->channels[i].vidq.timeout);
486
487 /* register v4l devices */
488 dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
489 &cx25821_video_device, "video");
490
491 err = video_register_device(dev->channels[i].video_dev,
492 VFL_TYPE_GRABBER, video_nr[dev->nr]);
493
494 if (err < 0)
495 goto fail_unreg;
496
497 }
498
499 /* set PCI interrupt */
500 cx_set(PCI_INT_MSK, 0xff);
501
502 /* initial device configuration */
503 mutex_lock(&dev->lock);
504#ifdef TUNER_FLAG
505 dev->tvnorm = cx25821_video_device.current_norm;
506 cx25821_set_tvnorm(dev, dev->tvnorm);
507#endif
508 mutex_unlock(&dev->lock);
509
510 return 0;
511
512fail_unreg:
513 cx25821_video_unregister(dev, i);
514 return err;
515}
516
517int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
518 unsigned int *size) 263 unsigned int *size)
519{ 264{
520 struct cx25821_fh *fh = q->priv_data; 265 struct cx25821_channel *chan = q->priv_data;
521 266
522 *size = fh->fmt->depth * fh->width * fh->height >> 3; 267 *size = chan->fmt->depth * chan->width * chan->height >> 3;
523 268
524 if (0 == *count) 269 if (0 == *count)
525 *count = 32; 270 *count = 32;
@@ -530,35 +275,34 @@ int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
530 return 0; 275 return 0;
531} 276}
532 277
533int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, 278static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
534 enum v4l2_field field) 279 enum v4l2_field field)
535{ 280{
536 struct cx25821_fh *fh = q->priv_data; 281 struct cx25821_channel *chan = q->priv_data;
537 struct cx25821_dev *dev = fh->dev; 282 struct cx25821_dev *dev = chan->dev;
538 struct cx25821_buffer *buf = 283 struct cx25821_buffer *buf =
539 container_of(vb, struct cx25821_buffer, vb); 284 container_of(vb, struct cx25821_buffer, vb);
540 int rc, init_buffer = 0; 285 int rc, init_buffer = 0;
541 u32 line0_offset; 286 u32 line0_offset;
542 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb); 287 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
543 int bpl_local = LINE_SIZE_D1; 288 int bpl_local = LINE_SIZE_D1;
544 int channel_opened = fh->channel_id;
545 289
546 BUG_ON(NULL == fh->fmt); 290 BUG_ON(NULL == chan->fmt);
547 if (fh->width < 48 || fh->width > 720 || 291 if (chan->width < 48 || chan->width > 720 ||
548 fh->height < 32 || fh->height > 576) 292 chan->height < 32 || chan->height > 576)
549 return -EINVAL; 293 return -EINVAL;
550 294
551 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3; 295 buf->vb.size = (chan->width * chan->height * chan->fmt->depth) >> 3;
552 296
553 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 297 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
554 return -EINVAL; 298 return -EINVAL;
555 299
556 if (buf->fmt != fh->fmt || 300 if (buf->fmt != chan->fmt ||
557 buf->vb.width != fh->width || 301 buf->vb.width != chan->width ||
558 buf->vb.height != fh->height || buf->vb.field != field) { 302 buf->vb.height != chan->height || buf->vb.field != field) {
559 buf->fmt = fh->fmt; 303 buf->fmt = chan->fmt;
560 buf->vb.width = fh->width; 304 buf->vb.width = chan->width;
561 buf->vb.height = fh->height; 305 buf->vb.height = chan->height;
562 buf->vb.field = field; 306 buf->vb.field = field;
563 init_buffer = 1; 307 init_buffer = 1;
564 } 308 }
@@ -575,34 +319,21 @@ int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
575 dprintk(1, "init_buffer=%d\n", init_buffer); 319 dprintk(1, "init_buffer=%d\n", init_buffer);
576 320
577 if (init_buffer) { 321 if (init_buffer) {
578 322 if (chan->pixel_formats == PIXEL_FRMT_411)
579 channel_opened = dev->channel_opened;
580 if (channel_opened < 0 || channel_opened > 7)
581 channel_opened = 7;
582
583 if (dev->channels[channel_opened].pixel_formats ==
584 PIXEL_FRMT_411)
585 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3; 323 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
586 else 324 else
587 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width); 325 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
588 326
589 if (dev->channels[channel_opened].pixel_formats == 327 if (chan->pixel_formats == PIXEL_FRMT_411) {
590 PIXEL_FRMT_411) {
591 bpl_local = buf->bpl; 328 bpl_local = buf->bpl;
592 } else { 329 } else {
593 bpl_local = buf->bpl; /* Default */ 330 bpl_local = buf->bpl; /* Default */
594 331
595 if (channel_opened >= 0 && channel_opened <= 7) { 332 if (chan->use_cif_resolution) {
596 if (dev->channels[channel_opened] 333 if (dev->tvnorm & V4L2_STD_625_50)
597 .use_cif_resolution) { 334 bpl_local = 352 << 1;
598 if (dev->tvnorm & V4L2_STD_PAL_BG || 335 else
599 dev->tvnorm & V4L2_STD_PAL_DK) 336 bpl_local = chan->cif_width << 1;
600 bpl_local = 352 << 1;
601 else
602 bpl_local = dev->channels[
603 channel_opened].
604 cif_width << 1;
605 }
606 } 337 }
607 } 338 }
608 339
@@ -645,8 +376,8 @@ int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
645 } 376 }
646 377
647 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n", 378 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
648 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth, 379 buf, buf->vb.i, chan->width, chan->height, chan->fmt->depth,
649 fh->fmt->name, (unsigned long)buf->risc.dma); 380 chan->fmt->name, (unsigned long)buf->risc.dma);
650 381
651 buf->vb.state = VIDEOBUF_PREPARED; 382 buf->vb.state = VIDEOBUF_PREPARED;
652 383
@@ -657,7 +388,7 @@ fail:
657 return rc; 388 return rc;
658} 389}
659 390
660void cx25821_buffer_release(struct videobuf_queue *q, 391static void cx25821_buffer_release(struct videobuf_queue *q,
661 struct videobuf_buffer *vb) 392 struct videobuf_buffer *vb)
662{ 393{
663 struct cx25821_buffer *buf = 394 struct cx25821_buffer *buf =
@@ -666,33 +397,11 @@ void cx25821_buffer_release(struct videobuf_queue *q,
666 cx25821_free_buffer(q, buf); 397 cx25821_free_buffer(q, buf);
667} 398}
668 399
669struct videobuf_queue *get_queue(struct cx25821_fh *fh) 400static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
670{
671 switch (fh->type) {
672 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
673 return &fh->vidq;
674 default:
675 BUG();
676 return NULL;
677 }
678}
679
680int cx25821_get_resource(struct cx25821_fh *fh, int resource)
681{
682 switch (fh->type) {
683 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
684 return resource;
685 default:
686 BUG();
687 return 0;
688 }
689}
690
691int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
692{ 401{
693 struct cx25821_fh *fh = file->private_data; 402 struct cx25821_channel *chan = video_drvdata(file);
694 403
695 return videobuf_mmap_mapper(get_queue(fh), vma); 404 return videobuf_mmap_mapper(&chan->vidq, vma);
696} 405}
697 406
698 407
@@ -701,9 +410,9 @@ static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
701 struct cx25821_buffer *buf = 410 struct cx25821_buffer *buf =
702 container_of(vb, struct cx25821_buffer, vb); 411 container_of(vb, struct cx25821_buffer, vb);
703 struct cx25821_buffer *prev; 412 struct cx25821_buffer *prev;
704 struct cx25821_fh *fh = vq->priv_data; 413 struct cx25821_channel *chan = vq->priv_data;
705 struct cx25821_dev *dev = fh->dev; 414 struct cx25821_dev *dev = chan->dev;
706 struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq; 415 struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
707 416
708 /* add jump to stopper */ 417 /* add jump to stopper */
709 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC); 418 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
@@ -720,8 +429,7 @@ static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
720 429
721 } else if (list_empty(&q->active)) { 430 } else if (list_empty(&q->active)) {
722 list_add_tail(&buf->vb.queue, &q->active); 431 list_add_tail(&buf->vb.queue, &q->active);
723 cx25821_start_video_dma(dev, q, buf, 432 cx25821_start_video_dma(dev, q, buf, chan->sram_channels);
724 dev->channels[fh->channel_id].sram_channels);
725 buf->vb.state = VIDEOBUF_ACTIVE; 433 buf->vb.state = VIDEOBUF_ACTIVE;
726 buf->count = q->count++; 434 buf->count = q->count++;
727 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT); 435 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
@@ -762,1183 +470,487 @@ static struct videobuf_queue_ops cx25821_video_qops = {
762 .buf_release = cx25821_buffer_release, 470 .buf_release = cx25821_buffer_release,
763}; 471};
764 472
765static int video_open(struct file *file)
766{
767 struct video_device *vdev = video_devdata(file);
768 struct cx25821_dev *h, *dev = video_drvdata(file);
769 struct cx25821_fh *fh;
770 struct list_head *list;
771 int minor = video_devdata(file)->minor;
772 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
773 u32 pix_format;
774 int ch_id = 0;
775 int i;
776
777 dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
778 v4l2_type_names[type]);
779
780 /* allocate + initialize per filehandle data */
781 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
782 if (NULL == fh)
783 return -ENOMEM;
784
785 mutex_lock(&cx25821_devlist_mutex);
786
787 list_for_each(list, &cx25821_devlist)
788 {
789 h = list_entry(list, struct cx25821_dev, devlist);
790
791 for (i = 0; i < MAX_VID_CHANNEL_NUM; i++) {
792 if (h->channels[i].video_dev &&
793 h->channels[i].video_dev->minor == minor) {
794 dev = h;
795 ch_id = i;
796 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
797 }
798 }
799 }
800
801 if (NULL == dev) {
802 mutex_unlock(&cx25821_devlist_mutex);
803 kfree(fh);
804 return -ENODEV;
805 }
806
807 file->private_data = fh;
808 fh->dev = dev;
809 fh->type = type;
810 fh->width = 720;
811 fh->channel_id = ch_id;
812
813 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
814 fh->height = 576;
815 else
816 fh->height = 480;
817
818 dev->channel_opened = fh->channel_id;
819 if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
820 pix_format = V4L2_PIX_FMT_Y41P;
821 else
822 pix_format = V4L2_PIX_FMT_YUYV;
823 fh->fmt = cx25821_format_by_fourcc(pix_format);
824
825 v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
826
827 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
828 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
829 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
830 fh, NULL);
831
832 dprintk(1, "post videobuf_queue_init()\n");
833 mutex_unlock(&cx25821_devlist_mutex);
834
835 return 0;
836}
837
838static ssize_t video_read(struct file *file, char __user * data, size_t count, 473static ssize_t video_read(struct file *file, char __user * data, size_t count,
839 loff_t *ppos) 474 loff_t *ppos)
840{ 475{
841 struct cx25821_fh *fh = file->private_data; 476 struct v4l2_fh *fh = file->private_data;
842 477 struct cx25821_channel *chan = video_drvdata(file);
843 switch (fh->type) { 478 struct cx25821_dev *dev = chan->dev;
844 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 479 int err = 0;
845 if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
846 return -EBUSY;
847 480
848 return videobuf_read_one(&fh->vidq, data, count, ppos, 481 if (mutex_lock_interruptible(&dev->lock))
849 file->f_flags & O_NONBLOCK); 482 return -ERESTARTSYS;
850 483 if (chan->streaming_fh && chan->streaming_fh != fh) {
851 default: 484 err = -EBUSY;
852 BUG(); 485 goto unlock;
853 return 0;
854 } 486 }
487 chan->streaming_fh = fh;
488
489 err = videobuf_read_one(&chan->vidq, data, count, ppos,
490 file->f_flags & O_NONBLOCK);
491unlock:
492 mutex_unlock(&dev->lock);
493 return err;
855} 494}
856 495
857static unsigned int video_poll(struct file *file, 496static unsigned int video_poll(struct file *file,
858 struct poll_table_struct *wait) 497 struct poll_table_struct *wait)
859{ 498{
860 struct cx25821_fh *fh = file->private_data; 499 struct cx25821_channel *chan = video_drvdata(file);
861 struct cx25821_buffer *buf; 500 unsigned long req_events = poll_requested_events(wait);
862 501 unsigned int res = v4l2_ctrl_poll(file, wait);
863 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) { 502
864 /* streaming capture */ 503 if (req_events & (POLLIN | POLLRDNORM))
865 if (list_empty(&fh->vidq.stream)) 504 res |= videobuf_poll_stream(file, &chan->vidq, wait);
866 return POLLERR; 505 return res;
867 buf = list_entry(fh->vidq.stream.next, 506
868 struct cx25821_buffer, vb.stream); 507 /* This doesn't belong in poll(). This can be done
869 } else { 508 * much better with vb2. We keep this code here as a
870 /* read() capture */ 509 * reminder.
871 buf = (struct cx25821_buffer *)fh->vidq.read_buf; 510 if ((res & POLLIN) && buf->vb.state == VIDEOBUF_DONE) {
872 if (NULL == buf) 511 struct cx25821_dev *dev = chan->dev;
873 return POLLERR; 512
874 } 513 if (dev && chan->use_cif_resolution) {
875 514 u8 cam_id = *((char *)buf->vb.baddr + 3);
876 poll_wait(file, &buf->vb.done, wait); 515 memcpy((char *)buf->vb.baddr,
877 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) { 516 (char *)buf->vb.baddr + (chan->width * 2),
878 if (buf->vb.state == VIDEOBUF_DONE) { 517 (chan->width * 2));
879 struct cx25821_dev *dev = fh->dev; 518 *((char *)buf->vb.baddr + 3) = cam_id;
880
881 if (dev && dev->channels[fh->channel_id]
882 .use_cif_resolution) {
883 u8 cam_id = *((char *)buf->vb.baddr + 3);
884 memcpy((char *)buf->vb.baddr,
885 (char *)buf->vb.baddr + (fh->width * 2),
886 (fh->width * 2));
887 *((char *)buf->vb.baddr + 3) = cam_id;
888 }
889 } 519 }
890
891 return POLLIN | POLLRDNORM;
892 } 520 }
893 521 */
894 return 0;
895} 522}
896 523
897static int video_release(struct file *file) 524static int video_release(struct file *file)
898{ 525{
899 struct cx25821_fh *fh = file->private_data; 526 struct cx25821_channel *chan = video_drvdata(file);
900 struct cx25821_dev *dev = fh->dev; 527 struct v4l2_fh *fh = file->private_data;
528 struct cx25821_dev *dev = chan->dev;
529 const struct sram_channel *sram_ch =
530 dev->channels[0].sram_channels;
901 531
532 mutex_lock(&dev->lock);
902 /* stop the risc engine and fifo */ 533 /* stop the risc engine and fifo */
903 cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */ 534 cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
904 535
905 /* stop video capture */ 536 /* stop video capture */
906 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) { 537 if (chan->streaming_fh == fh) {
907 videobuf_queue_cancel(&fh->vidq); 538 videobuf_queue_cancel(&chan->vidq);
908 cx25821_res_free(dev, fh, RESOURCE_VIDEO0); 539 chan->streaming_fh = NULL;
909 } 540 }
910 541
911 if (fh->vidq.read_buf) { 542 if (chan->vidq.read_buf) {
912 cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf); 543 cx25821_buffer_release(&chan->vidq, chan->vidq.read_buf);
913 kfree(fh->vidq.read_buf); 544 kfree(chan->vidq.read_buf);
914 } 545 }
915 546
916 videobuf_mmap_free(&fh->vidq); 547 videobuf_mmap_free(&chan->vidq);
917 548 mutex_unlock(&dev->lock);
918 v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
919 file->private_data = NULL;
920 kfree(fh);
921 549
922 return 0; 550 return v4l2_fh_release(file);
923} 551}
924 552
925static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) 553/* VIDEO IOCTLS */
926{
927 struct cx25821_fh *fh = priv;
928 struct cx25821_dev *dev = fh->dev;
929 554
930 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) 555static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
556 struct v4l2_fmtdesc *f)
557{
558 if (unlikely(f->index >= ARRAY_SIZE(formats)))
931 return -EINVAL; 559 return -EINVAL;
932 560
933 if (unlikely(i != fh->type)) 561 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
934 return -EINVAL; 562 f->pixelformat = formats[f->index].fourcc;
935 563
936 if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh, 564 return 0;
937 RESOURCE_VIDEO0)))) 565}
938 return -EBUSY; 566
567static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
568 struct v4l2_format *f)
569{
570 struct cx25821_channel *chan = video_drvdata(file);
571
572 f->fmt.pix.width = chan->width;
573 f->fmt.pix.height = chan->height;
574 f->fmt.pix.field = chan->vidq.field;
575 f->fmt.pix.pixelformat = chan->fmt->fourcc;
576 f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
577 f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
578 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
579 f->fmt.pix.priv = 0;
939 580
940 return videobuf_streamon(get_queue(fh)); 581 return 0;
941} 582}
942 583
943static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) 584static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
585 struct v4l2_format *f)
944{ 586{
945 struct cx25821_fh *fh = priv; 587 struct cx25821_channel *chan = video_drvdata(file);
946 struct cx25821_dev *dev = fh->dev; 588 struct cx25821_dev *dev = chan->dev;
947 int err, res; 589 const struct cx25821_fmt *fmt;
590 enum v4l2_field field = f->fmt.pix.field;
591 unsigned int maxw, maxh;
592 unsigned w;
948 593
949 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 594 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
950 return -EINVAL; 595 if (NULL == fmt)
951 if (i != fh->type)
952 return -EINVAL; 596 return -EINVAL;
597 maxw = 720;
598 maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
599
600 w = f->fmt.pix.width;
601 if (field != V4L2_FIELD_BOTTOM)
602 field = V4L2_FIELD_TOP;
603 if (w < 352) {
604 w = 176;
605 f->fmt.pix.height = maxh / 4;
606 } else if (w < 720) {
607 w = 352;
608 f->fmt.pix.height = maxh / 2;
609 } else {
610 w = 720;
611 f->fmt.pix.height = maxh;
612 field = V4L2_FIELD_INTERLACED;
613 }
614 f->fmt.pix.field = field;
615 f->fmt.pix.width = w;
616 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
617 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
618 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
619 f->fmt.pix.priv = 0;
953 620
954 res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
955 err = videobuf_streamoff(get_queue(fh));
956 if (err < 0)
957 return err;
958 cx25821_res_free(dev, fh, res);
959 return 0; 621 return 0;
960} 622}
961 623
962static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 624static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
963 struct v4l2_format *f) 625 struct v4l2_format *f)
964{ 626{
965 struct cx25821_fh *fh = priv; 627 struct cx25821_channel *chan = video_drvdata(file);
966 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; 628 struct cx25821_dev *dev = chan->dev;
967 struct v4l2_mbus_framefmt mbus_fmt;
968 int err;
969 int pix_format = PIXEL_FRMT_422; 629 int pix_format = PIXEL_FRMT_422;
630 int err;
970 631
971 if (fh) {
972 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
973 fh->prio);
974 if (0 != err)
975 return err;
976 }
977
978 dprintk(2, "%s()\n", __func__);
979 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f); 632 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
980 633
981 if (0 != err) 634 if (0 != err)
982 return err; 635 return err;
983 636
984 fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat); 637 chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
985 fh->vidq.field = f->fmt.pix.field; 638 chan->vidq.field = f->fmt.pix.field;
986 639 chan->width = f->fmt.pix.width;
987 /* check if width and height is valid based on set standard */ 640 chan->height = f->fmt.pix.height;
988 if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
989 fh->width = f->fmt.pix.width;
990
991 if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
992 fh->height = f->fmt.pix.height;
993 641
994 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P) 642 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
995 pix_format = PIXEL_FRMT_411; 643 pix_format = PIXEL_FRMT_411;
996 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
997 pix_format = PIXEL_FRMT_422;
998 else 644 else
999 return -EINVAL; 645 pix_format = PIXEL_FRMT_422;
1000 646
1001 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format); 647 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
1002 648
1003 /* check if cif resolution */ 649 /* check if cif resolution */
1004 if (fh->width == 320 || fh->width == 352) 650 if (chan->width == 320 || chan->width == 352)
1005 dev->channels[fh->channel_id].use_cif_resolution = 1; 651 chan->use_cif_resolution = 1;
1006 else 652 else
1007 dev->channels[fh->channel_id].use_cif_resolution = 0; 653 chan->use_cif_resolution = 0;
1008
1009 dev->channels[fh->channel_id].cif_width = fh->width;
1010 medusa_set_resolution(dev, fh->width, SRAM_CH00);
1011
1012 dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
1013 fh->height, fh->vidq.field);
1014 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1015 cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1016 654
655 chan->cif_width = chan->width;
656 medusa_set_resolution(dev, chan->width, SRAM_CH00);
1017 return 0; 657 return 0;
1018} 658}
1019 659
1020static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) 660static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1021{ 661{
1022 int ret_val = 0; 662 struct cx25821_channel *chan = video_drvdata(file);
1023 struct cx25821_fh *fh = priv;
1024 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1025 663
1026 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK); 664 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
665 return -EINVAL;
1027 666
1028 p->sequence = dev->channels[fh->channel_id].vidq.count; 667 if (chan->streaming_fh && chan->streaming_fh != priv)
668 return -EBUSY;
669 chan->streaming_fh = priv;
1029 670
1030 return ret_val; 671 return videobuf_streamon(&chan->vidq);
1031} 672}
1032 673
1033static int vidioc_log_status(struct file *file, void *priv) 674static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1034{ 675{
1035 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; 676 struct cx25821_channel *chan = video_drvdata(file);
1036 struct cx25821_fh *fh = priv;
1037 char name[32 + 2];
1038 677
1039 struct sram_channel *sram_ch = dev->channels[fh->channel_id] 678 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1040 .sram_channels; 679 return -EINVAL;
1041 u32 tmp = 0;
1042
1043 snprintf(name, sizeof(name), "%s/2", dev->name);
1044 pr_info("%s/2: ============ START LOG STATUS ============\n",
1045 dev->name);
1046 cx25821_call_all(dev, core, log_status);
1047 tmp = cx_read(sram_ch->dma_ctl);
1048 pr_info("Video input 0 is %s\n",
1049 (tmp & 0x11) ? "streaming" : "stopped");
1050 pr_info("%s/2: ============= END LOG STATUS =============\n",
1051 dev->name);
1052 return 0;
1053}
1054
1055static int vidioc_s_ctrl(struct file *file, void *priv,
1056 struct v4l2_control *ctl)
1057{
1058 struct cx25821_fh *fh = priv;
1059 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1060 int err;
1061 680
1062 if (fh) { 681 if (chan->streaming_fh && chan->streaming_fh != priv)
1063 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio, 682 return -EBUSY;
1064 fh->prio); 683 if (chan->streaming_fh == NULL)
1065 if (0 != err) 684 return 0;
1066 return err;
1067 }
1068 685
1069 return cx25821_set_control(dev, ctl, fh->channel_id); 686 chan->streaming_fh = NULL;
687 return videobuf_streamoff(&chan->vidq);
1070} 688}
1071 689
1072/* VIDEO IOCTLS */ 690static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1073int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1074 struct v4l2_format *f)
1075{ 691{
1076 struct cx25821_fh *fh = priv; 692 int ret_val = 0;
693 struct cx25821_channel *chan = video_drvdata(file);
1077 694
1078 f->fmt.pix.width = fh->width; 695 ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
1079 f->fmt.pix.height = fh->height; 696 p->sequence = chan->dma_vidq.count;
1080 f->fmt.pix.field = fh->vidq.field;
1081 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1082 f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
1083 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1084 697
1085 return 0; 698 return ret_val;
1086} 699}
1087 700
1088int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv, 701static int vidioc_log_status(struct file *file, void *priv)
1089 struct v4l2_format *f)
1090{ 702{
1091 struct cx25821_fmt *fmt; 703 struct cx25821_channel *chan = video_drvdata(file);
1092 enum v4l2_field field; 704 struct cx25821_dev *dev = chan->dev;
1093 unsigned int maxw, maxh; 705 const struct sram_channel *sram_ch = chan->sram_channels;
1094 706 u32 tmp = 0;
1095 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
1096 if (NULL == fmt)
1097 return -EINVAL;
1098
1099 field = f->fmt.pix.field;
1100 maxw = 720;
1101 maxh = 576;
1102
1103 if (V4L2_FIELD_ANY == field) {
1104 if (f->fmt.pix.height > maxh / 2)
1105 field = V4L2_FIELD_INTERLACED;
1106 else
1107 field = V4L2_FIELD_TOP;
1108 }
1109
1110 switch (field) {
1111 case V4L2_FIELD_TOP:
1112 case V4L2_FIELD_BOTTOM:
1113 maxh = maxh / 2;
1114 break;
1115 case V4L2_FIELD_INTERLACED:
1116 break;
1117 default:
1118 return -EINVAL;
1119 }
1120
1121 f->fmt.pix.field = field;
1122 if (f->fmt.pix.height < 32)
1123 f->fmt.pix.height = 32;
1124 if (f->fmt.pix.height > maxh)
1125 f->fmt.pix.height = maxh;
1126 if (f->fmt.pix.width < 48)
1127 f->fmt.pix.width = 48;
1128 if (f->fmt.pix.width > maxw)
1129 f->fmt.pix.width = maxw;
1130 f->fmt.pix.width &= ~0x03;
1131 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
1132 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1133 707
708 tmp = cx_read(sram_ch->dma_ctl);
709 pr_info("Video input 0 is %s\n",
710 (tmp & 0x11) ? "streaming" : "stopped");
1134 return 0; 711 return 0;
1135} 712}
1136 713
1137int cx25821_vidioc_querycap(struct file *file, void *priv, 714
715static int cx25821_vidioc_querycap(struct file *file, void *priv,
1138 struct v4l2_capability *cap) 716 struct v4l2_capability *cap)
1139{ 717{
1140 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; 718 struct cx25821_channel *chan = video_drvdata(file);
719 struct cx25821_dev *dev = chan->dev;
720 const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
721 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
722 const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
1141 723
1142 strcpy(cap->driver, "cx25821"); 724 strcpy(cap->driver, "cx25821");
1143 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card)); 725 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1144 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci)); 726 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1145 cap->version = CX25821_VERSION_CODE; 727 if (chan->id >= VID_CHANNEL_NUM)
1146 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | 728 cap->device_caps = cap_output;
1147 V4L2_CAP_STREAMING; 729 else
1148 if (UNSET != dev->tuner_type) 730 cap->device_caps = cap_input;
1149 cap->capabilities |= V4L2_CAP_TUNER; 731 cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
1150 return 0;
1151}
1152
1153int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1154 struct v4l2_fmtdesc *f)
1155{
1156 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1157 return -EINVAL;
1158
1159 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1160 f->pixelformat = formats[f->index].fourcc;
1161
1162 return 0; 732 return 0;
1163} 733}
1164 734
1165int cx25821_vidioc_reqbufs(struct file *file, void *priv, 735static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1166 struct v4l2_requestbuffers *p) 736 struct v4l2_requestbuffers *p)
1167{ 737{
1168 struct cx25821_fh *fh = priv; 738 struct cx25821_channel *chan = video_drvdata(file);
1169 return videobuf_reqbufs(get_queue(fh), p); 739
740 return videobuf_reqbufs(&chan->vidq, p);
1170} 741}
1171 742
1172int cx25821_vidioc_querybuf(struct file *file, void *priv, 743static int cx25821_vidioc_querybuf(struct file *file, void *priv,
1173 struct v4l2_buffer *p) 744 struct v4l2_buffer *p)
1174{ 745{
1175 struct cx25821_fh *fh = priv; 746 struct cx25821_channel *chan = video_drvdata(file);
1176 return videobuf_querybuf(get_queue(fh), p);
1177}
1178 747
1179int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) 748 return videobuf_querybuf(&chan->vidq, p);
1180{
1181 struct cx25821_fh *fh = priv;
1182 return videobuf_qbuf(get_queue(fh), p);
1183} 749}
1184 750
1185int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p) 751static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1186{ 752{
1187 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev; 753 struct cx25821_channel *chan = video_drvdata(file);
1188 struct cx25821_fh *fh = f;
1189
1190 *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1191 754
1192 return 0; 755 return videobuf_qbuf(&chan->vidq, p);
1193} 756}
1194 757
1195int cx25821_vidioc_s_priority(struct file *file, void *f, 758static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1196 enum v4l2_priority prio)
1197{ 759{
1198 struct cx25821_fh *fh = f; 760 struct cx25821_channel *chan = video_drvdata(file);
1199 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1200 761
1201 return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio, 762 *tvnorms = chan->dev->tvnorm;
1202 prio); 763 return 0;
1203} 764}
1204 765
1205#ifdef TUNER_FLAG
1206int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms) 766int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1207{ 767{
1208 struct cx25821_fh *fh = priv; 768 struct cx25821_channel *chan = video_drvdata(file);
1209 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; 769 struct cx25821_dev *dev = chan->dev;
1210 int err;
1211
1212 dprintk(1, "%s()\n", __func__);
1213
1214 if (fh) {
1215 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1216 fh->prio);
1217 if (0 != err)
1218 return err;
1219 }
1220 770
1221 if (dev->tvnorm == tvnorms) 771 if (dev->tvnorm == tvnorms)
1222 return 0; 772 return 0;
1223 773
1224 mutex_lock(&dev->lock); 774 dev->tvnorm = tvnorms;
1225 cx25821_set_tvnorm(dev, tvnorms); 775 chan->width = 720;
1226 mutex_unlock(&dev->lock); 776 chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
1227 777
1228 medusa_set_videostandard(dev); 778 medusa_set_videostandard(dev);
1229 779
1230 return 0; 780 return 0;
1231} 781}
1232#endif
1233 782
1234int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i) 783static int cx25821_vidioc_enum_input(struct file *file, void *priv,
784 struct v4l2_input *i)
1235{ 785{
1236 static const char * const iname[] = { 786 if (i->index)
1237 [CX25821_VMUX_COMPOSITE] = "Composite",
1238 [CX25821_VMUX_SVIDEO] = "S-Video",
1239 [CX25821_VMUX_DEBUG] = "for debug only",
1240 };
1241 unsigned int n;
1242 dprintk(1, "%s()\n", __func__);
1243
1244 n = i->index;
1245 if (n >= 2)
1246 return -EINVAL;
1247
1248 if (0 == INPUT(n)->type)
1249 return -EINVAL; 787 return -EINVAL;
1250 788
1251 i->type = V4L2_INPUT_TYPE_CAMERA; 789 i->type = V4L2_INPUT_TYPE_CAMERA;
1252 strcpy(i->name, iname[INPUT(n)->type]);
1253
1254 i->std = CX25821_NORMS; 790 i->std = CX25821_NORMS;
791 strcpy(i->name, "Composite");
1255 return 0; 792 return 0;
1256} 793}
1257 794
1258int cx25821_vidioc_enum_input(struct file *file, void *priv, 795static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1259 struct v4l2_input *i)
1260{
1261 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1262 dprintk(1, "%s()\n", __func__);
1263 return cx25821_enum_input(dev, i);
1264}
1265
1266int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1267{
1268 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1269
1270 *i = dev->input;
1271 dprintk(1, "%s(): returns %d\n", __func__, *i);
1272 return 0;
1273}
1274
1275int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1276{
1277 struct cx25821_fh *fh = priv;
1278 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1279 int err;
1280
1281 dprintk(1, "%s(%d)\n", __func__, i);
1282
1283 if (fh) {
1284 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1285 fh->prio);
1286 if (0 != err)
1287 return err;
1288 }
1289
1290 if (i >= CX25821_NR_INPUT) {
1291 dprintk(1, "%s(): -EINVAL\n", __func__);
1292 return -EINVAL;
1293 }
1294
1295 mutex_lock(&dev->lock);
1296 cx25821_video_mux(dev, i);
1297 mutex_unlock(&dev->lock);
1298 return 0;
1299}
1300
1301#ifdef TUNER_FLAG
1302int cx25821_vidioc_g_frequency(struct file *file, void *priv,
1303 struct v4l2_frequency *f)
1304{
1305 struct cx25821_fh *fh = priv;
1306 struct cx25821_dev *dev = fh->dev;
1307
1308 f->frequency = dev->freq;
1309
1310 cx25821_call_all(dev, tuner, g_frequency, f);
1311
1312 return 0;
1313}
1314
1315int cx25821_set_freq(struct cx25821_dev *dev, const struct v4l2_frequency *f)
1316{
1317 mutex_lock(&dev->lock);
1318 dev->freq = f->frequency;
1319
1320 cx25821_call_all(dev, tuner, s_frequency, f);
1321
1322 /* When changing channels it is required to reset TVAUDIO */
1323 msleep(10);
1324
1325 mutex_unlock(&dev->lock);
1326
1327 return 0;
1328}
1329
1330int cx25821_vidioc_s_frequency(struct file *file, void *priv,
1331 const struct v4l2_frequency *f)
1332{
1333 struct cx25821_fh *fh = priv;
1334 struct cx25821_dev *dev;
1335 int err;
1336
1337 if (fh) {
1338 dev = fh->dev;
1339 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1340 fh->prio);
1341 if (0 != err)
1342 return err;
1343 } else {
1344 pr_err("Invalid fh pointer!\n");
1345 return -EINVAL;
1346 }
1347
1348 return cx25821_set_freq(dev, f);
1349}
1350#endif
1351
1352#ifdef CONFIG_VIDEO_ADV_DEBUG
1353int cx25821_vidioc_g_register(struct file *file, void *fh,
1354 struct v4l2_dbg_register *reg)
1355{
1356 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1357
1358 if (!v4l2_chip_match_host(&reg->match))
1359 return -EINVAL;
1360
1361 cx25821_call_all(dev, core, g_register, reg);
1362
1363 return 0;
1364}
1365
1366int cx25821_vidioc_s_register(struct file *file, void *fh,
1367 const struct v4l2_dbg_register *reg)
1368{
1369 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1370
1371 if (!v4l2_chip_match_host(&reg->match))
1372 return -EINVAL;
1373
1374 cx25821_call_all(dev, core, s_register, reg);
1375
1376 return 0;
1377}
1378
1379#endif
1380
1381#ifdef TUNER_FLAG
1382int cx25821_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1383{
1384 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1385
1386 if (unlikely(UNSET == dev->tuner_type))
1387 return -EINVAL;
1388 if (0 != t->index)
1389 return -EINVAL;
1390
1391 strcpy(t->name, "Television");
1392 t->type = V4L2_TUNER_ANALOG_TV;
1393 t->capability = V4L2_TUNER_CAP_NORM;
1394 t->rangehigh = 0xffffffffUL;
1395
1396 t->signal = 0xffff; /* LOCKED */
1397 return 0;
1398}
1399
1400int cx25821_vidioc_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1401{
1402 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1403 struct cx25821_fh *fh = priv;
1404 int err;
1405
1406 if (fh) {
1407 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1408 fh->prio);
1409 if (0 != err)
1410 return err;
1411 }
1412
1413 dprintk(1, "%s()\n", __func__);
1414 if (UNSET == dev->tuner_type)
1415 return -EINVAL;
1416 if (0 != t->index)
1417 return -EINVAL;
1418
1419 return 0;
1420}
1421
1422#endif
1423/*****************************************************************************/
1424static const struct v4l2_queryctrl no_ctl = {
1425 .name = "42",
1426 .flags = V4L2_CTRL_FLAG_DISABLED,
1427};
1428
1429static struct v4l2_queryctrl cx25821_ctls[] = {
1430 /* --- video --- */
1431 {
1432 .id = V4L2_CID_BRIGHTNESS,
1433 .name = "Brightness",
1434 .minimum = 0,
1435 .maximum = 10000,
1436 .step = 1,
1437 .default_value = 6200,
1438 .type = V4L2_CTRL_TYPE_INTEGER,
1439 }, {
1440 .id = V4L2_CID_CONTRAST,
1441 .name = "Contrast",
1442 .minimum = 0,
1443 .maximum = 10000,
1444 .step = 1,
1445 .default_value = 5000,
1446 .type = V4L2_CTRL_TYPE_INTEGER,
1447 }, {
1448 .id = V4L2_CID_SATURATION,
1449 .name = "Saturation",
1450 .minimum = 0,
1451 .maximum = 10000,
1452 .step = 1,
1453 .default_value = 5000,
1454 .type = V4L2_CTRL_TYPE_INTEGER,
1455 }, {
1456 .id = V4L2_CID_HUE,
1457 .name = "Hue",
1458 .minimum = 0,
1459 .maximum = 10000,
1460 .step = 1,
1461 .default_value = 5000,
1462 .type = V4L2_CTRL_TYPE_INTEGER,
1463 }
1464};
1465static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1466
1467static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1468{ 796{
1469 int i; 797 *i = 0;
1470
1471 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1472 return -EINVAL;
1473 for (i = 0; i < CX25821_CTLS; i++)
1474 if (cx25821_ctls[i].id == qctrl->id)
1475 break;
1476 if (i == CX25821_CTLS) {
1477 *qctrl = no_ctl;
1478 return 0;
1479 }
1480 *qctrl = cx25821_ctls[i];
1481 return 0; 798 return 0;
1482} 799}
1483 800
1484int cx25821_vidioc_queryctrl(struct file *file, void *priv, 801static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1485 struct v4l2_queryctrl *qctrl)
1486{
1487 return cx25821_ctrl_query(qctrl);
1488}
1489
1490/* ------------------------------------------------------------------ */
1491/* VIDEO CTRL IOCTLS */
1492
1493static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1494{ 802{
1495 unsigned int i; 803 return i ? -EINVAL : 0;
1496
1497 for (i = 0; i < CX25821_CTLS; i++)
1498 if (cx25821_ctls[i].id == id)
1499 return cx25821_ctls + i;
1500 return NULL;
1501} 804}
1502 805
1503int cx25821_vidioc_g_ctrl(struct file *file, void *priv, 806static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
1504 struct v4l2_control *ctl)
1505{ 807{
1506 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; 808 struct cx25821_channel *chan =
1507 struct cx25821_fh *fh = priv; 809 container_of(ctrl->handler, struct cx25821_channel, hdl);
810 struct cx25821_dev *dev = chan->dev;
1508 811
1509 const struct v4l2_queryctrl *ctrl; 812 switch (ctrl->id) {
1510
1511 ctrl = ctrl_by_id(ctl->id);
1512
1513 if (NULL == ctrl)
1514 return -EINVAL;
1515 switch (ctl->id) {
1516 case V4L2_CID_BRIGHTNESS: 813 case V4L2_CID_BRIGHTNESS:
1517 ctl->value = dev->channels[fh->channel_id].ctl_bright; 814 medusa_set_brightness(dev, ctrl->val, chan->id);
1518 break; 815 break;
1519 case V4L2_CID_HUE: 816 case V4L2_CID_HUE:
1520 ctl->value = dev->channels[fh->channel_id].ctl_hue; 817 medusa_set_hue(dev, ctrl->val, chan->id);
1521 break; 818 break;
1522 case V4L2_CID_CONTRAST: 819 case V4L2_CID_CONTRAST:
1523 ctl->value = dev->channels[fh->channel_id].ctl_contrast; 820 medusa_set_contrast(dev, ctrl->val, chan->id);
1524 break; 821 break;
1525 case V4L2_CID_SATURATION: 822 case V4L2_CID_SATURATION:
1526 ctl->value = dev->channels[fh->channel_id].ctl_saturation; 823 medusa_set_saturation(dev, ctrl->val, chan->id);
1527 break;
1528 }
1529 return 0;
1530}
1531
1532int cx25821_set_control(struct cx25821_dev *dev,
1533 struct v4l2_control *ctl, int chan_num)
1534{
1535 int err;
1536 const struct v4l2_queryctrl *ctrl;
1537
1538 err = -EINVAL;
1539
1540 ctrl = ctrl_by_id(ctl->id);
1541
1542 if (NULL == ctrl)
1543 return err;
1544
1545 switch (ctrl->type) {
1546 case V4L2_CTRL_TYPE_BOOLEAN:
1547 case V4L2_CTRL_TYPE_MENU:
1548 case V4L2_CTRL_TYPE_INTEGER:
1549 if (ctl->value < ctrl->minimum)
1550 ctl->value = ctrl->minimum;
1551 if (ctl->value > ctrl->maximum)
1552 ctl->value = ctrl->maximum;
1553 break; 824 break;
1554 default: 825 default:
1555 /* nothing */ ;
1556 }
1557
1558 switch (ctl->id) {
1559 case V4L2_CID_BRIGHTNESS:
1560 dev->channels[chan_num].ctl_bright = ctl->value;
1561 medusa_set_brightness(dev, ctl->value, chan_num);
1562 break;
1563 case V4L2_CID_HUE:
1564 dev->channels[chan_num].ctl_hue = ctl->value;
1565 medusa_set_hue(dev, ctl->value, chan_num);
1566 break;
1567 case V4L2_CID_CONTRAST:
1568 dev->channels[chan_num].ctl_contrast = ctl->value;
1569 medusa_set_contrast(dev, ctl->value, chan_num);
1570 break;
1571 case V4L2_CID_SATURATION:
1572 dev->channels[chan_num].ctl_saturation = ctl->value;
1573 medusa_set_saturation(dev, ctl->value, chan_num);
1574 break;
1575 }
1576
1577 err = 0;
1578
1579 return err;
1580}
1581
1582static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1583{
1584 struct v4l2_control ctrl;
1585 int i;
1586 for (i = 0; i < CX25821_CTLS; i++) {
1587 ctrl.id = cx25821_ctls[i].id;
1588 ctrl.value = cx25821_ctls[i].default_value;
1589
1590 cx25821_set_control(dev, &ctrl, chan_num);
1591 }
1592}
1593
1594int cx25821_vidioc_cropcap(struct file *file, void *priv,
1595 struct v4l2_cropcap *cropcap)
1596{
1597 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1598
1599 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1600 return -EINVAL; 826 return -EINVAL;
1601 cropcap->bounds.top = 0;
1602 cropcap->bounds.left = 0;
1603 cropcap->bounds.width = 720;
1604 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1605 cropcap->pixelaspect.numerator =
1606 dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1607 cropcap->pixelaspect.denominator =
1608 dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1609 cropcap->defrect = cropcap->bounds;
1610 return 0;
1611}
1612
1613int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
1614{
1615 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1616 struct cx25821_fh *fh = priv;
1617 int err;
1618
1619 if (fh) {
1620 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1621 fh->prio);
1622 if (0 != err)
1623 return err;
1624 } 827 }
1625 /* cx25821_vidioc_s_crop not supported */ 828 return 0;
1626 return -EINVAL;
1627}
1628
1629int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1630{
1631 /* cx25821_vidioc_g_crop not supported */
1632 return -EINVAL;
1633} 829}
1634 830
1635int cx25821_vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm) 831static int cx25821_vidioc_enum_output(struct file *file, void *priv,
832 struct v4l2_output *o)
1636{ 833{
1637 /* medusa does not support video standard sensing of current input */ 834 if (o->index)
1638 *norm = CX25821_NORMS; 835 return -EINVAL;
1639 836
837 o->type = V4L2_INPUT_TYPE_CAMERA;
838 o->std = CX25821_NORMS;
839 strcpy(o->name, "Composite");
1640 return 0; 840 return 0;
1641} 841}
1642 842
1643int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm) 843static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
1644{ 844{
1645 if (tvnorm == V4L2_STD_PAL_BG) { 845 *o = 0;
1646 if (width == 352 || width == 720)
1647 return 1;
1648 else
1649 return 0;
1650 }
1651
1652 if (tvnorm == V4L2_STD_NTSC_M) {
1653 if (width == 320 || width == 352 || width == 720)
1654 return 1;
1655 else
1656 return 0;
1657 }
1658 return 0; 846 return 0;
1659} 847}
1660 848
1661int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm) 849static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
1662{ 850{
1663 if (tvnorm == V4L2_STD_PAL_BG) { 851 return o ? -EINVAL : 0;
1664 if (height == 576 || height == 288)
1665 return 1;
1666 else
1667 return 0;
1668 }
1669
1670 if (tvnorm == V4L2_STD_NTSC_M) {
1671 if (height == 480 || height == 240)
1672 return 1;
1673 else
1674 return 0;
1675 }
1676
1677 return 0;
1678} 852}
1679 853
1680static long video_ioctl_upstream9(struct file *file, unsigned int cmd, 854static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
1681 unsigned long arg) 855 struct v4l2_format *f)
1682{ 856{
1683 struct cx25821_fh *fh = file->private_data; 857 struct cx25821_channel *chan = video_drvdata(file);
1684 struct cx25821_dev *dev = fh->dev; 858 struct cx25821_dev *dev = chan->dev;
1685 int command = 0; 859 const struct cx25821_fmt *fmt;
1686 struct upstream_user_struct *data_from_user;
1687
1688 data_from_user = (struct upstream_user_struct *)arg;
1689
1690 if (!data_from_user) {
1691 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1692 return 0;
1693 }
1694
1695 command = data_from_user->command;
1696
1697 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1698 return 0;
1699
1700 dev->input_filename = data_from_user->input_filename;
1701 dev->input_audiofilename = data_from_user->input_filename;
1702 dev->vid_stdname = data_from_user->vid_stdname;
1703 dev->pixel_format = data_from_user->pixel_format;
1704 dev->channel_select = data_from_user->channel_select;
1705 dev->command = data_from_user->command;
1706
1707 switch (command) {
1708 case UPSTREAM_START_VIDEO:
1709 cx25821_start_upstream_video_ch1(dev, data_from_user);
1710 break;
1711
1712 case UPSTREAM_STOP_VIDEO:
1713 cx25821_stop_upstream_video_ch1(dev);
1714 break;
1715 }
1716 860
861 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
862 if (NULL == fmt)
863 return -EINVAL;
864 f->fmt.pix.width = 720;
865 f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
866 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
867 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
868 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
869 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
870 f->fmt.pix.priv = 0;
1717 return 0; 871 return 0;
1718} 872}
1719 873
1720static long video_ioctl_upstream10(struct file *file, unsigned int cmd, 874static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
1721 unsigned long arg) 875 struct v4l2_format *f)
1722{ 876{
1723 struct cx25821_fh *fh = file->private_data; 877 struct cx25821_channel *chan = video_drvdata(file);
1724 struct cx25821_dev *dev = fh->dev; 878 int err;
1725 int command = 0;
1726 struct upstream_user_struct *data_from_user;
1727
1728 data_from_user = (struct upstream_user_struct *)arg;
1729
1730 if (!data_from_user) {
1731 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1732 return 0;
1733 }
1734
1735 command = data_from_user->command;
1736
1737 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1738 return 0;
1739
1740 dev->input_filename_ch2 = data_from_user->input_filename;
1741 dev->input_audiofilename = data_from_user->input_filename;
1742 dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1743 dev->pixel_format_ch2 = data_from_user->pixel_format;
1744 dev->channel_select_ch2 = data_from_user->channel_select;
1745 dev->command_ch2 = data_from_user->command;
1746 879
1747 switch (command) { 880 err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
1748 case UPSTREAM_START_VIDEO:
1749 cx25821_start_upstream_video_ch2(dev, data_from_user);
1750 break;
1751 881
1752 case UPSTREAM_STOP_VIDEO: 882 if (0 != err)
1753 cx25821_stop_upstream_video_ch2(dev); 883 return err;
1754 break;
1755 }
1756 884
885 chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
886 chan->vidq.field = f->fmt.pix.field;
887 chan->width = f->fmt.pix.width;
888 chan->height = f->fmt.pix.height;
889 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
890 chan->pixel_formats = PIXEL_FRMT_411;
891 else
892 chan->pixel_formats = PIXEL_FRMT_422;
1757 return 0; 893 return 0;
1758} 894}
1759 895
1760static long video_ioctl_upstream11(struct file *file, unsigned int cmd, 896static ssize_t video_write(struct file *file, const char __user *data, size_t count,
1761 unsigned long arg) 897 loff_t *ppos)
1762{ 898{
1763 struct cx25821_fh *fh = file->private_data; 899 struct cx25821_channel *chan = video_drvdata(file);
1764 struct cx25821_dev *dev = fh->dev; 900 struct cx25821_dev *dev = chan->dev;
1765 int command = 0; 901 struct v4l2_fh *fh = file->private_data;
1766 struct upstream_user_struct *data_from_user; 902 int err = 0;
1767
1768 data_from_user = (struct upstream_user_struct *)arg;
1769 903
1770 if (!data_from_user) { 904 if (mutex_lock_interruptible(&dev->lock))
1771 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__); 905 return -ERESTARTSYS;
1772 return 0; 906 if (chan->streaming_fh && chan->streaming_fh != fh) {
907 err = -EBUSY;
908 goto unlock;
1773 } 909 }
1774 910 if (!chan->streaming_fh) {
1775 command = data_from_user->command; 911 err = cx25821_vidupstream_init(chan, chan->pixel_formats);
1776 912 if (err)
1777 if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO) 913 goto unlock;
1778 return 0; 914 chan->streaming_fh = fh;
1779
1780 dev->input_filename = data_from_user->input_filename;
1781 dev->input_audiofilename = data_from_user->input_filename;
1782 dev->vid_stdname = data_from_user->vid_stdname;
1783 dev->pixel_format = data_from_user->pixel_format;
1784 dev->channel_select = data_from_user->channel_select;
1785 dev->command = data_from_user->command;
1786
1787 switch (command) {
1788 case UPSTREAM_START_AUDIO:
1789 cx25821_start_upstream_audio(dev, data_from_user);
1790 break;
1791
1792 case UPSTREAM_STOP_AUDIO:
1793 cx25821_stop_upstream_audio(dev);
1794 break;
1795 } 915 }
1796 916
1797 return 0; 917 err = cx25821_write_frame(chan, data, count);
918 count -= err;
919 *ppos += err;
920
921unlock:
922 mutex_unlock(&dev->lock);
923 return err;
1798} 924}
1799 925
1800static long video_ioctl_set(struct file *file, unsigned int cmd, 926static int video_out_release(struct file *file)
1801 unsigned long arg)
1802{ 927{
1803 struct cx25821_fh *fh = file->private_data; 928 struct cx25821_channel *chan = video_drvdata(file);
1804 struct cx25821_dev *dev = fh->dev; 929 struct cx25821_dev *dev = chan->dev;
1805 struct downstream_user_struct *data_from_user; 930 struct v4l2_fh *fh = file->private_data;
1806 int command;
1807 int width = 720;
1808 int selected_channel = 0;
1809 int pix_format = 0;
1810 int i = 0;
1811 int cif_enable = 0;
1812 int cif_width = 0;
1813
1814 data_from_user = (struct downstream_user_struct *)arg;
1815
1816 if (!data_from_user) {
1817 pr_err("%s(): User data is INVALID. Returning\n", __func__);
1818 return 0;
1819 }
1820
1821 command = data_from_user->command;
1822
1823 if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1824 && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1825 && command != REG_WRITE && command != MEDUSA_READ
1826 && command != MEDUSA_WRITE) {
1827 return 0;
1828 }
1829
1830 switch (command) {
1831 case SET_VIDEO_STD:
1832 if (!strcmp(data_from_user->vid_stdname, "PAL"))
1833 dev->tvnorm = V4L2_STD_PAL_BG;
1834 else
1835 dev->tvnorm = V4L2_STD_NTSC_M;
1836 medusa_set_videostandard(dev);
1837 break;
1838
1839 case SET_PIXEL_FORMAT:
1840 selected_channel = data_from_user->decoder_select;
1841 pix_format = data_from_user->pixel_format;
1842
1843 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1844 selected_channel -= 4;
1845 selected_channel = selected_channel % 8;
1846 }
1847
1848 if (selected_channel >= 0)
1849 cx25821_set_pixel_format(dev, selected_channel,
1850 pix_format);
1851
1852 break;
1853
1854 case ENABLE_CIF_RESOLUTION:
1855 selected_channel = data_from_user->decoder_select;
1856 cif_enable = data_from_user->cif_resolution_enable;
1857 cif_width = data_from_user->cif_width;
1858
1859 if (cif_enable) {
1860 if (dev->tvnorm & V4L2_STD_PAL_BG
1861 || dev->tvnorm & V4L2_STD_PAL_DK) {
1862 width = 352;
1863 } else {
1864 width = cif_width;
1865 if (cif_width != 320 && cif_width != 352)
1866 width = 320;
1867 }
1868 }
1869
1870 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1871 selected_channel -= 4;
1872 selected_channel = selected_channel % 8;
1873 }
1874
1875 if (selected_channel <= 7 && selected_channel >= 0) {
1876 dev->channels[selected_channel].use_cif_resolution =
1877 cif_enable;
1878 dev->channels[selected_channel].cif_width = width;
1879 } else {
1880 for (i = 0; i < VID_CHANNEL_NUM; i++) {
1881 dev->channels[i].use_cif_resolution =
1882 cif_enable;
1883 dev->channels[i].cif_width = width;
1884 }
1885 }
1886 931
1887 medusa_set_resolution(dev, width, selected_channel); 932 mutex_lock(&dev->lock);
1888 break; 933 if (chan->streaming_fh == fh) {
1889 case REG_READ: 934 cx25821_stop_upstream_video(chan);
1890 data_from_user->reg_data = cx_read(data_from_user->reg_address); 935 chan->streaming_fh = NULL;
1891 break;
1892 case REG_WRITE:
1893 cx_write(data_from_user->reg_address, data_from_user->reg_data);
1894 break;
1895 case MEDUSA_READ:
1896 cx25821_i2c_read(&dev->i2c_bus[0],
1897 (u16) data_from_user->reg_address,
1898 &data_from_user->reg_data);
1899 break;
1900 case MEDUSA_WRITE:
1901 cx25821_i2c_write(&dev->i2c_bus[0],
1902 (u16) data_from_user->reg_address,
1903 data_from_user->reg_data);
1904 break;
1905 } 936 }
937 mutex_unlock(&dev->lock);
1906 938
1907 return 0; 939 return v4l2_fh_release(file);
1908} 940}
1909 941
1910static long cx25821_video_ioctl(struct file *file, 942static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
1911 unsigned int cmd, unsigned long arg) 943 .s_ctrl = cx25821_s_ctrl,
1912{ 944};
1913 int ret = 0;
1914
1915 struct cx25821_fh *fh = file->private_data;
1916
1917 /* check to see if it's the video upstream */
1918 if (fh->channel_id == SRAM_CH09) {
1919 ret = video_ioctl_upstream9(file, cmd, arg);
1920 return ret;
1921 } else if (fh->channel_id == SRAM_CH10) {
1922 ret = video_ioctl_upstream10(file, cmd, arg);
1923 return ret;
1924 } else if (fh->channel_id == SRAM_CH11) {
1925 ret = video_ioctl_upstream11(file, cmd, arg);
1926 ret = video_ioctl_set(file, cmd, arg);
1927 return ret;
1928 }
1929
1930 return video_ioctl2(file, cmd, arg);
1931}
1932 945
1933/* exported stuff */
1934static const struct v4l2_file_operations video_fops = { 946static const struct v4l2_file_operations video_fops = {
1935 .owner = THIS_MODULE, 947 .owner = THIS_MODULE,
1936 .open = video_open, 948 .open = v4l2_fh_open,
1937 .release = video_release, 949 .release = video_release,
1938 .read = video_read, 950 .read = video_read,
1939 .poll = video_poll, 951 .poll = video_poll,
1940 .mmap = cx25821_video_mmap, 952 .mmap = cx25821_video_mmap,
1941 .ioctl = cx25821_video_ioctl, 953 .unlocked_ioctl = video_ioctl2,
1942}; 954};
1943 955
1944static const struct v4l2_ioctl_ops video_ioctl_ops = { 956static const struct v4l2_ioctl_ops video_ioctl_ops = {
@@ -1951,40 +963,170 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
1951 .vidioc_querybuf = cx25821_vidioc_querybuf, 963 .vidioc_querybuf = cx25821_vidioc_querybuf,
1952 .vidioc_qbuf = cx25821_vidioc_qbuf, 964 .vidioc_qbuf = cx25821_vidioc_qbuf,
1953 .vidioc_dqbuf = vidioc_dqbuf, 965 .vidioc_dqbuf = vidioc_dqbuf,
1954#ifdef TUNER_FLAG 966 .vidioc_g_std = cx25821_vidioc_g_std,
1955 .vidioc_s_std = cx25821_vidioc_s_std, 967 .vidioc_s_std = cx25821_vidioc_s_std,
1956 .vidioc_querystd = cx25821_vidioc_querystd,
1957#endif
1958 .vidioc_cropcap = cx25821_vidioc_cropcap,
1959 .vidioc_s_crop = cx25821_vidioc_s_crop,
1960 .vidioc_g_crop = cx25821_vidioc_g_crop,
1961 .vidioc_enum_input = cx25821_vidioc_enum_input, 968 .vidioc_enum_input = cx25821_vidioc_enum_input,
1962 .vidioc_g_input = cx25821_vidioc_g_input, 969 .vidioc_g_input = cx25821_vidioc_g_input,
1963 .vidioc_s_input = cx25821_vidioc_s_input, 970 .vidioc_s_input = cx25821_vidioc_s_input,
1964 .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1965 .vidioc_s_ctrl = vidioc_s_ctrl,
1966 .vidioc_queryctrl = cx25821_vidioc_queryctrl,
1967 .vidioc_streamon = vidioc_streamon, 971 .vidioc_streamon = vidioc_streamon,
1968 .vidioc_streamoff = vidioc_streamoff, 972 .vidioc_streamoff = vidioc_streamoff,
1969 .vidioc_log_status = vidioc_log_status, 973 .vidioc_log_status = vidioc_log_status,
1970 .vidioc_g_priority = cx25821_vidioc_g_priority, 974 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1971 .vidioc_s_priority = cx25821_vidioc_s_priority, 975 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1972#ifdef TUNER_FLAG
1973 .vidioc_g_tuner = cx25821_vidioc_g_tuner,
1974 .vidioc_s_tuner = cx25821_vidioc_s_tuner,
1975 .vidioc_g_frequency = cx25821_vidioc_g_frequency,
1976 .vidioc_s_frequency = cx25821_vidioc_s_frequency,
1977#endif
1978#ifdef CONFIG_VIDEO_ADV_DEBUG
1979 .vidioc_g_register = cx25821_vidioc_g_register,
1980 .vidioc_s_register = cx25821_vidioc_s_register,
1981#endif
1982}; 976};
1983 977
1984struct video_device cx25821_videoioctl_template = { 978static const struct video_device cx25821_video_device = {
1985 .name = "cx25821-videoioctl", 979 .name = "cx25821-video",
1986 .fops = &video_fops, 980 .fops = &video_fops,
981 .release = video_device_release_empty,
982 .minor = -1,
1987 .ioctl_ops = &video_ioctl_ops, 983 .ioctl_ops = &video_ioctl_ops,
1988 .tvnorms = CX25821_NORMS, 984 .tvnorms = CX25821_NORMS,
1989 .current_norm = V4L2_STD_NTSC_M,
1990}; 985};
986
987static const struct v4l2_file_operations video_out_fops = {
988 .owner = THIS_MODULE,
989 .open = v4l2_fh_open,
990 .write = video_write,
991 .release = video_out_release,
992 .unlocked_ioctl = video_ioctl2,
993};
994
995static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
996 .vidioc_querycap = cx25821_vidioc_querycap,
997 .vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
998 .vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
999 .vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
1000 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1001 .vidioc_g_std = cx25821_vidioc_g_std,
1002 .vidioc_s_std = cx25821_vidioc_s_std,
1003 .vidioc_enum_output = cx25821_vidioc_enum_output,
1004 .vidioc_g_output = cx25821_vidioc_g_output,
1005 .vidioc_s_output = cx25821_vidioc_s_output,
1006 .vidioc_log_status = vidioc_log_status,
1007};
1008
1009static const struct video_device cx25821_video_out_device = {
1010 .name = "cx25821-video",
1011 .fops = &video_out_fops,
1012 .release = video_device_release_empty,
1013 .minor = -1,
1014 .ioctl_ops = &video_out_ioctl_ops,
1015 .tvnorms = CX25821_NORMS,
1016};
1017
1018void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
1019{
1020 cx_clear(PCI_INT_MSK, 1);
1021
1022 if (video_is_registered(&dev->channels[chan_num].vdev)) {
1023 video_unregister_device(&dev->channels[chan_num].vdev);
1024 v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
1025
1026 btcx_riscmem_free(dev->pci,
1027 &dev->channels[chan_num].dma_vidq.stopper);
1028 }
1029}
1030
1031int cx25821_video_register(struct cx25821_dev *dev)
1032{
1033 int err;
1034 int i;
1035
1036 /* initial device configuration */
1037 dev->tvnorm = V4L2_STD_NTSC_M;
1038
1039 spin_lock_init(&dev->slock);
1040
1041 for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
1042 struct cx25821_channel *chan = &dev->channels[i];
1043 struct video_device *vdev = &chan->vdev;
1044 struct v4l2_ctrl_handler *hdl = &chan->hdl;
1045 bool is_output = i > SRAM_CH08;
1046
1047 if (i == SRAM_CH08) /* audio channel */
1048 continue;
1049
1050 if (!is_output) {
1051 v4l2_ctrl_handler_init(hdl, 4);
1052 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1053 V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
1054 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1055 V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
1056 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1057 V4L2_CID_SATURATION, 0, 10000, 1, 5000);
1058 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1059 V4L2_CID_HUE, 0, 10000, 1, 5000);
1060 if (hdl->error) {
1061 err = hdl->error;
1062 goto fail_unreg;
1063 }
1064 err = v4l2_ctrl_handler_setup(hdl);
1065 if (err)
1066 goto fail_unreg;
1067 } else {
1068 chan->out = &dev->vid_out_data[i - SRAM_CH09];
1069 chan->out->chan = chan;
1070 }
1071
1072 cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
1073 chan->sram_channels->dma_ctl, 0x11, 0);
1074
1075 chan->sram_channels = &cx25821_sram_channels[i];
1076 chan->width = 720;
1077 if (dev->tvnorm & V4L2_STD_625_50)
1078 chan->height = 576;
1079 else
1080 chan->height = 480;
1081
1082 if (chan->pixel_formats == PIXEL_FRMT_411)
1083 chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
1084 else
1085 chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
1086
1087 cx_write(chan->sram_channels->int_stat, 0xffffffff);
1088
1089 INIT_LIST_HEAD(&chan->dma_vidq.active);
1090 INIT_LIST_HEAD(&chan->dma_vidq.queued);
1091
1092 chan->timeout_data.dev = dev;
1093 chan->timeout_data.channel = &cx25821_sram_channels[i];
1094 chan->dma_vidq.timeout.function = cx25821_vid_timeout;
1095 chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
1096 init_timer(&chan->dma_vidq.timeout);
1097
1098 if (!is_output)
1099 videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
1100 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1101 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
1102 chan, &dev->lock);
1103
1104 /* register v4l devices */
1105 *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
1106 vdev->v4l2_dev = &dev->v4l2_dev;
1107 if (!is_output)
1108 vdev->ctrl_handler = hdl;
1109 else
1110 vdev->vfl_dir = VFL_DIR_TX;
1111 vdev->lock = &dev->lock;
1112 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1113 snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
1114 video_set_drvdata(vdev, chan);
1115
1116 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1117 video_nr[dev->nr]);
1118
1119 if (err < 0)
1120 goto fail_unreg;
1121 }
1122
1123 /* set PCI interrupt */
1124 cx_set(PCI_INT_MSK, 0xff);
1125
1126 return 0;
1127
1128fail_unreg:
1129 while (i >= 0)
1130 cx25821_video_unregister(dev, i--);
1131 return err;
1132}