OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [media/] [video/] [vino.c] - Blame information for rev 1275

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * Driver for the VINO (Video In No Out) system found in SGI Indys.
3
 *
4
 * This file is subject to the terms and conditions of the GNU General Public
5
 * License version 2 as published by the Free Software Foundation.
6
 *
7
 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
8
 */
9
 
10
#include <linux/module.h>
11
#include <linux/kmod.h>
12
#include <linux/init.h>
13
#include <linux/types.h>
14
#include <linux/errno.h>
15
#include <linux/mm.h>
16
#include <linux/wrapper.h>
17
#include <linux/irq.h>
18
#include <linux/delay.h>
19
#include <linux/pci.h>
20
#include <linux/videodev.h>
21
#include <linux/video_decoder.h>
22
#include <linux/i2c.h>
23
#include <linux/i2c-algo-sgi.h>
24
 
25
#include <asm/paccess.h>
26
#include <asm/io.h>
27
#include <asm/sgi/ip22.h>
28
#include <asm/sgi/hpc3.h>
29
#include <asm/sgi/mc.h>
30
 
31
#include "vino.h"
32
 
33
/* debugging? */
34
#if 1
35
#define DEBUG(x...)     printk(x);
36
#else
37
#define DEBUG(x...)
38
#endif
39
 
40
/* VINO video size */
41
#define VINO_PAL_WIDTH          768
42
#define VINO_PAL_HEIGHT         576
43
#define VINO_NTSC_WIDTH         646
44
#define VINO_NTSC_HEIGHT        486
45
 
46
/* set this to some sensible values. note: VINO_MIN_WIDTH has to be 8*x */
47
#define VINO_MIN_WIDTH          32
48
#define VINO_MIN_HEIGHT         32
49
 
50
/* channel selection */
51
#define VINO_INPUT_COMP         0
52
#define VINO_INPUT_SVIDEO       1
53
#define VINO_INPUT_CAMERA       2
54
#define VINO_INPUT_CHANNELS     3
55
 
56
#define PAGE_RATIO      (PAGE_SIZE / VINO_PAGE_SIZE)
57
 
58
/* VINO ASIC registers */
59
struct sgi_vino *vino;
60
 
61
static const char *vinostr = "VINO IndyCam/TV";
62
static int threshold_a = 512;
63
static int threshold_b = 512;
64
 
65
struct vino_device {
66
        struct video_device vdev;
67
#define VINO_CHAN_A     1
68
#define VINO_CHAN_B     2
69
        int chan;
70
        int alpha;
71
        /* clipping... */
72
        unsigned int left, right, top, bottom;
73
        /* decimation used */
74
        unsigned int decimation;
75
        /* palette used, picture hue, etc */
76
        struct video_picture picture;
77
        /* VINO_INPUT_COMP, VINO_INPUT_SVIDEO or VINO_INPUT_CAMERA */
78
        unsigned int input;
79
        /* bytes per line */
80
        unsigned int line_size;
81
        /* descriptor table (virtual addresses) */
82
        unsigned long *desc;
83
        /* # of allocated pages */
84
        int page_count;
85
        /* descriptor table (dma addresses) */
86
        struct {
87
                dma_addr_t *cpu;
88
                dma_addr_t dma;
89
        } dma_desc;
90
        /* add some more space to let VINO trigger End Of Field interrupt
91
         * before reaching end of buffer */
92
#define VINO_FBUFSIZE   (VINO_PAL_WIDTH * VINO_PAL_HEIGHT * 4 + 2 * PAGE_SIZE)
93
        unsigned int frame_size;
94
#define VINO_BUF_UNUSED         0
95
#define VINO_BUF_GRABBING       1
96
#define VINO_BUF_DONE           2
97
        int buffer_state;
98
 
99
        wait_queue_head_t dma_wait;
100
        spinlock_t state_lock;
101
        struct semaphore sem;
102
 
103
        /* Make sure we only have one user at the time */
104
        int users;
105
};
106
 
107
struct vino_client {
108
        struct i2c_client *driver;
109
        int owner;
110
};
111
 
112
struct vino_video {
113
        struct vino_device chA;
114
        struct vino_device chB;
115
 
116
        struct vino_client decoder;
117
        struct vino_client camera;
118
        spinlock_t vino_lock;
119
        spinlock_t input_lock;
120
 
121
        /* Loaded into VINO descriptors to clear End Of Descriptors table
122
         * interupt condition */
123
        unsigned long dummy_desc;
124
        struct {
125
                dma_addr_t *cpu;
126
                dma_addr_t dma;
127
        } dummy_dma;
128
};
129
 
130
static struct vino_video *Vino;
131
 
132
/* --- */
133
 
134
unsigned i2c_vino_getctrl(void *data)
135
{
136
        return vino->i2c_control;
137
}
138
 
139
void i2c_vino_setctrl(void *data, unsigned val)
140
{
141
        vino->i2c_control = val;
142
}
143
 
144
unsigned i2c_vino_rdata(void *data)
145
{
146
        return vino->i2c_data;
147
}
148
 
149
void i2c_vino_wdata(void *data, unsigned val)
150
{
151
        vino->i2c_data = val;
152
}
153
 
154
static struct i2c_algo_sgi_data i2c_sgi_vino_data =
155
{
156
        .getctrl = &i2c_vino_getctrl,
157
        .setctrl = &i2c_vino_setctrl,
158
        .rdata   = &i2c_vino_rdata,
159
        .wdata   = &i2c_vino_wdata,
160
        .xfer_timeout = 200,
161
        .ack_timeout  = 1000,
162
};
163
 
164
/*
165
 * There are two possible clients on VINO I2C bus, so we limit usage only
166
 * to them.
167
 */
168
static int i2c_vino_client_reg(struct i2c_client *client)
169
{
170
        int res = 0;
171
 
172
        spin_lock(&Vino->input_lock);
173
        switch (client->driver->id) {
174
        case I2C_DRIVERID_SAA7191:
175
                if (Vino->decoder.driver)
176
                        res = -EBUSY;
177
                else
178
                        Vino->decoder.driver = client;
179
                break;
180
        case I2C_DRIVERID_INDYCAM:
181
                if (Vino->camera.driver)
182
                        res = -EBUSY;
183
                else
184
                        Vino->camera.driver = client;
185
                break;
186
        default:
187
                res = -ENODEV;
188
        }
189
        spin_unlock(&Vino->input_lock);
190
 
191
        return res;
192
}
193
 
194
static int i2c_vino_client_unreg(struct i2c_client *client)
195
{
196
        int res = 0;
197
 
198
        spin_lock(&Vino->input_lock);
199
        if (client == Vino->decoder.driver) {
200
                if (Vino->decoder.owner)
201
                        res = -EBUSY;
202
                else
203
                        Vino->decoder.driver = NULL;
204
        } else if (client == Vino->camera.driver) {
205
                if (Vino->camera.owner)
206
                        res = -EBUSY;
207
                else
208
                        Vino->camera.driver = NULL;
209
        }
210
        spin_unlock(&Vino->input_lock);
211
 
212
        return res;
213
}
214
 
215
static struct i2c_adapter vino_i2c_adapter =
216
{
217
        .name                   = "VINO I2C bus",
218
        .id                     = I2C_HW_SGI_VINO,
219
        .algo_data              = &i2c_sgi_vino_data,
220
        .client_register        = &i2c_vino_client_reg,
221
        .client_unregister      = &i2c_vino_client_unreg,
222
};
223
 
224
static int vino_i2c_add_bus(void)
225
{
226
        return i2c_sgi_add_bus(&vino_i2c_adapter);
227
}
228
 
229
static int vino_i2c_del_bus(void)
230
{
231
        return i2c_sgi_del_bus(&vino_i2c_adapter);
232
}
233
 
234
static int i2c_camera_command(unsigned int cmd, void *arg)
235
{
236
        return Vino->camera.driver->driver->command(Vino->camera.driver,
237
                                                    cmd, arg);
238
}
239
 
240
static int i2c_decoder_command(unsigned int cmd, void *arg)
241
{
242
        return Vino->decoder.driver->driver->command(Vino->decoder.driver,
243
                                                     cmd, arg);
244
}
245
 
246
/* --- */
247
 
248
static int bytes_per_pixel(struct vino_device *v)
249
{
250
        switch (v->picture.palette) {
251
        case VIDEO_PALETTE_GREY:
252
                return 1;
253
        case VIDEO_PALETTE_YUV422:
254
                return 2;
255
        default: /* VIDEO_PALETTE_RGB32 */
256
                return 4;
257
        }
258
}
259
 
260
static int get_capture_norm(struct vino_device *v)
261
{
262
        if (v->input == VINO_INPUT_CAMERA)
263
                return VIDEO_MODE_NTSC;
264
        else {
265
                /* TODO */
266
                return VIDEO_MODE_NTSC;
267
        }
268
}
269
 
270
/*
271
 * Set clipping. Try new values to fit, if they don't return -EINVAL
272
 */
273
static int set_clipping(struct vino_device *v, int x, int y, int w, int h,
274
                        int d)
275
{
276
        int maxwidth, maxheight, lsize;
277
 
278
        if (d < 1)
279
                d = 1;
280
        if (d > 8)
281
                d = 8;
282
        if (w / d < VINO_MIN_WIDTH || h / d < VINO_MIN_HEIGHT)
283
                return -EINVAL;
284
        if (get_capture_norm(v) == VIDEO_MODE_NTSC) {
285
                maxwidth = VINO_NTSC_WIDTH;
286
                maxheight = VINO_NTSC_HEIGHT;
287
        } else {
288
                maxwidth = VINO_PAL_WIDTH;
289
                maxheight = VINO_PAL_HEIGHT;
290
        }
291
        if (x < 0)
292
                x = 0;
293
        if (y < 0)
294
                y = 0;
295
        y &= ~1;        /* odd/even fields */
296
        if (x + w > maxwidth) {
297
                w = maxwidth - x;
298
                if (w / d < VINO_MIN_WIDTH)
299
                        x = maxwidth - VINO_MIN_WIDTH * d;
300
        }
301
        if (y + h > maxheight) {
302
                h = maxheight - y;
303
                if (h / d < VINO_MIN_HEIGHT)
304
                        y = maxheight - VINO_MIN_HEIGHT * d;
305
        }
306
        /* line size must be multiple of 8 bytes */
307
        lsize = (bytes_per_pixel(v) * w / d) & ~7;
308
        w = lsize * d / bytes_per_pixel(v);
309
        v->left = x;
310
        v->top = y;
311
        v->right = x + w;
312
        v->bottom = y + h;
313
        v->decimation = d;
314
        v->line_size = lsize;
315
        DEBUG("VINO: clipping %d, %d, %d, %d / %d - %d\n", v->left, v->top,
316
              v->right, v->bottom, v->decimation, v->line_size);
317
        return 0;
318
}
319
 
320
static int set_scaling(struct vino_device *v, int w, int h)
321
{
322
        int maxwidth, maxheight, lsize, d;
323
 
324
        if (w < VINO_MIN_WIDTH || h < VINO_MIN_HEIGHT)
325
                return -EINVAL;
326
        if (get_capture_norm(v) == VIDEO_MODE_NTSC) {
327
                maxwidth = VINO_NTSC_WIDTH;
328
                maxheight = VINO_NTSC_HEIGHT;
329
        } else {
330
                maxwidth = VINO_PAL_WIDTH;
331
                maxheight = VINO_PAL_HEIGHT;
332
        }
333
        if (w > maxwidth)
334
                w = maxwidth;
335
        if (h > maxheight)
336
                h = maxheight;
337
        d = max(maxwidth / w, maxheight / h);
338
        if (d > 8)
339
                d = 8;
340
        /* line size must be multiple of 8 bytes */
341
        lsize = (bytes_per_pixel(v) * w) & ~7;
342
        w = lsize * d / bytes_per_pixel(v);
343
        h *= d;
344
        if (v->left + w > maxwidth)
345
                v->left = maxwidth - w;
346
        if (v->top + h > maxheight)
347
                v->top = (maxheight - h) & ~1;  /* odd/even fields */
348
        /* FIXME: -1 bug... Verify clipping with video signal generator */
349
        v->right = v->left + w;
350
        v->bottom = v->top + h;
351
        v->decimation = d;
352
        v->line_size = lsize;
353
        DEBUG("VINO: scaling %d, %d, %d, %d / %d - %d\n", v->left, v->top,
354
              v->right, v->bottom, v->decimation, v->line_size);
355
 
356
        return 0;
357
}
358
 
359
/*
360
 * Prepare vino for DMA transfer... (execute only with vino_lock locked)
361
 */
362
static int dma_setup(struct vino_device *v)
363
{
364
        u32 ctrl, intr;
365
        struct sgi_vino_channel *ch;
366
 
367
        ch = (v->chan == VINO_CHAN_A) ? &vino->a : &vino->b;
368
        ch->page_index = 0;
369
        ch->line_count = 0;
370
        /* let VINO know where to transfer data */
371
        ch->start_desc_tbl = v->dma_desc.dma;
372
        ch->next_4_desc = v->dma_desc.dma;
373
        /* give vino time to fetch the first four descriptors, 5 usec
374
         * should be more than enough time */
375
        udelay(5);
376
        /* VINO line size register is set 8 bytes less than actual */
377
        ch->line_size = v->line_size - 8;
378
        /* set the alpha register */
379
        ch->alpha = v->alpha;
380
        /* set cliping registers */
381
        ch->clip_start = VINO_CLIP_ODD(v->top) | VINO_CLIP_EVEN(v->top+1) |
382
                         VINO_CLIP_X(v->left);
383
        ch->clip_end = VINO_CLIP_ODD(v->bottom) | VINO_CLIP_EVEN(v->bottom+1) |
384
                       VINO_CLIP_X(v->right);
385
        /* FIXME: end-of-field bug workaround
386
                       VINO_CLIP_X(VINO_PAL_WIDTH);
387
         */
388
        /* init the frame rate and norm (full frame rate only for now...) */
389
        ch->frame_rate = VINO_FRAMERT_RT(0x1fff) |
390
                         (get_capture_norm(v) == VIDEO_MODE_PAL ?
391
                          VINO_FRAMERT_PAL : 0);
392
        ctrl = vino->control;
393
        intr = vino->intr_status;
394
        if (v->chan == VINO_CHAN_A) {
395
                /* All interrupt conditions for this channel was cleared
396
                 * so clear the interrupt status register and enable
397
                 * interrupts */
398
                intr &= ~VINO_INTSTAT_A;
399
                ctrl |= VINO_CTRL_A_INT;
400
                /* enable synchronization */
401
                ctrl |= VINO_CTRL_A_SYNC_ENBL;
402
                /* enable frame assembly */
403
                ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
404
                /* set decimation used */
405
                if (v->decimation < 2)
406
                        ctrl &= ~VINO_CTRL_A_DEC_ENBL;
407
                else {
408
                        ctrl |= VINO_CTRL_A_DEC_ENBL;
409
                        ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
410
                        ctrl |= (v->decimation - 1) <<
411
                                VINO_CTRL_A_DEC_SCALE_SHIFT;
412
                }
413
                /* select input interface */
414
                if (v->input == VINO_INPUT_CAMERA)
415
                        ctrl |= VINO_CTRL_A_SELECT;
416
                else
417
                        ctrl &= ~VINO_CTRL_A_SELECT;
418
                /* palette */
419
                ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
420
                          VINO_CTRL_A_DITHER);
421
        } else {
422
                intr &= ~VINO_INTSTAT_B;
423
                ctrl |= VINO_CTRL_B_INT;
424
                ctrl |= VINO_CTRL_B_SYNC_ENBL;
425
                ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
426
                if (v->decimation < 2)
427
                        ctrl &= ~VINO_CTRL_B_DEC_ENBL;
428
                else {
429
                        ctrl |= VINO_CTRL_B_DEC_ENBL;
430
                        ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
431
                        ctrl |= (v->decimation - 1) <<
432
                                VINO_CTRL_B_DEC_SCALE_SHIFT;
433
                }
434
                if (v->input == VINO_INPUT_CAMERA)
435
                        ctrl |= VINO_CTRL_B_SELECT;
436
                else
437
                        ctrl &= ~VINO_CTRL_B_SELECT;
438
                ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
439
                          VINO_CTRL_B_DITHER);
440
        }
441
        /* set palette */
442
        switch (v->picture.palette) {
443
                case VIDEO_PALETTE_GREY:
444
                        ctrl |= (v->chan == VINO_CHAN_A) ?
445
                                VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
446
                        break;
447
                case VIDEO_PALETTE_RGB32:
448
                        ctrl |= (v->chan == VINO_CHAN_A) ?
449
                                VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
450
                        break;
451
#if 0
452
                /* FIXME: this is NOT in v4l API :-( */
453
                case VIDEO_PALETTE_RGB332:
454
                        ctrl |= (v->chan == VINO_CHAN_A) ?
455
                                VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
456
                                VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
457
                        break;
458
#endif
459
        }
460
        vino->control = ctrl;
461
        vino->intr_status = intr;
462
 
463
        return 0;
464
}
465
 
466
/* (execute only with vino_lock locked) */
467
static void dma_stop(struct vino_device *v)
468
{
469
        u32 ctrl = vino->control;
470
        ctrl &= (v->chan == VINO_CHAN_A) ?
471
                ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
472
        vino->control = ctrl;
473
}
474
 
475
/* (execute only with vino_lock locked) */
476
static void dma_go(struct vino_device *v)
477
{
478
        u32 ctrl = vino->control;
479
        ctrl |= (v->chan == VINO_CHAN_A) ?
480
                VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
481
        vino->control = ctrl;
482
}
483
 
484
/*
485
 * Load dummy page to descriptor registers. This prevents generating of
486
 * spurious interrupts. (execute only with vino_lock locked)
487
 */
488
static void clear_eod(struct vino_device *v)
489
{
490
        struct sgi_vino_channel *ch;
491
 
492
        DEBUG("VINO: chnl %c clear EOD\n", (v->chan == VINO_CHAN_A) ? 'A':'B');
493
        ch = (v->chan == VINO_CHAN_A) ? &vino->a : &vino->b;
494
        ch->page_index = 0;
495
        ch->line_count = 0;
496
        ch->start_desc_tbl = Vino->dummy_dma.dma;
497
        ch->next_4_desc = Vino->dummy_dma.dma;
498
        udelay(5);
499
}
500
 
501
static void field_done(struct vino_device *v)
502
{
503
        spin_lock(&v->state_lock);
504
        if (v->buffer_state == VINO_BUF_GRABBING)
505
                v->buffer_state = VINO_BUF_DONE;
506
        spin_unlock(&v->state_lock);
507
        wake_up(&v->dma_wait);
508
}
509
 
510
static void vino_interrupt(int irq, void *dev_id, struct pt_regs *regs)
511
{
512
        u32 intr, ctrl;
513
 
514
        spin_lock(&Vino->vino_lock);
515
        ctrl = vino->control;
516
        intr = vino->intr_status;
517
        DEBUG("VINO: intr status %04x\n", intr);
518
        if (intr & (VINO_INTSTAT_A_FIFO | VINO_INTSTAT_A_EOD)) {
519
                ctrl &= ~VINO_CTRL_A_DMA_ENBL;
520
                vino->control = ctrl;
521
                clear_eod(&Vino->chA);
522
        }
523
        if (intr & (VINO_INTSTAT_B_FIFO | VINO_INTSTAT_B_EOD)) {
524
                ctrl &= ~VINO_CTRL_B_DMA_ENBL;
525
                vino->control = ctrl;
526
                clear_eod(&Vino->chB);
527
        }
528
        vino->intr_status = ~intr;
529
        spin_unlock(&Vino->vino_lock);
530
        /* FIXME: For now we are assuming that interrupt means that frame is
531
         * done. That's not true, but we can live with such brokeness for
532
         * a while ;-) */
533
        field_done(&Vino->chA);
534
}
535
 
536
static int vino_grab(struct vino_device *v, int frame)
537
{
538
        int err = 0;
539
 
540
        spin_lock_irq(&v->state_lock);
541
        if (v->buffer_state == VINO_BUF_GRABBING)
542
                err = -EBUSY;
543
        v->buffer_state = VINO_BUF_GRABBING;
544
        spin_unlock_irq(&v->state_lock);
545
 
546
        if (err)
547
                return err;
548
 
549
        spin_lock_irq(&Vino->vino_lock);
550
        dma_setup(v);
551
        dma_go(v);
552
        spin_unlock_irq(&Vino->vino_lock);
553
 
554
        return 0;
555
}
556
 
557
static int vino_waitfor(struct vino_device *v, int frame)
558
{
559
        wait_queue_t wait;
560
        int i, err = 0;
561
 
562
        if (frame != 0)
563
                return -EINVAL;
564
 
565
        spin_lock_irq(&v->state_lock);
566
        switch (v->buffer_state) {
567
        case VINO_BUF_GRABBING:
568
                init_waitqueue_entry(&wait, current);
569
                /* add ourselves into wait queue */
570
                add_wait_queue(&v->dma_wait, &wait);
571
                /* and set current state */
572
                set_current_state(TASK_INTERRUPTIBLE);
573
                /* before releasing spinlock */
574
                spin_unlock_irq(&v->state_lock);
575
                /* to ensure that schedule_timeout will return imediately
576
                 * if VINO interrupt was triggred meanwhile */
577
                schedule_timeout(HZ / 10);
578
                if (signal_pending(current))
579
                        err = -EINTR;
580
                spin_lock_irq(&v->state_lock);
581
                remove_wait_queue(&v->dma_wait, &wait);
582
                /* don't rely on schedule_timeout return value and check what
583
                 * really happened */
584
                if (!err && v->buffer_state == VINO_BUF_GRABBING)
585
                        err = -EIO;
586
                /* fall through */
587
        case VINO_BUF_DONE:
588
                for (i = 0; i < v->page_count; i++)
589
                        pci_dma_sync_single(NULL, v->dma_desc.cpu[PAGE_RATIO*i],
590
                                            PAGE_SIZE, PCI_DMA_FROMDEVICE);
591
                v->buffer_state = VINO_BUF_UNUSED;
592
                break;
593
        default:
594
                err = -EINVAL;
595
        }
596
        spin_unlock_irq(&v->state_lock);
597
 
598
        if (err && err != -EINVAL) {
599
                DEBUG("VINO: waiting for frame failed\n");
600
                spin_lock_irq(&Vino->vino_lock);
601
                dma_stop(v);
602
                clear_eod(v);
603
                spin_unlock_irq(&Vino->vino_lock);
604
        }
605
 
606
        return err;
607
}
608
 
609
static int alloc_buffer(struct vino_device *v, int size)
610
{
611
        int count, i, j, err;
612
 
613
        err = i = 0;
614
        count = (size / PAGE_SIZE + 4) & ~3;
615
        v->desc = (unsigned long *) kmalloc(count * sizeof(unsigned long),
616
                                            GFP_KERNEL);
617
        if (!v->desc)
618
                return -ENOMEM;
619
 
620
        v->dma_desc.cpu = pci_alloc_consistent(NULL, PAGE_RATIO * (count+4) *
621
                                               sizeof(dma_addr_t),
622
                                               &v->dma_desc.dma);
623
        if (!v->dma_desc.cpu) {
624
                err = -ENOMEM;
625
                goto out_free_desc;
626
        }
627
        while (i < count) {
628
                dma_addr_t dma;
629
 
630
                v->desc[i] = get_zeroed_page(GFP_KERNEL | GFP_DMA);
631
                if (!v->desc[i])
632
                        break;
633
                dma = pci_map_single(NULL, (void *)v->desc[i], PAGE_SIZE,
634
                                     PCI_DMA_FROMDEVICE);
635
                for (j = 0; j < PAGE_RATIO; j++)
636
                        v->dma_desc.cpu[PAGE_RATIO * i + j ] =
637
                                dma + VINO_PAGE_SIZE * j;
638
                mem_map_reserve(virt_to_page(v->desc[i]));
639
                i++;
640
        }
641
        v->dma_desc.cpu[PAGE_RATIO * count] = VINO_DESC_STOP;
642
        if (i-- < count) {
643
                while (i >= 0) {
644
                        mem_map_unreserve(virt_to_page(v->desc[i]));
645
                        pci_unmap_single(NULL, v->dma_desc.cpu[PAGE_RATIO * i],
646
                                         PAGE_SIZE, PCI_DMA_FROMDEVICE);
647
                        free_page(v->desc[i]);
648
                        i--;
649
                }
650
                pci_free_consistent(NULL,
651
                                    PAGE_RATIO * (count+4) * sizeof(dma_addr_t),
652
                                    (void *)v->dma_desc.cpu, v->dma_desc.dma);
653
                err = -ENOBUFS;
654
                goto out_free_desc;
655
        }
656
        v->page_count = count;
657
        return 0;
658
 
659
out_free_desc:
660
        kfree(v->desc);
661
        return err;
662
}
663
 
664
static void free_buffer(struct vino_device *v)
665
{
666
        int i;
667
 
668
        for (i = 0; i < v->page_count; i++) {
669
                mem_map_unreserve(virt_to_page(v->desc[i]));
670
                pci_unmap_single(NULL, v->dma_desc.cpu[PAGE_RATIO * i],
671
                                 PAGE_SIZE, PCI_DMA_FROMDEVICE);
672
                free_page(v->desc[i]);
673
        }
674
        pci_free_consistent(NULL,
675
                            PAGE_RATIO * (v->page_count+4) * sizeof(dma_addr_t),
676
                            (void *)v->dma_desc.cpu, v->dma_desc.dma);
677
        kfree(v->desc);
678
}
679
 
680
static int vino_open(struct inode *inode, struct file *file)
681
{
682
        struct video_device *dev = video_devdata(file);
683
        struct vino_device *v = dev->priv;
684
        int err = 0;
685
 
686
        down(&v->sem);
687
        if (v->users) {
688
                err =  -EBUSY;
689
                goto out;
690
        }
691
        /* Check for input device (IndyCam, saa7191) availability.
692
         * Both DMA channels can run from the same source, but only
693
         * source owner is allowed to change its parameters */
694
        spin_lock(&Vino->input_lock);
695
        if (Vino->camera.driver) {
696
                v->input = VINO_INPUT_CAMERA;
697
                if (!Vino->camera.owner)
698
                        Vino->camera.owner = v->chan;
699
        }
700
        if (Vino->decoder.driver && Vino->camera.owner != v->chan) {
701
                /* There are two inputs (Composite and SVideo) but only
702
                 * one output available to VINO DMA engine */
703
                if (!Vino->decoder.owner) {
704
                        Vino->decoder.owner = v->chan;
705
                        v->input = VINO_INPUT_COMP;
706
                        i2c_decoder_command(DECODER_SET_INPUT, &v->input);
707
                } else
708
                        v->input = (v->chan == VINO_CHAN_A) ?
709
                                   Vino->chB.input : Vino->chA.input;
710
        }
711
        if (v->input == -1)
712
                err = -ENODEV;
713
        spin_unlock(&Vino->input_lock);
714
 
715
        if (err)
716
                goto out;
717
        if (alloc_buffer(v, VINO_FBUFSIZE)) {
718
                err = -ENOBUFS;
719
                goto out;
720
        }
721
        v->users++;
722
out:
723
        up(&v->sem);
724
        return err;
725
}
726
 
727
static int vino_close(struct inode *inode, struct file *file)
728
{
729
        struct video_device *dev = video_devdata(file);
730
        struct vino_device *v = dev->priv;
731
 
732
        down(&v->sem);
733
        v->users--;
734
        if (!v->users) {
735
                struct vino_device *w = (v->chan == VINO_CHAN_A) ?
736
                                        &Vino->chB : &Vino->chA;
737
                /* Eventually make other channel owner of input device */
738
                spin_lock(&Vino->input_lock);
739
                if (Vino->camera.owner == v->chan)
740
                        Vino->camera.owner = (w->input == VINO_INPUT_CAMERA) ?
741
                                             w->chan : 0;
742
                else if (Vino->decoder.owner == v->chan)
743
                        Vino->decoder.owner = (w->input == VINO_INPUT_COMP ||
744
                                               w->input == VINO_INPUT_SVIDEO) ?
745
                                              w->chan : 0;
746
                v->input = -1;
747
                spin_unlock(&Vino->input_lock);
748
 
749
                vino_waitfor(v, 0);
750
                free_buffer(v);
751
        }
752
        up(&v->sem);
753
        return 0;
754
}
755
 
756
static int vino_mmap(struct file *file, struct vm_area_struct *vma)
757
{
758
        struct video_device *dev = video_devdata(file);
759
        struct vino_device *v = dev->priv;
760
        unsigned long start = vma->vm_start;
761
        unsigned long size  = vma->vm_end - vma->vm_start;
762
        int i, err = 0;
763
 
764
        if (down_interruptible(&v->sem))
765
                return -EINTR;
766
        if (size > v->page_count * PAGE_SIZE) {
767
                err = -EINVAL;
768
                goto out;
769
        }
770
        for (i = 0; i < v->page_count; i++) {
771
                unsigned long page = virt_to_phys((void *)v->desc[i]);
772
                if (remap_page_range(start, page, PAGE_SIZE, PAGE_READONLY)) {
773
                        err = -EAGAIN;
774
                        goto out;
775
                }
776
                start += PAGE_SIZE;
777
                if (size <= PAGE_SIZE) break;
778
                size -= PAGE_SIZE;
779
        }
780
out:
781
        up(&v->sem);
782
        return err;
783
 
784
}
785
 
786
static int vino_do_ioctl(struct inode *inode, struct file *file,
787
                         unsigned int cmd, void *arg)
788
{
789
        struct video_device *dev = video_devdata(file);
790
        struct vino_device *v = dev->priv;
791
 
792
        switch (cmd) {
793
        case VIDIOCGCAP: {
794
                struct video_capability *cap = arg;
795
 
796
                strcpy(cap->name, vinostr);
797
                cap->type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE;
798
                cap->channels = VINO_INPUT_CHANNELS;
799
                cap->audios = 0;
800
                cap->maxwidth = VINO_PAL_WIDTH;
801
                cap->maxheight = VINO_PAL_HEIGHT;
802
                cap->minwidth = VINO_MIN_WIDTH;
803
                cap->minheight = VINO_MIN_HEIGHT;
804
                break;
805
        }
806
        case VIDIOCGCHAN: {
807
                struct video_channel *ch = arg;
808
 
809
                ch->flags = 0;
810
                ch->tuners = 0;
811
                switch (ch->channel) {
812
                case VINO_INPUT_COMP:
813
                        ch->norm = VIDEO_MODE_PAL | VIDEO_MODE_NTSC;
814
                        ch->type = VIDEO_TYPE_TV;
815
                        strcpy(ch->name, "Composite");
816
                        break;
817
                case VINO_INPUT_SVIDEO:
818
                        ch->norm = VIDEO_MODE_PAL | VIDEO_MODE_NTSC;
819
                        ch->type = VIDEO_TYPE_TV;
820
                        strcpy(ch->name, "S-Video");
821
                        break;
822
                case VINO_INPUT_CAMERA:
823
                        ch->norm = VIDEO_MODE_NTSC;
824
                        ch->type = VIDEO_TYPE_CAMERA;
825
                        strcpy(ch->name, "IndyCam");
826
                        break;
827
                default:
828
                        return -EINVAL;
829
                }
830
                break;
831
        }
832
        case VIDIOCSCHAN: {
833
                struct video_channel *ch = arg;
834
                int err = 0;
835
                struct vino_device *w = (v->chan == VINO_CHAN_A) ?
836
                                        &Vino->chB : &Vino->chA;
837
 
838
                spin_lock(&Vino->input_lock);
839
                switch (ch->channel) {
840
                case VINO_INPUT_COMP:
841
                case VINO_INPUT_SVIDEO:
842
                        if (!Vino->decoder.driver) {
843
                                err = -ENODEV;
844
                                break;
845
                        }
846
                        if (!Vino->decoder.owner)
847
                                Vino->decoder.owner = v->chan;
848
                        if (Vino->decoder.owner == v->chan)
849
                                i2c_decoder_command(DECODER_SET_INPUT,
850
                                                    &ch->channel);
851
                        else
852
                                if (ch->channel != w->input) {
853
                                        err = -EBUSY;
854
                                        break;
855
                                }
856
                        if (Vino->camera.owner == v->chan)
857
                                Vino->camera.owner =
858
                                        (w->input == VINO_INPUT_CAMERA) ?
859
                                        w->chan : 0;
860
                        break;
861
                case VINO_INPUT_CAMERA:
862
                        if (!Vino->camera.driver) {
863
                                err = -ENODEV;
864
                                break;
865
                        }
866
                        if (!Vino->camera.owner)
867
                                Vino->camera.owner = v->chan;
868
                        if (Vino->decoder.owner == v->chan)
869
                                Vino->decoder.owner =
870
                                        (w->input == VINO_INPUT_COMP ||
871
                                         w->input == VINO_INPUT_SVIDEO) ?
872
                                        w->chan : 0;
873
                        break;
874
                default:
875
                        err = -EINVAL;
876
                }
877
                if (!err)
878
                        v->input = ch->channel;
879
                spin_unlock(&Vino->input_lock);
880
 
881
                return err;
882
        }
883
        case VIDIOCGPICT: {
884
                struct video_picture *pic = arg;
885
 
886
                memcpy(pic, &v->picture, sizeof(struct video_picture));
887
                break;
888
        }
889
        case VIDIOCSPICT: {
890
                struct video_picture *pic = arg;
891
 
892
                switch (pic->palette) {
893
                case VIDEO_PALETTE_GREY:
894
                        pic->depth = 8;
895
                        break;
896
                case VIDEO_PALETTE_YUV422:
897
                        pic->depth = 16;
898
                        break;
899
                case VIDEO_PALETTE_RGB32:
900
                        pic->depth = 24;
901
                        break;
902
                default:
903
                        return -EINVAL;
904
                }
905
                if (v->picture.palette != pic->palette) {
906
                        v->picture.palette = pic->palette;
907
                        v->picture.depth = pic->depth;
908
                        /* TODO: we need to change line size */
909
                }
910
                DEBUG("XXX %d, %d\n", v->input, Vino->camera.owner);
911
                spin_lock(&Vino->input_lock);
912
                if (v->input == VINO_INPUT_CAMERA) {
913
                        if (Vino->camera.owner == v->chan) {
914
                                spin_unlock(&Vino->input_lock);
915
                                memcpy(&v->picture, pic,
916
                                        sizeof(struct video_picture));
917
                                i2c_camera_command(DECODER_SET_PICTURE, pic);
918
                                goto out_unlocked;
919
                        }
920
                } else {
921
                        if (Vino->decoder.owner == v->chan) {
922
                                spin_unlock(&Vino->input_lock);
923
                                memcpy(&v->picture, pic,
924
                                        sizeof(struct video_picture));
925
                                i2c_decoder_command(DECODER_SET_PICTURE, pic);
926
                                goto out_unlocked;
927
                        }
928
                }
929
                spin_unlock(&Vino->input_lock);
930
out_unlocked:
931
                break;
932
        }
933
        /* get cropping */
934
        case VIDIOCGCAPTURE: {
935
                struct video_capture *capture = arg;
936
 
937
                capture->x = v->left;
938
                capture->y = v->top;
939
                capture->width = v->right - v->left;
940
                capture->height = v->bottom - v->top;
941
                capture->decimation = v->decimation;
942
                capture->flags = 0;
943
                break;
944
        }
945
        /* set cropping */
946
        case VIDIOCSCAPTURE: {
947
                struct video_capture *capture = arg;
948
 
949
                return set_clipping(v, capture->x, capture->y, capture->width,
950
                                    capture->height, capture->decimation);
951
        }
952
        /* get scaling */
953
        case VIDIOCGWIN: {
954
                struct video_window *win = arg;
955
 
956
                memset(win, 0, sizeof(*win));
957
                win->width = (v->right - v->left) / v->decimation;
958
                win->height = (v->bottom - v->top) / v->decimation;
959
                break;
960
        }
961
        /* set scaling */
962
        case VIDIOCSWIN: {
963
                struct video_window *win = arg;
964
 
965
                if (win->x || win->y || win->clipcount || win->clips)
966
                        return -EINVAL;
967
                return set_scaling(v, win->width, win->height);
968
        }
969
        case VIDIOCGMBUF: {
970
                struct video_mbuf *buf = arg;
971
 
972
                buf->frames = 1;
973
                buf->offsets[0] = 0;
974
                buf->size = v->page_count * PAGE_SIZE;
975
                break;
976
        }
977
        case VIDIOCMCAPTURE: {
978
                struct video_mmap *mmap = arg;
979
 
980
                if (mmap->width != v->right - v->left ||
981
                    mmap->height != v->bottom - v->top ||
982
                    mmap->format != v->picture.palette ||
983
                    mmap->frame != 0)
984
                        return -EINVAL;
985
 
986
                return vino_grab(v, mmap->frame);
987
        }
988
        case VIDIOCSYNC:
989
                return vino_waitfor(v, *((int*)arg));
990
        default:
991
                return -ENOIOCTLCMD;
992
        }
993
        return 0;
994
}
995
 
996
static int vino_ioctl(struct inode *inode, struct file *file,
997
                      unsigned int cmd, unsigned long arg)
998
{
999
        struct video_device *dev = video_devdata(file);
1000
        struct vino_device *v = dev->priv;
1001
        int err;
1002
 
1003
        if (down_interruptible(&v->sem))
1004
                return -EINTR;
1005
        err = video_usercopy(inode, file, cmd, arg, vino_do_ioctl);
1006
        up(&v->sem);
1007
        return err;
1008
}
1009
 
1010
static struct file_operations vino_fops = {
1011
        .owner          = THIS_MODULE,
1012
        .open           = vino_open,
1013
        .release        = vino_close,
1014
        .ioctl          = vino_ioctl,
1015
        .mmap           = vino_mmap,
1016
        .llseek         = no_llseek,
1017
};
1018
 
1019
static const struct video_device vino_template = {
1020
        .owner          = THIS_MODULE,
1021
        .type           = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE,
1022
        .hardware       = VID_HARDWARE_VINO,
1023
        .name           = "VINO",
1024
        .fops           = &vino_fops,
1025
        .minor          = -1,
1026
};
1027
 
1028
static void init_channel_data(struct vino_device *v, int channel)
1029
{
1030
        init_waitqueue_head(&v->dma_wait);
1031
        init_MUTEX(&v->sem);
1032
        spin_lock_init(&v->state_lock);
1033
        memcpy(&v->vdev, &vino_template, sizeof(vino_template));
1034
        v->vdev.priv = v;
1035
        v->chan = channel;
1036
        v->input = -1;
1037
        v->picture.palette = VIDEO_PALETTE_GREY;
1038
        v->picture.depth = 8;
1039
        v->buffer_state = VINO_BUF_UNUSED;
1040
        v->users = 0;
1041
        set_clipping(v, 0, 0, VINO_NTSC_WIDTH, VINO_NTSC_HEIGHT, 1);
1042
}
1043
 
1044
static int __init vino_init(void)
1045
{
1046
        unsigned long rev;
1047
        dma_addr_t dma;
1048
        int i, ret = 0;
1049
 
1050
        /* VINO is Indy specific beast */
1051
        if (ip22_is_fullhouse())
1052
                return -ENODEV;
1053
 
1054
        /*
1055
         * VINO is in the EISA address space, so the sysid register will tell
1056
         * us if the EISA_PRESENT pin on MC has been pulled low.
1057
         *
1058
         * If EISA_PRESENT is not set we definitely don't have a VINO equiped
1059
         * system.
1060
         */
1061
        if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
1062
                printk(KERN_ERR "VINO not found\n");
1063
                return -ENODEV;
1064
        }
1065
 
1066
        vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
1067
        if (!vino)
1068
                return -EIO;
1069
 
1070
        /* Okay, once we know that VINO is present we'll read its revision
1071
         * safe way. One never knows... */
1072
        if (get_dbe(rev, &(vino->rev_id))) {
1073
                printk(KERN_ERR "VINO: failed to read revision register\n");
1074
                ret = -ENODEV;
1075
                goto out_unmap;
1076
        }
1077
        if (VINO_ID_VALUE(rev) != VINO_CHIP_ID) {
1078
                printk(KERN_ERR "VINO is not VINO (Rev/ID: 0x%04lx)\n", rev);
1079
                ret = -ENODEV;
1080
                goto out_unmap;
1081
        }
1082
        printk(KERN_INFO "VINO Rev: 0x%02lx\n", VINO_REV_NUM(rev));
1083
 
1084
        Vino = (struct vino_video *)
1085
                kmalloc(sizeof(struct vino_video), GFP_KERNEL);
1086
        if (!Vino) {
1087
                ret = -ENOMEM;
1088
                goto out_unmap;
1089
        }
1090
        memset(Vino, 0, sizeof(struct vino_video));
1091
 
1092
        Vino->dummy_desc = get_zeroed_page(GFP_KERNEL | GFP_DMA);
1093
        if (!Vino->dummy_desc) {
1094
                ret = -ENOMEM;
1095
                goto out_free_vino;
1096
        }
1097
        Vino->dummy_dma.cpu = pci_alloc_consistent(NULL, 4 * sizeof(dma_addr_t),
1098
                                                   &Vino->dummy_dma.dma);
1099
        if (!Vino->dummy_dma.cpu) {
1100
                ret = -ENOMEM;
1101
                goto out_free_dummy_desc;
1102
        }
1103
        dma = pci_map_single(NULL, (void *)Vino->dummy_desc, PAGE_SIZE,
1104
                             PCI_DMA_FROMDEVICE);
1105
        for (i = 0; i < 4; i++)
1106
                Vino->dummy_dma.cpu[i] = dma;
1107
 
1108
        vino->control = 0;
1109
        /* prevent VINO from throwing spurious interrupts */
1110
        vino->a.next_4_desc = Vino->dummy_dma.dma;
1111
        vino->b.next_4_desc = Vino->dummy_dma.dma;
1112
        udelay(5);
1113
        vino->intr_status = 0;
1114
        /* set threshold level */
1115
        vino->a.fifo_thres = threshold_a;
1116
        vino->b.fifo_thres = threshold_b;
1117
 
1118
        spin_lock_init(&Vino->vino_lock);
1119
        spin_lock_init(&Vino->input_lock);
1120
        init_channel_data(&Vino->chA, VINO_CHAN_A);
1121
        init_channel_data(&Vino->chB, VINO_CHAN_B);
1122
 
1123
        if (request_irq(SGI_VINO_IRQ, vino_interrupt, 0, vinostr, NULL)) {
1124
                printk(KERN_ERR "VINO: request irq%02d failed\n",
1125
                       SGI_VINO_IRQ);
1126
                ret = -EAGAIN;
1127
                goto out_unmap_dummy_desc;
1128
        }
1129
 
1130
        ret = vino_i2c_add_bus();
1131
        if (ret) {
1132
                printk(KERN_ERR "VINO: I2C bus registration failed\n");
1133
                goto out_free_irq;
1134
        }
1135
 
1136
        if (video_register_device(&Vino->chA.vdev, VFL_TYPE_GRABBER, -1) < 0) {
1137
                printk("%s, chnl %d: device registration failed.\n",
1138
                        Vino->chA.vdev.name, Vino->chA.chan);
1139
                ret = -EINVAL;
1140
                goto out_i2c_del_bus;
1141
        }
1142
        if (video_register_device(&Vino->chB.vdev, VFL_TYPE_GRABBER, -1) < 0) {
1143
                printk("%s, chnl %d: device registration failed.\n",
1144
                        Vino->chB.vdev.name, Vino->chB.chan);
1145
                ret = -EINVAL;
1146
                goto out_unregister_vdev;
1147
        }
1148
 
1149
#if defined(CONFIG_KMOD) && defined (MODULE)
1150
        request_module("saa7191");
1151
        request_module("indycam");
1152
#endif
1153
        return 0;
1154
 
1155
out_unregister_vdev:
1156
        video_unregister_device(&Vino->chA.vdev);
1157
out_i2c_del_bus:
1158
        vino_i2c_del_bus();
1159
out_free_irq:
1160
        free_irq(SGI_VINO_IRQ, NULL);
1161
out_unmap_dummy_desc:
1162
        pci_unmap_single(NULL, Vino->dummy_dma.dma, PAGE_SIZE,
1163
                         PCI_DMA_FROMDEVICE);
1164
        pci_free_consistent(NULL, 4 * sizeof(dma_addr_t),
1165
                            (void *)Vino->dummy_dma.cpu, Vino->dummy_dma.dma);
1166
out_free_dummy_desc:
1167
        free_page(Vino->dummy_desc);
1168
out_free_vino:
1169
        kfree(Vino);
1170
out_unmap:
1171
        iounmap(vino);
1172
 
1173
        return ret;
1174
}
1175
 
1176
static void __exit vino_exit(void)
1177
{
1178
        video_unregister_device(&Vino->chA.vdev);
1179
        video_unregister_device(&Vino->chB.vdev);
1180
        vino_i2c_del_bus();
1181
        free_irq(SGI_VINO_IRQ, NULL);
1182
        pci_unmap_single(NULL, Vino->dummy_dma.dma, PAGE_SIZE,
1183
                         PCI_DMA_FROMDEVICE);
1184
        pci_free_consistent(NULL, 4 * sizeof(dma_addr_t),
1185
                            (void *)Vino->dummy_dma.cpu, Vino->dummy_dma.dma);
1186
        free_page(Vino->dummy_desc);
1187
        kfree(Vino);
1188
        iounmap(vino);
1189
}
1190
 
1191
module_init(vino_init);
1192
module_exit(vino_exit);
1193
 
1194
MODULE_AUTHOR("Ladislav Michl <ladis@linux-mips.org>");
1195
MODULE_DESCRIPTION("Video4Linux driver for SGI Indy VINO (IndyCam)");
1196
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.