1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <media/saa7146_vv.h>
4 #include <linux/module.h>
6 /****************************************************************************/
7 /* resource management functions, shamelessly stolen from saa7134 driver */
9 int saa7146_res_get(struct saa7146_fh
*fh
, unsigned int bit
)
11 struct saa7146_dev
*dev
= fh
->dev
;
12 struct saa7146_vv
*vv
= dev
->vv_data
;
14 if (fh
->resources
& bit
) {
15 DEB_D("already allocated! want: 0x%02x, cur:0x%02x\n",
17 /* have it already allocated */
22 if (vv
->resources
& bit
) {
23 DEB_D("locked! vv->resources:0x%02x, we want:0x%02x\n",
25 /* no, someone else uses it */
28 /* it's free, grab it */
31 DEB_D("res: get 0x%02x, cur:0x%02x\n", bit
, vv
->resources
);
35 void saa7146_res_free(struct saa7146_fh
*fh
, unsigned int bits
)
37 struct saa7146_dev
*dev
= fh
->dev
;
38 struct saa7146_vv
*vv
= dev
->vv_data
;
40 BUG_ON((fh
->resources
& bits
) != bits
);
42 fh
->resources
&= ~bits
;
43 vv
->resources
&= ~bits
;
44 DEB_D("res: put 0x%02x, cur:0x%02x\n", bits
, vv
->resources
);
48 /********************************************************************************/
49 /* common dma functions */
51 void saa7146_dma_free(struct saa7146_dev
*dev
,struct videobuf_queue
*q
,
52 struct saa7146_buf
*buf
)
54 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
55 DEB_EE("dev:%p, buf:%p\n", dev
, buf
);
57 BUG_ON(in_interrupt());
59 videobuf_waiton(q
, &buf
->vb
, 0, 0);
60 videobuf_dma_unmap(q
->dev
, dma
);
61 videobuf_dma_free(dma
);
62 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
66 /********************************************************************************/
67 /* common buffer functions */
69 int saa7146_buffer_queue(struct saa7146_dev
*dev
,
70 struct saa7146_dmaqueue
*q
,
71 struct saa7146_buf
*buf
)
73 assert_spin_locked(&dev
->slock
);
74 DEB_EE("dev:%p, dmaq:%p, buf:%p\n", dev
, q
, buf
);
78 if (NULL
== q
->curr
) {
80 DEB_D("immediately activating buffer %p\n", buf
);
81 buf
->activate(dev
,buf
,NULL
);
83 list_add_tail(&buf
->vb
.queue
,&q
->queue
);
84 buf
->vb
.state
= VIDEOBUF_QUEUED
;
85 DEB_D("adding buffer %p to queue. (active buffer present)\n",
91 void saa7146_buffer_finish(struct saa7146_dev
*dev
,
92 struct saa7146_dmaqueue
*q
,
95 assert_spin_locked(&dev
->slock
);
96 DEB_EE("dev:%p, dmaq:%p, state:%d\n", dev
, q
, state
);
97 DEB_EE("q->curr:%p\n", q
->curr
);
101 /* finish current buffer */
102 if (NULL
== q
->curr
) {
103 DEB_D("aiii. no current buffer\n");
107 q
->curr
->vb
.state
= state
;
108 v4l2_get_timestamp(&q
->curr
->vb
.ts
);
109 wake_up(&q
->curr
->vb
.done
);
114 void saa7146_buffer_next(struct saa7146_dev
*dev
,
115 struct saa7146_dmaqueue
*q
, int vbi
)
117 struct saa7146_buf
*buf
,*next
= NULL
;
121 DEB_INT("dev:%p, dmaq:%p, vbi:%d\n", dev
, q
, vbi
);
123 assert_spin_locked(&dev
->slock
);
124 if (!list_empty(&q
->queue
)) {
125 /* activate next one from queue */
126 buf
= list_entry(q
->queue
.next
,struct saa7146_buf
,vb
.queue
);
127 list_del(&buf
->vb
.queue
);
128 if (!list_empty(&q
->queue
))
129 next
= list_entry(q
->queue
.next
,struct saa7146_buf
, vb
.queue
);
131 DEB_INT("next buffer: buf:%p, prev:%p, next:%p\n",
132 buf
, q
->queue
.prev
, q
->queue
.next
);
133 buf
->activate(dev
,buf
,next
);
135 DEB_INT("no next buffer. stopping.\n");
137 /* turn off video-dma3 */
138 saa7146_write(dev
,MC1
, MASK_20
);
140 /* nothing to do -- just prevent next video-dma1 transfer
141 by lowering the protection address */
143 // fixme: fix this for vflip != 0
145 saa7146_write(dev
, PROT_ADDR1
, 0);
146 saa7146_write(dev
, MC2
, (MASK_02
|MASK_18
));
148 /* write the address of the rps-program */
149 saa7146_write(dev
, RPS_ADDR0
, dev
->d_rps0
.dma_handle
);
151 saa7146_write(dev
, MC1
, (MASK_12
| MASK_28
));
154 printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
155 printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
156 printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
157 printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
158 printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));
159 printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
162 del_timer(&q
->timeout
);
166 void saa7146_buffer_timeout(unsigned long data
)
168 struct saa7146_dmaqueue
*q
= (struct saa7146_dmaqueue
*)data
;
169 struct saa7146_dev
*dev
= q
->dev
;
172 DEB_EE("dev:%p, dmaq:%p\n", dev
, q
);
174 spin_lock_irqsave(&dev
->slock
,flags
);
176 DEB_D("timeout on %p\n", q
->curr
);
177 saa7146_buffer_finish(dev
,q
,VIDEOBUF_ERROR
);
180 /* we don't restart the transfer here like other drivers do. when
181 a streaming capture is disabled, the timeout function will be
182 called for the current buffer. if we activate the next buffer now,
183 we mess up our capture logic. if a timeout occurs on another buffer,
184 then something is seriously broken before, so no need to buffer the
185 next capture IMHO... */
187 saa7146_buffer_next(dev,q);
189 spin_unlock_irqrestore(&dev
->slock
,flags
);
192 /********************************************************************************/
193 /* file operations */
195 static int fops_open(struct file
*file
)
197 struct video_device
*vdev
= video_devdata(file
);
198 struct saa7146_dev
*dev
= video_drvdata(file
);
199 struct saa7146_fh
*fh
= NULL
;
202 DEB_EE("file:%p, dev:%s\n", file
, video_device_node_name(vdev
));
204 if (mutex_lock_interruptible(vdev
->lock
))
207 DEB_D("using: %p\n", dev
);
209 /* check if an extension is registered */
210 if( NULL
== dev
->ext
) {
211 DEB_S("no extension registered for this device\n");
216 /* allocate per open data */
217 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
219 DEB_S("cannot allocate memory for per open data\n");
224 v4l2_fh_init(&fh
->fh
, vdev
);
226 file
->private_data
= &fh
->fh
;
229 if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
230 DEB_S("initializing vbi...\n");
231 if (dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
)
232 result
= saa7146_vbi_uops
.open(dev
,file
);
233 if (dev
->ext_vv_data
->vbi_fops
.open
)
234 dev
->ext_vv_data
->vbi_fops
.open(file
);
236 DEB_S("initializing video...\n");
237 result
= saa7146_video_uops
.open(dev
,file
);
244 if( 0 == try_module_get(dev
->ext
->module
)) {
250 v4l2_fh_add(&fh
->fh
);
252 if (fh
&& result
!= 0) {
254 file
->private_data
= NULL
;
256 mutex_unlock(vdev
->lock
);
260 static int fops_release(struct file
*file
)
262 struct video_device
*vdev
= video_devdata(file
);
263 struct saa7146_fh
*fh
= file
->private_data
;
264 struct saa7146_dev
*dev
= fh
->dev
;
266 DEB_EE("file:%p\n", file
);
268 mutex_lock(vdev
->lock
);
270 if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
271 if (dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
)
272 saa7146_vbi_uops
.release(dev
,file
);
273 if (dev
->ext_vv_data
->vbi_fops
.release
)
274 dev
->ext_vv_data
->vbi_fops
.release(file
);
276 saa7146_video_uops
.release(dev
,file
);
279 v4l2_fh_del(&fh
->fh
);
280 v4l2_fh_exit(&fh
->fh
);
281 module_put(dev
->ext
->module
);
282 file
->private_data
= NULL
;
285 mutex_unlock(vdev
->lock
);
290 static int fops_mmap(struct file
*file
, struct vm_area_struct
* vma
)
292 struct video_device
*vdev
= video_devdata(file
);
293 struct saa7146_fh
*fh
= file
->private_data
;
294 struct videobuf_queue
*q
;
297 switch (vdev
->vfl_type
) {
298 case VFL_TYPE_GRABBER
: {
299 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",
305 DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",
307 if (fh
->dev
->ext_vv_data
->capabilities
& V4L2_CAP_SLICED_VBI_OUTPUT
)
317 if (mutex_lock_interruptible(vdev
->lock
))
319 res
= videobuf_mmap_mapper(q
, vma
);
320 mutex_unlock(vdev
->lock
);
324 static unsigned int __fops_poll(struct file
*file
, struct poll_table_struct
*wait
)
326 struct video_device
*vdev
= video_devdata(file
);
327 struct saa7146_fh
*fh
= file
->private_data
;
328 struct videobuf_buffer
*buf
= NULL
;
329 struct videobuf_queue
*q
;
330 unsigned int res
= v4l2_ctrl_poll(file
, wait
);
332 DEB_EE("file:%p, poll:%p\n", file
, wait
);
334 if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
335 if (fh
->dev
->ext_vv_data
->capabilities
& V4L2_CAP_SLICED_VBI_OUTPUT
)
336 return res
| POLLOUT
| POLLWRNORM
;
337 if( 0 == fh
->vbi_q
.streaming
)
338 return res
| videobuf_poll_stream(file
, &fh
->vbi_q
, wait
);
341 DEB_D("using video queue\n");
345 if (!list_empty(&q
->stream
))
346 buf
= list_entry(q
->stream
.next
, struct videobuf_buffer
, stream
);
349 DEB_D("buf == NULL!\n");
350 return res
| POLLERR
;
353 poll_wait(file
, &buf
->done
, wait
);
354 if (buf
->state
== VIDEOBUF_DONE
|| buf
->state
== VIDEOBUF_ERROR
) {
355 DEB_D("poll succeeded!\n");
356 return res
| POLLIN
| POLLRDNORM
;
359 DEB_D("nothing to poll for, buf->state:%d\n", buf
->state
);
363 static unsigned int fops_poll(struct file
*file
, struct poll_table_struct
*wait
)
365 struct video_device
*vdev
= video_devdata(file
);
368 mutex_lock(vdev
->lock
);
369 res
= __fops_poll(file
, wait
);
370 mutex_unlock(vdev
->lock
);
374 static ssize_t
fops_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
376 struct video_device
*vdev
= video_devdata(file
);
377 struct saa7146_fh
*fh
= file
->private_data
;
380 switch (vdev
->vfl_type
) {
381 case VFL_TYPE_GRABBER
:
383 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun",
384 file, data, (unsigned long)count);
386 return saa7146_video_uops
.read(file
,data
,count
,ppos
);
389 DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n",
390 file, data, (unsigned long)count);
392 if (fh
->dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
) {
393 if (mutex_lock_interruptible(vdev
->lock
))
395 ret
= saa7146_vbi_uops
.read(file
, data
, count
, ppos
);
396 mutex_unlock(vdev
->lock
);
406 static ssize_t
fops_write(struct file
*file
, const char __user
*data
, size_t count
, loff_t
*ppos
)
408 struct video_device
*vdev
= video_devdata(file
);
409 struct saa7146_fh
*fh
= file
->private_data
;
412 switch (vdev
->vfl_type
) {
413 case VFL_TYPE_GRABBER
:
416 if (fh
->dev
->ext_vv_data
->vbi_fops
.write
) {
417 if (mutex_lock_interruptible(vdev
->lock
))
419 ret
= fh
->dev
->ext_vv_data
->vbi_fops
.write(file
, data
, count
, ppos
);
420 mutex_unlock(vdev
->lock
);
430 static const struct v4l2_file_operations video_fops
=
432 .owner
= THIS_MODULE
,
434 .release
= fops_release
,
439 .unlocked_ioctl
= video_ioctl2
,
442 static void vv_callback(struct saa7146_dev
*dev
, unsigned long status
)
446 DEB_INT("dev:%p, isr:0x%08x\n", dev
, (u32
)status
);
448 if (0 != (isr
& (MASK_27
))) {
449 DEB_INT("irq: RPS0 (0x%08x)\n", isr
);
450 saa7146_video_uops
.irq_done(dev
,isr
);
453 if (0 != (isr
& (MASK_28
))) {
454 u32 mc2
= saa7146_read(dev
, MC2
);
455 if( 0 != (mc2
& MASK_15
)) {
456 DEB_INT("irq: RPS1 vbi workaround (0x%08x)\n", isr
);
457 wake_up(&dev
->vv_data
->vbi_wq
);
458 saa7146_write(dev
,MC2
, MASK_31
);
461 DEB_INT("irq: RPS1 (0x%08x)\n", isr
);
462 saa7146_vbi_uops
.irq_done(dev
,isr
);
466 static const struct v4l2_ctrl_ops saa7146_ctrl_ops
= {
467 .s_ctrl
= saa7146_s_ctrl
,
470 int saa7146_vv_init(struct saa7146_dev
* dev
, struct saa7146_ext_vv
*ext_vv
)
472 struct v4l2_ctrl_handler
*hdl
= &dev
->ctrl_handler
;
473 struct v4l2_pix_format
*fmt
;
474 struct v4l2_vbi_format
*vbi
;
475 struct saa7146_vv
*vv
;
478 err
= v4l2_device_register(&dev
->pci
->dev
, &dev
->v4l2_dev
);
482 v4l2_ctrl_handler_init(hdl
, 6);
483 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
484 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
485 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
486 V4L2_CID_CONTRAST
, 0, 127, 1, 64);
487 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
488 V4L2_CID_SATURATION
, 0, 127, 1, 64);
489 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
490 V4L2_CID_VFLIP
, 0, 1, 1, 0);
491 v4l2_ctrl_new_std(hdl
, &saa7146_ctrl_ops
,
492 V4L2_CID_HFLIP
, 0, 1, 1, 0);
495 v4l2_ctrl_handler_free(hdl
);
498 dev
->v4l2_dev
.ctrl_handler
= hdl
;
500 vv
= kzalloc(sizeof(struct saa7146_vv
), GFP_KERNEL
);
502 ERR("out of memory. aborting.\n");
503 v4l2_ctrl_handler_free(hdl
);
506 ext_vv
->vid_ops
= saa7146_video_ioctl_ops
;
507 ext_vv
->vbi_ops
= saa7146_vbi_ioctl_ops
;
508 ext_vv
->core_ops
= &saa7146_video_ioctl_ops
;
510 DEB_EE("dev:%p\n", dev
);
512 /* set default values for video parts of the saa7146 */
513 saa7146_write(dev
, BCS_CTRL
, 0x80400040);
515 /* enable video-port pins */
516 saa7146_write(dev
, MC1
, (MASK_10
| MASK_26
));
518 /* save per-device extension data (one extension can
519 handle different devices that might need different
520 configuration data) */
521 dev
->ext_vv_data
= ext_vv
;
523 vv
->d_clipping
.cpu_addr
= pci_alloc_consistent(dev
->pci
, SAA7146_CLIPPING_MEM
, &vv
->d_clipping
.dma_handle
);
524 if( NULL
== vv
->d_clipping
.cpu_addr
) {
525 ERR("out of memory. aborting.\n");
527 v4l2_ctrl_handler_free(hdl
);
530 memset(vv
->d_clipping
.cpu_addr
, 0x0, SAA7146_CLIPPING_MEM
);
532 saa7146_video_uops
.init(dev
,vv
);
533 if (dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
)
534 saa7146_vbi_uops
.init(dev
,vv
);
536 fmt
= &vv
->ov_fb
.fmt
;
537 fmt
->width
= vv
->standard
->h_max_out
;
538 fmt
->height
= vv
->standard
->v_max_out
;
539 fmt
->pixelformat
= V4L2_PIX_FMT_RGB565
;
540 fmt
->bytesperline
= 2 * fmt
->width
;
541 fmt
->sizeimage
= fmt
->bytesperline
* fmt
->height
;
542 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
544 fmt
= &vv
->video_fmt
;
547 fmt
->pixelformat
= V4L2_PIX_FMT_BGR24
;
548 fmt
->field
= V4L2_FIELD_ANY
;
549 fmt
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
550 fmt
->bytesperline
= 3 * fmt
->width
;
551 fmt
->sizeimage
= fmt
->bytesperline
* fmt
->height
;
554 vbi
->sampling_rate
= 27000000;
555 vbi
->offset
= 248; /* todo */
556 vbi
->samples_per_line
= 720 * 2;
557 vbi
->sample_format
= V4L2_PIX_FMT_GREY
;
559 /* fixme: this only works for PAL */
565 init_timer(&vv
->vbi_read_timeout
);
567 vv
->ov_fb
.capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
568 vv
->ov_fb
.flags
= V4L2_FBUF_FLAG_PRIMARY
;
570 dev
->vv_callback
= &vv_callback
;
574 EXPORT_SYMBOL_GPL(saa7146_vv_init
);
576 int saa7146_vv_release(struct saa7146_dev
* dev
)
578 struct saa7146_vv
*vv
= dev
->vv_data
;
580 DEB_EE("dev:%p\n", dev
);
582 v4l2_device_unregister(&dev
->v4l2_dev
);
583 pci_free_consistent(dev
->pci
, SAA7146_CLIPPING_MEM
, vv
->d_clipping
.cpu_addr
, vv
->d_clipping
.dma_handle
);
584 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
587 dev
->vv_callback
= NULL
;
591 EXPORT_SYMBOL_GPL(saa7146_vv_release
);
593 int saa7146_register_device(struct video_device
**vid
, struct saa7146_dev
* dev
,
594 char *name
, int type
)
596 struct video_device
*vfd
;
600 DEB_EE("dev:%p, name:'%s', type:%d\n", dev
, name
, type
);
602 // released by vfd->release
603 vfd
= video_device_alloc();
607 vfd
->fops
= &video_fops
;
608 if (type
== VFL_TYPE_GRABBER
)
609 vfd
->ioctl_ops
= &dev
->ext_vv_data
->vid_ops
;
611 vfd
->ioctl_ops
= &dev
->ext_vv_data
->vbi_ops
;
612 vfd
->release
= video_device_release
;
613 vfd
->lock
= &dev
->v4l2_lock
;
614 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
616 set_bit(V4L2_FL_USE_FH_PRIO
, &vfd
->flags
);
617 for (i
= 0; i
< dev
->ext_vv_data
->num_stds
; i
++)
618 vfd
->tvnorms
|= dev
->ext_vv_data
->stds
[i
].id
;
619 strlcpy(vfd
->name
, name
, sizeof(vfd
->name
));
620 video_set_drvdata(vfd
, dev
);
622 err
= video_register_device(vfd
, type
, -1);
624 ERR("cannot register v4l2 device. skipping.\n");
625 video_device_release(vfd
);
629 pr_info("%s: registered device %s [v4l2]\n",
630 dev
->name
, video_device_node_name(vfd
));
635 EXPORT_SYMBOL_GPL(saa7146_register_device
);
637 int saa7146_unregister_device(struct video_device
**vid
, struct saa7146_dev
* dev
)
639 DEB_EE("dev:%p\n", dev
);
641 video_unregister_device(*vid
);
646 EXPORT_SYMBOL_GPL(saa7146_unregister_device
);
648 static int __init
saa7146_vv_init_module(void)
654 static void __exit
saa7146_vv_cleanup_module(void)
658 module_init(saa7146_vv_init_module
);
659 module_exit(saa7146_vv_cleanup_module
);
661 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
662 MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
663 MODULE_LICENSE("GPL");