1 #include <media/saa7146_vv.h>
2 #include <linux/module.h>
4 /****************************************************************************/
5 /* resource management functions, shamelessly stolen from saa7134 driver */
7 int saa7146_res_get(struct saa7146_fh
*fh
, unsigned int bit
)
9 struct saa7146_dev
*dev
= fh
->dev
;
10 struct saa7146_vv
*vv
= dev
->vv_data
;
12 if (fh
->resources
& bit
) {
13 DEB_D(("already allocated! want: 0x%02x, cur:0x%02x\n",bit
,vv
->resources
));
14 /* have it already allocated */
19 if (vv
->resources
& bit
) {
20 DEB_D(("locked! vv->resources:0x%02x, we want:0x%02x\n",vv
->resources
,bit
));
21 /* no, someone else uses it */
24 /* it's free, grab it */
27 DEB_D(("res: get 0x%02x, cur:0x%02x\n",bit
,vv
->resources
));
31 void saa7146_res_free(struct saa7146_fh
*fh
, unsigned int bits
)
33 struct saa7146_dev
*dev
= fh
->dev
;
34 struct saa7146_vv
*vv
= dev
->vv_data
;
36 BUG_ON((fh
->resources
& bits
) != bits
);
38 fh
->resources
&= ~bits
;
39 vv
->resources
&= ~bits
;
40 DEB_D(("res: put 0x%02x, cur:0x%02x\n",bits
,vv
->resources
));
44 /********************************************************************************/
45 /* common dma functions */
47 void saa7146_dma_free(struct saa7146_dev
*dev
,struct videobuf_queue
*q
,
48 struct saa7146_buf
*buf
)
50 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
51 DEB_EE(("dev:%p, buf:%p\n",dev
,buf
));
53 BUG_ON(in_interrupt());
55 videobuf_waiton(q
, &buf
->vb
, 0, 0);
56 videobuf_dma_unmap(q
->dev
, dma
);
57 videobuf_dma_free(dma
);
58 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
62 /********************************************************************************/
63 /* common buffer functions */
65 int saa7146_buffer_queue(struct saa7146_dev
*dev
,
66 struct saa7146_dmaqueue
*q
,
67 struct saa7146_buf
*buf
)
69 assert_spin_locked(&dev
->slock
);
70 DEB_EE(("dev:%p, dmaq:%p, buf:%p\n", dev
, q
, buf
));
74 if (NULL
== q
->curr
) {
76 DEB_D(("immediately activating buffer %p\n", buf
));
77 buf
->activate(dev
,buf
,NULL
);
79 list_add_tail(&buf
->vb
.queue
,&q
->queue
);
80 buf
->vb
.state
= VIDEOBUF_QUEUED
;
81 DEB_D(("adding buffer %p to queue. (active buffer present)\n", buf
));
86 void saa7146_buffer_finish(struct saa7146_dev
*dev
,
87 struct saa7146_dmaqueue
*q
,
90 assert_spin_locked(&dev
->slock
);
91 DEB_EE(("dev:%p, dmaq:%p, state:%d\n", dev
, q
, state
));
92 DEB_EE(("q->curr:%p\n",q
->curr
));
96 /* finish current buffer */
97 if (NULL
== q
->curr
) {
98 DEB_D(("aiii. no current buffer\n"));
102 q
->curr
->vb
.state
= state
;
103 do_gettimeofday(&q
->curr
->vb
.ts
);
104 wake_up(&q
->curr
->vb
.done
);
109 void saa7146_buffer_next(struct saa7146_dev
*dev
,
110 struct saa7146_dmaqueue
*q
, int vbi
)
112 struct saa7146_buf
*buf
,*next
= NULL
;
116 DEB_INT(("dev:%p, dmaq:%p, vbi:%d\n", dev
, q
, vbi
));
118 assert_spin_locked(&dev
->slock
);
119 if (!list_empty(&q
->queue
)) {
120 /* activate next one from queue */
121 buf
= list_entry(q
->queue
.next
,struct saa7146_buf
,vb
.queue
);
122 list_del(&buf
->vb
.queue
);
123 if (!list_empty(&q
->queue
))
124 next
= list_entry(q
->queue
.next
,struct saa7146_buf
, vb
.queue
);
126 DEB_INT(("next buffer: buf:%p, prev:%p, next:%p\n", buf
, q
->queue
.prev
,q
->queue
.next
));
127 buf
->activate(dev
,buf
,next
);
129 DEB_INT(("no next buffer. stopping.\n"));
131 /* turn off video-dma3 */
132 saa7146_write(dev
,MC1
, MASK_20
);
134 /* nothing to do -- just prevent next video-dma1 transfer
135 by lowering the protection address */
137 // fixme: fix this for vflip != 0
139 saa7146_write(dev
, PROT_ADDR1
, 0);
140 saa7146_write(dev
, MC2
, (MASK_02
|MASK_18
));
142 /* write the address of the rps-program */
143 saa7146_write(dev
, RPS_ADDR0
, dev
->d_rps0
.dma_handle
);
145 saa7146_write(dev
, MC1
, (MASK_12
| MASK_28
));
148 printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
149 printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
150 printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
151 printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
152 printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));
153 printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
156 del_timer(&q
->timeout
);
160 void saa7146_buffer_timeout(unsigned long data
)
162 struct saa7146_dmaqueue
*q
= (struct saa7146_dmaqueue
*)data
;
163 struct saa7146_dev
*dev
= q
->dev
;
166 DEB_EE(("dev:%p, dmaq:%p\n", dev
, q
));
168 spin_lock_irqsave(&dev
->slock
,flags
);
170 DEB_D(("timeout on %p\n", q
->curr
));
171 saa7146_buffer_finish(dev
,q
,VIDEOBUF_ERROR
);
174 /* we don't restart the transfer here like other drivers do. when
175 a streaming capture is disabled, the timeout function will be
176 called for the current buffer. if we activate the next buffer now,
177 we mess up our capture logic. if a timeout occurs on another buffer,
178 then something is seriously broken before, so no need to buffer the
179 next capture IMHO... */
181 saa7146_buffer_next(dev,q);
183 spin_unlock_irqrestore(&dev
->slock
,flags
);
186 /********************************************************************************/
187 /* file operations */
189 static int fops_open(struct file
*file
)
191 struct video_device
*vdev
= video_devdata(file
);
192 struct saa7146_dev
*dev
= video_drvdata(file
);
193 struct saa7146_fh
*fh
= NULL
;
196 enum v4l2_buf_type type
;
198 DEB_EE(("file:%p, dev:%s\n", file
, video_device_node_name(vdev
)));
200 if (mutex_lock_interruptible(&saa7146_devices_lock
))
203 DEB_D(("using: %p\n",dev
));
205 type
= vdev
->vfl_type
== VFL_TYPE_GRABBER
206 ? V4L2_BUF_TYPE_VIDEO_CAPTURE
207 : V4L2_BUF_TYPE_VBI_CAPTURE
;
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 file
->private_data
= fh
;
228 if( fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
229 DEB_S(("initializing vbi...\n"));
230 if (dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
)
231 result
= saa7146_vbi_uops
.open(dev
,file
);
232 if (dev
->ext_vv_data
->vbi_fops
.open
)
233 dev
->ext_vv_data
->vbi_fops
.open(file
);
235 DEB_S(("initializing video...\n"));
236 result
= saa7146_video_uops
.open(dev
,file
);
243 if( 0 == try_module_get(dev
->ext
->module
)) {
250 if (fh
&& result
!= 0) {
252 file
->private_data
= NULL
;
254 mutex_unlock(&saa7146_devices_lock
);
258 static int fops_release(struct file
*file
)
260 struct saa7146_fh
*fh
= file
->private_data
;
261 struct saa7146_dev
*dev
= fh
->dev
;
263 DEB_EE(("file:%p\n", file
));
265 if (mutex_lock_interruptible(&saa7146_devices_lock
))
268 if( fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
269 if (dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
)
270 saa7146_vbi_uops
.release(dev
,file
);
271 if (dev
->ext_vv_data
->vbi_fops
.release
)
272 dev
->ext_vv_data
->vbi_fops
.release(file
);
274 saa7146_video_uops
.release(dev
,file
);
277 module_put(dev
->ext
->module
);
278 file
->private_data
= NULL
;
281 mutex_unlock(&saa7146_devices_lock
);
286 static int fops_mmap(struct file
*file
, struct vm_area_struct
* vma
)
288 struct saa7146_fh
*fh
= file
->private_data
;
289 struct videobuf_queue
*q
;
292 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
293 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",file
, vma
));
297 case V4L2_BUF_TYPE_VBI_CAPTURE
: {
298 DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",file
, vma
));
307 return videobuf_mmap_mapper(q
,vma
);
310 static unsigned int fops_poll(struct file
*file
, struct poll_table_struct
*wait
)
312 struct saa7146_fh
*fh
= file
->private_data
;
313 struct videobuf_buffer
*buf
= NULL
;
314 struct videobuf_queue
*q
;
316 DEB_EE(("file:%p, poll:%p\n",file
, wait
));
318 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
319 if( 0 == fh
->vbi_q
.streaming
)
320 return videobuf_poll_stream(file
, &fh
->vbi_q
, wait
);
323 DEB_D(("using video queue.\n"));
327 if (!list_empty(&q
->stream
))
328 buf
= list_entry(q
->stream
.next
, struct videobuf_buffer
, stream
);
331 DEB_D(("buf == NULL!\n"));
335 poll_wait(file
, &buf
->done
, wait
);
336 if (buf
->state
== VIDEOBUF_DONE
|| buf
->state
== VIDEOBUF_ERROR
) {
337 DEB_D(("poll succeeded!\n"));
338 return POLLIN
|POLLRDNORM
;
341 DEB_D(("nothing to poll for, buf->state:%d\n",buf
->state
));
345 static ssize_t
fops_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
347 struct saa7146_fh
*fh
= file
->private_data
;
350 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
351 // DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun", file, data, (unsigned long)count));
352 return saa7146_video_uops
.read(file
,data
,count
,ppos
);
354 case V4L2_BUF_TYPE_VBI_CAPTURE
: {
355 // DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n", file, data, (unsigned long)count));
356 if (fh
->dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
)
357 return saa7146_vbi_uops
.read(file
,data
,count
,ppos
);
368 static ssize_t
fops_write(struct file
*file
, const char __user
*data
, size_t count
, loff_t
*ppos
)
370 struct saa7146_fh
*fh
= file
->private_data
;
373 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
375 case V4L2_BUF_TYPE_VBI_CAPTURE
:
376 if (fh
->dev
->ext_vv_data
->vbi_fops
.write
)
377 return fh
->dev
->ext_vv_data
->vbi_fops
.write(file
, data
, count
, ppos
);
386 static const struct v4l2_file_operations video_fops
=
388 .owner
= THIS_MODULE
,
390 .release
= fops_release
,
395 .unlocked_ioctl
= video_ioctl2
,
398 static void vv_callback(struct saa7146_dev
*dev
, unsigned long status
)
402 DEB_INT(("dev:%p, isr:0x%08x\n",dev
,(u32
)status
));
404 if (0 != (isr
& (MASK_27
))) {
405 DEB_INT(("irq: RPS0 (0x%08x).\n",isr
));
406 saa7146_video_uops
.irq_done(dev
,isr
);
409 if (0 != (isr
& (MASK_28
))) {
410 u32 mc2
= saa7146_read(dev
, MC2
);
411 if( 0 != (mc2
& MASK_15
)) {
412 DEB_INT(("irq: RPS1 vbi workaround (0x%08x).\n",isr
));
413 wake_up(&dev
->vv_data
->vbi_wq
);
414 saa7146_write(dev
,MC2
, MASK_31
);
417 DEB_INT(("irq: RPS1 (0x%08x).\n",isr
));
418 saa7146_vbi_uops
.irq_done(dev
,isr
);
422 int saa7146_vv_init(struct saa7146_dev
* dev
, struct saa7146_ext_vv
*ext_vv
)
424 struct saa7146_vv
*vv
;
427 err
= v4l2_device_register(&dev
->pci
->dev
, &dev
->v4l2_dev
);
431 vv
= kzalloc(sizeof(struct saa7146_vv
), GFP_KERNEL
);
433 ERR(("out of memory. aborting.\n"));
436 ext_vv
->ops
= saa7146_video_ioctl_ops
;
437 ext_vv
->core_ops
= &saa7146_video_ioctl_ops
;
439 DEB_EE(("dev:%p\n",dev
));
441 /* set default values for video parts of the saa7146 */
442 saa7146_write(dev
, BCS_CTRL
, 0x80400040);
444 /* enable video-port pins */
445 saa7146_write(dev
, MC1
, (MASK_10
| MASK_26
));
447 /* save per-device extension data (one extension can
448 handle different devices that might need different
449 configuration data) */
450 dev
->ext_vv_data
= ext_vv
;
452 vv
->d_clipping
.cpu_addr
= pci_alloc_consistent(dev
->pci
, SAA7146_CLIPPING_MEM
, &vv
->d_clipping
.dma_handle
);
453 if( NULL
== vv
->d_clipping
.cpu_addr
) {
454 ERR(("out of memory. aborting.\n"));
458 memset(vv
->d_clipping
.cpu_addr
, 0x0, SAA7146_CLIPPING_MEM
);
460 saa7146_video_uops
.init(dev
,vv
);
461 if (dev
->ext_vv_data
->capabilities
& V4L2_CAP_VBI_CAPTURE
)
462 saa7146_vbi_uops
.init(dev
,vv
);
465 dev
->vv_callback
= &vv_callback
;
469 EXPORT_SYMBOL_GPL(saa7146_vv_init
);
471 int saa7146_vv_release(struct saa7146_dev
* dev
)
473 struct saa7146_vv
*vv
= dev
->vv_data
;
475 DEB_EE(("dev:%p\n",dev
));
477 v4l2_device_unregister(&dev
->v4l2_dev
);
478 pci_free_consistent(dev
->pci
, SAA7146_CLIPPING_MEM
, vv
->d_clipping
.cpu_addr
, vv
->d_clipping
.dma_handle
);
481 dev
->vv_callback
= NULL
;
485 EXPORT_SYMBOL_GPL(saa7146_vv_release
);
487 int saa7146_register_device(struct video_device
**vid
, struct saa7146_dev
* dev
,
488 char *name
, int type
)
490 struct video_device
*vfd
;
494 DEB_EE(("dev:%p, name:'%s', type:%d\n",dev
,name
,type
));
496 // released by vfd->release
497 vfd
= video_device_alloc();
501 vfd
->fops
= &video_fops
;
502 vfd
->ioctl_ops
= &dev
->ext_vv_data
->ops
;
503 vfd
->release
= video_device_release
;
504 vfd
->lock
= &dev
->v4l2_lock
;
506 for (i
= 0; i
< dev
->ext_vv_data
->num_stds
; i
++)
507 vfd
->tvnorms
|= dev
->ext_vv_data
->stds
[i
].id
;
508 strlcpy(vfd
->name
, name
, sizeof(vfd
->name
));
509 video_set_drvdata(vfd
, dev
);
511 err
= video_register_device(vfd
, type
, -1);
513 ERR(("cannot register v4l2 device. skipping.\n"));
514 video_device_release(vfd
);
518 INFO(("%s: registered device %s [v4l2]\n",
519 dev
->name
, video_device_node_name(vfd
)));
524 EXPORT_SYMBOL_GPL(saa7146_register_device
);
526 int saa7146_unregister_device(struct video_device
**vid
, struct saa7146_dev
* dev
)
528 DEB_EE(("dev:%p\n",dev
));
530 video_unregister_device(*vid
);
535 EXPORT_SYMBOL_GPL(saa7146_unregister_device
);
537 static int __init
saa7146_vv_init_module(void)
543 static void __exit
saa7146_vv_cleanup_module(void)
547 module_init(saa7146_vv_init_module
);
548 module_exit(saa7146_vv_cleanup_module
);
550 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
551 MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
552 MODULE_LICENSE("GPL");