File: /usr/src/linux/drivers/media/video/meye.c

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