Subversion Repositories shark

Rev

Rev 773 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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