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