1 #include <media/saa7146_vv.h>
3 #define BOARD_CAN_DO_VBI(dev) (dev->revision != 0 && dev->vv_data->vbi_minor != -1)
5 /****************************************************************************/
6 /* resource management functions, shamelessly stolen from saa7134 driver */
8 int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
10 struct saa7146_dev *dev = fh->dev;
11 struct saa7146_vv *vv = dev->vv_data;
13 if (fh->resources & bit) {
14 DEB_D(("already allocated! want: 0x%02x, cur:0x%02x\n",bit,vv->resources));
15 /* have it already allocated */
20 mutex_lock(&dev->lock);
21 if (vv->resources & bit) {
22 DEB_D(("locked! vv->resources:0x%02x, we want:0x%02x\n",vv->resources,bit));
23 /* no, someone else uses it */
24 mutex_unlock(&dev->lock);
27 /* it's free, grab it */
30 DEB_D(("res: get 0x%02x, cur:0x%02x\n",bit,vv->resources));
31 mutex_unlock(&dev->lock);
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 mutex_lock(&dev->lock);
43 fh->resources &= ~bits;
44 vv->resources &= ~bits;
45 DEB_D(("res: put 0x%02x, cur:0x%02x\n",bits,vv->resources));
46 mutex_unlock(&dev->lock);
50 /********************************************************************************/
51 /* common dma functions */
53 void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
54 struct saa7146_buf *buf)
56 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
57 DEB_EE(("dev:%p, buf:%p\n",dev,buf));
59 BUG_ON(in_interrupt());
61 videobuf_waiton(&buf->vb,0,0);
62 videobuf_dma_unmap(q, dma);
63 videobuf_dma_free(dma);
64 buf->vb.state = VIDEOBUF_NEEDS_INIT;
68 /********************************************************************************/
69 /* common buffer functions */
71 int saa7146_buffer_queue(struct saa7146_dev *dev,
72 struct saa7146_dmaqueue *q,
73 struct saa7146_buf *buf)
75 assert_spin_locked(&dev->slock);
76 DEB_EE(("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf));
80 if (NULL == q->curr) {
82 DEB_D(("immediately activating buffer %p\n", buf));
83 buf->activate(dev,buf,NULL);
85 list_add_tail(&buf->vb.queue,&q->queue);
86 buf->vb.state = VIDEOBUF_QUEUED;
87 DEB_D(("adding buffer %p to queue. (active buffer present)\n", buf));
92 void saa7146_buffer_finish(struct saa7146_dev *dev,
93 struct saa7146_dmaqueue *q,
96 assert_spin_locked(&dev->slock);
97 DEB_EE(("dev:%p, dmaq:%p, state:%d\n", dev, q, state));
98 DEB_EE(("q->curr:%p\n",q->curr));
102 /* finish current buffer */
103 if (NULL == q->curr) {
104 DEB_D(("aiii. no current buffer\n"));
108 q->curr->vb.state = state;
109 do_gettimeofday(&q->curr->vb.ts);
110 wake_up(&q->curr->vb.done);
115 void saa7146_buffer_next(struct saa7146_dev *dev,
116 struct saa7146_dmaqueue *q, int vbi)
118 struct saa7146_buf *buf,*next = NULL;
122 DEB_INT(("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi));
124 assert_spin_locked(&dev->slock);
125 if (!list_empty(&q->queue)) {
126 /* activate next one from queue */
127 buf = list_entry(q->queue.next,struct saa7146_buf,vb.queue);
128 list_del(&buf->vb.queue);
129 if (!list_empty(&q->queue))
130 next = list_entry(q->queue.next,struct saa7146_buf, vb.queue);
132 DEB_INT(("next buffer: buf:%p, prev:%p, next:%p\n", 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 unsigned int minor = video_devdata(file)->minor;
198 struct video_device *vdev = video_devdata(file);
199 struct saa7146_dev *dev = video_drvdata(file);
200 struct saa7146_fh *fh = NULL;
203 enum v4l2_buf_type type;
205 DEB_EE(("file:%p, minor:%d\n", file, minor));
207 if (mutex_lock_interruptible(&saa7146_devices_lock))
210 DEB_D(("using: %p\n",dev));
212 type = vdev->vfl_type == VFL_TYPE_GRABBER
213 ? V4L2_BUF_TYPE_VIDEO_CAPTURE
214 : V4L2_BUF_TYPE_VBI_CAPTURE;
216 /* check if an extension is registered */
217 if( NULL == dev->ext ) {
218 DEB_S(("no extension registered for this device.\n"));
223 /* allocate per open data */
224 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
226 DEB_S(("cannot allocate memory for per open data.\n"));
231 file->private_data = fh;
235 if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
236 DEB_S(("initializing vbi...\n"));
237 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
238 result = saa7146_vbi_uops.open(dev,file);
239 if (dev->ext_vv_data->vbi_fops.open)
240 dev->ext_vv_data->vbi_fops.open(file);
242 DEB_S(("initializing video...\n"));
243 result = saa7146_video_uops.open(dev,file);
250 if( 0 == try_module_get(dev->ext->module)) {
257 if (fh && result != 0) {
259 file->private_data = NULL;
261 mutex_unlock(&saa7146_devices_lock);
265 static int fops_release(struct file *file)
267 struct saa7146_fh *fh = file->private_data;
268 struct saa7146_dev *dev = fh->dev;
270 DEB_EE(("file:%p\n", file));
272 if (mutex_lock_interruptible(&saa7146_devices_lock))
275 if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
276 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
277 saa7146_vbi_uops.release(dev,file);
278 if (dev->ext_vv_data->vbi_fops.release)
279 dev->ext_vv_data->vbi_fops.release(file);
281 saa7146_video_uops.release(dev,file);
284 module_put(dev->ext->module);
285 file->private_data = NULL;
288 mutex_unlock(&saa7146_devices_lock);
293 static int fops_mmap(struct file *file, struct vm_area_struct * vma)
295 struct saa7146_fh *fh = file->private_data;
296 struct videobuf_queue *q;
299 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
300 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",file, vma));
304 case V4L2_BUF_TYPE_VBI_CAPTURE: {
305 DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",file, vma));
314 return videobuf_mmap_mapper(q,vma);
317 static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait)
319 struct saa7146_fh *fh = file->private_data;
320 struct videobuf_buffer *buf = NULL;
321 struct videobuf_queue *q;
323 DEB_EE(("file:%p, poll:%p\n",file, wait));
325 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
326 if( 0 == fh->vbi_q.streaming )
327 return videobuf_poll_stream(file, &fh->vbi_q, wait);
330 DEB_D(("using video queue.\n"));
334 if (!list_empty(&q->stream))
335 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
338 DEB_D(("buf == NULL!\n"));
342 poll_wait(file, &buf->done, wait);
343 if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) {
344 DEB_D(("poll succeeded!\n"));
345 return POLLIN|POLLRDNORM;
348 DEB_D(("nothing to poll for, buf->state:%d\n",buf->state));
352 static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
354 struct saa7146_fh *fh = file->private_data;
357 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
358 // DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun", file, data, (unsigned long)count));
359 return saa7146_video_uops.read(file,data,count,ppos);
361 case V4L2_BUF_TYPE_VBI_CAPTURE: {
362 // DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n", file, data, (unsigned long)count));
363 if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
364 return saa7146_vbi_uops.read(file,data,count,ppos);
375 static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
377 struct saa7146_fh *fh = file->private_data;
380 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
382 case V4L2_BUF_TYPE_VBI_CAPTURE:
383 if (fh->dev->ext_vv_data->vbi_fops.write)
384 return fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
393 static const struct v4l2_file_operations video_fops =
395 .owner = THIS_MODULE,
397 .release = fops_release,
402 .ioctl = video_ioctl2,
405 static void vv_callback(struct saa7146_dev *dev, unsigned long status)
409 DEB_INT(("dev:%p, isr:0x%08x\n",dev,(u32)status));
411 if (0 != (isr & (MASK_27))) {
412 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
413 saa7146_video_uops.irq_done(dev,isr);
416 if (0 != (isr & (MASK_28))) {
417 u32 mc2 = saa7146_read(dev, MC2);
418 if( 0 != (mc2 & MASK_15)) {
419 DEB_INT(("irq: RPS1 vbi workaround (0x%08x).\n",isr));
420 wake_up(&dev->vv_data->vbi_wq);
421 saa7146_write(dev,MC2, MASK_31);
424 DEB_INT(("irq: RPS1 (0x%08x).\n",isr));
425 saa7146_vbi_uops.irq_done(dev,isr);
429 int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
431 struct saa7146_vv *vv;
434 err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev);
438 vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL);
440 ERR(("out of memory. aborting.\n"));
443 ext_vv->ops = saa7146_video_ioctl_ops;
444 ext_vv->core_ops = &saa7146_video_ioctl_ops;
446 DEB_EE(("dev:%p\n",dev));
448 /* set default values for video parts of the saa7146 */
449 saa7146_write(dev, BCS_CTRL, 0x80400040);
451 /* enable video-port pins */
452 saa7146_write(dev, MC1, (MASK_10 | MASK_26));
454 /* save per-device extension data (one extension can
455 handle different devices that might need different
456 configuration data) */
457 dev->ext_vv_data = ext_vv;
459 vv->video_minor = -1;
462 vv->d_clipping.cpu_addr = pci_alloc_consistent(dev->pci, SAA7146_CLIPPING_MEM, &vv->d_clipping.dma_handle);
463 if( NULL == vv->d_clipping.cpu_addr ) {
464 ERR(("out of memory. aborting.\n"));
468 memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM);
470 saa7146_video_uops.init(dev,vv);
471 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
472 saa7146_vbi_uops.init(dev,vv);
475 dev->vv_callback = &vv_callback;
479 EXPORT_SYMBOL_GPL(saa7146_vv_init);
481 int saa7146_vv_release(struct saa7146_dev* dev)
483 struct saa7146_vv *vv = dev->vv_data;
485 DEB_EE(("dev:%p\n",dev));
487 v4l2_device_unregister(&dev->v4l2_dev);
488 pci_free_consistent(dev->pci, SAA7146_CLIPPING_MEM, vv->d_clipping.cpu_addr, vv->d_clipping.dma_handle);
491 dev->vv_callback = NULL;
495 EXPORT_SYMBOL_GPL(saa7146_vv_release);
497 int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
498 char *name, int type)
500 struct saa7146_vv *vv = dev->vv_data;
501 struct video_device *vfd;
505 DEB_EE(("dev:%p, name:'%s', type:%d\n",dev,name,type));
507 // released by vfd->release
508 vfd = video_device_alloc();
512 vfd->fops = &video_fops;
513 vfd->ioctl_ops = &dev->ext_vv_data->ops;
514 vfd->release = video_device_release;
516 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
517 vfd->tvnorms |= dev->ext_vv_data->stds[i].id;
518 strlcpy(vfd->name, name, sizeof(vfd->name));
519 video_set_drvdata(vfd, dev);
521 err = video_register_device(vfd, type, -1);
523 ERR(("cannot register v4l2 device. skipping.\n"));
524 video_device_release(vfd);
528 if (VFL_TYPE_GRABBER == type)
529 vv->video_minor = vfd->minor;
531 vv->vbi_minor = vfd->minor;
533 INFO(("%s: registered device %s [v4l2]\n",
534 dev->name, video_device_node_name(vfd)));
539 EXPORT_SYMBOL_GPL(saa7146_register_device);
541 int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev)
543 struct saa7146_vv *vv = dev->vv_data;
545 DEB_EE(("dev:%p\n",dev));
547 if ((*vid)->vfl_type == VFL_TYPE_GRABBER) {
548 vv->video_minor = -1;
553 video_unregister_device(*vid);
558 EXPORT_SYMBOL_GPL(saa7146_unregister_device);
560 static int __init saa7146_vv_init_module(void)
566 static void __exit saa7146_vv_cleanup_module(void)
570 module_init(saa7146_vv_init_module);
571 module_exit(saa7146_vv_cleanup_module);
573 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
574 MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
575 MODULE_LICENSE("GPL");