Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / drivers / media / common / saa7146_fops.c
1 #include <media/saa7146_vv.h>
2
3 #define BOARD_CAN_DO_VBI(dev)   (dev->revision != 0 && dev->vv_data->vbi_minor != -1)
4
5 /****************************************************************************/
6 /* resource management functions, shamelessly stolen from saa7134 driver */
7
8 int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
9 {
10         struct saa7146_dev *dev = fh->dev;
11         struct saa7146_vv *vv = dev->vv_data;
12
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 */
16                 return 1;
17         }
18
19         /* is it free? */
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);
25                 return 0;
26         }
27         /* it's free, grab it */
28         fh->resources  |= bit;
29         vv->resources |= bit;
30         DEB_D(("res: get 0x%02x, cur:0x%02x\n",bit,vv->resources));
31         mutex_unlock(&dev->lock);
32         return 1;
33 }
34
35 void saa7146_res_free(struct saa7146_fh *fh, unsigned int bits)
36 {
37         struct saa7146_dev *dev = fh->dev;
38         struct saa7146_vv *vv = dev->vv_data;
39
40         BUG_ON((fh->resources & bits) != bits);
41
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);
47 }
48
49
50 /********************************************************************************/
51 /* common dma functions */
52
53 void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
54                                                 struct saa7146_buf *buf)
55 {
56         DEB_EE(("dev:%p, buf:%p\n",dev,buf));
57
58         BUG_ON(in_interrupt());
59
60         videobuf_waiton(&buf->vb,0,0);
61         videobuf_dma_unmap(q, &buf->vb.dma);
62         videobuf_dma_free(&buf->vb.dma);
63         buf->vb.state = STATE_NEEDS_INIT;
64 }
65
66
67 /********************************************************************************/
68 /* common buffer functions */
69
70 int saa7146_buffer_queue(struct saa7146_dev *dev,
71                          struct saa7146_dmaqueue *q,
72                          struct saa7146_buf *buf)
73 {
74         assert_spin_locked(&dev->slock);
75         DEB_EE(("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf));
76
77         BUG_ON(!q);
78
79         if (NULL == q->curr) {
80                 q->curr = buf;
81                 DEB_D(("immediately activating buffer %p\n", buf));
82                 buf->activate(dev,buf,NULL);
83         } else {
84                 list_add_tail(&buf->vb.queue,&q->queue);
85                 buf->vb.state = STATE_QUEUED;
86                 DEB_D(("adding buffer %p to queue. (active buffer present)\n", buf));
87         }
88         return 0;
89 }
90
91 void saa7146_buffer_finish(struct saa7146_dev *dev,
92                            struct saa7146_dmaqueue *q,
93                            int state)
94 {
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));
98
99         BUG_ON(!q->curr);
100
101         /* finish current buffer */
102         if (NULL == q->curr) {
103                 DEB_D(("aiii. no current buffer\n"));
104                 return;
105         }
106
107         q->curr->vb.state = state;
108         do_gettimeofday(&q->curr->vb.ts);
109         wake_up(&q->curr->vb.done);
110
111         q->curr = NULL;
112 }
113
114 void saa7146_buffer_next(struct saa7146_dev *dev,
115                          struct saa7146_dmaqueue *q, int vbi)
116 {
117         struct saa7146_buf *buf,*next = NULL;
118
119         BUG_ON(!q);
120
121         DEB_INT(("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi));
122
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);
130                 q->curr = buf;
131                 DEB_INT(("next buffer: buf:%p, prev:%p, next:%p\n", buf, q->queue.prev,q->queue.next));
132                 buf->activate(dev,buf,next);
133         } else {
134                 DEB_INT(("no next buffer. stopping.\n"));
135                 if( 0 != vbi ) {
136                         /* turn off video-dma3 */
137                         saa7146_write(dev,MC1, MASK_20);
138                 } else {
139                         /* nothing to do -- just prevent next video-dma1 transfer
140                            by lowering the protection address */
141
142                         // fixme: fix this for vflip != 0
143
144                         saa7146_write(dev, PROT_ADDR1, 0);
145                         saa7146_write(dev, MC2, (MASK_02|MASK_18));
146
147                         /* write the address of the rps-program */
148                         saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
149                         /* turn on rps */
150                         saa7146_write(dev, MC1, (MASK_12 | MASK_28));
151
152 /*
153                         printk("vdma%d.base_even:     0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
154                         printk("vdma%d.base_odd:      0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
155                         printk("vdma%d.prot_addr:     0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
156                         printk("vdma%d.base_page:     0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
157                         printk("vdma%d.pitch:         0x%08x\n", 1,saa7146_read(dev,PITCH1));
158                         printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
159 */
160                 }
161                 del_timer(&q->timeout);
162         }
163 }
164
165 void saa7146_buffer_timeout(unsigned long data)
166 {
167         struct saa7146_dmaqueue *q = (struct saa7146_dmaqueue*)data;
168         struct saa7146_dev *dev = q->dev;
169         unsigned long flags;
170
171         DEB_EE(("dev:%p, dmaq:%p\n", dev, q));
172
173         spin_lock_irqsave(&dev->slock,flags);
174         if (q->curr) {
175                 DEB_D(("timeout on %p\n", q->curr));
176                 saa7146_buffer_finish(dev,q,STATE_ERROR);
177         }
178
179         /* we don't restart the transfer here like other drivers do. when
180            a streaming capture is disabled, the timeout function will be
181            called for the current buffer. if we activate the next buffer now,
182            we mess up our capture logic. if a timeout occurs on another buffer,
183            then something is seriously broken before, so no need to buffer the
184            next capture IMHO... */
185 /*
186         saa7146_buffer_next(dev,q);
187 */
188         spin_unlock_irqrestore(&dev->slock,flags);
189 }
190
191 /********************************************************************************/
192 /* file operations */
193
194 static int fops_open(struct inode *inode, struct file *file)
195 {
196         unsigned int minor = iminor(inode);
197         struct saa7146_dev *h = NULL, *dev = NULL;
198         struct list_head *list;
199         struct saa7146_fh *fh = NULL;
200         int result = 0;
201
202         enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
203
204         DEB_EE(("inode:%p, file:%p, minor:%d\n",inode,file,minor));
205
206         if (mutex_lock_interruptible(&saa7146_devices_lock))
207                 return -ERESTARTSYS;
208
209         list_for_each(list,&saa7146_devices) {
210                 h = list_entry(list, struct saa7146_dev, item);
211                 if( NULL == h->vv_data ) {
212                         DEB_D(("device %p has not registered video devices.\n",h));
213                         continue;
214                 }
215                 DEB_D(("trying: %p @ major %d,%d\n",h,h->vv_data->video_minor,h->vv_data->vbi_minor));
216
217                 if (h->vv_data->video_minor == minor) {
218                         dev = h;
219                 }
220                 if (h->vv_data->vbi_minor == minor) {
221                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
222                         dev = h;
223                 }
224         }
225         if (NULL == dev) {
226                 DEB_S(("no such video device.\n"));
227                 result = -ENODEV;
228                 goto out;
229         }
230
231         DEB_D(("using: %p\n",dev));
232
233         /* check if an extension is registered */
234         if( NULL == dev->ext ) {
235                 DEB_S(("no extension registered for this device.\n"));
236                 result = -ENODEV;
237                 goto out;
238         }
239
240         /* allocate per open data */
241         fh = kzalloc(sizeof(*fh),GFP_KERNEL);
242         if (NULL == fh) {
243                 DEB_S(("cannot allocate memory for per open data.\n"));
244                 result = -ENOMEM;
245                 goto out;
246         }
247
248         file->private_data = fh;
249         fh->dev = dev;
250         fh->type = type;
251
252         if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
253                 DEB_S(("initializing vbi...\n"));
254                 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
255                         result = saa7146_vbi_uops.open(dev,file);
256                 if (dev->ext_vv_data->vbi_fops.open)
257                         dev->ext_vv_data->vbi_fops.open(inode, file);
258         } else {
259                 DEB_S(("initializing video...\n"));
260                 result = saa7146_video_uops.open(dev,file);
261         }
262
263         if (0 != result) {
264                 goto out;
265         }
266
267         if( 0 == try_module_get(dev->ext->module)) {
268                 result = -EINVAL;
269                 goto out;
270         }
271
272         result = 0;
273 out:
274         if( fh != 0 && result != 0 ) {
275                 kfree(fh);
276                 file->private_data = NULL;
277         }
278         mutex_unlock(&saa7146_devices_lock);
279         return result;
280 }
281
282 static int fops_release(struct inode *inode, struct file *file)
283 {
284         struct saa7146_fh  *fh  = file->private_data;
285         struct saa7146_dev *dev = fh->dev;
286
287         DEB_EE(("inode:%p, file:%p\n",inode,file));
288
289         if (mutex_lock_interruptible(&saa7146_devices_lock))
290                 return -ERESTARTSYS;
291
292         if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
293                 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
294                         saa7146_vbi_uops.release(dev,file);
295                 if (dev->ext_vv_data->vbi_fops.release)
296                         dev->ext_vv_data->vbi_fops.release(inode, file);
297         } else {
298                 saa7146_video_uops.release(dev,file);
299         }
300
301         module_put(dev->ext->module);
302         file->private_data = NULL;
303         kfree(fh);
304
305         mutex_unlock(&saa7146_devices_lock);
306
307         return 0;
308 }
309
310 static int fops_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
311 {
312 /*
313         DEB_EE(("inode:%p, file:%p, cmd:%d, arg:%li\n",inode, file, cmd, arg));
314 */
315         return video_usercopy(inode, file, cmd, arg, saa7146_video_do_ioctl);
316 }
317
318 static int fops_mmap(struct file *file, struct vm_area_struct * vma)
319 {
320         struct saa7146_fh *fh = file->private_data;
321         struct videobuf_queue *q;
322
323         switch (fh->type) {
324         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
325                 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",file, vma));
326                 q = &fh->video_q;
327                 break;
328                 }
329         case V4L2_BUF_TYPE_VBI_CAPTURE: {
330                 DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",file, vma));
331                 q = &fh->vbi_q;
332                 break;
333                 }
334         default:
335                 BUG();
336                 return 0;
337         }
338
339         return videobuf_mmap_mapper(q,vma);
340 }
341
342 static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait)
343 {
344         struct saa7146_fh *fh = file->private_data;
345         struct videobuf_buffer *buf = NULL;
346         struct videobuf_queue *q;
347
348         DEB_EE(("file:%p, poll:%p\n",file, wait));
349
350         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
351                 if( 0 == fh->vbi_q.streaming )
352                         return videobuf_poll_stream(file, &fh->vbi_q, wait);
353                 q = &fh->vbi_q;
354         } else {
355                 DEB_D(("using video queue.\n"));
356                 q = &fh->video_q;
357         }
358
359         if (!list_empty(&q->stream))
360                 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
361
362         if (!buf) {
363                 DEB_D(("buf == NULL!\n"));
364                 return POLLERR;
365         }
366
367         poll_wait(file, &buf->done, wait);
368         if (buf->state == STATE_DONE || buf->state == STATE_ERROR) {
369                 DEB_D(("poll succeeded!\n"));
370                 return POLLIN|POLLRDNORM;
371         }
372
373         DEB_D(("nothing to poll for, buf->state:%d\n",buf->state));
374         return 0;
375 }
376
377 static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
378 {
379         struct saa7146_fh *fh = file->private_data;
380
381         switch (fh->type) {
382         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
383 //              DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun", file, data, (unsigned long)count));
384                 return saa7146_video_uops.read(file,data,count,ppos);
385                 }
386         case V4L2_BUF_TYPE_VBI_CAPTURE: {
387 //              DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n", file, data, (unsigned long)count));
388                 if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
389                         return saa7146_vbi_uops.read(file,data,count,ppos);
390                 else
391                         return -EINVAL;
392                 }
393                 break;
394         default:
395                 BUG();
396                 return 0;
397         }
398 }
399
400 static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
401 {
402         struct saa7146_fh *fh = file->private_data;
403
404         switch (fh->type) {
405         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
406                 return -EINVAL;
407         case V4L2_BUF_TYPE_VBI_CAPTURE:
408                 if (fh->dev->ext_vv_data->vbi_fops.write)
409                         return fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
410                 else
411                         return -EINVAL;
412         default:
413                 BUG();
414                 return -EINVAL;
415         }
416 }
417
418 static const struct file_operations video_fops =
419 {
420         .owner          = THIS_MODULE,
421         .open           = fops_open,
422         .release        = fops_release,
423         .read           = fops_read,
424         .write          = fops_write,
425         .poll           = fops_poll,
426         .mmap           = fops_mmap,
427         .ioctl          = fops_ioctl,
428         .llseek         = no_llseek,
429 };
430
431 static void vv_callback(struct saa7146_dev *dev, unsigned long status)
432 {
433         u32 isr = status;
434
435         DEB_INT(("dev:%p, isr:0x%08x\n",dev,(u32)status));
436
437         if (0 != (isr & (MASK_27))) {
438                 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
439                 saa7146_video_uops.irq_done(dev,isr);
440         }
441
442         if (0 != (isr & (MASK_28))) {
443                 u32 mc2 = saa7146_read(dev, MC2);
444                 if( 0 != (mc2 & MASK_15)) {
445                         DEB_INT(("irq: RPS1 vbi workaround (0x%08x).\n",isr));
446                         wake_up(&dev->vv_data->vbi_wq);
447                         saa7146_write(dev,MC2, MASK_31);
448                         return;
449                 }
450                 DEB_INT(("irq: RPS1 (0x%08x).\n",isr));
451                 saa7146_vbi_uops.irq_done(dev,isr);
452         }
453 }
454
455 static struct video_device device_template =
456 {
457         .fops           = &video_fops,
458         .minor          = -1,
459 };
460
461 int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
462 {
463         struct saa7146_vv *vv = kzalloc (sizeof(struct saa7146_vv),GFP_KERNEL);
464         if( NULL == vv ) {
465                 ERR(("out of memory. aborting.\n"));
466                 return -1;
467         }
468
469         DEB_EE(("dev:%p\n",dev));
470
471         /* set default values for video parts of the saa7146 */
472         saa7146_write(dev, BCS_CTRL, 0x80400040);
473
474         /* enable video-port pins */
475         saa7146_write(dev, MC1, (MASK_10 | MASK_26));
476
477         /* save per-device extension data (one extension can
478            handle different devices that might need different
479            configuration data) */
480         dev->ext_vv_data = ext_vv;
481
482         vv->video_minor = -1;
483         vv->vbi_minor = -1;
484
485         vv->d_clipping.cpu_addr = pci_alloc_consistent(dev->pci, SAA7146_CLIPPING_MEM, &vv->d_clipping.dma_handle);
486         if( NULL == vv->d_clipping.cpu_addr ) {
487                 ERR(("out of memory. aborting.\n"));
488                 kfree(vv);
489                 return -1;
490         }
491         memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM);
492
493         saa7146_video_uops.init(dev,vv);
494         if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
495                 saa7146_vbi_uops.init(dev,vv);
496
497         dev->vv_data = vv;
498         dev->vv_callback = &vv_callback;
499
500         return 0;
501 }
502 EXPORT_SYMBOL_GPL(saa7146_vv_init);
503
504 int saa7146_vv_release(struct saa7146_dev* dev)
505 {
506         struct saa7146_vv *vv = dev->vv_data;
507
508         DEB_EE(("dev:%p\n",dev));
509
510         pci_free_consistent(dev->pci, SAA7146_CLIPPING_MEM, vv->d_clipping.cpu_addr, vv->d_clipping.dma_handle);
511         kfree(vv);
512         dev->vv_data = NULL;
513         dev->vv_callback = NULL;
514
515         return 0;
516 }
517 EXPORT_SYMBOL_GPL(saa7146_vv_release);
518
519 int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
520                             char *name, int type)
521 {
522         struct saa7146_vv *vv = dev->vv_data;
523         struct video_device *vfd;
524
525         DEB_EE(("dev:%p, name:'%s', type:%d\n",dev,name,type));
526
527         // released by vfd->release
528         vfd = video_device_alloc();
529         if (vfd == NULL)
530                 return -ENOMEM;
531
532         memcpy(vfd, &device_template, sizeof(struct video_device));
533         strlcpy(vfd->name, name, sizeof(vfd->name));
534         vfd->release = video_device_release;
535         vfd->priv = dev;
536
537         // fixme: -1 should be an insmod parameter *for the extension* (like "video_nr");
538         if (video_register_device(vfd, type, -1) < 0) {
539                 ERR(("cannot register v4l2 device. skipping.\n"));
540                 return -1;
541         }
542
543         if( VFL_TYPE_GRABBER == type ) {
544                 vv->video_minor = vfd->minor;
545                 INFO(("%s: registered device video%d [v4l2]\n",
546                         dev->name, vfd->minor & 0x1f));
547         } else {
548                 vv->vbi_minor = vfd->minor;
549                 INFO(("%s: registered device vbi%d [v4l2]\n",
550                         dev->name, vfd->minor & 0x1f));
551         }
552
553         *vid = vfd;
554         return 0;
555 }
556 EXPORT_SYMBOL_GPL(saa7146_register_device);
557
558 int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev)
559 {
560         struct saa7146_vv *vv = dev->vv_data;
561
562         DEB_EE(("dev:%p\n",dev));
563
564         if( VFL_TYPE_GRABBER == (*vid)->type ) {
565                 vv->video_minor = -1;
566         } else {
567                 vv->vbi_minor = -1;
568         }
569
570         video_unregister_device(*vid);
571         *vid = NULL;
572
573         return 0;
574 }
575 EXPORT_SYMBOL_GPL(saa7146_unregister_device);
576
577 static int __init saa7146_vv_init_module(void)
578 {
579         return 0;
580 }
581
582
583 static void __exit saa7146_vv_cleanup_module(void)
584 {
585 }
586
587 module_init(saa7146_vv_init_module);
588 module_exit(saa7146_vv_cleanup_module);
589
590 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
591 MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
592 MODULE_LICENSE("GPL");