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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * Motion Eye video4linux driver for Sony Vaio PictureBook
3
 *
4
 * Copyright (C) 2001-2003 Stelian Pop <stelian@popies.net>
5
 *
6
 * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
7
 *
8
 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
9
 *
10
 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
11
 *
12
 * Some parts borrowed from various video4linux drivers, especially
13
 * bttv-driver.c and zoran.c, see original files for credits.
14
 *
15
 * This program is free software; you can redistribute it and/or modify
16
 * it under the terms of the GNU General Public License as published by
17
 * the Free Software Foundation; either version 2 of the License, or
18
 * (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28
 */
29
#include <linux/config.h>
30
#include <linux/module.h>
31
#include <linux/pci.h>
32
#include <linux/sched.h>
33
#include <linux/init.h>
34
#include <linux/videodev.h>
35
#include <asm/uaccess.h>
36
#include <asm/io.h>
37
#include <linux/delay.h>
38
#include <linux/interrupt.h>
39
#include <linux/vmalloc.h>
40
 
41
#include "meye.h"
42
#include "linux/meye.h"
43
 
44
/* driver structure - only one possible */
45
static struct meye meye;
46
/* number of grab buffers */
47
static unsigned int gbuffers = 2;
48
/* size of a grab buffer */
49
static unsigned int gbufsize = MEYE_MAX_BUFSIZE;
50
/* /dev/videoX registration number */
51
static int video_nr = -1;
52
 
53
/****************************************************************************/
54
/* Queue routines                                                           */
55
/****************************************************************************/
56
 
57
/* Inits the queue */
58
static inline void meye_initq(struct meye_queue *queue) {
59
        queue->head = queue->tail = 0;
60
        queue->len = 0;
61
        queue->s_lock = (spinlock_t)SPIN_LOCK_UNLOCKED;
62
        init_waitqueue_head(&queue->proc_list);
63
}
64
 
65
/* Pulls an element from the queue */
66
static inline int meye_pullq(struct meye_queue *queue) {
67
        int result;
68
        unsigned long flags;
69
 
70
        spin_lock_irqsave(&queue->s_lock, flags);
71
        if (!queue->len) {
72
                spin_unlock_irqrestore(&queue->s_lock, flags);
73
                return -1;
74
        }
75
        result = queue->buf[queue->head];
76
        queue->head++;
77
        queue->head &= (MEYE_QUEUE_SIZE - 1);
78
        queue->len--;
79
        spin_unlock_irqrestore(&queue->s_lock, flags);
80
        return result;
81
}
82
 
83
/* Pushes an element into the queue */
84
static inline void meye_pushq(struct meye_queue *queue, int element) {
85
        unsigned long flags;
86
 
87
        spin_lock_irqsave(&queue->s_lock, flags);
88
        if (queue->len == MEYE_QUEUE_SIZE) {
89
                /* remove the first element */
90
                queue->head++;
91
                queue->head &= (MEYE_QUEUE_SIZE - 1);
92
                queue->len--;
93
        }
94
        queue->buf[queue->tail] = element;
95
        queue->tail++;
96
        queue->tail &= (MEYE_QUEUE_SIZE - 1);
97
        queue->len++;
98
 
99
        spin_unlock_irqrestore(&queue->s_lock, flags);
100
}
101
 
102
/* Tests if the queue is empty */
103
static inline int meye_emptyq(struct meye_queue *queue, int *elem) {
104
        int result;
105
        unsigned long flags;
106
 
107
        spin_lock_irqsave(&queue->s_lock, flags);
108
        result = (queue->len == 0);
109
        if (!result && elem)
110
                *elem = queue->buf[queue->head];
111
        spin_unlock_irqrestore(&queue->s_lock, flags);
112
        return result;
113
}
114
 
115
/****************************************************************************/
116
/* Memory allocation routines (stolen from bttv-driver.c)                   */
117
/****************************************************************************/
118
 
119
/* Here we want the physical address of the memory.
120
 * This is used when initializing the contents of the area.
121
 */
122
static inline unsigned long kvirt_to_pa(unsigned long adr) {
123
        unsigned long kva, ret;
124
 
125
        kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
126
        kva |= adr & (PAGE_SIZE-1); /* restore the offset */
127
        ret = __pa(kva);
128
        return ret;
129
}
130
 
131
static void *rvmalloc(unsigned long size) {
132
        void *mem;
133
        unsigned long adr;
134
 
135
        size = PAGE_ALIGN(size);
136
        mem = vmalloc_32(size);
137
        if (mem) {
138
                memset(mem, 0, size); /* Clear the ram out, no junk to the user */
139
                adr = (unsigned long)mem;
140
                while (size > 0) {
141
                        SetPageReserved(vmalloc_to_page((void *)adr));
142
                        adr += PAGE_SIZE;
143
                        size -= PAGE_SIZE;
144
                }
145
        }
146
        return mem;
147
}
148
 
149
static void rvfree(void * mem, unsigned long size) {
150
        unsigned long adr;
151
 
152
        if (mem) {
153
                adr = (unsigned long) mem;
154
                while ((long) size > 0) {
155
                        ClearPageReserved(vmalloc_to_page((void *)adr));
156
                        adr += PAGE_SIZE;
157
                        size -= PAGE_SIZE;
158
                }
159
                vfree(mem);
160
        }
161
}
162
 
163
/****************************************************************************/
164
/* dma_alloc_coherent / dma_free_coherent ported from 2.5                  */
165
/****************************************************************************/
166
 
167
void *dma_alloc_coherent(struct pci_dev *dev, size_t size,
168
                           dma_addr_t *dma_handle, int gfp)
169
{
170
        void *ret;
171
        /* ignore region specifiers */
172
        gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
173
 
174
        if (dev == NULL || ((u32)dev->dma_mask < 0xffffffff))
175
                gfp |= GFP_DMA;
176
        ret = (void *)__get_free_pages(gfp, get_order(size));
177
 
178
        if (ret != NULL) {
179
                memset(ret, 0, size);
180
                *dma_handle = virt_to_phys(ret);
181
        }
182
        return ret;
183
}
184
 
185
void dma_free_coherent(struct pci_dev *dev, size_t size,
186
                         void *vaddr, dma_addr_t dma_handle)
187
{
188
        free_pages((unsigned long)vaddr, get_order(size));
189
}
190
 
191
/*
192
 * return a page table pointing to N pages of locked memory
193
 *
194
 * NOTE: The meye device expects dma_addr_t size to be 32 bits
195
 * (the toc must be exactly 1024 entries each of them being 4 bytes
196
 * in size, the whole result being 4096 bytes). We're using here
197
 * dma_addr_t for correctness but the compilation of this driver is
198
 * disabled for HIGHMEM64G=y, where sizeof(dma_addr_t) != 4
199
 */
200
static int ptable_alloc(void) {
201
        dma_addr_t *pt;
202
        int i;
203
 
204
        memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
205
 
206
        meye.mchip_ptable_toc = dma_alloc_coherent(meye.mchip_dev,
207
                                                   PAGE_SIZE,
208
                                                   &meye.mchip_dmahandle,
209
                                                   GFP_KERNEL);
210
        if (!meye.mchip_ptable_toc) {
211
                meye.mchip_dmahandle = 0;
212
                return -1;
213
        }
214
 
215
        pt = meye.mchip_ptable_toc;
216
        for (i = 0; i < MCHIP_NB_PAGES; i++) {
217
                meye.mchip_ptable[i] = dma_alloc_coherent(meye.mchip_dev,
218
                                                          PAGE_SIZE,
219
                                                          pt,
220
                                                          GFP_KERNEL);
221
                if (!meye.mchip_ptable[i]) {
222
                        int j;
223
                        pt = meye.mchip_ptable_toc;
224
                        for (j = 0; j < i; ++j) {
225
                                dma_free_coherent(meye.mchip_dev,
226
                                                  PAGE_SIZE,
227
                                                  meye.mchip_ptable[j], *pt);
228
                                pt++;
229
                        }
230
                        dma_free_coherent(meye.mchip_dev,
231
                                          PAGE_SIZE,
232
                                          meye.mchip_ptable_toc,
233
                                          meye.mchip_dmahandle);
234
                        meye.mchip_ptable_toc = 0;
235
                        meye.mchip_dmahandle = 0;
236
                        return -1;
237
                }
238
                pt++;
239
        }
240
        return 0;
241
}
242
 
243
static void ptable_free(void) {
244
        dma_addr_t *pt;
245
        int i;
246
 
247
        pt = meye.mchip_ptable_toc;
248
        for (i = 0; i < MCHIP_NB_PAGES; i++) {
249
                if (meye.mchip_ptable[i])
250
                        dma_free_coherent(meye.mchip_dev,
251
                                          PAGE_SIZE,
252
                                          meye.mchip_ptable[i], *pt);
253
                pt++;
254
        }
255
 
256
        if (meye.mchip_ptable_toc)
257
                dma_free_coherent(meye.mchip_dev,
258
                                  PAGE_SIZE,
259
                                  meye.mchip_ptable_toc,
260
                                  meye.mchip_dmahandle);
261
 
262
        memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
263
        meye.mchip_ptable_toc = 0;
264
        meye.mchip_dmahandle = 0;
265
}
266
 
267
/* copy data from ptable into buf */
268
static void ptable_copy(u8 *buf, int start, int size, int pt_pages) {
269
        int i;
270
 
271
        for (i = 0; i < (size / PAGE_SIZE) * PAGE_SIZE; i += PAGE_SIZE) {
272
                memcpy(buf + i, meye.mchip_ptable[start++], PAGE_SIZE);
273
                if (start >= pt_pages)
274
                        start = 0;
275
        }
276
        memcpy(buf + i, meye.mchip_ptable[start], size % PAGE_SIZE);
277
}
278
 
279
 
280
/****************************************************************************/
281
/* JPEG tables at different qualities to load into the VRJ chip             */
282
/****************************************************************************/
283
 
284
/* return a set of quantisation tables based on a quality from 1 to 10 */
285
static u16 *jpeg_quantisation_tables(int *size, int quality) {
286
        static u16 tables0[] = {
287
                0xdbff, 0x4300, 0xff00, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
288
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
289
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
290
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
291
                0xffff, 0xffff, 0xffff,
292
                0xdbff, 0x4300, 0xff01, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
293
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
294
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
295
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
296
                0xffff, 0xffff, 0xffff,
297
        };
298
        static u16 tables1[] = {
299
                0xdbff, 0x4300, 0x5000, 0x3c37, 0x3c46, 0x5032, 0x4146, 0x5a46,
300
                0x5055, 0x785f, 0x82c8, 0x6e78, 0x786e, 0xaff5, 0x91b9, 0xffc8,
301
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
302
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
303
                0xffff, 0xffff, 0xffff,
304
                0xdbff, 0x4300, 0x5501, 0x5a5a, 0x6978, 0xeb78, 0x8282, 0xffeb,
305
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
306
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
307
                0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
308
                0xffff, 0xffff, 0xffff,
309
        };
310
        static u16 tables2[] = {
311
                0xdbff, 0x4300, 0x2800, 0x1e1c, 0x1e23, 0x2819, 0x2123, 0x2d23,
312
                0x282b, 0x3c30, 0x4164, 0x373c, 0x3c37, 0x587b, 0x495d, 0x9164,
313
                0x9980, 0x8f96, 0x8c80, 0xa08a, 0xe6b4, 0xa0c3, 0xdaaa, 0x8aad,
314
                0xc88c, 0xcbff, 0xeeda, 0xfff5, 0xffff, 0xc19b, 0xffff, 0xfaff,
315
                0xe6ff, 0xfffd, 0xfff8,
316
                0xdbff, 0x4300, 0x2b01, 0x2d2d, 0x353c, 0x763c, 0x4141, 0xf876,
317
                0x8ca5, 0xf8a5, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
318
                0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
319
                0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
320
                0xf8f8, 0xf8f8, 0xfff8,
321
        };
322
        static u16 tables3[] = {
323
                0xdbff, 0x4300, 0x1b00, 0x1412, 0x1417, 0x1b11, 0x1617, 0x1e17,
324
                0x1b1c, 0x2820, 0x2b42, 0x2528, 0x2825, 0x3a51, 0x303d, 0x6042,
325
                0x6555, 0x5f64, 0x5d55, 0x6a5b, 0x9978, 0x6a81, 0x9071, 0x5b73,
326
                0x855d, 0x86b5, 0x9e90, 0xaba3, 0xabad, 0x8067, 0xc9bc, 0xa6ba,
327
                0x99c7, 0xaba8, 0xffa4,
328
                0xdbff, 0x4300, 0x1c01, 0x1e1e, 0x2328, 0x4e28, 0x2b2b, 0xa44e,
329
                0x5d6e, 0xa46e, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
330
                0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
331
                0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
332
                0xa4a4, 0xa4a4, 0xffa4,
333
        };
334
        static u16 tables4[] = {
335
                0xdbff, 0x4300, 0x1400, 0x0f0e, 0x0f12, 0x140d, 0x1012, 0x1712,
336
                0x1415, 0x1e18, 0x2132, 0x1c1e, 0x1e1c, 0x2c3d, 0x242e, 0x4932,
337
                0x4c40, 0x474b, 0x4640, 0x5045, 0x735a, 0x5062, 0x6d55, 0x4556,
338
                0x6446, 0x6588, 0x776d, 0x817b, 0x8182, 0x604e, 0x978d, 0x7d8c,
339
                0x7396, 0x817e, 0xff7c,
340
                0xdbff, 0x4300, 0x1501, 0x1717, 0x1a1e, 0x3b1e, 0x2121, 0x7c3b,
341
                0x4653, 0x7c53, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
342
                0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
343
                0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
344
                0x7c7c, 0x7c7c, 0xff7c,
345
        };
346
        static u16 tables5[] = {
347
                0xdbff, 0x4300, 0x1000, 0x0c0b, 0x0c0e, 0x100a, 0x0d0e, 0x120e,
348
                0x1011, 0x1813, 0x1a28, 0x1618, 0x1816, 0x2331, 0x1d25, 0x3a28,
349
                0x3d33, 0x393c, 0x3833, 0x4037, 0x5c48, 0x404e, 0x5744, 0x3745,
350
                0x5038, 0x516d, 0x5f57, 0x6762, 0x6768, 0x4d3e, 0x7971, 0x6470,
351
                0x5c78, 0x6765, 0xff63,
352
                0xdbff, 0x4300, 0x1101, 0x1212, 0x1518, 0x2f18, 0x1a1a, 0x632f,
353
                0x3842, 0x6342, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
354
                0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
355
                0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
356
                0x6363, 0x6363, 0xff63,
357
        };
358
        static u16 tables6[] = {
359
                0xdbff, 0x4300, 0x0d00, 0x0a09, 0x0a0b, 0x0d08, 0x0a0b, 0x0e0b,
360
                0x0d0e, 0x130f, 0x1520, 0x1213, 0x1312, 0x1c27, 0x171e, 0x2e20,
361
                0x3129, 0x2e30, 0x2d29, 0x332c, 0x4a3a, 0x333e, 0x4636, 0x2c37,
362
                0x402d, 0x4157, 0x4c46, 0x524e, 0x5253, 0x3e32, 0x615a, 0x505a,
363
                0x4a60, 0x5251, 0xff4f,
364
                0xdbff, 0x4300, 0x0e01, 0x0e0e, 0x1113, 0x2613, 0x1515, 0x4f26,
365
                0x2d35, 0x4f35, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
366
                0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
367
                0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
368
                0x4f4f, 0x4f4f, 0xff4f,
369
        };
370
        static u16 tables7[] = {
371
                0xdbff, 0x4300, 0x0a00, 0x0707, 0x0708, 0x0a06, 0x0808, 0x0b08,
372
                0x0a0a, 0x0e0b, 0x1018, 0x0d0e, 0x0e0d, 0x151d, 0x1116, 0x2318,
373
                0x251f, 0x2224, 0x221f, 0x2621, 0x372b, 0x262f, 0x3429, 0x2129,
374
                0x3022, 0x3141, 0x3934, 0x3e3b, 0x3e3e, 0x2e25, 0x4944, 0x3c43,
375
                0x3748, 0x3e3d, 0xff3b,
376
                0xdbff, 0x4300, 0x0a01, 0x0b0b, 0x0d0e, 0x1c0e, 0x1010, 0x3b1c,
377
                0x2228, 0x3b28, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
378
                0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
379
                0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
380
                0x3b3b, 0x3b3b, 0xff3b,
381
        };
382
        static u16 tables8[] = {
383
                0xdbff, 0x4300, 0x0600, 0x0504, 0x0506, 0x0604, 0x0506, 0x0706,
384
                0x0607, 0x0a08, 0x0a10, 0x090a, 0x0a09, 0x0e14, 0x0c0f, 0x1710,
385
                0x1814, 0x1718, 0x1614, 0x1a16, 0x251d, 0x1a1f, 0x231b, 0x161c,
386
                0x2016, 0x202c, 0x2623, 0x2927, 0x292a, 0x1f19, 0x302d, 0x282d,
387
                0x2530, 0x2928, 0xff28,
388
                0xdbff, 0x4300, 0x0701, 0x0707, 0x080a, 0x130a, 0x0a0a, 0x2813,
389
                0x161a, 0x281a, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
390
                0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
391
                0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
392
                0x2828, 0x2828, 0xff28,
393
        };
394
        static u16 tables9[] = {
395
                0xdbff, 0x4300, 0x0300, 0x0202, 0x0203, 0x0302, 0x0303, 0x0403,
396
                0x0303, 0x0504, 0x0508, 0x0405, 0x0504, 0x070a, 0x0607, 0x0c08,
397
                0x0c0a, 0x0b0c, 0x0b0a, 0x0d0b, 0x120e, 0x0d10, 0x110e, 0x0b0e,
398
                0x100b, 0x1016, 0x1311, 0x1514, 0x1515, 0x0f0c, 0x1817, 0x1416,
399
                0x1218, 0x1514, 0xff14,
400
                0xdbff, 0x4300, 0x0301, 0x0404, 0x0405, 0x0905, 0x0505, 0x1409,
401
                0x0b0d, 0x140d, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
402
                0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
403
                0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
404
                0x1414, 0x1414, 0xff14,
405
        };
406
        static u16 tables10[] = {
407
                0xdbff, 0x4300, 0x0100, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
408
                0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
409
                0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
410
                0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
411
                0x0101, 0x0101, 0xff01,
412
                0xdbff, 0x4300, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
413
                0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
414
                0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
415
                0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
416
                0x0101, 0x0101, 0xff01,
417
        };
418
 
419
        switch (quality) {
420
        case 0:
421
                *size = sizeof(tables0);
422
                return tables0;
423
        case 1:
424
                *size = sizeof(tables1);
425
                return tables1;
426
        case 2:
427
                *size = sizeof(tables2);
428
                return tables2;
429
        case 3:
430
                *size = sizeof(tables3);
431
                return tables3;
432
        case 4:
433
                *size = sizeof(tables4);
434
                return tables4;
435
        case 5:
436
                *size = sizeof(tables5);
437
                return tables5;
438
        case 6:
439
                *size = sizeof(tables6);
440
                return tables6;
441
        case 7:
442
                *size = sizeof(tables7);
443
                return tables7;
444
        case 8:
445
                *size = sizeof(tables8);
446
                return tables8;
447
        case 9:
448
                *size = sizeof(tables9);
449
                return tables9;
450
        case 10:
451
                *size = sizeof(tables10);
452
                return tables10;
453
        default:
454
                printk(KERN_WARNING "meye: invalid quality level %d - using 8\n", quality);
455
                *size = sizeof(tables8);
456
                return tables8;
457
        }
458
        return NULL;
459
}
460
 
461
/* return a generic set of huffman tables */
462
static u16 *jpeg_huffman_tables(int *size) {
463
        static u16 tables[] = {
464
                0xC4FF, 0xB500, 0x0010, 0x0102, 0x0303, 0x0402, 0x0503, 0x0405,
465
                0x0004, 0x0100, 0x017D, 0x0302, 0x0400, 0x0511, 0x2112, 0x4131,
466
                0x1306, 0x6151, 0x2207, 0x1471, 0x8132, 0xA191, 0x2308, 0xB142,
467
                0x15C1, 0xD152, 0x24F0, 0x6233, 0x8272, 0x0A09, 0x1716, 0x1918,
468
                0x251A, 0x2726, 0x2928, 0x342A, 0x3635, 0x3837, 0x3A39, 0x4443,
469
                0x4645, 0x4847, 0x4A49, 0x5453, 0x5655, 0x5857, 0x5A59, 0x6463,
470
                0x6665, 0x6867, 0x6A69, 0x7473, 0x7675, 0x7877, 0x7A79, 0x8483,
471
                0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998, 0xA29A,
472
                0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6, 0xB9B8,
473
                0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4, 0xD7D6,
474
                0xD9D8, 0xE1DA, 0xE3E2, 0xE5E4, 0xE7E6, 0xE9E8, 0xF1EA, 0xF3F2,
475
                0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
476
                0xC4FF, 0xB500, 0x0011, 0x0102, 0x0402, 0x0304, 0x0704, 0x0405,
477
                0x0004, 0x0201, 0x0077, 0x0201, 0x1103, 0x0504, 0x3121, 0x1206,
478
                0x5141, 0x6107, 0x1371, 0x3222, 0x0881, 0x4214, 0xA191, 0xC1B1,
479
                0x2309, 0x5233, 0x15F0, 0x7262, 0x0AD1, 0x2416, 0xE134, 0xF125,
480
                0x1817, 0x1A19, 0x2726, 0x2928, 0x352A, 0x3736, 0x3938, 0x433A,
481
                0x4544, 0x4746, 0x4948, 0x534A, 0x5554, 0x5756, 0x5958, 0x635A,
482
                0x6564, 0x6766, 0x6968, 0x736A, 0x7574, 0x7776, 0x7978, 0x827A,
483
                0x8483, 0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998,
484
                0xA29A, 0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6,
485
                0xB9B8, 0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4,
486
                0xD7D6, 0xD9D8, 0xE2DA, 0xE4E3, 0xE6E5, 0xE8E7, 0xEAE9, 0xF3F2,
487
                0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
488
                0xC4FF, 0x1F00, 0x0000, 0x0501, 0x0101, 0x0101, 0x0101, 0x0000,
489
                0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
490
                0xFF0B,
491
                0xC4FF, 0x1F00, 0x0001, 0x0103, 0x0101, 0x0101, 0x0101, 0x0101,
492
                0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
493
                0xFF0B
494
        };
495
 
496
        *size = sizeof(tables);
497
        return tables;
498
}
499
 
500
/****************************************************************************/
501
/* MCHIP low-level functions                                                */
502
/****************************************************************************/
503
 
504
/* waits for the specified miliseconds */
505
static inline void wait_ms(unsigned int ms) {
506
        if (!in_interrupt()) {
507
                set_current_state(TASK_UNINTERRUPTIBLE);
508
                schedule_timeout(1 + ms * HZ / 1000);
509
        }
510
        else
511
                mdelay(ms);
512
}
513
 
514
/* returns the horizontal capture size */
515
static inline int mchip_hsize(void) {
516
        return meye.params.subsample ? 320 : 640;
517
}
518
 
519
/* returns the vertical capture size */
520
static inline int mchip_vsize(void) {
521
        return meye.params.subsample ? 240 : 480;
522
}
523
 
524
/* waits for a register to be available */
525
static void mchip_sync(int reg) {
526
        u32 status;
527
        int i;
528
 
529
        if (reg == MCHIP_MM_FIFO_DATA) {
530
                for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
531
                        status = readl(meye.mchip_mmregs + MCHIP_MM_FIFO_STATUS);
532
                        if (!(status & MCHIP_MM_FIFO_WAIT)) {
533
                                printk(KERN_WARNING "meye: fifo not ready\n");
534
                                return;
535
                        }
536
                        if (status & MCHIP_MM_FIFO_READY)
537
                                return;
538
                        udelay(1);
539
                }
540
        }
541
        else if (reg > 0x80) {
542
                u32 mask = (reg < 0x100) ? MCHIP_HIC_STATUS_MCC_RDY
543
                                         : MCHIP_HIC_STATUS_VRJ_RDY;
544
                for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
545
                        status = readl(meye.mchip_mmregs + MCHIP_HIC_STATUS);
546
                        if (status & mask)
547
                                return;
548
                        udelay(1);
549
                }
550
        }
551
        else
552
                return;
553
        printk(KERN_WARNING "meye: mchip_sync() timeout on reg 0x%x status=0x%x\n", reg, status);
554
}
555
 
556
/* sets a value into the register */
557
static inline void mchip_set(int reg, u32 v) {
558
        mchip_sync(reg);
559
        writel(v, meye.mchip_mmregs + reg);
560
}
561
 
562
/* get the register value */
563
static inline u32 mchip_read(int reg) {
564
        mchip_sync(reg);
565
        return readl(meye.mchip_mmregs + reg);
566
}
567
 
568
/* wait for a register to become a particular value */
569
static inline int mchip_delay(u32 reg, u32 v) {
570
        int n = 10;
571
        while (--n && mchip_read(reg) != v)
572
                udelay(1);
573
        return n;
574
}
575
 
576
/* setup subsampling */
577
static void mchip_subsample(void) {
578
        mchip_set(MCHIP_MCC_R_SAMPLING, meye.params.subsample);
579
        mchip_set(MCHIP_MCC_R_XRANGE, mchip_hsize());
580
        mchip_set(MCHIP_MCC_R_YRANGE, mchip_vsize());
581
        mchip_set(MCHIP_MCC_B_XRANGE, mchip_hsize());
582
        mchip_set(MCHIP_MCC_B_YRANGE, mchip_vsize());
583
        mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
584
}
585
 
586
/* set the framerate into the mchip */
587
static void mchip_set_framerate(void) {
588
        mchip_set(MCHIP_HIC_S_RATE, meye.params.framerate);
589
}
590
 
591
/* load some huffman and quantisation tables into the VRJ chip ready
592
   for JPEG compression */
593
static void mchip_load_tables(void) {
594
        int i;
595
        int size;
596
        u16 *tables;
597
 
598
        tables = jpeg_huffman_tables(&size);
599
        for (i = 0; i < size / 2; i++)
600
                writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
601
 
602
        tables = jpeg_quantisation_tables(&size, meye.params.quality);
603
        for (i = 0; i < size / 2; i++)
604
                writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
605
}
606
 
607
/* setup the VRJ parameters in the chip */
608
static void mchip_vrj_setup(u8 mode) {
609
 
610
        mchip_set(MCHIP_VRJ_BUS_MODE, 5);
611
        mchip_set(MCHIP_VRJ_SIGNAL_ACTIVE_LEVEL, 0x1f);
612
        mchip_set(MCHIP_VRJ_PDAT_USE, 1);
613
        mchip_set(MCHIP_VRJ_IRQ_FLAG, 0xa0);
614
        mchip_set(MCHIP_VRJ_MODE_SPECIFY, mode);
615
        mchip_set(MCHIP_VRJ_NUM_LINES, mchip_vsize());
616
        mchip_set(MCHIP_VRJ_NUM_PIXELS, mchip_hsize());
617
        mchip_set(MCHIP_VRJ_NUM_COMPONENTS, 0x1b);
618
        mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_LO, 0xFFFF);
619
        mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_HI, 0xFFFF);
620
        mchip_set(MCHIP_VRJ_COMP_DATA_FORMAT, 0xC);
621
        mchip_set(MCHIP_VRJ_RESTART_INTERVAL, 0);
622
        mchip_set(MCHIP_VRJ_SOF1, 0x601);
623
        mchip_set(MCHIP_VRJ_SOF2, 0x1502);
624
        mchip_set(MCHIP_VRJ_SOF3, 0x1503);
625
        mchip_set(MCHIP_VRJ_SOF4, 0x1596);
626
        mchip_set(MCHIP_VRJ_SOS,  0x0ed0);
627
 
628
        mchip_load_tables();
629
}
630
 
631
/* sets the DMA parameters into the chip */
632
static void mchip_dma_setup(u32 dma_addr) {
633
        int i;
634
 
635
        mchip_set(MCHIP_MM_PT_ADDR, dma_addr);
636
        for (i = 0; i < 4; i++)
637
                mchip_set(MCHIP_MM_FIR(i), 0);
638
        meye.mchip_fnum = 0;
639
}
640
 
641
/* setup for DMA transfers - also zeros the framebuffer */
642
static int mchip_dma_alloc(void) {
643
        if (!meye.mchip_dmahandle)
644
                if (ptable_alloc())
645
                        return -1;
646
        return 0;
647
}
648
 
649
/* frees the DMA buffer */
650
static void mchip_dma_free(void) {
651
        if (meye.mchip_dmahandle) {
652
                mchip_dma_setup(0);
653
                ptable_free();
654
        }
655
}
656
 
657
/* stop any existing HIC action and wait for any dma to complete then
658
   reset the dma engine */
659
static void mchip_hic_stop(void) {
660
        int i, j;
661
 
662
        meye.mchip_mode = MCHIP_HIC_MODE_NOOP;
663
        if (!(mchip_read(MCHIP_HIC_STATUS) & MCHIP_HIC_STATUS_BUSY))
664
                return;
665
        for (i = 0; i < 20; ++i) {
666
                mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_STOP);
667
                mchip_delay(MCHIP_HIC_CMD, 0);
668
                for (j = 0; j < 100; ++j) {
669
                        if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
670
                                return;
671
                        wait_ms(1);
672
                }
673
                printk(KERN_ERR "meye: need to reset HIC!\n");
674
 
675
                mchip_set(MCHIP_HIC_CTL, MCHIP_HIC_CTL_SOFT_RESET);
676
                wait_ms(250);
677
        }
678
        printk(KERN_ERR "meye: resetting HIC hanged!\n");
679
}
680
 
681
/****************************************************************************/
682
/* MCHIP frame processing functions                                         */
683
/****************************************************************************/
684
 
685
/* get the next ready frame from the dma engine */
686
static u32 mchip_get_frame(void) {
687
        u32 v;
688
 
689
        v = mchip_read(MCHIP_MM_FIR(meye.mchip_fnum));
690
        return v;
691
}
692
 
693
/* frees the current frame from the dma engine */
694
static void mchip_free_frame(void) {
695
        mchip_set(MCHIP_MM_FIR(meye.mchip_fnum), 0);
696
        meye.mchip_fnum++;
697
        meye.mchip_fnum %= 4;
698
}
699
 
700
/* read one frame from the framebuffer assuming it was captured using
701
   a uncompressed transfer */
702
static void mchip_cont_read_frame(u32 v, u8 *buf, int size) {
703
        int pt_id;
704
 
705
        pt_id = (v >> 17) & 0x3FF;
706
 
707
        ptable_copy(buf, pt_id, size, MCHIP_NB_PAGES);
708
 
709
}
710
 
711
/* read a compressed frame from the framebuffer */
712
static int mchip_comp_read_frame(u32 v, u8 *buf, int size) {
713
        int pt_start, pt_end, trailer;
714
        int fsize;
715
        int i;
716
 
717
        pt_start = (v >> 19) & 0xFF;
718
        pt_end = (v >> 11) & 0xFF;
719
        trailer = (v >> 1) & 0x3FF;
720
 
721
        if (pt_end < pt_start)
722
                fsize = (MCHIP_NB_PAGES_MJPEG - pt_start) * PAGE_SIZE +
723
                        pt_end * PAGE_SIZE + trailer * 4;
724
        else
725
                fsize = (pt_end - pt_start) * PAGE_SIZE + trailer * 4;
726
 
727
        if (fsize > size) {
728
                printk(KERN_WARNING "meye: oversized compressed frame %d\n",
729
                       fsize);
730
                return -1;
731
        }
732
 
733
        ptable_copy(buf, pt_start, fsize, MCHIP_NB_PAGES_MJPEG);
734
 
735
 
736
#ifdef MEYE_JPEG_CORRECTION
737
 
738
        /* Some mchip generated jpeg frames are incorrect. In most
739
         * (all ?) of those cases, the final EOI (0xff 0xd9) marker
740
         * is not present at the end of the frame.
741
         *
742
         * Since adding the final marker is not enough to restore
743
         * the jpeg integrity, we drop the frame.
744
         */
745
 
746
        for (i = fsize - 1; i > 0 && buf[i] == 0xff; i--) ;
747
 
748
        if (i < 2 || buf[i - 1] != 0xff || buf[i] != 0xd9)
749
                return -1;
750
 
751
#endif
752
 
753
        return fsize;
754
}
755
 
756
/* take a picture into SDRAM */
757
static void mchip_take_picture(void) {
758
        int i;
759
 
760
        mchip_hic_stop();
761
        mchip_subsample();
762
        mchip_dma_setup(meye.mchip_dmahandle);
763
 
764
        mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_CAP);
765
        mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
766
 
767
        mchip_delay(MCHIP_HIC_CMD, 0);
768
 
769
        for (i = 0; i < 100; ++i) {
770
                if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
771
                        break;
772
                wait_ms(1);
773
        }
774
}
775
 
776
/* dma a previously taken picture into a buffer */
777
static void mchip_get_picture(u8 *buf, int bufsize) {
778
        u32 v;
779
        int i;
780
 
781
        mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_OUT);
782
        mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
783
 
784
        mchip_delay(MCHIP_HIC_CMD, 0);
785
        for (i = 0; i < 100; ++i) {
786
                if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
787
                        break;
788
                wait_ms(1);
789
        }
790
        for (i = 0; i < 4 ; ++i) {
791
                v = mchip_get_frame();
792
                if (v & MCHIP_MM_FIR_RDY) {
793
                        mchip_cont_read_frame(v, buf, bufsize);
794
                        break;
795
                }
796
                mchip_free_frame();
797
        }
798
}
799
 
800
/* start continuous dma capture */
801
static void mchip_continuous_start(void) {
802
        mchip_hic_stop();
803
        mchip_subsample();
804
        mchip_set_framerate();
805
        mchip_dma_setup(meye.mchip_dmahandle);
806
 
807
        meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
808
 
809
        mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_CONT_OUT);
810
        mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
811
 
812
        mchip_delay(MCHIP_HIC_CMD, 0);
813
}
814
 
815
/* compress one frame into a buffer */
816
static int mchip_compress_frame(u8 *buf, int bufsize) {
817
        u32 v;
818
        int len = -1, i;
819
 
820
        mchip_vrj_setup(0x3f);
821
        udelay(50);
822
 
823
        mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_COMP);
824
        mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
825
 
826
        mchip_delay(MCHIP_HIC_CMD, 0);
827
        for (i = 0; i < 100; ++i) {
828
                if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
829
                        break;
830
                wait_ms(1);
831
        }
832
 
833
        for (i = 0; i < 4 ; ++i) {
834
                v = mchip_get_frame();
835
                if (v & MCHIP_MM_FIR_RDY) {
836
                        len = mchip_comp_read_frame(v, buf, bufsize);
837
                        break;
838
                }
839
                mchip_free_frame();
840
        }
841
        return len;
842
}
843
 
844
#if 0
845
/* uncompress one image into a buffer */
846
static int mchip_uncompress_frame(u8 *img, int imgsize, u8 *buf, int bufsize) {
847
        mchip_vrj_setup(0x3f);
848
        udelay(50);
849
 
850
        mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_DECOMP);
851
        mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
852
 
853
        mchip_delay(MCHIP_HIC_CMD, 0);
854
 
855
        return mchip_comp_read_frame(buf, bufsize);
856
}
857
#endif
858
 
859
/* start continuous compressed capture */
860
static void mchip_cont_compression_start(void) {
861
        mchip_hic_stop();
862
        mchip_vrj_setup(0x3f);
863
        mchip_subsample();
864
        mchip_set_framerate();
865
        mchip_dma_setup(meye.mchip_dmahandle);
866
 
867
        meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
868
 
869
        mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_CONT_COMP);
870
        mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
871
 
872
        mchip_delay(MCHIP_HIC_CMD, 0);
873
}
874
 
875
/****************************************************************************/
876
/* Interrupt handling                                                       */
877
/****************************************************************************/
878
 
879
static irqreturn_t meye_irq(int irq, void *dev_id, struct pt_regs *regs) {
880
        u32 v;
881
        int reqnr;
882
        v = mchip_read(MCHIP_MM_INTA);
883
 
884
        while (1) {
885
                v = mchip_get_frame();
886
                if (!(v & MCHIP_MM_FIR_RDY))
887
                        return IRQ_NONE;
888
                switch (meye.mchip_mode) {
889
 
890
                case MCHIP_HIC_MODE_CONT_OUT:
891
                        if (!meye_emptyq(&meye.grabq, NULL)) {
892
                                int nr = meye_pullq(&meye.grabq);
893
                                mchip_cont_read_frame(
894
                                        v,
895
                                        meye.grab_fbuffer + gbufsize * nr,
896
                                        mchip_hsize() * mchip_vsize() * 2);
897
                                meye.grab_buffer[nr].state = MEYE_BUF_DONE;
898
                                wake_up_interruptible(&meye.grabq.proc_list);
899
                        }
900
                        break;
901
 
902
                case MCHIP_HIC_MODE_CONT_COMP:
903
                        if (!meye_emptyq(&meye.grabq, &reqnr)) {
904
                                int size;
905
                                size = mchip_comp_read_frame(
906
                                        v,
907
                                        meye.grab_fbuffer + gbufsize * reqnr,
908
                                        gbufsize);
909
                                if (size == -1)
910
                                        break;
911
                                reqnr = meye_pullq(&meye.grabq);
912
                                meye.grab_buffer[reqnr].size = size;
913
                                meye.grab_buffer[reqnr].state = MEYE_BUF_DONE;
914
                                wake_up_interruptible(&meye.grabq.proc_list);
915
                        }
916
                        break;
917
 
918
                default:
919
                        /* do not free frame, since it can be a snap */
920
                        return IRQ_NONE;
921
                } /* switch */
922
 
923
                mchip_free_frame();
924
        }
925
        return IRQ_HANDLED;
926
}
927
 
928
/****************************************************************************/
929
/* video4linux integration                                                  */
930
/****************************************************************************/
931
 
932
static int meye_open(struct inode *inode, struct file *file) {
933
        int i, err;
934
 
935
        err = video_exclusive_open(inode,file);
936
        if (err < 0)
937
                return err;
938
 
939
        if (mchip_dma_alloc()) {
940
                printk(KERN_ERR "meye: mchip framebuffer allocation failed\n");
941
                video_exclusive_release(inode,file);
942
                return -ENOBUFS;
943
        }
944
        mchip_hic_stop();
945
        meye_initq(&meye.grabq);
946
        for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
947
                meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
948
        return 0;
949
}
950
 
951
static int meye_release(struct inode *inode, struct file *file) {
952
        mchip_hic_stop();
953
        mchip_dma_free();
954
        video_exclusive_release(inode,file);
955
        return 0;
956
}
957
 
958
static int meye_do_ioctl(struct inode *inode, struct file *file,
959
                         unsigned int cmd, void *arg) {
960
 
961
        switch (cmd) {
962
 
963
        case VIDIOCGCAP: {
964
                struct video_capability *b = arg;
965
                strcpy(b->name,meye.video_dev->name);
966
                b->type = VID_TYPE_CAPTURE;
967
                b->channels = 1;
968
                b->audios = 0;
969
                b->maxwidth = 640;
970
                b->maxheight = 480;
971
                b->minwidth = 320;
972
                b->minheight = 240;
973
                break;
974
        }
975
 
976
        case VIDIOCGCHAN: {
977
                struct video_channel *v = arg;
978
                v->flags = 0;
979
                v->tuners = 0;
980
                v->type = VIDEO_TYPE_CAMERA;
981
                if (v->channel != 0)
982
                        return -EINVAL;
983
                strcpy(v->name,"Camera");
984
                break;
985
        }
986
 
987
        case VIDIOCSCHAN: {
988
                struct video_channel *v = arg;
989
                if (v->channel != 0)
990
                        return -EINVAL;
991
                break;
992
        }
993
 
994
        case VIDIOCGPICT: {
995
                struct video_picture *p = arg;
996
                *p = meye.picture;
997
                break;
998
        }
999
 
1000
        case VIDIOCSPICT: {
1001
                struct video_picture *p = arg;
1002
                if (p->depth != 2)
1003
                        return -EINVAL;
1004
                if (p->palette != VIDEO_PALETTE_YUV422)
1005
                        return -EINVAL;
1006
                down(&meye.lock);
1007
                sonypi_camera_command(SONYPI_COMMAND_SETCAMERABRIGHTNESS,
1008
                                      p->brightness >> 10);
1009
                sonypi_camera_command(SONYPI_COMMAND_SETCAMERAHUE,
1010
                                      p->hue >> 10);
1011
                sonypi_camera_command(SONYPI_COMMAND_SETCAMERACOLOR,
1012
                                      p->colour >> 10);
1013
                sonypi_camera_command(SONYPI_COMMAND_SETCAMERACONTRAST,
1014
                                      p->contrast >> 10);
1015
                meye.picture = *p;
1016
                up(&meye.lock);
1017
                break;
1018
        }
1019
 
1020
        case VIDIOCSYNC: {
1021
                int *i = arg;
1022
 
1023
                if (*i < 0 || *i >= gbuffers)
1024
                        return -EINVAL;
1025
 
1026
                switch (meye.grab_buffer[*i].state) {
1027
 
1028
                case MEYE_BUF_UNUSED:
1029
                        return -EINVAL;
1030
                case MEYE_BUF_USING:
1031
                        if (wait_event_interruptible(meye.grabq.proc_list,
1032
                                                     (meye.grab_buffer[*i].state != MEYE_BUF_USING)))
1033
                                return -EINTR;
1034
                        /* fall through */
1035
                case MEYE_BUF_DONE:
1036
                        meye.grab_buffer[*i].state = MEYE_BUF_UNUSED;
1037
                }
1038
                break;
1039
        }
1040
 
1041
        case VIDIOCMCAPTURE: {
1042
                struct video_mmap *vm = arg;
1043
                int restart = 0;
1044
 
1045
                if (vm->frame >= gbuffers || vm->frame < 0)
1046
                        return -EINVAL;
1047
                if (vm->format != VIDEO_PALETTE_YUV422)
1048
                        return -EINVAL;
1049
                if (vm->height * vm->width * 2 > gbufsize)
1050
                        return -EINVAL;
1051
                if (!meye.grab_fbuffer)
1052
                        return -EINVAL;
1053
                if (meye.grab_buffer[vm->frame].state != MEYE_BUF_UNUSED)
1054
                        return -EBUSY;
1055
 
1056
                down(&meye.lock);
1057
                if (vm->width == 640 && vm->height == 480) {
1058
                        if (meye.params.subsample) {
1059
                                meye.params.subsample = 0;
1060
                                restart = 1;
1061
                        }
1062
                }
1063
                else if (vm->width == 320 && vm->height == 240) {
1064
                        if (!meye.params.subsample) {
1065
                                meye.params.subsample = 1;
1066
                                restart = 1;
1067
                        }
1068
                }
1069
                else {
1070
                        up(&meye.lock);
1071
                        return -EINVAL;
1072
                }
1073
 
1074
                if (restart || meye.mchip_mode != MCHIP_HIC_MODE_CONT_OUT)
1075
                        mchip_continuous_start();
1076
                meye.grab_buffer[vm->frame].state = MEYE_BUF_USING;
1077
                meye_pushq(&meye.grabq, vm->frame);
1078
                up(&meye.lock);
1079
                break;
1080
        }
1081
 
1082
        case VIDIOCGMBUF: {
1083
                struct video_mbuf *vm = arg;
1084
                int i;
1085
 
1086
                memset(vm, 0 , sizeof(*vm));
1087
                vm->size = gbufsize * gbuffers;
1088
                vm->frames = gbuffers;
1089
                for (i = 0; i < gbuffers; i++)
1090
                        vm->offsets[i] = i * gbufsize;
1091
                break;
1092
        }
1093
 
1094
        case MEYEIOC_G_PARAMS: {
1095
                struct meye_params *p = arg;
1096
                *p = meye.params;
1097
                break;
1098
        }
1099
 
1100
        case MEYEIOC_S_PARAMS: {
1101
                struct meye_params *jp = arg;
1102
                if (jp->subsample > 1)
1103
                        return -EINVAL;
1104
                if (jp->quality > 10)
1105
                        return -EINVAL;
1106
                if (jp->sharpness > 63 || jp->agc > 63 || jp->picture > 63)
1107
                        return -EINVAL;
1108
                if (jp->framerate > 31)
1109
                        return -EINVAL;
1110
                down(&meye.lock);
1111
                if (meye.params.subsample != jp->subsample ||
1112
                    meye.params.quality != jp->quality)
1113
                        mchip_hic_stop();       /* need restart */
1114
                meye.params = *jp;
1115
                sonypi_camera_command(SONYPI_COMMAND_SETCAMERASHARPNESS,
1116
                                      meye.params.sharpness);
1117
                sonypi_camera_command(SONYPI_COMMAND_SETCAMERAAGC,
1118
                                      meye.params.agc);
1119
                sonypi_camera_command(SONYPI_COMMAND_SETCAMERAPICTURE,
1120
                                      meye.params.picture);
1121
                up(&meye.lock);
1122
                break;
1123
        }
1124
 
1125
        case MEYEIOC_QBUF_CAPT: {
1126
                int *nb = arg;
1127
 
1128
                if (!meye.grab_fbuffer)
1129
                        return -EINVAL;
1130
                if (*nb >= gbuffers)
1131
                        return -EINVAL;
1132
                if (*nb < 0) {
1133
                        /* stop capture */
1134
                        mchip_hic_stop();
1135
                        return 0;
1136
                }
1137
                if (meye.grab_buffer[*nb].state != MEYE_BUF_UNUSED)
1138
                        return -EBUSY;
1139
                down(&meye.lock);
1140
                if (meye.mchip_mode != MCHIP_HIC_MODE_CONT_COMP)
1141
                        mchip_cont_compression_start();
1142
                meye.grab_buffer[*nb].state = MEYE_BUF_USING;
1143
                meye_pushq(&meye.grabq, *nb);
1144
                up(&meye.lock);
1145
                break;
1146
        }
1147
 
1148
        case MEYEIOC_SYNC: {
1149
                int *i = arg;
1150
 
1151
                if (*i < 0 || *i >= gbuffers)
1152
                        return -EINVAL;
1153
 
1154
                switch (meye.grab_buffer[*i].state) {
1155
 
1156
                case MEYE_BUF_UNUSED:
1157
                        return -EINVAL;
1158
                case MEYE_BUF_USING:
1159
                        if (wait_event_interruptible(meye.grabq.proc_list,
1160
                                                     (meye.grab_buffer[*i].state != MEYE_BUF_USING)))
1161
                                return -EINTR;
1162
                        /* fall through */
1163
                case MEYE_BUF_DONE:
1164
                        meye.grab_buffer[*i].state = MEYE_BUF_UNUSED;
1165
                }
1166
                *i = meye.grab_buffer[*i].size;
1167
                break;
1168
        }
1169
 
1170
        case MEYEIOC_STILLCAPT: {
1171
 
1172
                if (!meye.grab_fbuffer)
1173
                        return -EINVAL;
1174
                if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
1175
                        return -EBUSY;
1176
                down(&meye.lock);
1177
                meye.grab_buffer[0].state = MEYE_BUF_USING;
1178
                mchip_take_picture();
1179
                mchip_get_picture(
1180
                        meye.grab_fbuffer,
1181
                        mchip_hsize() * mchip_vsize() * 2);
1182
                meye.grab_buffer[0].state = MEYE_BUF_DONE;
1183
                up(&meye.lock);
1184
                break;
1185
        }
1186
 
1187
        case MEYEIOC_STILLJCAPT: {
1188
                int *len = arg;
1189
 
1190
                if (!meye.grab_fbuffer)
1191
                        return -EINVAL;
1192
                if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
1193
                        return -EBUSY;
1194
                down(&meye.lock);
1195
                meye.grab_buffer[0].state = MEYE_BUF_USING;
1196
                *len = -1;
1197
                while (*len == -1) {
1198
                        mchip_take_picture();
1199
                        *len = mchip_compress_frame(meye.grab_fbuffer, gbufsize);
1200
                }
1201
                meye.grab_buffer[0].state = MEYE_BUF_DONE;
1202
                up(&meye.lock);
1203
                break;
1204
        }
1205
 
1206
        default:
1207
                return -ENOIOCTLCMD;
1208
 
1209
        } /* switch */
1210
 
1211
        return 0;
1212
}
1213
 
1214
static int meye_ioctl(struct inode *inode, struct file *file,
1215
                     unsigned int cmd, unsigned long arg)
1216
{
1217
        return video_usercopy(inode, file, cmd, arg, meye_do_ioctl);
1218
}
1219
 
1220
static int meye_mmap(struct file *file, struct vm_area_struct *vma) {
1221
        unsigned long start = vma->vm_start;
1222
        unsigned long size  = vma->vm_end - vma->vm_start;
1223
        unsigned long page, pos;
1224
 
1225
        down(&meye.lock);
1226
        if (size > gbuffers * gbufsize) {
1227
                up(&meye.lock);
1228
                return -EINVAL;
1229
        }
1230
        if (!meye.grab_fbuffer) {
1231
                /* lazy allocation */
1232
                meye.grab_fbuffer = rvmalloc(gbuffers*gbufsize);
1233
                if (!meye.grab_fbuffer) {
1234
                        printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
1235
                        up(&meye.lock);
1236
                        return -ENOMEM;
1237
                }
1238
        }
1239
        pos = (unsigned long)meye.grab_fbuffer;
1240
 
1241
        while (size > 0) {
1242
                page = kvirt_to_pa(pos);
1243
                if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED)) {
1244
                        up(&meye.lock);
1245
                        return -EAGAIN;
1246
                }
1247
                start += PAGE_SIZE;
1248
                pos += PAGE_SIZE;
1249
                size -= PAGE_SIZE;
1250
        }
1251
        up(&meye.lock);
1252
        return 0;
1253
}
1254
 
1255
static struct file_operations meye_fops = {
1256
        .owner          = THIS_MODULE,
1257
        .open           = meye_open,
1258
        .release        = meye_release,
1259
        .mmap           = meye_mmap,
1260
        .ioctl          = meye_ioctl,
1261
        .llseek         = no_llseek,
1262
};
1263
 
1264
static struct video_device meye_template = {
1265
        .owner          = THIS_MODULE,
1266
        .name           = "meye",
1267
        .type           = VID_TYPE_CAPTURE,
1268
        .hardware       = VID_HARDWARE_MEYE,
1269
        .fops           = &meye_fops,
1270
        .release        = video_device_release,
1271
        .minor          = -1,
1272
};
1273
 
1274
#ifdef CONFIG_PM
1275
static int meye_suspend(struct pci_dev *pdev, u32 state)
1276
{
1277
        pci_save_state(pdev, meye.pm_state);
1278
        meye.pm_mchip_mode = meye.mchip_mode;
1279
        mchip_hic_stop();
1280
        mchip_set(MCHIP_MM_INTA, 0x0);
1281
        return 0;
1282
}
1283
 
1284
static int meye_resume(struct pci_dev *pdev)
1285
{
1286
        pci_restore_state(pdev, meye.pm_state);
1287
        pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
1288
 
1289
        mchip_delay(MCHIP_HIC_CMD, 0);
1290
        mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
1291
        wait_ms(1);
1292
        mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
1293
        wait_ms(1);
1294
        mchip_set(MCHIP_MM_PCI_MODE, 5);
1295
        wait_ms(1);
1296
        mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
1297
 
1298
        switch (meye.pm_mchip_mode) {
1299
        case MCHIP_HIC_MODE_CONT_OUT:
1300
                mchip_continuous_start();
1301
                break;
1302
        case MCHIP_HIC_MODE_CONT_COMP:
1303
                mchip_cont_compression_start();
1304
                break;
1305
        }
1306
        return 0;
1307
}
1308
#endif
1309
 
1310
static int __devinit meye_probe(struct pci_dev *pcidev,
1311
                                const struct pci_device_id *ent) {
1312
        int ret;
1313
        unsigned long mchip_adr;
1314
        u8 revision;
1315
 
1316
        if (meye.mchip_dev != NULL) {
1317
                printk(KERN_ERR "meye: only one device allowed!\n");
1318
                ret = -EBUSY;
1319
                goto out1;
1320
        }
1321
 
1322
        meye.mchip_dev = pcidev;
1323
        meye.video_dev = video_device_alloc();
1324
        if (!meye.video_dev) {
1325
                printk(KERN_ERR "meye: video_device_alloc() failed!\n");
1326
                ret = -EBUSY;
1327
                goto out1;
1328
        }
1329
        memcpy(meye.video_dev, &meye_template, sizeof(meye_template));
1330
 
1331
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERA, 1);
1332
 
1333
        if ((ret = pci_enable_device(meye.mchip_dev))) {
1334
                printk(KERN_ERR "meye: pci_enable_device failed\n");
1335
                goto out2;
1336
        }
1337
 
1338
        meye.mchip_irq = pcidev->irq;
1339
        mchip_adr = pci_resource_start(meye.mchip_dev,0);
1340
        if (!mchip_adr) {
1341
                printk(KERN_ERR "meye: mchip has no device base address\n");
1342
                ret = -EIO;
1343
                goto out3;
1344
        }
1345
        if (!request_mem_region(pci_resource_start(meye.mchip_dev, 0),
1346
                                pci_resource_len(meye.mchip_dev, 0),
1347
                                "meye")) {
1348
                ret = -EIO;
1349
                printk(KERN_ERR "meye: request_mem_region failed\n");
1350
                goto out3;
1351
        }
1352
 
1353
        pci_read_config_byte(meye.mchip_dev, PCI_REVISION_ID, &revision);
1354
 
1355
        pci_set_master(meye.mchip_dev);
1356
 
1357
        pci_write_config_byte(meye.mchip_dev, PCI_CACHE_LINE_SIZE, 8);
1358
        pci_write_config_byte(meye.mchip_dev, PCI_LATENCY_TIMER, 64);
1359
 
1360
        if ((ret = request_irq(meye.mchip_irq, meye_irq,
1361
                               SA_INTERRUPT | SA_SHIRQ, "meye", meye_irq))) {
1362
                printk(KERN_ERR "meye: request_irq failed (ret=%d)\n", ret);
1363
                goto out4;
1364
        }
1365
 
1366
        meye.mchip_mmregs = ioremap(mchip_adr, MCHIP_MM_REGS);
1367
        if (!meye.mchip_mmregs) {
1368
                printk(KERN_ERR "meye: ioremap failed\n");
1369
                ret = -EIO;
1370
                goto out5;
1371
        }
1372
 
1373
        /* Ask the camera to perform a soft reset. */
1374
        pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
1375
 
1376
        mchip_delay(MCHIP_HIC_CMD, 0);
1377
        mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
1378
 
1379
        wait_ms(1);
1380
        mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
1381
 
1382
        wait_ms(1);
1383
        mchip_set(MCHIP_MM_PCI_MODE, 5);
1384
 
1385
        wait_ms(1);
1386
        mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
1387
 
1388
        if (video_register_device(meye.video_dev, VFL_TYPE_GRABBER, video_nr) < 0) {
1389
 
1390
                printk(KERN_ERR "meye: video_register_device failed\n");
1391
                ret = -EIO;
1392
                goto out6;
1393
        }
1394
 
1395
        printk(KERN_INFO "meye: Motion Eye Camera Driver v%d.%d.\n",
1396
               MEYE_DRIVER_MAJORVERSION,
1397
               MEYE_DRIVER_MINORVERSION);
1398
        printk(KERN_INFO "meye: mchip KL5A72002 rev. %d, base %lx, irq %d\n",
1399
                revision, mchip_adr, meye.mchip_irq);
1400
 
1401
        /* init all fields */
1402
        init_MUTEX(&meye.lock);
1403
 
1404
        meye.picture.depth = 2;
1405
        meye.picture.palette = VIDEO_PALETTE_YUV422;
1406
        meye.picture.brightness = 32 << 10;
1407
        meye.picture.hue = 32 << 10;
1408
        meye.picture.colour = 32 << 10;
1409
        meye.picture.contrast = 32 << 10;
1410
        meye.picture.whiteness = 0;
1411
        meye.params.subsample = 0;
1412
        meye.params.quality = 7;
1413
        meye.params.sharpness = 32;
1414
        meye.params.agc = 48;
1415
        meye.params.picture = 0;
1416
        meye.params.framerate = 0;
1417
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERABRIGHTNESS, 32);
1418
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERAHUE, 32);
1419
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERACOLOR, 32);
1420
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERACONTRAST, 32);
1421
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERASHARPNESS, 32);
1422
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERAPICTURE, 0);
1423
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERAAGC, 48);
1424
 
1425
        return 0;
1426
out6:
1427
        iounmap(meye.mchip_mmregs);
1428
out5:
1429
        free_irq(meye.mchip_irq, meye_irq);
1430
out4:
1431
        release_mem_region(pci_resource_start(meye.mchip_dev, 0),
1432
                           pci_resource_len(meye.mchip_dev, 0));
1433
out3:
1434
        pci_disable_device(meye.mchip_dev);
1435
out2:
1436
        video_device_release(meye.video_dev);
1437
        meye.video_dev = NULL;
1438
 
1439
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERA, 0);
1440
out1:
1441
        return ret;
1442
}
1443
 
1444
static void __devexit meye_remove(struct pci_dev *pcidev) {
1445
 
1446
        video_unregister_device(meye.video_dev);
1447
 
1448
        mchip_hic_stop();
1449
 
1450
        mchip_dma_free();
1451
 
1452
        /* disable interrupts */
1453
        mchip_set(MCHIP_MM_INTA, 0x0);
1454
 
1455
        free_irq(meye.mchip_irq, meye_irq);
1456
 
1457
        iounmap(meye.mchip_mmregs);
1458
 
1459
        release_mem_region(pci_resource_start(meye.mchip_dev, 0),
1460
                           pci_resource_len(meye.mchip_dev, 0));
1461
 
1462
        pci_disable_device(meye.mchip_dev);
1463
 
1464
        if (meye.grab_fbuffer)
1465
                rvfree(meye.grab_fbuffer, gbuffers*gbufsize);
1466
 
1467
        sonypi_camera_command(SONYPI_COMMAND_SETCAMERA, 0);
1468
 
1469
        printk(KERN_INFO "meye: removed\n");
1470
}
1471
 
1472
static struct pci_device_id meye_pci_tbl[] = {
1473
        { PCI_VENDOR_ID_KAWASAKI, PCI_DEVICE_ID_MCHIP_KL5A72002,
1474
          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1475
        { }
1476
};
1477
 
1478
MODULE_DEVICE_TABLE(pci, meye_pci_tbl);
1479
 
1480
static struct pci_driver meye_driver = {
1481
        .name           = "meye",
1482
        .id_table       = meye_pci_tbl,
1483
        .probe          = meye_probe,
1484
        .remove         = __devexit_p(meye_remove),
1485
#ifdef CONFIG_PM
1486
        .suspend        = meye_suspend,
1487
        .resume         = meye_resume,
1488
#endif
1489
};
1490
 
1491
static int __init meye_init_module(void) {
1492
        if (gbuffers < 2)
1493
                gbuffers = 2;
1494
        if (gbuffers > MEYE_MAX_BUFNBRS)
1495
                gbuffers = MEYE_MAX_BUFNBRS;
1496
        if (gbufsize < 0 || gbufsize > MEYE_MAX_BUFSIZE)
1497
                gbufsize = MEYE_MAX_BUFSIZE;
1498
        printk(KERN_INFO "meye: using %d buffers with %dk (%dk total) for capture\n",
1499
               gbuffers, gbufsize/1024, gbuffers*gbufsize/1024);
1500
        return pci_module_init(&meye_driver);
1501
}
1502
 
1503
static void __exit meye_cleanup_module(void) {
1504
        pci_unregister_driver(&meye_driver);
1505
}
1506
 
1507
#ifndef MODULE
1508
static int __init meye_setup(char *str) {
1509
        int ints[4];
1510
 
1511
        str = get_options(str, ARRAY_SIZE(ints), ints);
1512
        if (ints[0] <= 0)
1513
                goto out;
1514
        gbuffers = ints[1];
1515
        if (ints[0] == 1)
1516
                goto out;
1517
        gbufsize = ints[2];
1518
        if (ints[0] == 2)
1519
                goto out;
1520
        video_nr = ints[3];
1521
out:
1522
        return 1;
1523
}
1524
 
1525
__setup("meye=", meye_setup);
1526
#endif
1527
 
1528
MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
1529
MODULE_DESCRIPTION("video4linux driver for the MotionEye camera");
1530
MODULE_LICENSE("GPL");
1531
 
1532
MODULE_PARM(gbuffers,"i");
1533
MODULE_PARM_DESC(gbuffers,"number of capture buffers, default is 2 (32 max)");
1534
MODULE_PARM(gbufsize,"i");
1535
MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 614400");
1536
MODULE_PARM(video_nr,"i");
1537
MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
1538
 
1539
/* Module entry points */
1540
module_init(meye_init_module);
1541
module_exit(meye_cleanup_module);

powered by: WebSVN 2.1.0

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