Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cx25821-video.c
Go to the documentation of this file.
1 /*
2  * Driver for the Conexant CX25821 PCIe bridge
3  *
4  * Copyright (C) 2009 Conexant Systems Inc.
6  * Based on Steven Toth <[email protected]> cx23885 driver
7  * Parts adapted/taken from Eduardo Moscoso Rubino
8  * Copyright (C) 2009 Eduardo Moscoso Rubino <[email protected]>
9  *
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  *
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26 
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 
29 #include "cx25821-video.h"
30 
31 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
32 MODULE_AUTHOR("Hiep Huynh <[email protected]>");
33 MODULE_LICENSE("GPL");
34 
35 static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
36 static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
37 
38 module_param_array(video_nr, int, NULL, 0444);
39 module_param_array(radio_nr, int, NULL, 0444);
40 
41 MODULE_PARM_DESC(video_nr, "video device numbers");
42 MODULE_PARM_DESC(radio_nr, "radio device numbers");
43 
44 static unsigned int video_debug = VIDEO_DEBUG;
45 module_param(video_debug, int, 0644);
46 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
47 
48 static unsigned int irq_debug;
49 module_param(irq_debug, int, 0644);
50 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
51 
52 unsigned int vid_limit = 16;
53 module_param(vid_limit, int, 0644);
54 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
55 
56 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
57 
58 static const struct v4l2_file_operations video_fops;
59 static const struct v4l2_ioctl_ops video_ioctl_ops;
60 
61 #define FORMAT_FLAGS_PACKED 0x01
62 
63 struct cx25821_fmt formats[] = {
64  {
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",
71  .fourcc = V4L2_PIX_FMT_Y41P,
72  .depth = 12,
73  .flags = FORMAT_FLAGS_PACKED,
74  }, {
75  .name = "4:2:2, packed, YUYV",
76  .fourcc = V4L2_PIX_FMT_YUYV,
77  .depth = 16,
78  .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  },
90 };
91 
93 {
94  return ARRAY_SIZE(formats);
95 }
96 
98 {
99  unsigned int i;
100 
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++)
105  if (formats[i].fourcc == fourcc)
106  return formats + i;
107 
108  pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
109  return NULL;
110 }
111 
113  u32 count)
114 {
115  struct cx25821_buffer *buf;
116  int bc;
117 
118  for (bc = 0;; bc++) {
119  if (list_empty(&q->active)) {
120  dprintk(1, "bc=%d (=0: active empty)\n", bc);
121  break;
122  }
123 
124  buf = list_entry(q->active.next, struct cx25821_buffer,
125  vb.queue);
126 
127  /* count comes from the hw and it is 16bit wide --
128  * this trick handles wrap-arounds correctly for
129  * up to 32767 buffers in flight... */
130  if ((s16) (count - buf->count) < 0)
131  break;
132 
133  do_gettimeofday(&buf->vb.ts);
134  buf->vb.state = VIDEOBUF_DONE;
135  list_del(&buf->vb.queue);
136  wake_up(&buf->vb.done);
137  }
138 
139  if (list_empty(&q->active))
140  del_timer(&q->timeout);
141  else
143  if (bc != 1)
144  pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
145 }
146 
147 #ifdef TUNER_FLAG
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 
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;
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 /*
183 static 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 */
202 int 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 
225 int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
226 {
227  return fh->resources & bit;
228 }
229 
230 int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
231 {
232  return fh->dev->channels[fh->channel_id].resources & bit;
233 }
234 
235 void 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 
248 int 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 
267  struct cx25821_dmaqueue *q,
268  struct cx25821_buffer *buf,
269  struct sram_channel *channel)
270 {
271  int tmp = 0;
272 
273  /* setup fifo + format */
274  cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
275 
276  /* reset counter */
277  cx_write(channel->gpcnt_ctl, 3);
278  q->count = 1;
279 
280  /* enable irq */
281  cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
282  cx_set(channel->int_msk, 0x11);
283 
284  /* start dma */
285  cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
286 
287  /* make sure upstream setting if any is reversed */
288  tmp = cx_read(VID_CH_MODE_SEL);
289  cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
290 
291  return 0;
292 }
293 
295  struct cx25821_dmaqueue *q,
296  struct sram_channel *channel)
297 {
298  struct cx25821_buffer *buf, *prev;
299  struct list_head *item;
300 
301  if (!list_empty(&q->active)) {
302  buf = list_entry(q->active.next, struct cx25821_buffer,
303  vb.queue);
304 
305  cx25821_start_video_dma(dev, q, buf, channel);
306 
307  list_for_each(item, &q->active) {
308  buf = list_entry(item, struct cx25821_buffer, vb.queue);
309  buf->count = q->count++;
310  }
311 
313  return 0;
314  }
315 
316  prev = NULL;
317  for (;;) {
318  if (list_empty(&q->queued))
319  return 0;
320 
321  buf = list_entry(q->queued.next, struct cx25821_buffer,
322  vb.queue);
323 
324  if (NULL == prev) {
325  list_move_tail(&buf->vb.queue, &q->active);
326  cx25821_start_video_dma(dev, q, buf, channel);
327  buf->vb.state = VIDEOBUF_ACTIVE;
328  buf->count = q->count++;
330  } else if (prev->vb.width == buf->vb.width &&
331  prev->vb.height == buf->vb.height &&
332  prev->fmt == buf->fmt) {
333  list_move_tail(&buf->vb.queue, &q->active);
334  buf->vb.state = VIDEOBUF_ACTIVE;
335  buf->count = q->count++;
336  prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
337  prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
338  } else {
339  return 0;
340  }
341  prev = buf;
342  }
343 }
344 
345 void cx25821_vid_timeout(unsigned long data)
346 {
347  struct cx25821_data *timeout_data = (struct cx25821_data *)data;
348  struct cx25821_dev *dev = timeout_data->dev;
349  struct sram_channel *channel = timeout_data->channel;
350  struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
351  struct cx25821_buffer *buf;
352  unsigned long flags;
353 
354  /* cx25821_sram_channel_dump(dev, channel); */
355  cx_clear(channel->dma_ctl, 0x11);
356 
357  spin_lock_irqsave(&dev->slock, flags);
358  while (!list_empty(&q->active)) {
359  buf = list_entry(q->active.next, struct cx25821_buffer,
360  vb.queue);
361  list_del(&buf->vb.queue);
362 
363  buf->vb.state = VIDEOBUF_ERROR;
364  wake_up(&buf->vb.done);
365  }
366 
367  cx25821_restart_video_queue(dev, q, channel);
368  spin_unlock_irqrestore(&dev->slock, flags);
369 }
370 
372 {
373  u32 count = 0;
374  int handled = 0;
375  u32 mask;
376  struct sram_channel *channel = dev->channels[chan_num].sram_channels;
377 
378  mask = cx_read(channel->int_msk);
379  if (0 == (status & mask))
380  return handled;
381 
382  cx_write(channel->int_stat, status);
383 
384  /* risc op code error */
385  if (status & (1 << 16)) {
386  pr_warn("%s, %s: video risc op code error\n",
387  dev->name, channel->name);
388  cx_clear(channel->dma_ctl, 0x11);
389  cx25821_sram_channel_dump(dev, channel);
390  }
391 
392  /* risc1 y */
393  if (status & FLD_VID_DST_RISC1) {
394  spin_lock(&dev->slock);
395  count = cx_read(channel->gpcnt);
396  cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
397  count);
398  spin_unlock(&dev->slock);
399  handled++;
400  }
401 
402  /* risc2 y */
403  if (status & 0x10) {
404  dprintk(2, "stopper video\n");
405  spin_lock(&dev->slock);
407  &dev->channels[channel->i].vidq, channel);
408  spin_unlock(&dev->slock);
409  handled++;
410  }
411  return handled;
412 }
413 
415 {
416  if (dev->ioctl_dev) {
417  if (video_is_registered(dev->ioctl_dev))
419  else
421 
422  dev->ioctl_dev = NULL;
423  }
424 }
425 
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))
433  dev->channels[chan_num].video_dev);
434  else
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 
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 =
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 
512 fail_unreg:
513  cx25821_video_unregister(dev, i);
514  return err;
515 }
516 
517 int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
518  unsigned int *size)
519 {
520  struct cx25821_fh *fh = q->priv_data;
521 
522  *size = fh->fmt->depth * fh->width * fh->height >> 3;
523 
524  if (0 == *count)
525  *count = 32;
526 
527  if (*size * *count > vid_limit * 1024 * 1024)
528  *count = (vid_limit * 1024 * 1024) / *size;
529 
530  return 0;
531 }
532 
534  enum v4l2_field field)
535 {
536  struct cx25821_fh *fh = q->priv_data;
537  struct cx25821_dev *dev = fh->dev;
538  struct cx25821_buffer *buf =
539  container_of(vb, struct cx25821_buffer, vb);
540  int rc, init_buffer = 0;
541  u32 line0_offset;
542  struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
543  int bpl_local = LINE_SIZE_D1;
544  int channel_opened = fh->channel_id;
545 
546  BUG_ON(NULL == fh->fmt);
547  if (fh->width < 48 || fh->width > 720 ||
548  fh->height < 32 || fh->height > 576)
549  return -EINVAL;
550 
551  buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
552 
553  if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
554  return -EINVAL;
555 
556  if (buf->fmt != fh->fmt ||
557  buf->vb.width != fh->width ||
558  buf->vb.height != fh->height || buf->vb.field != field) {
559  buf->fmt = fh->fmt;
560  buf->vb.width = fh->width;
561  buf->vb.height = fh->height;
562  buf->vb.field = field;
563  init_buffer = 1;
564  }
565 
566  if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
567  init_buffer = 1;
568  rc = videobuf_iolock(q, &buf->vb, NULL);
569  if (0 != rc) {
570  printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
571  goto fail;
572  }
573  }
574 
575  dprintk(1, "init_buffer=%d\n", init_buffer);
576 
577  if (init_buffer) {
578 
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 ==
585  buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
586  else
587  buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
588 
589  if (dev->channels[channel_opened].pixel_formats ==
590  PIXEL_FRMT_411) {
591  bpl_local = buf->bpl;
592  } else {
593  bpl_local = buf->bpl; /* Default */
594 
595  if (channel_opened >= 0 && channel_opened <= 7) {
596  if (dev->channels[channel_opened]
597  .use_cif_resolution) {
598  if (dev->tvnorm & V4L2_STD_PAL_BG ||
599  dev->tvnorm & V4L2_STD_PAL_DK)
600  bpl_local = 352 << 1;
601  else
602  bpl_local = dev->channels[
603  channel_opened].
604  cif_width << 1;
605  }
606  }
607  }
608 
609  switch (buf->vb.field) {
610  case V4L2_FIELD_TOP:
611  cx25821_risc_buffer(dev->pci, &buf->risc,
612  dma->sglist, 0, UNSET,
613  buf->bpl, 0, buf->vb.height);
614  break;
615  case V4L2_FIELD_BOTTOM:
616  cx25821_risc_buffer(dev->pci, &buf->risc,
617  dma->sglist, UNSET, 0,
618  buf->bpl, 0, buf->vb.height);
619  break;
621  /* All other formats are top field first */
622  line0_offset = 0;
623  dprintk(1, "top field first\n");
624 
625  cx25821_risc_buffer(dev->pci, &buf->risc,
626  dma->sglist, line0_offset,
627  bpl_local, bpl_local, bpl_local,
628  buf->vb.height >> 1);
629  break;
630  case V4L2_FIELD_SEQ_TB:
631  cx25821_risc_buffer(dev->pci, &buf->risc,
632  dma->sglist,
633  0, buf->bpl * (buf->vb.height >> 1),
634  buf->bpl, 0, buf->vb.height >> 1);
635  break;
636  case V4L2_FIELD_SEQ_BT:
637  cx25821_risc_buffer(dev->pci, &buf->risc,
638  dma->sglist,
639  buf->bpl * (buf->vb.height >> 1), 0,
640  buf->bpl, 0, buf->vb.height >> 1);
641  break;
642  default:
643  BUG();
644  }
645  }
646 
647  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,
649  fh->fmt->name, (unsigned long)buf->risc.dma);
650 
651  buf->vb.state = VIDEOBUF_PREPARED;
652 
653  return 0;
654 
655 fail:
656  cx25821_free_buffer(q, buf);
657  return rc;
658 }
659 
661  struct videobuf_buffer *vb)
662 {
663  struct cx25821_buffer *buf =
664  container_of(vb, struct cx25821_buffer, vb);
665 
666  cx25821_free_buffer(q, buf);
667 }
668 
670 {
671  switch (fh->type) {
673  return &fh->vidq;
674  default:
675  BUG();
676  return NULL;
677  }
678 }
679 
681 {
682  switch (fh->type) {
684  return resource;
685  default:
686  BUG();
687  return 0;
688  }
689 }
690 
691 int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
692 {
693  struct cx25821_fh *fh = file->private_data;
694 
695  return videobuf_mmap_mapper(get_queue(fh), vma);
696 }
697 
698 
699 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
700 {
701  struct cx25821_buffer *buf =
702  container_of(vb, struct cx25821_buffer, vb);
703  struct cx25821_buffer *prev;
704  struct cx25821_fh *fh = vq->priv_data;
705  struct cx25821_dev *dev = fh->dev;
706  struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
707 
708  /* add jump to stopper */
709  buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
710  buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
711  buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
712 
713  dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
714 
715  if (!list_empty(&q->queued)) {
716  list_add_tail(&buf->vb.queue, &q->queued);
717  buf->vb.state = VIDEOBUF_QUEUED;
718  dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
719  buf->vb.i);
720 
721  } else if (list_empty(&q->active)) {
722  list_add_tail(&buf->vb.queue, &q->active);
723  cx25821_start_video_dma(dev, q, buf,
724  dev->channels[fh->channel_id].sram_channels);
725  buf->vb.state = VIDEOBUF_ACTIVE;
726  buf->count = q->count++;
727  mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
728  dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
729  buf, buf->vb.i, buf->count, q->count);
730  } else {
731  prev = list_entry(q->active.prev, struct cx25821_buffer,
732  vb.queue);
733  if (prev->vb.width == buf->vb.width
734  && prev->vb.height == buf->vb.height
735  && prev->fmt == buf->fmt) {
736  list_add_tail(&buf->vb.queue, &q->active);
737  buf->vb.state = VIDEOBUF_ACTIVE;
738  buf->count = q->count++;
739  prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
740 
741  /* 64 bit bits 63-32 */
742  prev->risc.jmp[2] = cpu_to_le32(0);
743  dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
744  buf, buf->vb.i, buf->count);
745 
746  } else {
747  list_add_tail(&buf->vb.queue, &q->queued);
748  buf->vb.state = VIDEOBUF_QUEUED;
749  dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
750  buf->vb.i);
751  }
752  }
753 
754  if (list_empty(&q->active))
755  dprintk(2, "active queue empty!\n");
756 }
757 
758 static struct videobuf_queue_ops cx25821_video_qops = {
759  .buf_setup = cx25821_buffer_setup,
760  .buf_prepare = cx25821_buffer_prepare,
761  .buf_queue = buffer_queue,
762  .buf_release = cx25821_buffer_release,
763 };
764 
765 static 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;
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;
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,
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 
838 static ssize_t video_read(struct file *file, char __user * data, size_t count,
839  loff_t *ppos)
840 {
841  struct cx25821_fh *fh = file->private_data;
842 
843  switch (fh->type) {
846  return -EBUSY;
847 
848  return videobuf_read_one(&fh->vidq, data, count, ppos,
849  file->f_flags & O_NONBLOCK);
850 
851  default:
852  BUG();
853  return 0;
854  }
855 }
856 
857 static unsigned int video_poll(struct file *file,
858  struct poll_table_struct *wait)
859 {
860  struct cx25821_fh *fh = file->private_data;
861  struct cx25821_buffer *buf;
862 
864  /* streaming capture */
865  if (list_empty(&fh->vidq.stream))
866  return POLLERR;
867  buf = list_entry(fh->vidq.stream.next,
868  struct cx25821_buffer, vb.stream);
869  } else {
870  /* read() capture */
871  buf = (struct cx25821_buffer *)fh->vidq.read_buf;
872  if (NULL == buf)
873  return POLLERR;
874  }
875 
876  poll_wait(file, &buf->vb.done, wait);
877  if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
878  if (buf->vb.state == VIDEOBUF_DONE) {
879  struct cx25821_dev *dev = fh->dev;
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  }
890 
891  return POLLIN | POLLRDNORM;
892  }
893 
894  return 0;
895 }
896 
897 static int video_release(struct file *file)
898 {
899  struct cx25821_fh *fh = file->private_data;
900  struct cx25821_dev *dev = fh->dev;
901 
902  /* stop the risc engine and fifo */
903  cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
904 
905  /* stop video capture */
909  }
910 
911  if (fh->vidq.read_buf) {
912  cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
913  kfree(fh->vidq.read_buf);
914  }
915 
916  videobuf_mmap_free(&fh->vidq);
917 
918  v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
919  file->private_data = NULL;
920  kfree(fh);
921 
922  return 0;
923 }
924 
925 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
926 {
927  struct cx25821_fh *fh = priv;
928  struct cx25821_dev *dev = fh->dev;
929 
931  return -EINVAL;
932 
933  if (unlikely(i != fh->type))
934  return -EINVAL;
935 
937  RESOURCE_VIDEO0))))
938  return -EBUSY;
939 
940  return videobuf_streamon(get_queue(fh));
941 }
942 
943 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
944 {
945  struct cx25821_fh *fh = priv;
946  struct cx25821_dev *dev = fh->dev;
947  int err, res;
948 
950  return -EINVAL;
951  if (i != fh->type)
952  return -EINVAL;
953 
955  err = videobuf_streamoff(get_queue(fh));
956  if (err < 0)
957  return err;
958  cx25821_res_free(dev, fh, res);
959  return 0;
960 }
961 
962 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
963  struct v4l2_format *f)
964 {
965  struct cx25821_fh *fh = priv;
966  struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
967  struct v4l2_mbus_framefmt mbus_fmt;
968  int err;
969  int pix_format = PIXEL_FRMT_422;
970 
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);
980 
981  if (0 != err)
982  return err;
983 
984  fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
985  fh->vidq.field = f->fmt.pix.field;
986 
987  /* check if width and height is valid based on set standard */
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 
994  if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
995  pix_format = PIXEL_FRMT_411;
996  else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
997  pix_format = PIXEL_FRMT_422;
998  else
999  return -EINVAL;
1000 
1001  cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
1002 
1003  /* check if cif resolution */
1004  if (fh->width == 320 || fh->width == 352)
1005  dev->channels[fh->channel_id].use_cif_resolution = 1;
1006  else
1007  dev->channels[fh->channel_id].use_cif_resolution = 0;
1008 
1009  dev->channels[fh->channel_id].cif_width = fh->width;
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 
1017  return 0;
1018 }
1019 
1020 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1021 {
1022  int ret_val = 0;
1023  struct cx25821_fh *fh = priv;
1024  struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1025 
1026  ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
1027 
1028  p->sequence = dev->channels[fh->channel_id].vidq.count;
1029 
1030  return ret_val;
1031 }
1032 
1033 static int vidioc_log_status(struct file *file, void *priv)
1034 {
1035  struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1036  struct cx25821_fh *fh = priv;
1037  char name[32 + 2];
1038 
1039  struct sram_channel *sram_ch = dev->channels[fh->channel_id]
1040  .sram_channels;
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 
1055 static 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 
1062  if (fh) {
1063  err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1064  fh->prio);
1065  if (0 != err)
1066  return err;
1067  }
1068 
1069  return cx25821_set_control(dev, ctl, fh->channel_id);
1070 }
1071 
1072 /* VIDEO IOCTLS */
1073 int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1074  struct v4l2_format *f)
1075 {
1076  struct cx25821_fh *fh = priv;
1077 
1078  f->fmt.pix.width = fh->width;
1079  f->fmt.pix.height = fh->height;
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 
1085  return 0;
1086 }
1087 
1088 int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1089  struct v4l2_format *f)
1090 {
1091  struct cx25821_fmt *fmt;
1092  enum v4l2_field field;
1093  unsigned int maxw, maxh;
1094 
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 
1134  return 0;
1135 }
1136 
1137 int cx25821_vidioc_querycap(struct file *file, void *priv,
1138  struct v4l2_capability *cap)
1139 {
1140  struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1141 
1142  strcpy(cap->driver, "cx25821");
1143  strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1144  sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1148  if (UNSET != dev->tuner_type)
1149  cap->capabilities |= V4L2_CAP_TUNER;
1150  return 0;
1151 }
1152 
1153 int 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;
1163 }
1164 
1165 int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1166  struct v4l2_requestbuffers *p)
1167 {
1168  struct cx25821_fh *fh = priv;
1169  return videobuf_reqbufs(get_queue(fh), p);
1170 }
1171 
1172 int cx25821_vidioc_querybuf(struct file *file, void *priv,
1173  struct v4l2_buffer *p)
1174 {
1175  struct cx25821_fh *fh = priv;
1176  return videobuf_querybuf(get_queue(fh), p);
1177 }
1178 
1179 int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1180 {
1181  struct cx25821_fh *fh = priv;
1182  return videobuf_qbuf(get_queue(fh), p);
1183 }
1184 
1185 int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1186 {
1187  struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1188  struct cx25821_fh *fh = f;
1189 
1190  *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1191 
1192  return 0;
1193 }
1194 
1195 int cx25821_vidioc_s_priority(struct file *file, void *f,
1196  enum v4l2_priority prio)
1197 {
1198  struct cx25821_fh *fh = f;
1199  struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1200 
1201  return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1202  prio);
1203 }
1204 
1205 #ifdef TUNER_FLAG
1206 int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id * tvnorms)
1207 {
1208  struct cx25821_fh *fh = priv;
1209  struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->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 
1221  if (dev->tvnorm == *tvnorms)
1222  return 0;
1223 
1224  mutex_lock(&dev->lock);
1225  cx25821_set_tvnorm(dev, *tvnorms);
1226  mutex_unlock(&dev->lock);
1227 
1229 
1230  return 0;
1231 }
1232 #endif
1233 
1234 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
1235 {
1236  static const char * const iname[] = {
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;
1250 
1252  strcpy(i->name, iname[INPUT(n)->type]);
1253 
1254  i->std = CX25821_NORMS;
1255  return 0;
1256 }
1257 
1258 int cx25821_vidioc_enum_input(struct file *file, void *priv,
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 
1266 int 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 
1275 int 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
1302 int 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 
1315 int cx25821_set_freq(struct cx25821_dev *dev, 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 
1330 int cx25821_vidioc_s_frequency(struct file *file, void *priv,
1331  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
1353 int 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 
1366 int cx25821_vidioc_s_register(struct file *file, void *fh,
1367  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
1382 int 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");
1394  t->rangehigh = 0xffffffffUL;
1395 
1396  t->signal = 0xffff; /* LOCKED */
1397  return 0;
1398 }
1399 
1400 int cx25821_vidioc_s_tuner(struct file *file, void *priv, 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 /*****************************************************************************/
1424 static const struct v4l2_queryctrl no_ctl = {
1425  .name = "42",
1426  .flags = V4L2_CTRL_FLAG_DISABLED,
1427 };
1428 
1429 static 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 };
1465 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1466 
1467 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1468 {
1469  int i;
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;
1482 }
1483 
1484 int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1485  struct v4l2_queryctrl *qctrl)
1486 {
1487  return cx25821_ctrl_query(qctrl);
1488 }
1489 
1490 /* ------------------------------------------------------------------ */
1491 /* VIDEO CTRL IOCTLS */
1492 
1493 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1494 {
1495  unsigned int i;
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 }
1502 
1503 int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
1504  struct v4l2_control *ctl)
1505 {
1506  struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1507  struct cx25821_fh *fh = priv;
1508 
1509  const struct v4l2_queryctrl *ctrl;
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:
1517  ctl->value = dev->channels[fh->channel_id].ctl_bright;
1518  break;
1519  case V4L2_CID_HUE:
1520  ctl->value = dev->channels[fh->channel_id].ctl_hue;
1521  break;
1522  case V4L2_CID_CONTRAST:
1523  ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1524  break;
1525  case V4L2_CID_SATURATION:
1526  ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1527  break;
1528  }
1529  return 0;
1530 }
1531 
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) {
1547  case V4L2_CTRL_TYPE_MENU:
1549  if (ctl->value < ctrl->minimum)
1550  ctl->value = ctrl->minimum;
1551  if (ctl->value > ctrl->maximum)
1552  ctl->value = ctrl->maximum;
1553  break;
1554  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 
1582 static 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 
1594 int 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;
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 
1613 int 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  }
1625  /* cx25821_vidioc_s_crop not supported */
1626  return -EINVAL;
1627 }
1628 
1629 int 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 }
1634 
1635 int cx25821_vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm)
1636 {
1637  /* medusa does not support video standard sensing of current input */
1638  *norm = CX25821_NORMS;
1639 
1640  return 0;
1641 }
1642 
1644 {
1645  if (tvnorm == V4L2_STD_PAL_BG) {
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;
1659 }
1660 
1662 {
1663  if (tvnorm == V4L2_STD_PAL_BG) {
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 }
1679 
1680 static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1681  unsigned long arg)
1682 {
1683  struct cx25821_fh *fh = file->private_data;
1684  struct cx25821_dev *dev = fh->dev;
1685  int command = 0;
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:
1714  break;
1715  }
1716 
1717  return 0;
1718 }
1719 
1720 static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1721  unsigned long arg)
1722 {
1723  struct cx25821_fh *fh = file->private_data;
1724  struct cx25821_dev *dev = fh->dev;
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 
1747  switch (command) {
1748  case UPSTREAM_START_VIDEO:
1749  cx25821_start_upstream_video_ch2(dev, data_from_user);
1750  break;
1751 
1752  case UPSTREAM_STOP_VIDEO:
1754  break;
1755  }
1756 
1757  return 0;
1758 }
1759 
1760 static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1761  unsigned long arg)
1762 {
1763  struct cx25821_fh *fh = file->private_data;
1764  struct cx25821_dev *dev = fh->dev;
1765  int command = 0;
1766  struct upstream_user_struct *data_from_user;
1767 
1768  data_from_user = (struct upstream_user_struct *)arg;
1769 
1770  if (!data_from_user) {
1771  pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1772  return 0;
1773  }
1774 
1775  command = data_from_user->command;
1776 
1777  if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1778  return 0;
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:
1794  break;
1795  }
1796 
1797  return 0;
1798 }
1799 
1800 static long video_ioctl_set(struct file *file, unsigned int cmd,
1801  unsigned long arg)
1802 {
1803  struct cx25821_fh *fh = file->private_data;
1804  struct cx25821_dev *dev = fh->dev;
1805  struct downstream_user_struct *data_from_user;
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;
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 
1887  medusa_set_resolution(dev, width, selected_channel);
1888  break;
1889  case REG_READ:
1890  data_from_user->reg_data = cx_read(data_from_user->reg_address);
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  }
1906 
1907  return 0;
1908 }
1909 
1910 static long cx25821_video_ioctl(struct file *file,
1911  unsigned int cmd, unsigned long arg)
1912 {
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 
1933 /* exported stuff */
1934 static const struct v4l2_file_operations video_fops = {
1935  .owner = THIS_MODULE,
1936  .open = video_open,
1937  .release = video_release,
1938  .read = video_read,
1939  .poll = video_poll,
1940  .mmap = cx25821_video_mmap,
1941  .ioctl = cx25821_video_ioctl,
1942 };
1943 
1944 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1945  .vidioc_querycap = cx25821_vidioc_querycap,
1946  .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1947  .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1948  .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1949  .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1950  .vidioc_reqbufs = cx25821_vidioc_reqbufs,
1951  .vidioc_querybuf = cx25821_vidioc_querybuf,
1952  .vidioc_qbuf = cx25821_vidioc_qbuf,
1953  .vidioc_dqbuf = vidioc_dqbuf,
1954 #ifdef TUNER_FLAG
1955  .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,
1962  .vidioc_g_input = cx25821_vidioc_g_input,
1963  .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,
1968  .vidioc_streamoff = vidioc_streamoff,
1969  .vidioc_log_status = vidioc_log_status,
1970  .vidioc_g_priority = cx25821_vidioc_g_priority,
1971  .vidioc_s_priority = cx25821_vidioc_s_priority,
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 };
1983 
1985  .name = "cx25821-videoioctl",
1986  .fops = &video_fops,
1987  .ioctl_ops = &video_ioctl_ops,
1988  .tvnorms = CX25821_NORMS,
1989  .current_norm = V4L2_STD_NTSC_M,
1990 };