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