]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/pci/meye/meye.c
Replace <asm/uaccess.h> with <linux/uaccess.h> globally
[mirror_ubuntu-bionic-kernel.git] / drivers / media / pci / meye / meye.c
CommitLineData
1da177e4
LT
1/*
2 * Motion Eye video4linux driver for Sony Vaio PictureBook
3 *
4 * Copyright (C) 2001-2004 Stelian Pop <stelian@popies.net>
5 *
96de0e25 6 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
1da177e4
LT
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 */
1da177e4
LT
29#include <linux/module.h>
30#include <linux/pci.h>
d43c36dc 31#include <linux/sched.h>
1da177e4 32#include <linux/init.h>
5a0e3ad6 33#include <linux/gfp.h>
51270617 34#include <linux/videodev2.h>
5e87efa3 35#include <media/v4l2-common.h>
51270617 36#include <media/v4l2-device.h>
35ea11ff 37#include <media/v4l2-ioctl.h>
ed986d1f
HV
38#include <media/v4l2-fh.h>
39#include <media/v4l2-event.h>
7c0f6ba6 40#include <linux/uaccess.h>
1da177e4
LT
41#include <asm/io.h>
42#include <linux/delay.h>
43#include <linux/interrupt.h>
44#include <linux/vmalloc.h>
d013a068 45#include <linux/dma-mapping.h>
1da177e4
LT
46
47#include "meye.h"
48#include <linux/meye.h>
49
50MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
6ec6e0ce 51MODULE_DESCRIPTION("v4l2 driver for the MotionEye camera");
1da177e4
LT
52MODULE_LICENSE("GPL");
53MODULE_VERSION(MEYE_DRIVER_VERSION);
54
1da177e4
LT
55/* number of grab buffers */
56static unsigned int gbuffers = 2;
57module_param(gbuffers, int, 0444);
58MODULE_PARM_DESC(gbuffers, "number of capture buffers, default is 2 (32 max)");
59
60/* size of a grab buffer */
61static unsigned int gbufsize = MEYE_MAX_BUFSIZE;
62module_param(gbufsize, int, 0444);
f8a3dcb5 63MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 614400 (will be rounded up to a page multiple)");
1da177e4
LT
64
65/* /dev/videoX registration number */
66static int video_nr = -1;
67module_param(video_nr, int, 0444);
68MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
69
70/* driver structure - only one possible */
71static struct meye meye;
72
73/****************************************************************************/
74/* Memory allocation routines (stolen from bttv-driver.c) */
75/****************************************************************************/
76static void *rvmalloc(unsigned long size)
77{
78 void *mem;
79 unsigned long adr;
80
81 size = PAGE_ALIGN(size);
82 mem = vmalloc_32(size);
83 if (mem) {
84 memset(mem, 0, size);
85 adr = (unsigned long) mem;
86 while (size > 0) {
87 SetPageReserved(vmalloc_to_page((void *)adr));
88 adr += PAGE_SIZE;
89 size -= PAGE_SIZE;
90 }
91 }
92 return mem;
93}
94
95static void rvfree(void * mem, unsigned long size)
96{
97 unsigned long adr;
98
99 if (mem) {
100 adr = (unsigned long) mem;
101 while ((long) size > 0) {
102 ClearPageReserved(vmalloc_to_page((void *)adr));
103 adr += PAGE_SIZE;
104 size -= PAGE_SIZE;
105 }
106 vfree(mem);
107 }
108}
109
110/*
111 * return a page table pointing to N pages of locked memory
112 *
113 * NOTE: The meye device expects DMA addresses on 32 bits, we build
114 * a table of 1024 entries = 4 bytes * 1024 = 4096 bytes.
115 */
116static int ptable_alloc(void)
117{
118 u32 *pt;
119 int i;
120
121 memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
122
123 /* give only 32 bit DMA addresses */
284901a9 124 if (dma_set_mask(&meye.mchip_dev->dev, DMA_BIT_MASK(32)))
1da177e4
LT
125 return -1;
126
127 meye.mchip_ptable_toc = dma_alloc_coherent(&meye.mchip_dev->dev,
128 PAGE_SIZE,
129 &meye.mchip_dmahandle,
130 GFP_KERNEL);
131 if (!meye.mchip_ptable_toc) {
132 meye.mchip_dmahandle = 0;
133 return -1;
134 }
135
136 pt = meye.mchip_ptable_toc;
137 for (i = 0; i < MCHIP_NB_PAGES; i++) {
138 dma_addr_t dma;
139 meye.mchip_ptable[i] = dma_alloc_coherent(&meye.mchip_dev->dev,
140 PAGE_SIZE,
141 &dma,
142 GFP_KERNEL);
143 if (!meye.mchip_ptable[i]) {
144 int j;
145 pt = meye.mchip_ptable_toc;
146 for (j = 0; j < i; ++j) {
147 dma = (dma_addr_t) *pt;
148 dma_free_coherent(&meye.mchip_dev->dev,
149 PAGE_SIZE,
150 meye.mchip_ptable[j], dma);
151 pt++;
152 }
153 dma_free_coherent(&meye.mchip_dev->dev,
154 PAGE_SIZE,
155 meye.mchip_ptable_toc,
156 meye.mchip_dmahandle);
157 meye.mchip_ptable_toc = NULL;
158 meye.mchip_dmahandle = 0;
159 return -1;
160 }
161 *pt = (u32) dma;
162 pt++;
163 }
164 return 0;
165}
166
167static void ptable_free(void)
168{
169 u32 *pt;
170 int i;
171
172 pt = meye.mchip_ptable_toc;
173 for (i = 0; i < MCHIP_NB_PAGES; i++) {
174 dma_addr_t dma = (dma_addr_t) *pt;
175 if (meye.mchip_ptable[i])
176 dma_free_coherent(&meye.mchip_dev->dev,
177 PAGE_SIZE,
178 meye.mchip_ptable[i], dma);
179 pt++;
180 }
181
182 if (meye.mchip_ptable_toc)
183 dma_free_coherent(&meye.mchip_dev->dev,
184 PAGE_SIZE,
185 meye.mchip_ptable_toc,
186 meye.mchip_dmahandle);
187
188 memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
189 meye.mchip_ptable_toc = NULL;
190 meye.mchip_dmahandle = 0;
191}
192
193/* copy data from ptable into buf */
194static void ptable_copy(u8 *buf, int start, int size, int pt_pages)
195{
196 int i;
197
198 for (i = 0; i < (size / PAGE_SIZE) * PAGE_SIZE; i += PAGE_SIZE) {
199 memcpy(buf + i, meye.mchip_ptable[start++], PAGE_SIZE);
200 if (start >= pt_pages)
201 start = 0;
202 }
203 memcpy(buf + i, meye.mchip_ptable[start], size % PAGE_SIZE);
204}
205
206/****************************************************************************/
207/* JPEG tables at different qualities to load into the VRJ chip */
208/****************************************************************************/
209
210/* return a set of quantisation tables based on a quality from 1 to 10 */
211static u16 *jpeg_quantisation_tables(int *length, int quality)
212{
213 static u16 jpeg_tables[][70] = { {
214 0xdbff, 0x4300, 0xff00, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
215 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
216 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
217 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
218 0xffff, 0xffff, 0xffff,
219 0xdbff, 0x4300, 0xff01, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
220 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
221 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
222 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
223 0xffff, 0xffff, 0xffff,
224 },
225 {
226 0xdbff, 0x4300, 0x5000, 0x3c37, 0x3c46, 0x5032, 0x4146, 0x5a46,
227 0x5055, 0x785f, 0x82c8, 0x6e78, 0x786e, 0xaff5, 0x91b9, 0xffc8,
228 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
229 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
230 0xffff, 0xffff, 0xffff,
231 0xdbff, 0x4300, 0x5501, 0x5a5a, 0x6978, 0xeb78, 0x8282, 0xffeb,
232 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
233 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
234 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
235 0xffff, 0xffff, 0xffff,
236 },
237 {
238 0xdbff, 0x4300, 0x2800, 0x1e1c, 0x1e23, 0x2819, 0x2123, 0x2d23,
239 0x282b, 0x3c30, 0x4164, 0x373c, 0x3c37, 0x587b, 0x495d, 0x9164,
240 0x9980, 0x8f96, 0x8c80, 0xa08a, 0xe6b4, 0xa0c3, 0xdaaa, 0x8aad,
241 0xc88c, 0xcbff, 0xeeda, 0xfff5, 0xffff, 0xc19b, 0xffff, 0xfaff,
242 0xe6ff, 0xfffd, 0xfff8,
243 0xdbff, 0x4300, 0x2b01, 0x2d2d, 0x353c, 0x763c, 0x4141, 0xf876,
244 0x8ca5, 0xf8a5, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
245 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
246 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
247 0xf8f8, 0xf8f8, 0xfff8,
248 },
249 {
250 0xdbff, 0x4300, 0x1b00, 0x1412, 0x1417, 0x1b11, 0x1617, 0x1e17,
251 0x1b1c, 0x2820, 0x2b42, 0x2528, 0x2825, 0x3a51, 0x303d, 0x6042,
252 0x6555, 0x5f64, 0x5d55, 0x6a5b, 0x9978, 0x6a81, 0x9071, 0x5b73,
253 0x855d, 0x86b5, 0x9e90, 0xaba3, 0xabad, 0x8067, 0xc9bc, 0xa6ba,
254 0x99c7, 0xaba8, 0xffa4,
255 0xdbff, 0x4300, 0x1c01, 0x1e1e, 0x2328, 0x4e28, 0x2b2b, 0xa44e,
256 0x5d6e, 0xa46e, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
257 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
258 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
259 0xa4a4, 0xa4a4, 0xffa4,
260 },
261 {
262 0xdbff, 0x4300, 0x1400, 0x0f0e, 0x0f12, 0x140d, 0x1012, 0x1712,
263 0x1415, 0x1e18, 0x2132, 0x1c1e, 0x1e1c, 0x2c3d, 0x242e, 0x4932,
264 0x4c40, 0x474b, 0x4640, 0x5045, 0x735a, 0x5062, 0x6d55, 0x4556,
265 0x6446, 0x6588, 0x776d, 0x817b, 0x8182, 0x604e, 0x978d, 0x7d8c,
266 0x7396, 0x817e, 0xff7c,
267 0xdbff, 0x4300, 0x1501, 0x1717, 0x1a1e, 0x3b1e, 0x2121, 0x7c3b,
268 0x4653, 0x7c53, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
269 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
270 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
271 0x7c7c, 0x7c7c, 0xff7c,
272 },
273 {
274 0xdbff, 0x4300, 0x1000, 0x0c0b, 0x0c0e, 0x100a, 0x0d0e, 0x120e,
275 0x1011, 0x1813, 0x1a28, 0x1618, 0x1816, 0x2331, 0x1d25, 0x3a28,
276 0x3d33, 0x393c, 0x3833, 0x4037, 0x5c48, 0x404e, 0x5744, 0x3745,
277 0x5038, 0x516d, 0x5f57, 0x6762, 0x6768, 0x4d3e, 0x7971, 0x6470,
278 0x5c78, 0x6765, 0xff63,
279 0xdbff, 0x4300, 0x1101, 0x1212, 0x1518, 0x2f18, 0x1a1a, 0x632f,
280 0x3842, 0x6342, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
281 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
282 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
283 0x6363, 0x6363, 0xff63,
284 },
285 {
286 0xdbff, 0x4300, 0x0d00, 0x0a09, 0x0a0b, 0x0d08, 0x0a0b, 0x0e0b,
287 0x0d0e, 0x130f, 0x1520, 0x1213, 0x1312, 0x1c27, 0x171e, 0x2e20,
288 0x3129, 0x2e30, 0x2d29, 0x332c, 0x4a3a, 0x333e, 0x4636, 0x2c37,
289 0x402d, 0x4157, 0x4c46, 0x524e, 0x5253, 0x3e32, 0x615a, 0x505a,
290 0x4a60, 0x5251, 0xff4f,
291 0xdbff, 0x4300, 0x0e01, 0x0e0e, 0x1113, 0x2613, 0x1515, 0x4f26,
292 0x2d35, 0x4f35, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
293 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
294 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
295 0x4f4f, 0x4f4f, 0xff4f,
296 },
297 {
298 0xdbff, 0x4300, 0x0a00, 0x0707, 0x0708, 0x0a06, 0x0808, 0x0b08,
299 0x0a0a, 0x0e0b, 0x1018, 0x0d0e, 0x0e0d, 0x151d, 0x1116, 0x2318,
300 0x251f, 0x2224, 0x221f, 0x2621, 0x372b, 0x262f, 0x3429, 0x2129,
301 0x3022, 0x3141, 0x3934, 0x3e3b, 0x3e3e, 0x2e25, 0x4944, 0x3c43,
302 0x3748, 0x3e3d, 0xff3b,
303 0xdbff, 0x4300, 0x0a01, 0x0b0b, 0x0d0e, 0x1c0e, 0x1010, 0x3b1c,
304 0x2228, 0x3b28, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
305 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
306 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
307 0x3b3b, 0x3b3b, 0xff3b,
308 },
309 {
310 0xdbff, 0x4300, 0x0600, 0x0504, 0x0506, 0x0604, 0x0506, 0x0706,
311 0x0607, 0x0a08, 0x0a10, 0x090a, 0x0a09, 0x0e14, 0x0c0f, 0x1710,
312 0x1814, 0x1718, 0x1614, 0x1a16, 0x251d, 0x1a1f, 0x231b, 0x161c,
313 0x2016, 0x202c, 0x2623, 0x2927, 0x292a, 0x1f19, 0x302d, 0x282d,
314 0x2530, 0x2928, 0xff28,
315 0xdbff, 0x4300, 0x0701, 0x0707, 0x080a, 0x130a, 0x0a0a, 0x2813,
316 0x161a, 0x281a, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
317 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
318 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
319 0x2828, 0x2828, 0xff28,
320 },
321 {
322 0xdbff, 0x4300, 0x0300, 0x0202, 0x0203, 0x0302, 0x0303, 0x0403,
323 0x0303, 0x0504, 0x0508, 0x0405, 0x0504, 0x070a, 0x0607, 0x0c08,
324 0x0c0a, 0x0b0c, 0x0b0a, 0x0d0b, 0x120e, 0x0d10, 0x110e, 0x0b0e,
325 0x100b, 0x1016, 0x1311, 0x1514, 0x1515, 0x0f0c, 0x1817, 0x1416,
326 0x1218, 0x1514, 0xff14,
327 0xdbff, 0x4300, 0x0301, 0x0404, 0x0405, 0x0905, 0x0505, 0x1409,
328 0x0b0d, 0x140d, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
329 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
330 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
331 0x1414, 0x1414, 0xff14,
332 },
333 {
334 0xdbff, 0x4300, 0x0100, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
335 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
336 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
337 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
338 0x0101, 0x0101, 0xff01,
339 0xdbff, 0x4300, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
340 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
341 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
342 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
343 0x0101, 0x0101, 0xff01,
344 } };
345
346 if (quality < 0 || quality > 10) {
347 printk(KERN_WARNING
348 "meye: invalid quality level %d - using 8\n", quality);
349 quality = 8;
350 }
351
352 *length = ARRAY_SIZE(jpeg_tables[quality]);
353 return jpeg_tables[quality];
354}
355
356/* return a generic set of huffman tables */
357static u16 *jpeg_huffman_tables(int *length)
358{
359 static u16 tables[] = {
360 0xC4FF, 0xB500, 0x0010, 0x0102, 0x0303, 0x0402, 0x0503, 0x0405,
361 0x0004, 0x0100, 0x017D, 0x0302, 0x0400, 0x0511, 0x2112, 0x4131,
362 0x1306, 0x6151, 0x2207, 0x1471, 0x8132, 0xA191, 0x2308, 0xB142,
363 0x15C1, 0xD152, 0x24F0, 0x6233, 0x8272, 0x0A09, 0x1716, 0x1918,
364 0x251A, 0x2726, 0x2928, 0x342A, 0x3635, 0x3837, 0x3A39, 0x4443,
365 0x4645, 0x4847, 0x4A49, 0x5453, 0x5655, 0x5857, 0x5A59, 0x6463,
366 0x6665, 0x6867, 0x6A69, 0x7473, 0x7675, 0x7877, 0x7A79, 0x8483,
367 0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998, 0xA29A,
368 0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6, 0xB9B8,
369 0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4, 0xD7D6,
370 0xD9D8, 0xE1DA, 0xE3E2, 0xE5E4, 0xE7E6, 0xE9E8, 0xF1EA, 0xF3F2,
371 0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
372 0xC4FF, 0xB500, 0x0011, 0x0102, 0x0402, 0x0304, 0x0704, 0x0405,
373 0x0004, 0x0201, 0x0077, 0x0201, 0x1103, 0x0504, 0x3121, 0x1206,
374 0x5141, 0x6107, 0x1371, 0x3222, 0x0881, 0x4214, 0xA191, 0xC1B1,
375 0x2309, 0x5233, 0x15F0, 0x7262, 0x0AD1, 0x2416, 0xE134, 0xF125,
376 0x1817, 0x1A19, 0x2726, 0x2928, 0x352A, 0x3736, 0x3938, 0x433A,
377 0x4544, 0x4746, 0x4948, 0x534A, 0x5554, 0x5756, 0x5958, 0x635A,
378 0x6564, 0x6766, 0x6968, 0x736A, 0x7574, 0x7776, 0x7978, 0x827A,
379 0x8483, 0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998,
380 0xA29A, 0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6,
381 0xB9B8, 0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4,
382 0xD7D6, 0xD9D8, 0xE2DA, 0xE4E3, 0xE6E5, 0xE8E7, 0xEAE9, 0xF3F2,
383 0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
384 0xC4FF, 0x1F00, 0x0000, 0x0501, 0x0101, 0x0101, 0x0101, 0x0000,
385 0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
386 0xFF0B,
387 0xC4FF, 0x1F00, 0x0001, 0x0103, 0x0101, 0x0101, 0x0101, 0x0101,
388 0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
389 0xFF0B
390 };
391
392 *length = ARRAY_SIZE(tables);
393 return tables;
394}
395
396/****************************************************************************/
397/* MCHIP low-level functions */
398/****************************************************************************/
399
400/* returns the horizontal capture size */
401static inline int mchip_hsize(void)
402{
403 return meye.params.subsample ? 320 : 640;
404}
405
406/* returns the vertical capture size */
407static inline int mchip_vsize(void)
408{
409 return meye.params.subsample ? 240 : 480;
410}
411
412/* waits for a register to be available */
413static void mchip_sync(int reg)
414{
415 u32 status;
416 int i;
417
418 if (reg == MCHIP_MM_FIFO_DATA) {
419 for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
420 status = readl(meye.mchip_mmregs +
421 MCHIP_MM_FIFO_STATUS);
422 if (!(status & MCHIP_MM_FIFO_WAIT)) {
423 printk(KERN_WARNING "meye: fifo not ready\n");
424 return;
425 }
426 if (status & MCHIP_MM_FIFO_READY)
427 return;
428 udelay(1);
429 }
430 } else if (reg > 0x80) {
431 u32 mask = (reg < 0x100) ? MCHIP_HIC_STATUS_MCC_RDY
432 : MCHIP_HIC_STATUS_VRJ_RDY;
433 for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
434 status = readl(meye.mchip_mmregs + MCHIP_HIC_STATUS);
435 if (status & mask)
436 return;
437 udelay(1);
438 }
439 } else
440 return;
441 printk(KERN_WARNING
442 "meye: mchip_sync() timeout on reg 0x%x status=0x%x\n",
443 reg, status);
444}
445
446/* sets a value into the register */
447static inline void mchip_set(int reg, u32 v)
448{
449 mchip_sync(reg);
450 writel(v, meye.mchip_mmregs + reg);
451}
452
453/* get the register value */
454static inline u32 mchip_read(int reg)
455{
456 mchip_sync(reg);
457 return readl(meye.mchip_mmregs + reg);
458}
459
460/* wait for a register to become a particular value */
461static inline int mchip_delay(u32 reg, u32 v)
462{
463 int n = 10;
464 while (--n && mchip_read(reg) != v)
465 udelay(1);
466 return n;
467}
468
469/* setup subsampling */
470static void mchip_subsample(void)
471{
472 mchip_set(MCHIP_MCC_R_SAMPLING, meye.params.subsample);
473 mchip_set(MCHIP_MCC_R_XRANGE, mchip_hsize());
474 mchip_set(MCHIP_MCC_R_YRANGE, mchip_vsize());
475 mchip_set(MCHIP_MCC_B_XRANGE, mchip_hsize());
476 mchip_set(MCHIP_MCC_B_YRANGE, mchip_vsize());
477 mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
478}
479
480/* set the framerate into the mchip */
481static void mchip_set_framerate(void)
482{
483 mchip_set(MCHIP_HIC_S_RATE, meye.params.framerate);
484}
485
486/* load some huffman and quantisation tables into the VRJ chip ready
487 for JPEG compression */
488static void mchip_load_tables(void)
489{
490 int i;
491 int length;
492 u16 *tables;
493
494 tables = jpeg_huffman_tables(&length);
495 for (i = 0; i < length; i++)
496 writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
497
498 tables = jpeg_quantisation_tables(&length, meye.params.quality);
499 for (i = 0; i < length; i++)
500 writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
501}
502
503/* setup the VRJ parameters in the chip */
504static void mchip_vrj_setup(u8 mode)
505{
506 mchip_set(MCHIP_VRJ_BUS_MODE, 5);
507 mchip_set(MCHIP_VRJ_SIGNAL_ACTIVE_LEVEL, 0x1f);
508 mchip_set(MCHIP_VRJ_PDAT_USE, 1);
509 mchip_set(MCHIP_VRJ_IRQ_FLAG, 0xa0);
510 mchip_set(MCHIP_VRJ_MODE_SPECIFY, mode);
511 mchip_set(MCHIP_VRJ_NUM_LINES, mchip_vsize());
512 mchip_set(MCHIP_VRJ_NUM_PIXELS, mchip_hsize());
513 mchip_set(MCHIP_VRJ_NUM_COMPONENTS, 0x1b);
514 mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_LO, 0xFFFF);
515 mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_HI, 0xFFFF);
516 mchip_set(MCHIP_VRJ_COMP_DATA_FORMAT, 0xC);
517 mchip_set(MCHIP_VRJ_RESTART_INTERVAL, 0);
518 mchip_set(MCHIP_VRJ_SOF1, 0x601);
519 mchip_set(MCHIP_VRJ_SOF2, 0x1502);
520 mchip_set(MCHIP_VRJ_SOF3, 0x1503);
521 mchip_set(MCHIP_VRJ_SOF4, 0x1596);
522 mchip_set(MCHIP_VRJ_SOS, 0x0ed0);
523
524 mchip_load_tables();
525}
526
527/* sets the DMA parameters into the chip */
528static void mchip_dma_setup(dma_addr_t dma_addr)
529{
530 int i;
531
532 mchip_set(MCHIP_MM_PT_ADDR, (u32)dma_addr);
533 for (i = 0; i < 4; i++)
534 mchip_set(MCHIP_MM_FIR(i), 0);
535 meye.mchip_fnum = 0;
536}
537
538/* setup for DMA transfers - also zeros the framebuffer */
539static int mchip_dma_alloc(void)
540{
541 if (!meye.mchip_dmahandle)
542 if (ptable_alloc())
543 return -1;
544 return 0;
545}
546
547/* frees the DMA buffer */
548static void mchip_dma_free(void)
549{
550 if (meye.mchip_dmahandle) {
551 mchip_dma_setup(0);
552 ptable_free();
553 }
554}
555
556/* stop any existing HIC action and wait for any dma to complete then
557 reset the dma engine */
558static void mchip_hic_stop(void)
559{
560 int i, j;
561
562 meye.mchip_mode = MCHIP_HIC_MODE_NOOP;
563 if (!(mchip_read(MCHIP_HIC_STATUS) & MCHIP_HIC_STATUS_BUSY))
564 return;
565 for (i = 0; i < 20; ++i) {
566 mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_STOP);
567 mchip_delay(MCHIP_HIC_CMD, 0);
568 for (j = 0; j < 100; ++j) {
569 if (mchip_delay(MCHIP_HIC_STATUS,
570 MCHIP_HIC_STATUS_IDLE))
571 return;
572 msleep(1);
573 }
574 printk(KERN_ERR "meye: need to reset HIC!\n");
575
576 mchip_set(MCHIP_HIC_CTL, MCHIP_HIC_CTL_SOFT_RESET);
577 msleep(250);
578 }
579 printk(KERN_ERR "meye: resetting HIC hanged!\n");
580}
581
582/****************************************************************************/
583/* MCHIP frame processing functions */
584/****************************************************************************/
585
586/* get the next ready frame from the dma engine */
587static u32 mchip_get_frame(void)
588{
29a8d979 589 return mchip_read(MCHIP_MM_FIR(meye.mchip_fnum));
1da177e4
LT
590}
591
592/* frees the current frame from the dma engine */
593static void mchip_free_frame(void)
594{
595 mchip_set(MCHIP_MM_FIR(meye.mchip_fnum), 0);
596 meye.mchip_fnum++;
597 meye.mchip_fnum %= 4;
598}
599
600/* read one frame from the framebuffer assuming it was captured using
601 a uncompressed transfer */
602static void mchip_cont_read_frame(u32 v, u8 *buf, int size)
603{
604 int pt_id;
605
606 pt_id = (v >> 17) & 0x3FF;
607
608 ptable_copy(buf, pt_id, size, MCHIP_NB_PAGES);
609}
610
611/* read a compressed frame from the framebuffer */
612static int mchip_comp_read_frame(u32 v, u8 *buf, int size)
613{
614 int pt_start, pt_end, trailer;
615 int fsize;
616 int i;
617
618 pt_start = (v >> 19) & 0xFF;
619 pt_end = (v >> 11) & 0xFF;
620 trailer = (v >> 1) & 0x3FF;
621
622 if (pt_end < pt_start)
623 fsize = (MCHIP_NB_PAGES_MJPEG - pt_start) * PAGE_SIZE +
624 pt_end * PAGE_SIZE + trailer * 4;
625 else
626 fsize = (pt_end - pt_start) * PAGE_SIZE + trailer * 4;
627
628 if (fsize > size) {
629 printk(KERN_WARNING "meye: oversized compressed frame %d\n",
630 fsize);
631 return -1;
632 }
633
634 ptable_copy(buf, pt_start, fsize, MCHIP_NB_PAGES_MJPEG);
635
636#ifdef MEYE_JPEG_CORRECTION
637
638 /* Some mchip generated jpeg frames are incorrect. In most
639 * (all ?) of those cases, the final EOI (0xff 0xd9) marker
640 * is not present at the end of the frame.
641 *
642 * Since adding the final marker is not enough to restore
643 * the jpeg integrity, we drop the frame.
644 */
645
646 for (i = fsize - 1; i > 0 && buf[i] == 0xff; i--) ;
647
648 if (i < 2 || buf[i - 1] != 0xff || buf[i] != 0xd9)
649 return -1;
650
651#endif
652
653 return fsize;
654}
655
656/* take a picture into SDRAM */
657static void mchip_take_picture(void)
658{
659 int i;
660
661 mchip_hic_stop();
662 mchip_subsample();
663 mchip_dma_setup(meye.mchip_dmahandle);
664
665 mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_CAP);
666 mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
667
668 mchip_delay(MCHIP_HIC_CMD, 0);
669
670 for (i = 0; i < 100; ++i) {
671 if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
672 break;
673 msleep(1);
674 }
675}
676
677/* dma a previously taken picture into a buffer */
678static void mchip_get_picture(u8 *buf, int bufsize)
679{
680 u32 v;
681 int i;
682
683 mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_OUT);
684 mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
685
686 mchip_delay(MCHIP_HIC_CMD, 0);
687 for (i = 0; i < 100; ++i) {
688 if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
689 break;
690 msleep(1);
691 }
692 for (i = 0; i < 4; ++i) {
693 v = mchip_get_frame();
694 if (v & MCHIP_MM_FIR_RDY) {
695 mchip_cont_read_frame(v, buf, bufsize);
696 break;
697 }
698 mchip_free_frame();
699 }
700}
701
702/* start continuous dma capture */
703static void mchip_continuous_start(void)
704{
705 mchip_hic_stop();
706 mchip_subsample();
707 mchip_set_framerate();
708 mchip_dma_setup(meye.mchip_dmahandle);
709
710 meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
711
712 mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_CONT_OUT);
713 mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
714
715 mchip_delay(MCHIP_HIC_CMD, 0);
716}
717
718/* compress one frame into a buffer */
719static int mchip_compress_frame(u8 *buf, int bufsize)
720{
721 u32 v;
722 int len = -1, i;
723
724 mchip_vrj_setup(0x3f);
725 udelay(50);
726
727 mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_COMP);
728 mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
729
730 mchip_delay(MCHIP_HIC_CMD, 0);
731 for (i = 0; i < 100; ++i) {
732 if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
733 break;
734 msleep(1);
735 }
736
737 for (i = 0; i < 4; ++i) {
738 v = mchip_get_frame();
739 if (v & MCHIP_MM_FIR_RDY) {
740 len = mchip_comp_read_frame(v, buf, bufsize);
741 break;
742 }
743 mchip_free_frame();
744 }
745 return len;
746}
747
748#if 0
749/* uncompress one image into a buffer */
750static int mchip_uncompress_frame(u8 *img, int imgsize, u8 *buf, int bufsize)
751{
752 mchip_vrj_setup(0x3f);
753 udelay(50);
754
755 mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_DECOMP);
756 mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
757
758 mchip_delay(MCHIP_HIC_CMD, 0);
759
760 return mchip_comp_read_frame(buf, bufsize);
761}
762#endif
763
764/* start continuous compressed capture */
765static void mchip_cont_compression_start(void)
766{
767 mchip_hic_stop();
768 mchip_vrj_setup(0x3f);
769 mchip_subsample();
770 mchip_set_framerate();
771 mchip_dma_setup(meye.mchip_dmahandle);
772
773 meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
774
775 mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_CONT_COMP);
776 mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
777
778 mchip_delay(MCHIP_HIC_CMD, 0);
779}
780
781/****************************************************************************/
782/* Interrupt handling */
783/****************************************************************************/
784
7d12e780 785static irqreturn_t meye_irq(int irq, void *dev_id)
1da177e4
LT
786{
787 u32 v;
788 int reqnr;
ff699e6b 789 static int sequence;
1da177e4
LT
790
791 v = mchip_read(MCHIP_MM_INTA);
792
793 if (meye.mchip_mode != MCHIP_HIC_MODE_CONT_OUT &&
794 meye.mchip_mode != MCHIP_HIC_MODE_CONT_COMP)
795 return IRQ_NONE;
796
797again:
798 v = mchip_get_frame();
799 if (!(v & MCHIP_MM_FIR_RDY))
800 return IRQ_HANDLED;
801
802 if (meye.mchip_mode == MCHIP_HIC_MODE_CONT_OUT) {
7acd72eb 803 if (kfifo_out_locked(&meye.grabq, (unsigned char *)&reqnr,
c1e13f25 804 sizeof(int), &meye.grabq_lock) != sizeof(int)) {
1da177e4
LT
805 mchip_free_frame();
806 return IRQ_HANDLED;
807 }
808 mchip_cont_read_frame(v, meye.grab_fbuffer + gbufsize * reqnr,
809 mchip_hsize() * mchip_vsize() * 2);
810 meye.grab_buffer[reqnr].size = mchip_hsize() * mchip_vsize() * 2;
811 meye.grab_buffer[reqnr].state = MEYE_BUF_DONE;
8e6057b5 812 v4l2_get_timestamp(&meye.grab_buffer[reqnr].timestamp);
1da177e4 813 meye.grab_buffer[reqnr].sequence = sequence++;
7acd72eb 814 kfifo_in_locked(&meye.doneq, (unsigned char *)&reqnr,
c1e13f25 815 sizeof(int), &meye.doneq_lock);
1da177e4
LT
816 wake_up_interruptible(&meye.proc_list);
817 } else {
818 int size;
819 size = mchip_comp_read_frame(v, meye.grab_temp, gbufsize);
820 if (size == -1) {
821 mchip_free_frame();
822 goto again;
823 }
7acd72eb 824 if (kfifo_out_locked(&meye.grabq, (unsigned char *)&reqnr,
c1e13f25 825 sizeof(int), &meye.grabq_lock) != sizeof(int)) {
1da177e4
LT
826 mchip_free_frame();
827 goto again;
828 }
829 memcpy(meye.grab_fbuffer + gbufsize * reqnr, meye.grab_temp,
830 size);
831 meye.grab_buffer[reqnr].size = size;
832 meye.grab_buffer[reqnr].state = MEYE_BUF_DONE;
8e6057b5 833 v4l2_get_timestamp(&meye.grab_buffer[reqnr].timestamp);
1da177e4 834 meye.grab_buffer[reqnr].sequence = sequence++;
7acd72eb 835 kfifo_in_locked(&meye.doneq, (unsigned char *)&reqnr,
c1e13f25 836 sizeof(int), &meye.doneq_lock);
1da177e4
LT
837 wake_up_interruptible(&meye.proc_list);
838 }
839 mchip_free_frame();
840 goto again;
841}
842
843/****************************************************************************/
844/* video4linux integration */
845/****************************************************************************/
846
bec43661 847static int meye_open(struct file *file)
1da177e4 848{
7d43cd53 849 int i;
1da177e4 850
7d43cd53
HV
851 if (test_and_set_bit(0, &meye.in_use))
852 return -EBUSY;
1da177e4
LT
853
854 mchip_hic_stop();
855
856 if (mchip_dma_alloc()) {
857 printk(KERN_ERR "meye: mchip framebuffer allocation failed\n");
7d43cd53 858 clear_bit(0, &meye.in_use);
1da177e4
LT
859 return -ENOBUFS;
860 }
861
862 for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
863 meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
45465487
SS
864 kfifo_reset(&meye.grabq);
865 kfifo_reset(&meye.doneq);
ed986d1f 866 return v4l2_fh_open(file);
1da177e4
LT
867}
868
bec43661 869static int meye_release(struct file *file)
1da177e4
LT
870{
871 mchip_hic_stop();
872 mchip_dma_free();
7d43cd53 873 clear_bit(0, &meye.in_use);
ed986d1f 874 return v4l2_fh_release(file);
1da177e4
LT
875}
876
6ec6e0ce 877static int meyeioc_g_params(struct meye_params *p)
1da177e4 878{
6ec6e0ce
DSL
879 *p = meye.params;
880 return 0;
881}
1da177e4 882
6ec6e0ce
DSL
883static int meyeioc_s_params(struct meye_params *jp)
884{
885 if (jp->subsample > 1)
886 return -EINVAL;
1da177e4 887
6ec6e0ce
DSL
888 if (jp->quality > 10)
889 return -EINVAL;
1da177e4 890
6ec6e0ce
DSL
891 if (jp->sharpness > 63 || jp->agc > 63 || jp->picture > 63)
892 return -EINVAL;
1da177e4 893
6ec6e0ce
DSL
894 if (jp->framerate > 31)
895 return -EINVAL;
1da177e4 896
6ec6e0ce 897 mutex_lock(&meye.lock);
1da177e4 898
6ec6e0ce
DSL
899 if (meye.params.subsample != jp->subsample ||
900 meye.params.quality != jp->quality)
901 mchip_hic_stop(); /* need restart */
902
903 meye.params = *jp;
904 sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERASHARPNESS,
905 meye.params.sharpness);
906 sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAAGC,
907 meye.params.agc);
908 sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAPICTURE,
909 meye.params.picture);
910 mutex_unlock(&meye.lock);
1da177e4 911
6ec6e0ce
DSL
912 return 0;
913}
1da177e4 914
6ec6e0ce
DSL
915static int meyeioc_qbuf_capt(int *nb)
916{
917 if (!meye.grab_fbuffer)
918 return -EINVAL;
1da177e4 919
6ec6e0ce
DSL
920 if (*nb >= gbuffers)
921 return -EINVAL;
1da177e4 922
6ec6e0ce
DSL
923 if (*nb < 0) {
924 /* stop capture */
925 mchip_hic_stop();
926 return 0;
1da177e4
LT
927 }
928
6ec6e0ce
DSL
929 if (meye.grab_buffer[*nb].state != MEYE_BUF_UNUSED)
930 return -EBUSY;
1da177e4 931
6ec6e0ce 932 mutex_lock(&meye.lock);
1da177e4 933
6ec6e0ce
DSL
934 if (meye.mchip_mode != MCHIP_HIC_MODE_CONT_COMP)
935 mchip_cont_compression_start();
1da177e4 936
6ec6e0ce 937 meye.grab_buffer[*nb].state = MEYE_BUF_USING;
7acd72eb 938 kfifo_in_locked(&meye.grabq, (unsigned char *)nb, sizeof(int),
c1e13f25 939 &meye.grabq_lock);
6ec6e0ce 940 mutex_unlock(&meye.lock);
1da177e4 941
6ec6e0ce
DSL
942 return 0;
943}
1da177e4 944
6ec6e0ce
DSL
945static int meyeioc_sync(struct file *file, void *fh, int *i)
946{
947 int unused;
1da177e4 948
6ec6e0ce
DSL
949 if (*i < 0 || *i >= gbuffers)
950 return -EINVAL;
951
952 mutex_lock(&meye.lock);
953 switch (meye.grab_buffer[*i].state) {
954
955 case MEYE_BUF_UNUSED:
3593cab5 956 mutex_unlock(&meye.lock);
6ec6e0ce
DSL
957 return -EINVAL;
958 case MEYE_BUF_USING:
959 if (file->f_flags & O_NONBLOCK) {
960 mutex_unlock(&meye.lock);
961 return -EAGAIN;
962 }
963 if (wait_event_interruptible(meye.proc_list,
964 (meye.grab_buffer[*i].state != MEYE_BUF_USING))) {
965 mutex_unlock(&meye.lock);
966 return -EINTR;
967 }
968 /* fall through */
969 case MEYE_BUF_DONE:
970 meye.grab_buffer[*i].state = MEYE_BUF_UNUSED;
9842c38e
SS
971 if (kfifo_out_locked(&meye.doneq, (unsigned char *)&unused,
972 sizeof(int), &meye.doneq_lock) != sizeof(int))
973 break;
1da177e4 974 }
6ec6e0ce
DSL
975 *i = meye.grab_buffer[*i].size;
976 mutex_unlock(&meye.lock);
977 return 0;
978}
979
980static int meyeioc_stillcapt(void)
981{
982 if (!meye.grab_fbuffer)
983 return -EINVAL;
1da177e4 984
6ec6e0ce
DSL
985 if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
986 return -EBUSY;
1da177e4 987
6ec6e0ce
DSL
988 mutex_lock(&meye.lock);
989 meye.grab_buffer[0].state = MEYE_BUF_USING;
990 mchip_take_picture();
1da177e4 991
6ec6e0ce
DSL
992 mchip_get_picture(meye.grab_fbuffer,
993 mchip_hsize() * mchip_vsize() * 2);
1da177e4 994
6ec6e0ce
DSL
995 meye.grab_buffer[0].state = MEYE_BUF_DONE;
996 mutex_unlock(&meye.lock);
997
998 return 0;
999}
1000
1001static int meyeioc_stilljcapt(int *len)
1002{
1003 if (!meye.grab_fbuffer)
1004 return -EINVAL;
1005
1006 if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
1007 return -EBUSY;
1da177e4 1008
6ec6e0ce
DSL
1009 mutex_lock(&meye.lock);
1010 meye.grab_buffer[0].state = MEYE_BUF_USING;
1011 *len = -1;
1da177e4 1012
6ec6e0ce 1013 while (*len == -1) {
1da177e4 1014 mchip_take_picture();
6ec6e0ce 1015 *len = mchip_compress_frame(meye.grab_fbuffer, gbufsize);
1da177e4
LT
1016 }
1017
6ec6e0ce
DSL
1018 meye.grab_buffer[0].state = MEYE_BUF_DONE;
1019 mutex_unlock(&meye.lock);
1020 return 0;
1021}
1da177e4 1022
6ec6e0ce
DSL
1023static int vidioc_querycap(struct file *file, void *fh,
1024 struct v4l2_capability *cap)
1025{
6ec6e0ce
DSL
1026 strcpy(cap->driver, "meye");
1027 strcpy(cap->card, "meye");
1028 sprintf(cap->bus_info, "PCI:%s", pci_name(meye.mchip_dev));
1da177e4 1029
ed986d1f 1030 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
6ec6e0ce 1031 V4L2_CAP_STREAMING;
ed986d1f 1032 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
6ec6e0ce
DSL
1033
1034 return 0;
1035}
1da177e4 1036
6ec6e0ce
DSL
1037static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
1038{
1039 if (i->index != 0)
1040 return -EINVAL;
1041
6ec6e0ce
DSL
1042 strcpy(i->name, "Camera");
1043 i->type = V4L2_INPUT_TYPE_CAMERA;
1044
1045 return 0;
1046}
1047
1048static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1049{
1050 *i = 0;
1051 return 0;
1052}
1053
1054static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
1055{
1056 if (i != 0)
1057 return -EINVAL;
1da177e4 1058
6ec6e0ce
DSL
1059 return 0;
1060}
1061
ed986d1f 1062static int meye_s_ctrl(struct v4l2_ctrl *ctrl)
6ec6e0ce
DSL
1063{
1064 mutex_lock(&meye.lock);
ed986d1f 1065 switch (ctrl->id) {
6ec6e0ce
DSL
1066 case V4L2_CID_BRIGHTNESS:
1067 sony_pic_camera_command(
ed986d1f
HV
1068 SONY_PIC_COMMAND_SETCAMERABRIGHTNESS, ctrl->val);
1069 meye.brightness = ctrl->val << 10;
6ec6e0ce
DSL
1070 break;
1071 case V4L2_CID_HUE:
1072 sony_pic_camera_command(
ed986d1f
HV
1073 SONY_PIC_COMMAND_SETCAMERAHUE, ctrl->val);
1074 meye.hue = ctrl->val << 10;
6ec6e0ce
DSL
1075 break;
1076 case V4L2_CID_CONTRAST:
1077 sony_pic_camera_command(
ed986d1f
HV
1078 SONY_PIC_COMMAND_SETCAMERACONTRAST, ctrl->val);
1079 meye.contrast = ctrl->val << 10;
6ec6e0ce
DSL
1080 break;
1081 case V4L2_CID_SATURATION:
1082 sony_pic_camera_command(
ed986d1f
HV
1083 SONY_PIC_COMMAND_SETCAMERACOLOR, ctrl->val);
1084 meye.colour = ctrl->val << 10;
6ec6e0ce 1085 break;
ed986d1f 1086 case V4L2_CID_MEYE_AGC:
6ec6e0ce 1087 sony_pic_camera_command(
ed986d1f
HV
1088 SONY_PIC_COMMAND_SETCAMERAAGC, ctrl->val);
1089 meye.params.agc = ctrl->val;
6ec6e0ce
DSL
1090 break;
1091 case V4L2_CID_SHARPNESS:
6ec6e0ce 1092 sony_pic_camera_command(
ed986d1f
HV
1093 SONY_PIC_COMMAND_SETCAMERASHARPNESS, ctrl->val);
1094 meye.params.sharpness = ctrl->val;
6ec6e0ce 1095 break;
ed986d1f 1096 case V4L2_CID_MEYE_PICTURE:
6ec6e0ce 1097 sony_pic_camera_command(
ed986d1f
HV
1098 SONY_PIC_COMMAND_SETCAMERAPICTURE, ctrl->val);
1099 meye.params.picture = ctrl->val;
6ec6e0ce 1100 break;
ed986d1f
HV
1101 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1102 meye.params.quality = ctrl->val;
1da177e4 1103 break;
ed986d1f
HV
1104 case V4L2_CID_MEYE_FRAMERATE:
1105 meye.params.framerate = ctrl->val;
6ec6e0ce
DSL
1106 break;
1107 default:
1108 mutex_unlock(&meye.lock);
1109 return -EINVAL;
1da177e4 1110 }
6ec6e0ce
DSL
1111 mutex_unlock(&meye.lock);
1112
1113 return 0;
1114}
1da177e4 1115
78b526a4 1116static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
6ec6e0ce
DSL
1117 struct v4l2_fmtdesc *f)
1118{
1119 if (f->index > 1)
1120 return -EINVAL;
1da177e4 1121
6ec6e0ce
DSL
1122 if (f->index == 0) {
1123 /* standard YUV 422 capture */
6ec6e0ce
DSL
1124 f->flags = 0;
1125 strcpy(f->description, "YUV422");
1126 f->pixelformat = V4L2_PIX_FMT_YUYV;
1127 } else {
1128 /* compressed MJPEG capture */
6ec6e0ce
DSL
1129 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1130 strcpy(f->description, "MJPEG");
1131 f->pixelformat = V4L2_PIX_FMT_MJPEG;
1da177e4
LT
1132 }
1133
6ec6e0ce
DSL
1134 return 0;
1135}
1da177e4 1136
78b526a4 1137static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
6ec6e0ce
DSL
1138 struct v4l2_format *f)
1139{
6ec6e0ce
DSL
1140 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
1141 f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
1142 return -EINVAL;
1143
1144 if (f->fmt.pix.field != V4L2_FIELD_ANY &&
1145 f->fmt.pix.field != V4L2_FIELD_NONE)
1146 return -EINVAL;
1147
1148 f->fmt.pix.field = V4L2_FIELD_NONE;
1149
1150 if (f->fmt.pix.width <= 320) {
1151 f->fmt.pix.width = 320;
1152 f->fmt.pix.height = 240;
1153 } else {
1154 f->fmt.pix.width = 640;
1155 f->fmt.pix.height = 480;
1da177e4
LT
1156 }
1157
6ec6e0ce
DSL
1158 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
1159 f->fmt.pix.sizeimage = f->fmt.pix.height *
1160 f->fmt.pix.bytesperline;
1161 f->fmt.pix.colorspace = 0;
1da177e4 1162
6ec6e0ce
DSL
1163 return 0;
1164}
1165
78b526a4
HV
1166static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
1167 struct v4l2_format *f)
6ec6e0ce 1168{
6ec6e0ce
DSL
1169 switch (meye.mchip_mode) {
1170 case MCHIP_HIC_MODE_CONT_OUT:
1171 default:
1172 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1173 break;
1174 case MCHIP_HIC_MODE_CONT_COMP:
1175 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1da177e4
LT
1176 break;
1177 }
1178
6ec6e0ce
DSL
1179 f->fmt.pix.field = V4L2_FIELD_NONE;
1180 f->fmt.pix.width = mchip_hsize();
1181 f->fmt.pix.height = mchip_vsize();
1182 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
1183 f->fmt.pix.sizeimage = f->fmt.pix.height *
1184 f->fmt.pix.bytesperline;
6ec6e0ce
DSL
1185
1186 return 0;
1187}
1188
78b526a4
HV
1189static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
1190 struct v4l2_format *f)
6ec6e0ce 1191{
6ec6e0ce
DSL
1192 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
1193 f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
1194 return -EINVAL;
1195
1196 if (f->fmt.pix.field != V4L2_FIELD_ANY &&
1197 f->fmt.pix.field != V4L2_FIELD_NONE)
1198 return -EINVAL;
1199
1200 f->fmt.pix.field = V4L2_FIELD_NONE;
1201 mutex_lock(&meye.lock);
1202
1203 if (f->fmt.pix.width <= 320) {
1204 f->fmt.pix.width = 320;
1205 f->fmt.pix.height = 240;
1206 meye.params.subsample = 1;
1207 } else {
1208 f->fmt.pix.width = 640;
1209 f->fmt.pix.height = 480;
1210 meye.params.subsample = 0;
1da177e4
LT
1211 }
1212
6ec6e0ce
DSL
1213 switch (f->fmt.pix.pixelformat) {
1214 case V4L2_PIX_FMT_YUYV:
1215 meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
1216 break;
1217 case V4L2_PIX_FMT_MJPEG:
1218 meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
1da177e4
LT
1219 break;
1220 }
1221
6ec6e0ce
DSL
1222 mutex_unlock(&meye.lock);
1223 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
1224 f->fmt.pix.sizeimage = f->fmt.pix.height *
1225 f->fmt.pix.bytesperline;
1226 f->fmt.pix.colorspace = 0;
1da177e4 1227
6ec6e0ce
DSL
1228 return 0;
1229}
1da177e4 1230
6ec6e0ce
DSL
1231static int vidioc_reqbufs(struct file *file, void *fh,
1232 struct v4l2_requestbuffers *req)
1233{
1234 int i;
1da177e4 1235
6ec6e0ce
DSL
1236 if (req->memory != V4L2_MEMORY_MMAP)
1237 return -EINVAL;
1da177e4 1238
6ec6e0ce
DSL
1239 if (meye.grab_fbuffer && req->count == gbuffers) {
1240 /* already allocated, no modifications */
1241 return 0;
1da177e4
LT
1242 }
1243
6ec6e0ce
DSL
1244 mutex_lock(&meye.lock);
1245 if (meye.grab_fbuffer) {
1246 for (i = 0; i < gbuffers; i++)
1247 if (meye.vma_use_count[i]) {
1248 mutex_unlock(&meye.lock);
1249 return -EINVAL;
1250 }
1251 rvfree(meye.grab_fbuffer, gbuffers * gbufsize);
1252 meye.grab_fbuffer = NULL;
1da177e4
LT
1253 }
1254
6ec6e0ce
DSL
1255 gbuffers = max(2, min((int)req->count, MEYE_MAX_BUFNBRS));
1256 req->count = gbuffers;
1257 meye.grab_fbuffer = rvmalloc(gbuffers * gbufsize);
1258
1259 if (!meye.grab_fbuffer) {
f8a3dcb5 1260 printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
3593cab5 1261 mutex_unlock(&meye.lock);
6ec6e0ce 1262 return -ENOMEM;
1da177e4
LT
1263 }
1264
6ec6e0ce
DSL
1265 for (i = 0; i < gbuffers; i++)
1266 meye.vma_use_count[i] = 0;
1da177e4 1267
6ec6e0ce 1268 mutex_unlock(&meye.lock);
1da177e4 1269
6ec6e0ce
DSL
1270 return 0;
1271}
1272
1273static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1274{
6174523c 1275 unsigned int index = buf->index;
6ec6e0ce 1276
6174523c 1277 if (index >= gbuffers)
6ec6e0ce
DSL
1278 return -EINVAL;
1279
6ec6e0ce 1280 buf->bytesused = meye.grab_buffer[index].size;
1b18e7a0 1281 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
6ec6e0ce
DSL
1282
1283 if (meye.grab_buffer[index].state == MEYE_BUF_USING)
1284 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1285
1286 if (meye.grab_buffer[index].state == MEYE_BUF_DONE)
1287 buf->flags |= V4L2_BUF_FLAG_DONE;
1288
1289 buf->field = V4L2_FIELD_NONE;
1290 buf->timestamp = meye.grab_buffer[index].timestamp;
1291 buf->sequence = meye.grab_buffer[index].sequence;
1292 buf->memory = V4L2_MEMORY_MMAP;
1293 buf->m.offset = index * gbufsize;
1294 buf->length = gbufsize;
1295
1296 return 0;
1297}
1298
1299static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1300{
6ec6e0ce
DSL
1301 if (buf->memory != V4L2_MEMORY_MMAP)
1302 return -EINVAL;
1303
6174523c 1304 if (buf->index >= gbuffers)
6ec6e0ce
DSL
1305 return -EINVAL;
1306
1307 if (meye.grab_buffer[buf->index].state != MEYE_BUF_UNUSED)
1308 return -EINVAL;
1309
1310 mutex_lock(&meye.lock);
1311 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1312 buf->flags &= ~V4L2_BUF_FLAG_DONE;
1313 meye.grab_buffer[buf->index].state = MEYE_BUF_USING;
7acd72eb 1314 kfifo_in_locked(&meye.grabq, (unsigned char *)&buf->index,
c1e13f25 1315 sizeof(int), &meye.grabq_lock);
6ec6e0ce
DSL
1316 mutex_unlock(&meye.lock);
1317
1318 return 0;
1319}
1320
1321static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1322{
1323 int reqnr;
1324
6ec6e0ce
DSL
1325 if (buf->memory != V4L2_MEMORY_MMAP)
1326 return -EINVAL;
1327
1328 mutex_lock(&meye.lock);
1329
45465487 1330 if (kfifo_len(&meye.doneq) == 0 && file->f_flags & O_NONBLOCK) {
3593cab5 1331 mutex_unlock(&meye.lock);
6ec6e0ce 1332 return -EAGAIN;
1da177e4
LT
1333 }
1334
6ec6e0ce 1335 if (wait_event_interruptible(meye.proc_list,
45465487 1336 kfifo_len(&meye.doneq) != 0) < 0) {
3593cab5 1337 mutex_unlock(&meye.lock);
6ec6e0ce 1338 return -EINTR;
1da177e4
LT
1339 }
1340
7acd72eb 1341 if (!kfifo_out_locked(&meye.doneq, (unsigned char *)&reqnr,
c1e13f25 1342 sizeof(int), &meye.doneq_lock)) {
6ec6e0ce
DSL
1343 mutex_unlock(&meye.lock);
1344 return -EBUSY;
1345 }
1da177e4 1346
6ec6e0ce 1347 if (meye.grab_buffer[reqnr].state != MEYE_BUF_DONE) {
3593cab5 1348 mutex_unlock(&meye.lock);
6ec6e0ce 1349 return -EINVAL;
1da177e4
LT
1350 }
1351
6ec6e0ce
DSL
1352 buf->index = reqnr;
1353 buf->bytesused = meye.grab_buffer[reqnr].size;
1b18e7a0 1354 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
6ec6e0ce
DSL
1355 buf->field = V4L2_FIELD_NONE;
1356 buf->timestamp = meye.grab_buffer[reqnr].timestamp;
1357 buf->sequence = meye.grab_buffer[reqnr].sequence;
1358 buf->memory = V4L2_MEMORY_MMAP;
1359 buf->m.offset = reqnr * gbufsize;
1360 buf->length = gbufsize;
1361 meye.grab_buffer[reqnr].state = MEYE_BUF_UNUSED;
1362 mutex_unlock(&meye.lock);
1363
1364 return 0;
1365}
1da177e4 1366
6ec6e0ce
DSL
1367static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1368{
1369 mutex_lock(&meye.lock);
1370
1371 switch (meye.mchip_mode) {
1372 case MCHIP_HIC_MODE_CONT_OUT:
1373 mchip_continuous_start();
1374 break;
1375 case MCHIP_HIC_MODE_CONT_COMP:
1376 mchip_cont_compression_start();
1377 break;
1da177e4 1378 default:
6ec6e0ce
DSL
1379 mutex_unlock(&meye.lock);
1380 return -EINVAL;
1da177e4
LT
1381 }
1382
6ec6e0ce
DSL
1383 mutex_unlock(&meye.lock);
1384
1da177e4
LT
1385 return 0;
1386}
1387
6ec6e0ce 1388static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1da177e4 1389{
6ec6e0ce
DSL
1390 mutex_lock(&meye.lock);
1391 mchip_hic_stop();
45465487
SS
1392 kfifo_reset(&meye.grabq);
1393 kfifo_reset(&meye.doneq);
6ec6e0ce
DSL
1394
1395 for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
1396 meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
1397
1398 mutex_unlock(&meye.lock);
1399 return 0;
1400}
1401
99cd47bc 1402static long vidioc_default(struct file *file, void *fh, bool valid_prio,
6d43be77 1403 unsigned int cmd, void *arg)
6ec6e0ce
DSL
1404{
1405 switch (cmd) {
1406 case MEYEIOC_G_PARAMS:
1407 return meyeioc_g_params((struct meye_params *) arg);
1408
1409 case MEYEIOC_S_PARAMS:
1410 return meyeioc_s_params((struct meye_params *) arg);
1411
1412 case MEYEIOC_QBUF_CAPT:
1413 return meyeioc_qbuf_capt((int *) arg);
1414
1415 case MEYEIOC_SYNC:
1416 return meyeioc_sync(file, fh, (int *) arg);
1417
1418 case MEYEIOC_STILLCAPT:
1419 return meyeioc_stillcapt();
1420
1421 case MEYEIOC_STILLJCAPT:
1422 return meyeioc_stilljcapt((int *) arg);
1423
1424 default:
d1c754a9 1425 return -ENOTTY;
6ec6e0ce
DSL
1426 }
1427
1da177e4
LT
1428}
1429
1430static unsigned int meye_poll(struct file *file, poll_table *wait)
1431{
ed986d1f 1432 unsigned int res = v4l2_ctrl_poll(file, wait);
1da177e4 1433
3593cab5 1434 mutex_lock(&meye.lock);
1da177e4 1435 poll_wait(file, &meye.proc_list, wait);
45465487 1436 if (kfifo_len(&meye.doneq))
ed986d1f 1437 res |= POLLIN | POLLRDNORM;
3593cab5 1438 mutex_unlock(&meye.lock);
1da177e4
LT
1439 return res;
1440}
1441
1442static void meye_vm_open(struct vm_area_struct *vma)
1443{
d6144028 1444 long idx = (long)vma->vm_private_data;
1da177e4
LT
1445 meye.vma_use_count[idx]++;
1446}
1447
1448static void meye_vm_close(struct vm_area_struct *vma)
1449{
d6144028 1450 long idx = (long)vma->vm_private_data;
1da177e4
LT
1451 meye.vma_use_count[idx]--;
1452}
1453
f0f37e2f 1454static const struct vm_operations_struct meye_vm_ops = {
1da177e4
LT
1455 .open = meye_vm_open,
1456 .close = meye_vm_close,
1457};
1458
1459static int meye_mmap(struct file *file, struct vm_area_struct *vma)
1460{
1461 unsigned long start = vma->vm_start;
1462 unsigned long size = vma->vm_end - vma->vm_start;
1463 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1464 unsigned long page, pos;
1465
3593cab5 1466 mutex_lock(&meye.lock);
1da177e4 1467 if (size > gbuffers * gbufsize) {
3593cab5 1468 mutex_unlock(&meye.lock);
1da177e4
LT
1469 return -EINVAL;
1470 }
1471 if (!meye.grab_fbuffer) {
1472 int i;
1473
1474 /* lazy allocation */
1475 meye.grab_fbuffer = rvmalloc(gbuffers*gbufsize);
1476 if (!meye.grab_fbuffer) {
1477 printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
3593cab5 1478 mutex_unlock(&meye.lock);
1da177e4
LT
1479 return -ENOMEM;
1480 }
1481 for (i = 0; i < gbuffers; i++)
1482 meye.vma_use_count[i] = 0;
1483 }
1484 pos = (unsigned long)meye.grab_fbuffer + offset;
1485
1486 while (size > 0) {
1487 page = vmalloc_to_pfn((void *)pos);
1488 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
3593cab5 1489 mutex_unlock(&meye.lock);
1da177e4
LT
1490 return -EAGAIN;
1491 }
1492 start += PAGE_SIZE;
1493 pos += PAGE_SIZE;
1494 if (size > PAGE_SIZE)
1495 size -= PAGE_SIZE;
1496 else
1497 size = 0;
1498 }
1499
1500 vma->vm_ops = &meye_vm_ops;
1501 vma->vm_flags &= ~VM_IO; /* not I/O memory */
314e51b9 1502 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1da177e4
LT
1503 vma->vm_private_data = (void *) (offset / gbufsize);
1504 meye_vm_open(vma);
1505
3593cab5 1506 mutex_unlock(&meye.lock);
1da177e4
LT
1507 return 0;
1508}
1509
bec43661 1510static const struct v4l2_file_operations meye_fops = {
1da177e4
LT
1511 .owner = THIS_MODULE,
1512 .open = meye_open,
1513 .release = meye_release,
1514 .mmap = meye_mmap,
61df3c9b 1515 .unlocked_ioctl = video_ioctl2,
1da177e4 1516 .poll = meye_poll,
1da177e4
LT
1517};
1518
a399810c 1519static const struct v4l2_ioctl_ops meye_ioctl_ops = {
6ec6e0ce
DSL
1520 .vidioc_querycap = vidioc_querycap,
1521 .vidioc_enum_input = vidioc_enum_input,
1522 .vidioc_g_input = vidioc_g_input,
1523 .vidioc_s_input = vidioc_s_input,
78b526a4
HV
1524 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1525 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1526 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1527 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
6ec6e0ce
DSL
1528 .vidioc_reqbufs = vidioc_reqbufs,
1529 .vidioc_querybuf = vidioc_querybuf,
1530 .vidioc_qbuf = vidioc_qbuf,
1531 .vidioc_dqbuf = vidioc_dqbuf,
1532 .vidioc_streamon = vidioc_streamon,
1533 .vidioc_streamoff = vidioc_streamoff,
ed986d1f
HV
1534 .vidioc_log_status = v4l2_ctrl_log_status,
1535 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1536 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
6ec6e0ce 1537 .vidioc_default = vidioc_default,
1da177e4
LT
1538};
1539
a399810c 1540static struct video_device meye_template = {
a399810c 1541 .name = "meye",
a399810c
HV
1542 .fops = &meye_fops,
1543 .ioctl_ops = &meye_ioctl_ops,
e239129c 1544 .release = video_device_release_empty,
a399810c
HV
1545};
1546
ed986d1f
HV
1547static const struct v4l2_ctrl_ops meye_ctrl_ops = {
1548 .s_ctrl = meye_s_ctrl,
1549};
1550
1da177e4 1551#ifdef CONFIG_PM
a2910689 1552static int meye_suspend(struct pci_dev *pdev, pm_message_t state)
1da177e4
LT
1553{
1554 pci_save_state(pdev);
1555 meye.pm_mchip_mode = meye.mchip_mode;
1556 mchip_hic_stop();
1557 mchip_set(MCHIP_MM_INTA, 0x0);
1558 return 0;
1559}
1560
1561static int meye_resume(struct pci_dev *pdev)
1562{
1563 pci_restore_state(pdev);
1564 pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
1565
1566 mchip_delay(MCHIP_HIC_CMD, 0);
1567 mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
1568 msleep(1);
1569 mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
1570 msleep(1);
1571 mchip_set(MCHIP_MM_PCI_MODE, 5);
1572 msleep(1);
1573 mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
1574
1575 switch (meye.pm_mchip_mode) {
1576 case MCHIP_HIC_MODE_CONT_OUT:
1577 mchip_continuous_start();
1578 break;
1579 case MCHIP_HIC_MODE_CONT_COMP:
1580 mchip_cont_compression_start();
1581 break;
1582 }
1583 return 0;
1584}
1585#endif
1586
4c62e976 1587static int meye_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
1da177e4 1588{
ed986d1f
HV
1589 static const struct v4l2_ctrl_config ctrl_agc = {
1590 .id = V4L2_CID_MEYE_AGC,
1591 .type = V4L2_CTRL_TYPE_INTEGER,
1592 .ops = &meye_ctrl_ops,
1593 .name = "AGC",
1594 .max = 63,
1595 .step = 1,
1596 .def = 48,
1597 .flags = V4L2_CTRL_FLAG_SLIDER,
1598 };
1599 static const struct v4l2_ctrl_config ctrl_picture = {
1600 .id = V4L2_CID_MEYE_PICTURE,
1601 .type = V4L2_CTRL_TYPE_INTEGER,
1602 .ops = &meye_ctrl_ops,
1603 .name = "Picture",
1604 .max = 63,
1605 .step = 1,
1606 };
1607 static const struct v4l2_ctrl_config ctrl_framerate = {
1608 .id = V4L2_CID_MEYE_FRAMERATE,
1609 .type = V4L2_CTRL_TYPE_INTEGER,
1610 .ops = &meye_ctrl_ops,
1611 .name = "Framerate",
1612 .max = 31,
1613 .step = 1,
1614 };
51270617 1615 struct v4l2_device *v4l2_dev = &meye.v4l2_dev;
1da177e4
LT
1616 int ret = -EBUSY;
1617 unsigned long mchip_adr;
1da177e4
LT
1618
1619 if (meye.mchip_dev != NULL) {
1620 printk(KERN_ERR "meye: only one device allowed!\n");
e239129c 1621 return ret;
1da177e4
LT
1622 }
1623
51270617
HV
1624 ret = v4l2_device_register(&pcidev->dev, v4l2_dev);
1625 if (ret < 0) {
1626 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
1627 return ret;
1628 }
ef0e3c26 1629 ret = -ENOMEM;
1da177e4 1630 meye.mchip_dev = pcidev;
1da177e4 1631
1da177e4
LT
1632 meye.grab_temp = vmalloc(MCHIP_NB_PAGES_MJPEG * PAGE_SIZE);
1633 if (!meye.grab_temp) {
51270617 1634 v4l2_err(v4l2_dev, "grab buffer allocation failed\n");
1da177e4
LT
1635 goto outvmalloc;
1636 }
1637
1638 spin_lock_init(&meye.grabq_lock);
c1e13f25
SS
1639 if (kfifo_alloc(&meye.grabq, sizeof(int) * MEYE_MAX_BUFNBRS,
1640 GFP_KERNEL)) {
51270617 1641 v4l2_err(v4l2_dev, "fifo allocation failed\n");
1da177e4
LT
1642 goto outkfifoalloc1;
1643 }
1644 spin_lock_init(&meye.doneq_lock);
c1e13f25
SS
1645 if (kfifo_alloc(&meye.doneq, sizeof(int) * MEYE_MAX_BUFNBRS,
1646 GFP_KERNEL)) {
51270617 1647 v4l2_err(v4l2_dev, "fifo allocation failed\n");
1da177e4
LT
1648 goto outkfifoalloc2;
1649 }
1650
e239129c
HV
1651 meye.vdev = meye_template;
1652 meye.vdev.v4l2_dev = &meye.v4l2_dev;
1da177e4 1653
5b5aff83 1654 ret = -EIO;
cbefb762 1655 if ((ret = sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 1))) {
51270617 1656 v4l2_err(v4l2_dev, "meye: unable to power on the camera\n");
f8a3dcb5 1657 v4l2_err(v4l2_dev, "meye: did you enable the camera in sonypi using the module options ?\n");
1da177e4
LT
1658 goto outsonypienable;
1659 }
1660
1da177e4 1661 if ((ret = pci_enable_device(meye.mchip_dev))) {
51270617 1662 v4l2_err(v4l2_dev, "meye: pci_enable_device failed\n");
1da177e4
LT
1663 goto outenabledev;
1664 }
1665
1666 mchip_adr = pci_resource_start(meye.mchip_dev,0);
1667 if (!mchip_adr) {
51270617 1668 v4l2_err(v4l2_dev, "meye: mchip has no device base address\n");
1da177e4
LT
1669 goto outregions;
1670 }
1671 if (!request_mem_region(pci_resource_start(meye.mchip_dev, 0),
1672 pci_resource_len(meye.mchip_dev, 0),
1673 "meye")) {
51270617 1674 v4l2_err(v4l2_dev, "meye: request_mem_region failed\n");
1da177e4
LT
1675 goto outregions;
1676 }
1677 meye.mchip_mmregs = ioremap(mchip_adr, MCHIP_MM_REGS);
1678 if (!meye.mchip_mmregs) {
51270617 1679 v4l2_err(v4l2_dev, "meye: ioremap failed\n");
1da177e4
LT
1680 goto outremap;
1681 }
1682
1683 meye.mchip_irq = pcidev->irq;
1684 if (request_irq(meye.mchip_irq, meye_irq,
3e018fe4 1685 IRQF_SHARED, "meye", meye_irq)) {
51270617 1686 v4l2_err(v4l2_dev, "request_irq failed\n");
1da177e4
LT
1687 goto outreqirq;
1688 }
1689
1da177e4
LT
1690 pci_write_config_byte(meye.mchip_dev, PCI_CACHE_LINE_SIZE, 8);
1691 pci_write_config_byte(meye.mchip_dev, PCI_LATENCY_TIMER, 64);
1692
1693 pci_set_master(meye.mchip_dev);
1694
1695 /* Ask the camera to perform a soft reset. */
1696 pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
1697
1698 mchip_delay(MCHIP_HIC_CMD, 0);
1699 mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
1700
1701 msleep(1);
1702 mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
1703
1704 msleep(1);
1705 mchip_set(MCHIP_MM_PCI_MODE, 5);
1706
1707 msleep(1);
1708 mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
1709
3593cab5 1710 mutex_init(&meye.lock);
1da177e4 1711 init_waitqueue_head(&meye.proc_list);
ed986d1f
HV
1712
1713 v4l2_ctrl_handler_init(&meye.hdl, 3);
1714 v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
1715 V4L2_CID_BRIGHTNESS, 0, 63, 1, 32);
1716 v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
1717 V4L2_CID_HUE, 0, 63, 1, 32);
1718 v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
1719 V4L2_CID_CONTRAST, 0, 63, 1, 32);
1720 v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
1721 V4L2_CID_SATURATION, 0, 63, 1, 32);
1722 v4l2_ctrl_new_custom(&meye.hdl, &ctrl_agc, NULL);
1723 v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
1724 V4L2_CID_SHARPNESS, 0, 63, 1, 32);
1725 v4l2_ctrl_new_custom(&meye.hdl, &ctrl_picture, NULL);
1726 v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
1727 V4L2_CID_JPEG_COMPRESSION_QUALITY, 0, 10, 1, 8);
1728 v4l2_ctrl_new_custom(&meye.hdl, &ctrl_framerate, NULL);
1729 if (meye.hdl.error) {
1730 v4l2_err(v4l2_dev, "couldn't register controls\n");
1731 goto outvideoreg;
1732 }
1733
1734 v4l2_ctrl_handler_setup(&meye.hdl);
e239129c 1735 meye.vdev.ctrl_handler = &meye.hdl;
1da177e4 1736
e239129c 1737 if (video_register_device(&meye.vdev, VFL_TYPE_GRABBER,
61df3c9b
HV
1738 video_nr) < 0) {
1739 v4l2_err(v4l2_dev, "video_register_device failed\n");
1740 goto outvideoreg;
1741 }
1742
51270617 1743 v4l2_info(v4l2_dev, "Motion Eye Camera Driver v%s.\n",
1da177e4 1744 MEYE_DRIVER_VERSION);
51270617 1745 v4l2_info(v4l2_dev, "mchip KL5A72002 rev. %d, base %lx, irq %d\n",
44c10138 1746 meye.mchip_dev->revision, mchip_adr, meye.mchip_irq);
1da177e4
LT
1747
1748 return 0;
1749
1750outvideoreg:
ed986d1f 1751 v4l2_ctrl_handler_free(&meye.hdl);
1da177e4
LT
1752 free_irq(meye.mchip_irq, meye_irq);
1753outreqirq:
1754 iounmap(meye.mchip_mmregs);
1755outremap:
1756 release_mem_region(pci_resource_start(meye.mchip_dev, 0),
1757 pci_resource_len(meye.mchip_dev, 0));
1758outregions:
1759 pci_disable_device(meye.mchip_dev);
1760outenabledev:
cbefb762 1761 sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
1da177e4 1762outsonypienable:
45465487 1763 kfifo_free(&meye.doneq);
1da177e4 1764outkfifoalloc2:
45465487 1765 kfifo_free(&meye.grabq);
1da177e4
LT
1766outkfifoalloc1:
1767 vfree(meye.grab_temp);
1768outvmalloc:
1da177e4
LT
1769 return ret;
1770}
1771
4c62e976 1772static void meye_remove(struct pci_dev *pcidev)
1da177e4 1773{
e239129c 1774 video_unregister_device(&meye.vdev);
1da177e4
LT
1775
1776 mchip_hic_stop();
1777
1778 mchip_dma_free();
1779
1780 /* disable interrupts */
1781 mchip_set(MCHIP_MM_INTA, 0x0);
1782
1783 free_irq(meye.mchip_irq, meye_irq);
1784
1785 iounmap(meye.mchip_mmregs);
1786
1787 release_mem_region(pci_resource_start(meye.mchip_dev, 0),
1788 pci_resource_len(meye.mchip_dev, 0));
1789
1790 pci_disable_device(meye.mchip_dev);
1791
cbefb762 1792 sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
1da177e4 1793
45465487
SS
1794 kfifo_free(&meye.doneq);
1795 kfifo_free(&meye.grabq);
1da177e4
LT
1796
1797 vfree(meye.grab_temp);
1798
1799 if (meye.grab_fbuffer) {
1800 rvfree(meye.grab_fbuffer, gbuffers*gbufsize);
1801 meye.grab_fbuffer = NULL;
1802 }
1803
1804 printk(KERN_INFO "meye: removed\n");
1805}
1806
1807static struct pci_device_id meye_pci_tbl[] = {
76e9741d 1808 { PCI_VDEVICE(KAWASAKI, PCI_DEVICE_ID_MCHIP_KL5A72002), 0 },
1da177e4
LT
1809 { }
1810};
1811
1812MODULE_DEVICE_TABLE(pci, meye_pci_tbl);
1813
1814static struct pci_driver meye_driver = {
1815 .name = "meye",
1816 .id_table = meye_pci_tbl,
1817 .probe = meye_probe,
4c62e976 1818 .remove = meye_remove,
1da177e4
LT
1819#ifdef CONFIG_PM
1820 .suspend = meye_suspend,
1821 .resume = meye_resume,
1822#endif
1823};
1824
1825static int __init meye_init(void)
1826{
1827 gbuffers = max(2, min((int)gbuffers, MEYE_MAX_BUFNBRS));
83fa235b 1828 if (gbufsize > MEYE_MAX_BUFSIZE)
1da177e4
LT
1829 gbufsize = MEYE_MAX_BUFSIZE;
1830 gbufsize = PAGE_ALIGN(gbufsize);
f8a3dcb5 1831 printk(KERN_INFO "meye: using %d buffers with %dk (%dk total) for capture\n",
1da177e4
LT
1832 gbuffers,
1833 gbufsize / 1024, gbuffers * gbufsize / 1024);
1834 return pci_register_driver(&meye_driver);
1835}
1836
1837static void __exit meye_exit(void)
1838{
1839 pci_unregister_driver(&meye_driver);
1840}
1841
1842module_init(meye_init);
1843module_exit(meye_exit);