Revert "tracing: Include module.h in define_trace.h"
[pandora-kernel.git] / drivers / media / common / saa7146_fops.c
1 #include <media/saa7146_vv.h>
2 #include <linux/module.h>
3
4 /****************************************************************************/
5 /* resource management functions, shamelessly stolen from saa7134 driver */
6
7 int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
8 {
9         struct saa7146_dev *dev = fh->dev;
10         struct saa7146_vv *vv = dev->vv_data;
11
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 */
15                 return 1;
16         }
17
18         /* is it free? */
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 */
22                 return 0;
23         }
24         /* it's free, grab it */
25         fh->resources  |= bit;
26         vv->resources |= bit;
27         DEB_D(("res: get 0x%02x, cur:0x%02x\n",bit,vv->resources));
28         return 1;
29 }
30
31 void saa7146_res_free(struct saa7146_fh *fh, unsigned int bits)
32 {
33         struct saa7146_dev *dev = fh->dev;
34         struct saa7146_vv *vv = dev->vv_data;
35
36         BUG_ON((fh->resources & bits) != bits);
37
38         fh->resources  &= ~bits;
39         vv->resources &= ~bits;
40         DEB_D(("res: put 0x%02x, cur:0x%02x\n",bits,vv->resources));
41 }
42
43
44 /********************************************************************************/
45 /* common dma functions */
46
47 void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
48                                                 struct saa7146_buf *buf)
49 {
50         struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
51         DEB_EE(("dev:%p, buf:%p\n",dev,buf));
52
53         BUG_ON(in_interrupt());
54
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;
59 }
60
61
62 /********************************************************************************/
63 /* common buffer functions */
64
65 int saa7146_buffer_queue(struct saa7146_dev *dev,
66                          struct saa7146_dmaqueue *q,
67                          struct saa7146_buf *buf)
68 {
69         assert_spin_locked(&dev->slock);
70         DEB_EE(("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf));
71
72         BUG_ON(!q);
73
74         if (NULL == q->curr) {
75                 q->curr = buf;
76                 DEB_D(("immediately activating buffer %p\n", buf));
77                 buf->activate(dev,buf,NULL);
78         } else {
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));
82         }
83         return 0;
84 }
85
86 void saa7146_buffer_finish(struct saa7146_dev *dev,
87                            struct saa7146_dmaqueue *q,
88                            int state)
89 {
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));
93
94         BUG_ON(!q->curr);
95
96         /* finish current buffer */
97         if (NULL == q->curr) {
98                 DEB_D(("aiii. no current buffer\n"));
99                 return;
100         }
101
102         q->curr->vb.state = state;
103         do_gettimeofday(&q->curr->vb.ts);
104         wake_up(&q->curr->vb.done);
105
106         q->curr = NULL;
107 }
108
109 void saa7146_buffer_next(struct saa7146_dev *dev,
110                          struct saa7146_dmaqueue *q, int vbi)
111 {
112         struct saa7146_buf *buf,*next = NULL;
113
114         BUG_ON(!q);
115
116         DEB_INT(("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi));
117
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);
125                 q->curr = buf;
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);
128         } else {
129                 DEB_INT(("no next buffer. stopping.\n"));
130                 if( 0 != vbi ) {
131                         /* turn off video-dma3 */
132                         saa7146_write(dev,MC1, MASK_20);
133                 } else {
134                         /* nothing to do -- just prevent next video-dma1 transfer
135                            by lowering the protection address */
136
137                         // fixme: fix this for vflip != 0
138
139                         saa7146_write(dev, PROT_ADDR1, 0);
140                         saa7146_write(dev, MC2, (MASK_02|MASK_18));
141
142                         /* write the address of the rps-program */
143                         saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
144                         /* turn on rps */
145                         saa7146_write(dev, MC1, (MASK_12 | MASK_28));
146
147 /*
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));
154 */
155                 }
156                 del_timer(&q->timeout);
157         }
158 }
159
160 void saa7146_buffer_timeout(unsigned long data)
161 {
162         struct saa7146_dmaqueue *q = (struct saa7146_dmaqueue*)data;
163         struct saa7146_dev *dev = q->dev;
164         unsigned long flags;
165
166         DEB_EE(("dev:%p, dmaq:%p\n", dev, q));
167
168         spin_lock_irqsave(&dev->slock,flags);
169         if (q->curr) {
170                 DEB_D(("timeout on %p\n", q->curr));
171                 saa7146_buffer_finish(dev,q,VIDEOBUF_ERROR);
172         }
173
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... */
180 /*
181         saa7146_buffer_next(dev,q);
182 */
183         spin_unlock_irqrestore(&dev->slock,flags);
184 }
185
186 /********************************************************************************/
187 /* file operations */
188
189 static int fops_open(struct file *file)
190 {
191         struct video_device *vdev = video_devdata(file);
192         struct saa7146_dev *dev = video_drvdata(file);
193         struct saa7146_fh *fh = NULL;
194         int result = 0;
195
196         enum v4l2_buf_type type;
197
198         DEB_EE(("file:%p, dev:%s\n", file, video_device_node_name(vdev)));
199
200         if (mutex_lock_interruptible(&saa7146_devices_lock))
201                 return -ERESTARTSYS;
202
203         DEB_D(("using: %p\n",dev));
204
205         type = vdev->vfl_type == VFL_TYPE_GRABBER
206              ? V4L2_BUF_TYPE_VIDEO_CAPTURE
207              : V4L2_BUF_TYPE_VBI_CAPTURE;
208
209         /* check if an extension is registered */
210         if( NULL == dev->ext ) {
211                 DEB_S(("no extension registered for this device.\n"));
212                 result = -ENODEV;
213                 goto out;
214         }
215
216         /* allocate per open data */
217         fh = kzalloc(sizeof(*fh),GFP_KERNEL);
218         if (NULL == fh) {
219                 DEB_S(("cannot allocate memory for per open data.\n"));
220                 result = -ENOMEM;
221                 goto out;
222         }
223
224         file->private_data = fh;
225         fh->dev = dev;
226         fh->type = type;
227
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);
234         } else {
235                 DEB_S(("initializing video...\n"));
236                 result = saa7146_video_uops.open(dev,file);
237         }
238
239         if (0 != result) {
240                 goto out;
241         }
242
243         if( 0 == try_module_get(dev->ext->module)) {
244                 result = -EINVAL;
245                 goto out;
246         }
247
248         result = 0;
249 out:
250         if (fh && result != 0) {
251                 kfree(fh);
252                 file->private_data = NULL;
253         }
254         mutex_unlock(&saa7146_devices_lock);
255         return result;
256 }
257
258 static int fops_release(struct file *file)
259 {
260         struct saa7146_fh  *fh  = file->private_data;
261         struct saa7146_dev *dev = fh->dev;
262
263         DEB_EE(("file:%p\n", file));
264
265         if (mutex_lock_interruptible(&saa7146_devices_lock))
266                 return -ERESTARTSYS;
267
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);
273         } else {
274                 saa7146_video_uops.release(dev,file);
275         }
276
277         module_put(dev->ext->module);
278         file->private_data = NULL;
279         kfree(fh);
280
281         mutex_unlock(&saa7146_devices_lock);
282
283         return 0;
284 }
285
286 static int fops_mmap(struct file *file, struct vm_area_struct * vma)
287 {
288         struct saa7146_fh *fh = file->private_data;
289         struct videobuf_queue *q;
290
291         switch (fh->type) {
292         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
293                 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",file, vma));
294                 q = &fh->video_q;
295                 break;
296                 }
297         case V4L2_BUF_TYPE_VBI_CAPTURE: {
298                 DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",file, vma));
299                 q = &fh->vbi_q;
300                 break;
301                 }
302         default:
303                 BUG();
304                 return 0;
305         }
306
307         return videobuf_mmap_mapper(q,vma);
308 }
309
310 static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait)
311 {
312         struct saa7146_fh *fh = file->private_data;
313         struct videobuf_buffer *buf = NULL;
314         struct videobuf_queue *q;
315
316         DEB_EE(("file:%p, poll:%p\n",file, wait));
317
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);
321                 q = &fh->vbi_q;
322         } else {
323                 DEB_D(("using video queue.\n"));
324                 q = &fh->video_q;
325         }
326
327         if (!list_empty(&q->stream))
328                 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
329
330         if (!buf) {
331                 DEB_D(("buf == NULL!\n"));
332                 return POLLERR;
333         }
334
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;
339         }
340
341         DEB_D(("nothing to poll for, buf->state:%d\n",buf->state));
342         return 0;
343 }
344
345 static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
346 {
347         struct saa7146_fh *fh = file->private_data;
348
349         switch (fh->type) {
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);
353                 }
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);
358                 else
359                         return -EINVAL;
360                 }
361                 break;
362         default:
363                 BUG();
364                 return 0;
365         }
366 }
367
368 static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
369 {
370         struct saa7146_fh *fh = file->private_data;
371
372         switch (fh->type) {
373         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
374                 return -EINVAL;
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);
378                 else
379                         return -EINVAL;
380         default:
381                 BUG();
382                 return -EINVAL;
383         }
384 }
385
386 static const struct v4l2_file_operations video_fops =
387 {
388         .owner          = THIS_MODULE,
389         .open           = fops_open,
390         .release        = fops_release,
391         .read           = fops_read,
392         .write          = fops_write,
393         .poll           = fops_poll,
394         .mmap           = fops_mmap,
395         .unlocked_ioctl = video_ioctl2,
396 };
397
398 static void vv_callback(struct saa7146_dev *dev, unsigned long status)
399 {
400         u32 isr = status;
401
402         DEB_INT(("dev:%p, isr:0x%08x\n",dev,(u32)status));
403
404         if (0 != (isr & (MASK_27))) {
405                 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
406                 saa7146_video_uops.irq_done(dev,isr);
407         }
408
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);
415                         return;
416                 }
417                 DEB_INT(("irq: RPS1 (0x%08x).\n",isr));
418                 saa7146_vbi_uops.irq_done(dev,isr);
419         }
420 }
421
422 int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
423 {
424         struct saa7146_vv *vv;
425         int err;
426
427         err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev);
428         if (err)
429                 return err;
430
431         vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL);
432         if (vv == NULL) {
433                 ERR(("out of memory. aborting.\n"));
434                 return -ENOMEM;
435         }
436         ext_vv->ops = saa7146_video_ioctl_ops;
437         ext_vv->core_ops = &saa7146_video_ioctl_ops;
438
439         DEB_EE(("dev:%p\n",dev));
440
441         /* set default values for video parts of the saa7146 */
442         saa7146_write(dev, BCS_CTRL, 0x80400040);
443
444         /* enable video-port pins */
445         saa7146_write(dev, MC1, (MASK_10 | MASK_26));
446
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;
451
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"));
455                 kfree(vv);
456                 return -1;
457         }
458         memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM);
459
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);
463
464         dev->vv_data = vv;
465         dev->vv_callback = &vv_callback;
466
467         return 0;
468 }
469 EXPORT_SYMBOL_GPL(saa7146_vv_init);
470
471 int saa7146_vv_release(struct saa7146_dev* dev)
472 {
473         struct saa7146_vv *vv = dev->vv_data;
474
475         DEB_EE(("dev:%p\n",dev));
476
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);
479         kfree(vv);
480         dev->vv_data = NULL;
481         dev->vv_callback = NULL;
482
483         return 0;
484 }
485 EXPORT_SYMBOL_GPL(saa7146_vv_release);
486
487 int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
488                             char *name, int type)
489 {
490         struct video_device *vfd;
491         int err;
492         int i;
493
494         DEB_EE(("dev:%p, name:'%s', type:%d\n",dev,name,type));
495
496         // released by vfd->release
497         vfd = video_device_alloc();
498         if (vfd == NULL)
499                 return -ENOMEM;
500
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;
505         vfd->tvnorms = 0;
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);
510
511         err = video_register_device(vfd, type, -1);
512         if (err < 0) {
513                 ERR(("cannot register v4l2 device. skipping.\n"));
514                 video_device_release(vfd);
515                 return err;
516         }
517
518         INFO(("%s: registered device %s [v4l2]\n",
519                 dev->name, video_device_node_name(vfd)));
520
521         *vid = vfd;
522         return 0;
523 }
524 EXPORT_SYMBOL_GPL(saa7146_register_device);
525
526 int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev)
527 {
528         DEB_EE(("dev:%p\n",dev));
529
530         video_unregister_device(*vid);
531         *vid = NULL;
532
533         return 0;
534 }
535 EXPORT_SYMBOL_GPL(saa7146_unregister_device);
536
537 static int __init saa7146_vv_init_module(void)
538 {
539         return 0;
540 }
541
542
543 static void __exit saa7146_vv_cleanup_module(void)
544 {
545 }
546
547 module_init(saa7146_vv_init_module);
548 module_exit(saa7146_vv_cleanup_module);
549
550 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
551 MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
552 MODULE_LICENSE("GPL");