Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /*************************************************************************** |
2 | * V4L2 driver for SN9C10x PC Camera Controllers * | |
3 | * * | |
a966f3e7 | 4 | * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> * |
1da177e4 LT |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | |
19 | ***************************************************************************/ | |
20 | ||
21 | #include <linux/module.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/param.h> | |
25 | #include <linux/moduleparam.h> | |
26 | #include <linux/errno.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/string.h> | |
29 | #include <linux/device.h> | |
30 | #include <linux/fs.h> | |
31 | #include <linux/delay.h> | |
32 | #include <linux/stddef.h> | |
33 | #include <linux/compiler.h> | |
34 | #include <linux/ioctl.h> | |
35 | #include <linux/poll.h> | |
36 | #include <linux/stat.h> | |
37 | #include <linux/mm.h> | |
38 | #include <linux/vmalloc.h> | |
39 | #include <linux/page-flags.h> | |
40 | #include <linux/byteorder/generic.h> | |
41 | #include <asm/page.h> | |
42 | #include <asm/uaccess.h> | |
43 | ||
44 | #include "sn9c102.h" | |
45 | ||
46 | /*****************************************************************************/ | |
47 | ||
48 | MODULE_DEVICE_TABLE(usb, sn9c102_id_table); | |
49 | ||
50 | MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL); | |
51 | MODULE_DESCRIPTION(SN9C102_MODULE_NAME); | |
52 | MODULE_VERSION(SN9C102_MODULE_VERSION); | |
53 | MODULE_LICENSE(SN9C102_MODULE_LICENSE); | |
54 | ||
55 | static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1}; | |
56 | module_param_array(video_nr, short, NULL, 0444); | |
57 | MODULE_PARM_DESC(video_nr, | |
58 | "\n<-1|n[,...]> Specify V4L2 minor mode number." | |
59 | "\n -1 = use next available (default)" | |
60 | "\n n = use minor number n (integer >= 0)" | |
61 | "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES) | |
62 | " cameras this way." | |
63 | "\nFor example:" | |
64 | "\nvideo_nr=-1,2,-1 would assign minor number 2 to" | |
65 | "\nthe second camera and use auto for the first" | |
66 | "\none and for every other camera." | |
67 | "\n"); | |
68 | ||
69 | static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] = | |
70 | SN9C102_FORCE_MUNMAP}; | |
71 | module_param_array(force_munmap, bool, NULL, 0444); | |
72 | MODULE_PARM_DESC(force_munmap, | |
a966f3e7 LR |
73 | "\n<0|1[,...]> Force the application to unmap previously" |
74 | "\nmapped buffer memory before calling any VIDIOC_S_CROP or" | |
75 | "\nVIDIOC_S_FMT ioctl's. Not all the applications support" | |
76 | "\nthis feature. This parameter is specific for each" | |
1da177e4 LT |
77 | "\ndetected camera." |
78 | "\n 0 = do not force memory unmapping" | |
79 | "\n 1 = force memory unmapping (save memory)" | |
80 | "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"." | |
81 | "\n"); | |
82 | ||
83 | #ifdef SN9C102_DEBUG | |
84 | static unsigned short debug = SN9C102_DEBUG_LEVEL; | |
85 | module_param(debug, ushort, 0644); | |
86 | MODULE_PARM_DESC(debug, | |
87 | "\n<n> Debugging information level, from 0 to 3:" | |
88 | "\n0 = none (use carefully)" | |
89 | "\n1 = critical errors" | |
90 | "\n2 = significant informations" | |
91 | "\n3 = more verbose messages" | |
92 | "\nLevel 3 is useful for testing only, when only " | |
93 | "one device is used." | |
94 | "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"." | |
95 | "\n"); | |
96 | #endif | |
97 | ||
98 | /*****************************************************************************/ | |
99 | ||
100 | static sn9c102_sof_header_t sn9c102_sof_header[] = { | |
101 | {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00}, | |
102 | {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01}, | |
103 | }; | |
104 | ||
a966f3e7 LR |
105 | static sn9c103_sof_header_t sn9c103_sof_header[] = { |
106 | {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20}, | |
107 | }; | |
1da177e4 LT |
108 | |
109 | static sn9c102_eof_header_t sn9c102_eof_header[] = { | |
110 | {0x00, 0x00, 0x00, 0x00}, | |
111 | {0x40, 0x00, 0x00, 0x00}, | |
112 | {0x80, 0x00, 0x00, 0x00}, | |
113 | {0xc0, 0x00, 0x00, 0x00}, | |
114 | }; | |
115 | ||
116 | /*****************************************************************************/ | |
117 | ||
118 | static void* rvmalloc(size_t size) | |
119 | { | |
120 | void* mem; | |
121 | unsigned long adr; | |
122 | ||
123 | size = PAGE_ALIGN(size); | |
124 | ||
125 | mem = vmalloc_32((unsigned long)size); | |
126 | if (!mem) | |
127 | return NULL; | |
128 | ||
129 | memset(mem, 0, size); | |
130 | ||
131 | adr = (unsigned long)mem; | |
132 | while (size > 0) { | |
133 | SetPageReserved(vmalloc_to_page((void *)adr)); | |
134 | adr += PAGE_SIZE; | |
135 | size -= PAGE_SIZE; | |
136 | } | |
137 | ||
138 | return mem; | |
139 | } | |
140 | ||
141 | ||
142 | static void rvfree(void* mem, size_t size) | |
143 | { | |
144 | unsigned long adr; | |
145 | ||
146 | if (!mem) | |
147 | return; | |
148 | ||
149 | size = PAGE_ALIGN(size); | |
150 | ||
151 | adr = (unsigned long)mem; | |
152 | while (size > 0) { | |
153 | ClearPageReserved(vmalloc_to_page((void *)adr)); | |
154 | adr += PAGE_SIZE; | |
155 | size -= PAGE_SIZE; | |
156 | } | |
157 | ||
158 | vfree(mem); | |
159 | } | |
160 | ||
161 | ||
162 | static u32 | |
163 | sn9c102_request_buffers(struct sn9c102_device* cam, u32 count, | |
164 | enum sn9c102_io_method io) | |
165 | { | |
166 | struct v4l2_pix_format* p = &(cam->sensor->pix_format); | |
167 | struct v4l2_rect* r = &(cam->sensor->cropcap.bounds); | |
168 | const size_t imagesize = cam->module_param.force_munmap || | |
169 | io == IO_READ ? | |
170 | (p->width * p->height * p->priv) / 8 : | |
171 | (r->width * r->height * p->priv) / 8; | |
172 | void* buff = NULL; | |
173 | u32 i; | |
174 | ||
175 | if (count > SN9C102_MAX_FRAMES) | |
176 | count = SN9C102_MAX_FRAMES; | |
177 | ||
178 | cam->nbuffers = count; | |
179 | while (cam->nbuffers > 0) { | |
180 | if ((buff = rvmalloc(cam->nbuffers * PAGE_ALIGN(imagesize)))) | |
181 | break; | |
182 | cam->nbuffers--; | |
183 | } | |
184 | ||
185 | for (i = 0; i < cam->nbuffers; i++) { | |
186 | cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize); | |
187 | cam->frame[i].buf.index = i; | |
188 | cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize); | |
189 | cam->frame[i].buf.length = imagesize; | |
190 | cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
191 | cam->frame[i].buf.sequence = 0; | |
192 | cam->frame[i].buf.field = V4L2_FIELD_NONE; | |
193 | cam->frame[i].buf.memory = V4L2_MEMORY_MMAP; | |
194 | cam->frame[i].buf.flags = 0; | |
195 | } | |
196 | ||
197 | return cam->nbuffers; | |
198 | } | |
199 | ||
200 | ||
201 | static void sn9c102_release_buffers(struct sn9c102_device* cam) | |
202 | { | |
203 | if (cam->nbuffers) { | |
204 | rvfree(cam->frame[0].bufmem, | |
0b67ba63 | 205 | cam->nbuffers * PAGE_ALIGN(cam->frame[0].buf.length)); |
1da177e4 LT |
206 | cam->nbuffers = 0; |
207 | } | |
a966f3e7 | 208 | cam->frame_current = NULL; |
1da177e4 LT |
209 | } |
210 | ||
211 | ||
212 | static void sn9c102_empty_framequeues(struct sn9c102_device* cam) | |
213 | { | |
214 | u32 i; | |
215 | ||
216 | INIT_LIST_HEAD(&cam->inqueue); | |
217 | INIT_LIST_HEAD(&cam->outqueue); | |
218 | ||
219 | for (i = 0; i < SN9C102_MAX_FRAMES; i++) { | |
220 | cam->frame[i].state = F_UNUSED; | |
221 | cam->frame[i].buf.bytesused = 0; | |
222 | } | |
223 | } | |
224 | ||
225 | ||
a966f3e7 LR |
226 | static void sn9c102_requeue_outqueue(struct sn9c102_device* cam) |
227 | { | |
228 | struct sn9c102_frame_t *i; | |
229 | ||
230 | list_for_each_entry(i, &cam->outqueue, frame) { | |
231 | i->state = F_QUEUED; | |
232 | list_add(&i->frame, &cam->inqueue); | |
233 | } | |
234 | ||
235 | INIT_LIST_HEAD(&cam->outqueue); | |
236 | } | |
237 | ||
238 | ||
1da177e4 LT |
239 | static void sn9c102_queue_unusedframes(struct sn9c102_device* cam) |
240 | { | |
241 | unsigned long lock_flags; | |
242 | u32 i; | |
243 | ||
244 | for (i = 0; i < cam->nbuffers; i++) | |
245 | if (cam->frame[i].state == F_UNUSED) { | |
246 | cam->frame[i].state = F_QUEUED; | |
247 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | |
248 | list_add_tail(&cam->frame[i].frame, &cam->inqueue); | |
249 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
250 | } | |
251 | } | |
252 | ||
253 | /*****************************************************************************/ | |
254 | ||
a966f3e7 LR |
255 | int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index) |
256 | { | |
257 | struct usb_device* udev = cam->usbdev; | |
258 | int i, res; | |
259 | ||
260 | if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg)) | |
261 | return -1; | |
262 | ||
263 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | |
264 | index, 0, buff, sizeof(buff), | |
265 | SN9C102_CTRL_TIMEOUT*sizeof(buff)); | |
266 | if (res < 0) { | |
267 | DBG(3, "Failed to write registers (index 0x%02X, error %d)", | |
268 | index, res); | |
269 | return -1; | |
270 | } | |
271 | ||
272 | for (i = 0; i < sizeof(buff); i++) | |
273 | cam->reg[index+i] = buff[i]; | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
278 | ||
1da177e4 LT |
279 | int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index) |
280 | { | |
281 | struct usb_device* udev = cam->usbdev; | |
282 | u8* buff = cam->control_buffer; | |
283 | int res; | |
284 | ||
a966f3e7 LR |
285 | if (index >= ARRAY_SIZE(cam->reg)) |
286 | return -1; | |
287 | ||
1da177e4 LT |
288 | *buff = value; |
289 | ||
290 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | |
291 | index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); | |
292 | if (res < 0) { | |
293 | DBG(3, "Failed to write a register (value 0x%02X, index " | |
a966f3e7 | 294 | "0x%02X, error %d)", value, index, res); |
1da177e4 LT |
295 | return -1; |
296 | } | |
297 | ||
298 | cam->reg[index] = value; | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
303 | ||
304 | /* NOTE: reading some registers always returns 0 */ | |
305 | static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index) | |
306 | { | |
307 | struct usb_device* udev = cam->usbdev; | |
308 | u8* buff = cam->control_buffer; | |
309 | int res; | |
310 | ||
311 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1, | |
312 | index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); | |
313 | if (res < 0) | |
314 | DBG(3, "Failed to read a register (index 0x%02X, error %d)", | |
a966f3e7 | 315 | index, res); |
1da177e4 LT |
316 | |
317 | return (res >= 0) ? (int)(*buff) : -1; | |
318 | } | |
319 | ||
320 | ||
321 | int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index) | |
322 | { | |
a966f3e7 LR |
323 | if (index >= ARRAY_SIZE(cam->reg)) |
324 | return -1; | |
1da177e4 LT |
325 | |
326 | return cam->reg[index]; | |
327 | } | |
328 | ||
329 | ||
330 | static int | |
331 | sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor) | |
332 | { | |
333 | int i, r; | |
334 | ||
335 | for (i = 1; i <= 5; i++) { | |
336 | r = sn9c102_read_reg(cam, 0x08); | |
337 | if (r < 0) | |
338 | return -EIO; | |
339 | if (r & 0x04) | |
340 | return 0; | |
341 | if (sensor->frequency & SN9C102_I2C_400KHZ) | |
342 | udelay(5*16); | |
343 | else | |
344 | udelay(16*16); | |
345 | } | |
346 | return -EBUSY; | |
347 | } | |
348 | ||
349 | ||
350 | static int | |
351 | sn9c102_i2c_detect_read_error(struct sn9c102_device* cam, | |
352 | struct sn9c102_sensor* sensor) | |
353 | { | |
354 | int r; | |
355 | r = sn9c102_read_reg(cam, 0x08); | |
356 | return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0; | |
357 | } | |
358 | ||
359 | ||
360 | static int | |
361 | sn9c102_i2c_detect_write_error(struct sn9c102_device* cam, | |
362 | struct sn9c102_sensor* sensor) | |
363 | { | |
364 | int r; | |
365 | r = sn9c102_read_reg(cam, 0x08); | |
366 | return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0; | |
367 | } | |
368 | ||
369 | ||
370 | int | |
371 | sn9c102_i2c_try_raw_read(struct sn9c102_device* cam, | |
372 | struct sn9c102_sensor* sensor, u8 data0, u8 data1, | |
373 | u8 n, u8 buffer[]) | |
374 | { | |
375 | struct usb_device* udev = cam->usbdev; | |
376 | u8* data = cam->control_buffer; | |
377 | int err = 0, res; | |
378 | ||
379 | /* Write cycle */ | |
380 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | |
381 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10; | |
382 | data[1] = data0; /* I2C slave id */ | |
383 | data[2] = data1; /* address */ | |
384 | data[7] = 0x10; | |
385 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | |
386 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); | |
387 | if (res < 0) | |
388 | err += res; | |
389 | ||
390 | err += sn9c102_i2c_wait(cam, sensor); | |
391 | ||
392 | /* Read cycle - n bytes */ | |
393 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | |
394 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | | |
395 | (n << 4) | 0x02; | |
396 | data[1] = data0; | |
397 | data[7] = 0x10; | |
398 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | |
399 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); | |
400 | if (res < 0) | |
401 | err += res; | |
402 | ||
403 | err += sn9c102_i2c_wait(cam, sensor); | |
404 | ||
405 | /* The first read byte will be placed in data[4] */ | |
406 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1, | |
407 | 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT); | |
408 | if (res < 0) | |
409 | err += res; | |
410 | ||
411 | err += sn9c102_i2c_detect_read_error(cam, sensor); | |
412 | ||
413 | PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1, | |
a966f3e7 | 414 | data[4]); |
1da177e4 LT |
415 | |
416 | if (err) { | |
a966f3e7 | 417 | DBG(3, "I2C read failed for %s image sensor", sensor->name); |
1da177e4 LT |
418 | return -1; |
419 | } | |
420 | ||
421 | if (buffer) | |
422 | memcpy(buffer, data, sizeof(buffer)); | |
423 | ||
424 | return (int)data[4]; | |
425 | } | |
426 | ||
427 | ||
428 | int | |
429 | sn9c102_i2c_try_raw_write(struct sn9c102_device* cam, | |
430 | struct sn9c102_sensor* sensor, u8 n, u8 data0, | |
431 | u8 data1, u8 data2, u8 data3, u8 data4, u8 data5) | |
432 | { | |
433 | struct usb_device* udev = cam->usbdev; | |
434 | u8* data = cam->control_buffer; | |
435 | int err = 0, res; | |
436 | ||
437 | /* Write cycle. It usually is address + value */ | |
438 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | |
439 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | |
440 | | ((n - 1) << 4); | |
441 | data[1] = data0; | |
442 | data[2] = data1; | |
443 | data[3] = data2; | |
444 | data[4] = data3; | |
445 | data[5] = data4; | |
446 | data[6] = data5; | |
447 | data[7] = 0x14; | |
448 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | |
449 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); | |
450 | if (res < 0) | |
451 | err += res; | |
452 | ||
453 | err += sn9c102_i2c_wait(cam, sensor); | |
454 | err += sn9c102_i2c_detect_write_error(cam, sensor); | |
455 | ||
456 | if (err) | |
a966f3e7 | 457 | DBG(3, "I2C write failed for %s image sensor", sensor->name); |
1da177e4 LT |
458 | |
459 | PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, " | |
460 | "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X", | |
a966f3e7 | 461 | n, data0, data1, data2, data3, data4, data5); |
1da177e4 LT |
462 | |
463 | return err ? -1 : 0; | |
464 | } | |
465 | ||
466 | ||
467 | int | |
468 | sn9c102_i2c_try_read(struct sn9c102_device* cam, | |
469 | struct sn9c102_sensor* sensor, u8 address) | |
470 | { | |
471 | return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id, | |
472 | address, 1, NULL); | |
473 | } | |
474 | ||
475 | ||
b9df978f | 476 | int |
1da177e4 LT |
477 | sn9c102_i2c_try_write(struct sn9c102_device* cam, |
478 | struct sn9c102_sensor* sensor, u8 address, u8 value) | |
479 | { | |
480 | return sn9c102_i2c_try_raw_write(cam, sensor, 3, | |
481 | sensor->i2c_slave_id, address, | |
482 | value, 0, 0, 0); | |
483 | } | |
484 | ||
485 | ||
486 | int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address) | |
487 | { | |
488 | if (!cam->sensor) | |
489 | return -1; | |
490 | ||
491 | return sn9c102_i2c_try_read(cam, cam->sensor, address); | |
492 | } | |
493 | ||
494 | ||
495 | int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value) | |
496 | { | |
497 | if (!cam->sensor) | |
498 | return -1; | |
499 | ||
500 | return sn9c102_i2c_try_write(cam, cam->sensor, address, value); | |
501 | } | |
502 | ||
503 | /*****************************************************************************/ | |
504 | ||
505 | static void* | |
506 | sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len) | |
507 | { | |
a966f3e7 LR |
508 | size_t soflen = 0, i; |
509 | u8 j, n = 0; | |
510 | ||
511 | switch (cam->bridge) { | |
512 | case BRIDGE_SN9C101: | |
513 | case BRIDGE_SN9C102: | |
514 | soflen = sizeof(sn9c102_sof_header_t); | |
515 | n = sizeof(sn9c102_sof_header) / soflen; | |
516 | break; | |
517 | case BRIDGE_SN9C103: | |
518 | soflen = sizeof(sn9c103_sof_header_t); | |
519 | n = sizeof(sn9c103_sof_header) / soflen; | |
520 | } | |
1da177e4 | 521 | |
a966f3e7 | 522 | for (i = 0; (len >= soflen) && (i <= len - soflen); i++) |
1da177e4 | 523 | for (j = 0; j < n; j++) |
a966f3e7 LR |
524 | /* The invariable part of the header is 6 bytes long */ |
525 | if ((cam->bridge != BRIDGE_SN9C103 && | |
526 | !memcmp(mem + i, sn9c102_sof_header[j], 6)) || | |
527 | (cam->bridge == BRIDGE_SN9C103 && | |
528 | !memcmp(mem + i, sn9c103_sof_header[j], 6))) { | |
1da177e4 LT |
529 | memcpy(cam->sof_header, mem + i, soflen); |
530 | /* Skip the header */ | |
531 | return mem + i + soflen; | |
532 | } | |
533 | ||
534 | return NULL; | |
535 | } | |
536 | ||
537 | ||
538 | static void* | |
539 | sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len) | |
540 | { | |
541 | size_t eoflen = sizeof(sn9c102_eof_header_t), i; | |
542 | unsigned j, n = sizeof(sn9c102_eof_header) / eoflen; | |
543 | ||
544 | if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X) | |
545 | return NULL; /* EOF header does not exist in compressed data */ | |
546 | ||
547 | for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++) | |
548 | for (j = 0; j < n; j++) | |
549 | if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen)) | |
550 | return mem + i; | |
551 | ||
552 | return NULL; | |
553 | } | |
554 | ||
555 | ||
556 | static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs) | |
557 | { | |
558 | struct sn9c102_device* cam = urb->context; | |
559 | struct sn9c102_frame_t** f; | |
a966f3e7 | 560 | size_t imagesize, soflen; |
1da177e4 LT |
561 | u8 i; |
562 | int err = 0; | |
563 | ||
564 | if (urb->status == -ENOENT) | |
565 | return; | |
566 | ||
567 | f = &cam->frame_current; | |
568 | ||
569 | if (cam->stream == STREAM_INTERRUPT) { | |
570 | cam->stream = STREAM_OFF; | |
571 | if ((*f)) | |
572 | (*f)->state = F_QUEUED; | |
a966f3e7 | 573 | DBG(3, "Stream interrupted"); |
1da177e4 LT |
574 | wake_up_interruptible(&cam->wait_stream); |
575 | } | |
576 | ||
577 | if (cam->state & DEV_DISCONNECTED) | |
578 | return; | |
579 | ||
580 | if (cam->state & DEV_MISCONFIGURED) { | |
581 | wake_up_interruptible(&cam->wait_frame); | |
582 | return; | |
583 | } | |
584 | ||
585 | if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue)) | |
586 | goto resubmit_urb; | |
587 | ||
588 | if (!(*f)) | |
589 | (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t, | |
590 | frame); | |
591 | ||
592 | imagesize = (cam->sensor->pix_format.width * | |
593 | cam->sensor->pix_format.height * | |
594 | cam->sensor->pix_format.priv) / 8; | |
595 | ||
a966f3e7 LR |
596 | soflen = (cam->bridge) == BRIDGE_SN9C103 ? |
597 | sizeof(sn9c103_sof_header_t) : | |
598 | sizeof(sn9c102_sof_header_t); | |
599 | ||
1da177e4 LT |
600 | for (i = 0; i < urb->number_of_packets; i++) { |
601 | unsigned int img, len, status; | |
602 | void *pos, *sof, *eof; | |
603 | ||
604 | len = urb->iso_frame_desc[i].actual_length; | |
605 | status = urb->iso_frame_desc[i].status; | |
606 | pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer; | |
607 | ||
608 | if (status) { | |
a966f3e7 | 609 | DBG(3, "Error in isochronous frame"); |
1da177e4 LT |
610 | (*f)->state = F_ERROR; |
611 | continue; | |
612 | } | |
613 | ||
a966f3e7 | 614 | PDBGG("Isochrnous frame: length %u, #%u i", len, i); |
1da177e4 LT |
615 | |
616 | redo: | |
617 | sof = sn9c102_find_sof_header(cam, pos, len); | |
618 | if (!sof) { | |
619 | eof = sn9c102_find_eof_header(cam, pos, len); | |
620 | if ((*f)->state == F_GRABBING) { | |
621 | end_of_frame: | |
622 | img = len; | |
623 | ||
624 | if (eof) | |
625 | img = (eof > pos) ? eof - pos - 1 : 0; | |
626 | ||
627 | if ((*f)->buf.bytesused+img > imagesize) { | |
628 | u32 b = (*f)->buf.bytesused + img - | |
629 | imagesize; | |
630 | img = imagesize - (*f)->buf.bytesused; | |
631 | DBG(3, "Expected EOF not found: " | |
a966f3e7 | 632 | "video frame cut"); |
1da177e4 LT |
633 | if (eof) |
634 | DBG(3, "Exceeded limit: +%u " | |
a966f3e7 | 635 | "bytes", (unsigned)(b)); |
1da177e4 LT |
636 | } |
637 | ||
638 | memcpy((*f)->bufmem + (*f)->buf.bytesused, pos, | |
639 | img); | |
640 | ||
641 | if ((*f)->buf.bytesused == 0) | |
642 | do_gettimeofday(&(*f)->buf.timestamp); | |
643 | ||
644 | (*f)->buf.bytesused += img; | |
645 | ||
646 | if ((*f)->buf.bytesused == imagesize || | |
647 | (cam->sensor->pix_format.pixelformat == | |
648 | V4L2_PIX_FMT_SN9C10X && eof)) { | |
649 | u32 b = (*f)->buf.bytesused; | |
650 | (*f)->state = F_DONE; | |
651 | (*f)->buf.sequence= ++cam->frame_count; | |
a966f3e7 | 652 | spin_lock(&cam->queue_lock); |
1da177e4 LT |
653 | list_move_tail(&(*f)->frame, |
654 | &cam->outqueue); | |
655 | if (!list_empty(&cam->inqueue)) | |
656 | (*f) = list_entry( | |
657 | cam->inqueue.next, | |
658 | struct sn9c102_frame_t, | |
659 | frame ); | |
660 | else | |
661 | (*f) = NULL; | |
a966f3e7 | 662 | spin_unlock(&cam->queue_lock); |
1da177e4 | 663 | memcpy(cam->sysfs.frame_header, |
a966f3e7 LR |
664 | cam->sof_header, soflen); |
665 | DBG(3, "Video frame captured: %lu " | |
666 | "bytes", (unsigned long)(b)); | |
1da177e4 LT |
667 | |
668 | if (!(*f)) | |
669 | goto resubmit_urb; | |
670 | ||
671 | } else if (eof) { | |
672 | (*f)->state = F_ERROR; | |
673 | DBG(3, "Not expected EOF after %lu " | |
674 | "bytes of image data", | |
a966f3e7 LR |
675 | (unsigned long) |
676 | ((*f)->buf.bytesused)); | |
1da177e4 LT |
677 | } |
678 | ||
679 | if (sof) /* (1) */ | |
680 | goto start_of_frame; | |
681 | ||
682 | } else if (eof) { | |
a966f3e7 | 683 | DBG(3, "EOF without SOF"); |
1da177e4 LT |
684 | continue; |
685 | ||
686 | } else { | |
a966f3e7 | 687 | PDBGG("Ignoring pointless isochronous frame"); |
1da177e4 LT |
688 | continue; |
689 | } | |
690 | ||
691 | } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) { | |
692 | start_of_frame: | |
693 | (*f)->state = F_GRABBING; | |
694 | (*f)->buf.bytesused = 0; | |
695 | len -= (sof - pos); | |
696 | pos = sof; | |
a966f3e7 | 697 | DBG(3, "SOF detected: new video frame"); |
1da177e4 LT |
698 | if (len) |
699 | goto redo; | |
700 | ||
701 | } else if ((*f)->state == F_GRABBING) { | |
702 | eof = sn9c102_find_eof_header(cam, pos, len); | |
703 | if (eof && eof < sof) | |
704 | goto end_of_frame; /* (1) */ | |
705 | else { | |
706 | if (cam->sensor->pix_format.pixelformat == | |
707 | V4L2_PIX_FMT_SN9C10X) { | |
a966f3e7 | 708 | eof = sof - soflen; |
1da177e4 LT |
709 | goto end_of_frame; |
710 | } else { | |
711 | DBG(3, "SOF before expected EOF after " | |
712 | "%lu bytes of image data", | |
a966f3e7 LR |
713 | (unsigned long) |
714 | ((*f)->buf.bytesused)); | |
1da177e4 LT |
715 | goto start_of_frame; |
716 | } | |
717 | } | |
718 | } | |
719 | } | |
720 | ||
721 | resubmit_urb: | |
722 | urb->dev = cam->usbdev; | |
723 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
724 | if (err < 0 && err != -EPERM) { | |
725 | cam->state |= DEV_MISCONFIGURED; | |
a966f3e7 | 726 | DBG(1, "usb_submit_urb() failed"); |
1da177e4 LT |
727 | } |
728 | ||
729 | wake_up_interruptible(&cam->wait_frame); | |
730 | } | |
731 | ||
732 | ||
733 | static int sn9c102_start_transfer(struct sn9c102_device* cam) | |
734 | { | |
735 | struct usb_device *udev = cam->usbdev; | |
736 | struct urb* urb; | |
a966f3e7 LR |
737 | const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512, |
738 | 680, 800, 900, 1023}; | |
739 | const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512, | |
740 | 680, 800, 900, 1003}; | |
741 | const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ? | |
742 | sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] : | |
743 | sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING]; | |
1da177e4 LT |
744 | s8 i, j; |
745 | int err = 0; | |
746 | ||
747 | for (i = 0; i < SN9C102_URBS; i++) { | |
748 | cam->transfer_buffer[i] = kmalloc(SN9C102_ISO_PACKETS * psz, | |
749 | GFP_KERNEL); | |
750 | if (!cam->transfer_buffer[i]) { | |
751 | err = -ENOMEM; | |
a966f3e7 | 752 | DBG(1, "Not enough memory"); |
1da177e4 LT |
753 | goto free_buffers; |
754 | } | |
755 | } | |
756 | ||
757 | for (i = 0; i < SN9C102_URBS; i++) { | |
758 | urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL); | |
759 | cam->urb[i] = urb; | |
760 | if (!urb) { | |
761 | err = -ENOMEM; | |
a966f3e7 | 762 | DBG(1, "usb_alloc_urb() failed"); |
1da177e4 LT |
763 | goto free_urbs; |
764 | } | |
765 | urb->dev = udev; | |
766 | urb->context = cam; | |
767 | urb->pipe = usb_rcvisocpipe(udev, 1); | |
768 | urb->transfer_flags = URB_ISO_ASAP; | |
769 | urb->number_of_packets = SN9C102_ISO_PACKETS; | |
770 | urb->complete = sn9c102_urb_complete; | |
771 | urb->transfer_buffer = cam->transfer_buffer[i]; | |
772 | urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS; | |
773 | urb->interval = 1; | |
774 | for (j = 0; j < SN9C102_ISO_PACKETS; j++) { | |
775 | urb->iso_frame_desc[j].offset = psz * j; | |
776 | urb->iso_frame_desc[j].length = psz; | |
777 | } | |
778 | } | |
779 | ||
780 | /* Enable video */ | |
781 | if (!(cam->reg[0x01] & 0x04)) { | |
782 | err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01); | |
783 | if (err) { | |
784 | err = -EIO; | |
a966f3e7 | 785 | DBG(1, "I/O hardware error"); |
1da177e4 LT |
786 | goto free_urbs; |
787 | } | |
788 | } | |
789 | ||
790 | err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING); | |
791 | if (err) { | |
a966f3e7 | 792 | DBG(1, "usb_set_interface() failed"); |
1da177e4 LT |
793 | goto free_urbs; |
794 | } | |
795 | ||
796 | cam->frame_current = NULL; | |
797 | ||
798 | for (i = 0; i < SN9C102_URBS; i++) { | |
799 | err = usb_submit_urb(cam->urb[i], GFP_KERNEL); | |
800 | if (err) { | |
801 | for (j = i-1; j >= 0; j--) | |
802 | usb_kill_urb(cam->urb[j]); | |
a966f3e7 | 803 | DBG(1, "usb_submit_urb() failed, error %d", err); |
1da177e4 LT |
804 | goto free_urbs; |
805 | } | |
806 | } | |
807 | ||
808 | return 0; | |
809 | ||
810 | free_urbs: | |
811 | for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++) | |
812 | usb_free_urb(cam->urb[i]); | |
813 | ||
814 | free_buffers: | |
815 | for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++) | |
816 | kfree(cam->transfer_buffer[i]); | |
817 | ||
818 | return err; | |
819 | } | |
820 | ||
821 | ||
822 | static int sn9c102_stop_transfer(struct sn9c102_device* cam) | |
823 | { | |
824 | struct usb_device *udev = cam->usbdev; | |
825 | s8 i; | |
826 | int err = 0; | |
827 | ||
828 | if (cam->state & DEV_DISCONNECTED) | |
829 | return 0; | |
830 | ||
831 | for (i = SN9C102_URBS-1; i >= 0; i--) { | |
832 | usb_kill_urb(cam->urb[i]); | |
833 | usb_free_urb(cam->urb[i]); | |
834 | kfree(cam->transfer_buffer[i]); | |
835 | } | |
836 | ||
837 | err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */ | |
838 | if (err) | |
a966f3e7 | 839 | DBG(3, "usb_set_interface() failed"); |
1da177e4 LT |
840 | |
841 | return err; | |
842 | } | |
843 | ||
844 | ||
7107627b | 845 | static int sn9c102_stream_interrupt(struct sn9c102_device* cam) |
1da177e4 LT |
846 | { |
847 | int err = 0; | |
848 | ||
849 | cam->stream = STREAM_INTERRUPT; | |
850 | err = wait_event_timeout(cam->wait_stream, | |
851 | (cam->stream == STREAM_OFF) || | |
852 | (cam->state & DEV_DISCONNECTED), | |
853 | SN9C102_URB_TIMEOUT); | |
854 | if (cam->state & DEV_DISCONNECTED) | |
855 | return -ENODEV; | |
856 | else if (err) { | |
857 | cam->state |= DEV_MISCONFIGURED; | |
858 | DBG(1, "The camera is misconfigured. To use it, close and " | |
a966f3e7 | 859 | "open /dev/video%d again.", cam->v4ldev->minor); |
1da177e4 LT |
860 | return err; |
861 | } | |
862 | ||
863 | return 0; | |
864 | } | |
865 | ||
866 | /*****************************************************************************/ | |
867 | ||
868 | static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count) | |
869 | { | |
870 | char str[5]; | |
871 | char* endp; | |
872 | unsigned long val; | |
873 | ||
874 | if (len < 4) { | |
875 | strncpy(str, buff, len); | |
876 | str[len+1] = '\0'; | |
877 | } else { | |
878 | strncpy(str, buff, 4); | |
879 | str[4] = '\0'; | |
880 | } | |
881 | ||
882 | val = simple_strtoul(str, &endp, 0); | |
883 | ||
884 | *count = 0; | |
885 | if (val <= 0xff) | |
886 | *count = (ssize_t)(endp - str); | |
887 | if ((*count) && (len == *count+1) && (buff[*count] == '\n')) | |
888 | *count += 1; | |
889 | ||
890 | return (u8)val; | |
891 | } | |
892 | ||
893 | /* | |
894 | NOTE 1: being inside one of the following methods implies that the v4l | |
895 | device exists for sure (see kobjects and reference counters) | |
896 | NOTE 2: buffers are PAGE_SIZE long | |
897 | */ | |
898 | ||
899 | static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf) | |
900 | { | |
901 | struct sn9c102_device* cam; | |
902 | ssize_t count; | |
903 | ||
904 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
905 | return -ERESTARTSYS; | |
906 | ||
907 | cam = video_get_drvdata(to_video_device(cd)); | |
908 | if (!cam) { | |
909 | up(&sn9c102_sysfs_lock); | |
910 | return -ENODEV; | |
911 | } | |
912 | ||
913 | count = sprintf(buf, "%u\n", cam->sysfs.reg); | |
914 | ||
915 | up(&sn9c102_sysfs_lock); | |
916 | ||
917 | return count; | |
918 | } | |
919 | ||
920 | ||
921 | static ssize_t | |
922 | sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len) | |
923 | { | |
924 | struct sn9c102_device* cam; | |
925 | u8 index; | |
926 | ssize_t count; | |
927 | ||
928 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
929 | return -ERESTARTSYS; | |
930 | ||
931 | cam = video_get_drvdata(to_video_device(cd)); | |
932 | if (!cam) { | |
933 | up(&sn9c102_sysfs_lock); | |
934 | return -ENODEV; | |
935 | } | |
936 | ||
937 | index = sn9c102_strtou8(buf, len, &count); | |
938 | if (index > 0x1f || !count) { | |
939 | up(&sn9c102_sysfs_lock); | |
940 | return -EINVAL; | |
941 | } | |
942 | ||
943 | cam->sysfs.reg = index; | |
944 | ||
a966f3e7 LR |
945 | DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg); |
946 | DBG(3, "Written bytes: %zd", count); | |
1da177e4 LT |
947 | |
948 | up(&sn9c102_sysfs_lock); | |
949 | ||
950 | return count; | |
951 | } | |
952 | ||
953 | ||
954 | static ssize_t sn9c102_show_val(struct class_device* cd, char* buf) | |
955 | { | |
956 | struct sn9c102_device* cam; | |
957 | ssize_t count; | |
958 | int val; | |
959 | ||
960 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
961 | return -ERESTARTSYS; | |
962 | ||
963 | cam = video_get_drvdata(to_video_device(cd)); | |
964 | if (!cam) { | |
965 | up(&sn9c102_sysfs_lock); | |
966 | return -ENODEV; | |
967 | } | |
968 | ||
969 | if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) { | |
970 | up(&sn9c102_sysfs_lock); | |
971 | return -EIO; | |
972 | } | |
973 | ||
974 | count = sprintf(buf, "%d\n", val); | |
975 | ||
a966f3e7 | 976 | DBG(3, "Read bytes: %zd", count); |
1da177e4 LT |
977 | |
978 | up(&sn9c102_sysfs_lock); | |
979 | ||
980 | return count; | |
981 | } | |
982 | ||
983 | ||
984 | static ssize_t | |
985 | sn9c102_store_val(struct class_device* cd, const char* buf, size_t len) | |
986 | { | |
987 | struct sn9c102_device* cam; | |
988 | u8 value; | |
989 | ssize_t count; | |
990 | int err; | |
991 | ||
992 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
993 | return -ERESTARTSYS; | |
994 | ||
995 | cam = video_get_drvdata(to_video_device(cd)); | |
996 | if (!cam) { | |
997 | up(&sn9c102_sysfs_lock); | |
998 | return -ENODEV; | |
999 | } | |
1000 | ||
1001 | value = sn9c102_strtou8(buf, len, &count); | |
1002 | if (!count) { | |
1003 | up(&sn9c102_sysfs_lock); | |
1004 | return -EINVAL; | |
1005 | } | |
1006 | ||
1007 | err = sn9c102_write_reg(cam, value, cam->sysfs.reg); | |
1008 | if (err) { | |
1009 | up(&sn9c102_sysfs_lock); | |
1010 | return -EIO; | |
1011 | } | |
1012 | ||
1013 | DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X", | |
a966f3e7 LR |
1014 | cam->sysfs.reg, value); |
1015 | DBG(3, "Written bytes: %zd", count); | |
1da177e4 LT |
1016 | |
1017 | up(&sn9c102_sysfs_lock); | |
1018 | ||
1019 | return count; | |
1020 | } | |
1021 | ||
1022 | ||
1023 | static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf) | |
1024 | { | |
1025 | struct sn9c102_device* cam; | |
1026 | ssize_t count; | |
1027 | ||
1028 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
1029 | return -ERESTARTSYS; | |
1030 | ||
1031 | cam = video_get_drvdata(to_video_device(cd)); | |
1032 | if (!cam) { | |
1033 | up(&sn9c102_sysfs_lock); | |
1034 | return -ENODEV; | |
1035 | } | |
1036 | ||
1037 | count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg); | |
1038 | ||
a966f3e7 | 1039 | DBG(3, "Read bytes: %zd", count); |
1da177e4 LT |
1040 | |
1041 | up(&sn9c102_sysfs_lock); | |
1042 | ||
1043 | return count; | |
1044 | } | |
1045 | ||
1046 | ||
1047 | static ssize_t | |
1048 | sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len) | |
1049 | { | |
1050 | struct sn9c102_device* cam; | |
1051 | u8 index; | |
1052 | ssize_t count; | |
1053 | ||
1054 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
1055 | return -ERESTARTSYS; | |
1056 | ||
1057 | cam = video_get_drvdata(to_video_device(cd)); | |
1058 | if (!cam) { | |
1059 | up(&sn9c102_sysfs_lock); | |
1060 | return -ENODEV; | |
1061 | } | |
1062 | ||
1063 | index = sn9c102_strtou8(buf, len, &count); | |
1064 | if (!count) { | |
1065 | up(&sn9c102_sysfs_lock); | |
1066 | return -EINVAL; | |
1067 | } | |
1068 | ||
1069 | cam->sysfs.i2c_reg = index; | |
1070 | ||
a966f3e7 LR |
1071 | DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg); |
1072 | DBG(3, "Written bytes: %zd", count); | |
1da177e4 LT |
1073 | |
1074 | up(&sn9c102_sysfs_lock); | |
1075 | ||
1076 | return count; | |
1077 | } | |
1078 | ||
1079 | ||
1080 | static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf) | |
1081 | { | |
1082 | struct sn9c102_device* cam; | |
1083 | ssize_t count; | |
1084 | int val; | |
1085 | ||
1086 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
1087 | return -ERESTARTSYS; | |
1088 | ||
1089 | cam = video_get_drvdata(to_video_device(cd)); | |
1090 | if (!cam) { | |
1091 | up(&sn9c102_sysfs_lock); | |
1092 | return -ENODEV; | |
1093 | } | |
1094 | ||
1095 | if (!(cam->sensor->sysfs_ops & SN9C102_I2C_READ)) { | |
1096 | up(&sn9c102_sysfs_lock); | |
1097 | return -ENOSYS; | |
1098 | } | |
1099 | ||
1100 | if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) { | |
1101 | up(&sn9c102_sysfs_lock); | |
1102 | return -EIO; | |
1103 | } | |
1104 | ||
1105 | count = sprintf(buf, "%d\n", val); | |
1106 | ||
a966f3e7 | 1107 | DBG(3, "Read bytes: %zd", count); |
1da177e4 LT |
1108 | |
1109 | up(&sn9c102_sysfs_lock); | |
1110 | ||
1111 | return count; | |
1112 | } | |
1113 | ||
1114 | ||
1115 | static ssize_t | |
1116 | sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len) | |
1117 | { | |
1118 | struct sn9c102_device* cam; | |
1119 | u8 value; | |
1120 | ssize_t count; | |
1121 | int err; | |
1122 | ||
1123 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
1124 | return -ERESTARTSYS; | |
1125 | ||
1126 | cam = video_get_drvdata(to_video_device(cd)); | |
1127 | if (!cam) { | |
1128 | up(&sn9c102_sysfs_lock); | |
1129 | return -ENODEV; | |
1130 | } | |
1131 | ||
1132 | if (!(cam->sensor->sysfs_ops & SN9C102_I2C_WRITE)) { | |
1133 | up(&sn9c102_sysfs_lock); | |
1134 | return -ENOSYS; | |
1135 | } | |
1136 | ||
1137 | value = sn9c102_strtou8(buf, len, &count); | |
1138 | if (!count) { | |
1139 | up(&sn9c102_sysfs_lock); | |
1140 | return -EINVAL; | |
1141 | } | |
1142 | ||
1143 | err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value); | |
1144 | if (err) { | |
1145 | up(&sn9c102_sysfs_lock); | |
1146 | return -EIO; | |
1147 | } | |
1148 | ||
1149 | DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X", | |
a966f3e7 LR |
1150 | cam->sysfs.i2c_reg, value); |
1151 | DBG(3, "Written bytes: %zd", count); | |
1da177e4 LT |
1152 | |
1153 | up(&sn9c102_sysfs_lock); | |
1154 | ||
1155 | return count; | |
1156 | } | |
1157 | ||
1158 | ||
1159 | static ssize_t | |
1160 | sn9c102_store_green(struct class_device* cd, const char* buf, size_t len) | |
1161 | { | |
1162 | struct sn9c102_device* cam; | |
1163 | enum sn9c102_bridge bridge; | |
1164 | ssize_t res = 0; | |
1165 | u8 value; | |
1166 | ssize_t count; | |
1167 | ||
1168 | if (down_interruptible(&sn9c102_sysfs_lock)) | |
1169 | return -ERESTARTSYS; | |
1170 | ||
1171 | cam = video_get_drvdata(to_video_device(cd)); | |
1172 | if (!cam) { | |
1173 | up(&sn9c102_sysfs_lock); | |
1174 | return -ENODEV; | |
1175 | } | |
1176 | ||
1177 | bridge = cam->bridge; | |
1178 | ||
1179 | up(&sn9c102_sysfs_lock); | |
1180 | ||
1181 | value = sn9c102_strtou8(buf, len, &count); | |
1182 | if (!count) | |
1183 | return -EINVAL; | |
1184 | ||
1185 | switch (bridge) { | |
1186 | case BRIDGE_SN9C101: | |
1187 | case BRIDGE_SN9C102: | |
1188 | if (value > 0x0f) | |
1189 | return -EINVAL; | |
1190 | if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0) | |
1191 | res = sn9c102_store_val(cd, buf, len); | |
1192 | break; | |
1193 | case BRIDGE_SN9C103: | |
1194 | if (value > 0x7f) | |
1195 | return -EINVAL; | |
1196 | if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0) | |
1197 | res = sn9c102_store_val(cd, buf, len); | |
1198 | break; | |
1199 | } | |
1200 | ||
1201 | return res; | |
1202 | } | |
1203 | ||
1204 | ||
1205 | static ssize_t | |
1206 | sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len) | |
1207 | { | |
1208 | ssize_t res = 0; | |
1209 | u8 value; | |
1210 | ssize_t count; | |
1211 | ||
1212 | value = sn9c102_strtou8(buf, len, &count); | |
1213 | if (!count || value > 0x7f) | |
1214 | return -EINVAL; | |
1215 | ||
1216 | if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0) | |
1217 | res = sn9c102_store_val(cd, buf, len); | |
1218 | ||
1219 | return res; | |
1220 | } | |
1221 | ||
1222 | ||
1223 | static ssize_t | |
1224 | sn9c102_store_red(struct class_device* cd, const char* buf, size_t len) | |
1225 | { | |
1226 | ssize_t res = 0; | |
1227 | u8 value; | |
1228 | ssize_t count; | |
1229 | ||
1230 | value = sn9c102_strtou8(buf, len, &count); | |
1231 | if (!count || value > 0x7f) | |
1232 | return -EINVAL; | |
1233 | ||
1234 | if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0) | |
1235 | res = sn9c102_store_val(cd, buf, len); | |
1236 | ||
1237 | return res; | |
1238 | } | |
1239 | ||
1240 | ||
1241 | static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf) | |
1242 | { | |
1243 | struct sn9c102_device* cam; | |
1244 | ssize_t count; | |
1245 | ||
1246 | cam = video_get_drvdata(to_video_device(cd)); | |
1247 | if (!cam) | |
1248 | return -ENODEV; | |
1249 | ||
1250 | count = sizeof(cam->sysfs.frame_header); | |
1251 | memcpy(buf, cam->sysfs.frame_header, count); | |
1252 | ||
a966f3e7 | 1253 | DBG(3, "Frame header, read bytes: %zd", count); |
1da177e4 LT |
1254 | |
1255 | return count; | |
1256 | } | |
1257 | ||
1258 | ||
1259 | static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR, | |
1260 | sn9c102_show_reg, sn9c102_store_reg); | |
1261 | static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR, | |
1262 | sn9c102_show_val, sn9c102_store_val); | |
1263 | static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR, | |
1264 | sn9c102_show_i2c_reg, sn9c102_store_i2c_reg); | |
1265 | static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR, | |
1266 | sn9c102_show_i2c_val, sn9c102_store_i2c_val); | |
1267 | static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green); | |
1268 | static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue); | |
1269 | static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red); | |
1270 | static CLASS_DEVICE_ATTR(frame_header, S_IRUGO, | |
1271 | sn9c102_show_frame_header, NULL); | |
1272 | ||
1273 | ||
1274 | static void sn9c102_create_sysfs(struct sn9c102_device* cam) | |
1275 | { | |
1276 | struct video_device *v4ldev = cam->v4ldev; | |
1277 | ||
1278 | video_device_create_file(v4ldev, &class_device_attr_reg); | |
1279 | video_device_create_file(v4ldev, &class_device_attr_val); | |
1280 | video_device_create_file(v4ldev, &class_device_attr_frame_header); | |
1281 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) | |
1282 | video_device_create_file(v4ldev, &class_device_attr_green); | |
1283 | else if (cam->bridge == BRIDGE_SN9C103) { | |
1284 | video_device_create_file(v4ldev, &class_device_attr_blue); | |
1285 | video_device_create_file(v4ldev, &class_device_attr_red); | |
1286 | } | |
a966f3e7 | 1287 | if (cam->sensor && cam->sensor->sysfs_ops) { |
1da177e4 LT |
1288 | video_device_create_file(v4ldev, &class_device_attr_i2c_reg); |
1289 | video_device_create_file(v4ldev, &class_device_attr_i2c_val); | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | /*****************************************************************************/ | |
1294 | ||
1295 | static int | |
1296 | sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix) | |
1297 | { | |
1298 | int err = 0; | |
1299 | ||
1300 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) | |
1301 | err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18); | |
1302 | else | |
1303 | err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18); | |
1304 | ||
1305 | return err ? -EIO : 0; | |
1306 | } | |
1307 | ||
1308 | ||
1309 | static int | |
1310 | sn9c102_set_compression(struct sn9c102_device* cam, | |
1311 | struct v4l2_jpegcompression* compression) | |
1312 | { | |
1313 | int err = 0; | |
1314 | ||
1315 | if (compression->quality == 0) | |
1316 | err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17); | |
1317 | else if (compression->quality == 1) | |
1318 | err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17); | |
1319 | ||
1320 | return err ? -EIO : 0; | |
1321 | } | |
1322 | ||
1323 | ||
1324 | static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale) | |
1325 | { | |
1326 | u8 r = 0; | |
1327 | int err = 0; | |
1328 | ||
1329 | if (scale == 1) | |
1330 | r = cam->reg[0x18] & 0xcf; | |
1331 | else if (scale == 2) { | |
1332 | r = cam->reg[0x18] & 0xcf; | |
1333 | r |= 0x10; | |
1334 | } else if (scale == 4) | |
1335 | r = cam->reg[0x18] | 0x20; | |
1336 | ||
1337 | err += sn9c102_write_reg(cam, r, 0x18); | |
1338 | if (err) | |
1339 | return -EIO; | |
1340 | ||
a966f3e7 | 1341 | PDBGG("Scaling factor: %u", scale); |
1da177e4 LT |
1342 | |
1343 | return 0; | |
1344 | } | |
1345 | ||
1346 | ||
1347 | static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect) | |
1348 | { | |
1349 | struct sn9c102_sensor* s = cam->sensor; | |
1350 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left), | |
1351 | v_start = (u8)(rect->top - s->cropcap.bounds.top), | |
1352 | h_size = (u8)(rect->width / 16), | |
1353 | v_size = (u8)(rect->height / 16); | |
1354 | int err = 0; | |
1355 | ||
1356 | err += sn9c102_write_reg(cam, h_start, 0x12); | |
1357 | err += sn9c102_write_reg(cam, v_start, 0x13); | |
1358 | err += sn9c102_write_reg(cam, h_size, 0x15); | |
1359 | err += sn9c102_write_reg(cam, v_size, 0x16); | |
1360 | if (err) | |
1361 | return -EIO; | |
1362 | ||
1363 | PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size " | |
a966f3e7 | 1364 | "%u %u %u %u", h_start, v_start, h_size, v_size); |
1da177e4 LT |
1365 | |
1366 | return 0; | |
1367 | } | |
1368 | ||
1369 | ||
1370 | static int sn9c102_init(struct sn9c102_device* cam) | |
1371 | { | |
1372 | struct sn9c102_sensor* s = cam->sensor; | |
1373 | struct v4l2_control ctrl; | |
1374 | struct v4l2_queryctrl *qctrl; | |
1375 | struct v4l2_rect* rect; | |
52950ed4 | 1376 | u8 i = 0; |
1da177e4 LT |
1377 | int err = 0; |
1378 | ||
1379 | if (!(cam->state & DEV_INITIALIZED)) { | |
1380 | init_waitqueue_head(&cam->open); | |
1381 | qctrl = s->qctrl; | |
1382 | rect = &(s->cropcap.defrect); | |
1383 | } else { /* use current values */ | |
1384 | qctrl = s->_qctrl; | |
1385 | rect = &(s->_rect); | |
1386 | } | |
1387 | ||
1388 | err += sn9c102_set_scale(cam, rect->width / s->pix_format.width); | |
1389 | err += sn9c102_set_crop(cam, rect); | |
1390 | if (err) | |
1391 | return err; | |
1392 | ||
1393 | if (s->init) { | |
1394 | err = s->init(cam); | |
1395 | if (err) { | |
a966f3e7 | 1396 | DBG(3, "Sensor initialization failed"); |
1da177e4 LT |
1397 | return err; |
1398 | } | |
1399 | } | |
1400 | ||
1401 | if (!(cam->state & DEV_INITIALIZED)) | |
1402 | cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1; | |
1403 | else | |
1404 | err += sn9c102_set_compression(cam, &cam->compression); | |
1405 | err += sn9c102_set_pix_format(cam, &s->pix_format); | |
1406 | if (s->set_pix_format) | |
1407 | err += s->set_pix_format(cam, &s->pix_format); | |
1408 | if (err) | |
1409 | return err; | |
1410 | ||
1411 | if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X) | |
52950ed4 | 1412 | DBG(3, "Compressed video format is active, quality %d", |
a966f3e7 | 1413 | cam->compression.quality); |
1da177e4 | 1414 | else |
a966f3e7 | 1415 | DBG(3, "Uncompressed video format is active"); |
1da177e4 LT |
1416 | |
1417 | if (s->set_crop) | |
1418 | if ((err = s->set_crop(cam, rect))) { | |
a966f3e7 | 1419 | DBG(3, "set_crop() failed"); |
1da177e4 LT |
1420 | return err; |
1421 | } | |
1422 | ||
1423 | if (s->set_ctrl) { | |
52950ed4 TK |
1424 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
1425 | if (s->qctrl[i].id != 0 && | |
1da177e4 LT |
1426 | !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) { |
1427 | ctrl.id = s->qctrl[i].id; | |
1428 | ctrl.value = qctrl[i].default_value; | |
1429 | err = s->set_ctrl(cam, &ctrl); | |
1430 | if (err) { | |
1431 | DBG(3, "Set %s control failed", | |
a966f3e7 | 1432 | s->qctrl[i].name); |
1da177e4 LT |
1433 | return err; |
1434 | } | |
1435 | DBG(3, "Image sensor supports '%s' control", | |
a966f3e7 | 1436 | s->qctrl[i].name); |
1da177e4 LT |
1437 | } |
1438 | } | |
1439 | ||
1440 | if (!(cam->state & DEV_INITIALIZED)) { | |
1441 | init_MUTEX(&cam->fileop_sem); | |
1442 | spin_lock_init(&cam->queue_lock); | |
1443 | init_waitqueue_head(&cam->wait_frame); | |
1444 | init_waitqueue_head(&cam->wait_stream); | |
1445 | cam->nreadbuffers = 2; | |
1446 | memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl)); | |
52950ed4 | 1447 | memcpy(&(s->_rect), &(s->cropcap.defrect), |
1da177e4 LT |
1448 | sizeof(struct v4l2_rect)); |
1449 | cam->state |= DEV_INITIALIZED; | |
1450 | } | |
1451 | ||
a966f3e7 | 1452 | DBG(2, "Initialization succeeded"); |
1da177e4 LT |
1453 | return 0; |
1454 | } | |
1455 | ||
1456 | ||
1457 | static void sn9c102_release_resources(struct sn9c102_device* cam) | |
1458 | { | |
1459 | down(&sn9c102_sysfs_lock); | |
1460 | ||
a966f3e7 | 1461 | DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor); |
1da177e4 LT |
1462 | video_set_drvdata(cam->v4ldev, NULL); |
1463 | video_unregister_device(cam->v4ldev); | |
1464 | ||
1465 | up(&sn9c102_sysfs_lock); | |
1466 | ||
1467 | kfree(cam->control_buffer); | |
1468 | } | |
1469 | ||
1470 | /*****************************************************************************/ | |
1471 | ||
1472 | static int sn9c102_open(struct inode* inode, struct file* filp) | |
1473 | { | |
1474 | struct sn9c102_device* cam; | |
1475 | int err = 0; | |
1476 | ||
1477 | /* | |
1478 | This is the only safe way to prevent race conditions with | |
1479 | disconnect | |
1480 | */ | |
1481 | if (!down_read_trylock(&sn9c102_disconnect)) | |
1482 | return -ERESTARTSYS; | |
1483 | ||
1484 | cam = video_get_drvdata(video_devdata(filp)); | |
1485 | ||
1486 | if (down_interruptible(&cam->dev_sem)) { | |
1487 | up_read(&sn9c102_disconnect); | |
1488 | return -ERESTARTSYS; | |
1489 | } | |
1490 | ||
1491 | if (cam->users) { | |
a966f3e7 | 1492 | DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor); |
1da177e4 LT |
1493 | if ((filp->f_flags & O_NONBLOCK) || |
1494 | (filp->f_flags & O_NDELAY)) { | |
1495 | err = -EWOULDBLOCK; | |
1496 | goto out; | |
1497 | } | |
1498 | up(&cam->dev_sem); | |
1499 | err = wait_event_interruptible_exclusive(cam->open, | |
1500 | cam->state & DEV_DISCONNECTED | |
1501 | || !cam->users); | |
1502 | if (err) { | |
1503 | up_read(&sn9c102_disconnect); | |
1504 | return err; | |
1505 | } | |
1506 | if (cam->state & DEV_DISCONNECTED) { | |
1507 | up_read(&sn9c102_disconnect); | |
1508 | return -ENODEV; | |
1509 | } | |
1510 | down(&cam->dev_sem); | |
1511 | } | |
1512 | ||
1513 | ||
1514 | if (cam->state & DEV_MISCONFIGURED) { | |
1515 | err = sn9c102_init(cam); | |
1516 | if (err) { | |
1517 | DBG(1, "Initialization failed again. " | |
a966f3e7 | 1518 | "I will retry on next open()."); |
1da177e4 LT |
1519 | goto out; |
1520 | } | |
1521 | cam->state &= ~DEV_MISCONFIGURED; | |
1522 | } | |
1523 | ||
1524 | if ((err = sn9c102_start_transfer(cam))) | |
1525 | goto out; | |
1526 | ||
1527 | filp->private_data = cam; | |
1528 | cam->users++; | |
1529 | cam->io = IO_NONE; | |
1530 | cam->stream = STREAM_OFF; | |
1531 | cam->nbuffers = 0; | |
1532 | cam->frame_count = 0; | |
1533 | sn9c102_empty_framequeues(cam); | |
1534 | ||
a966f3e7 | 1535 | DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor); |
1da177e4 LT |
1536 | |
1537 | out: | |
1538 | up(&cam->dev_sem); | |
1539 | up_read(&sn9c102_disconnect); | |
1540 | return err; | |
1541 | } | |
1542 | ||
1543 | ||
1544 | static int sn9c102_release(struct inode* inode, struct file* filp) | |
1545 | { | |
1546 | struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); | |
1547 | ||
1548 | down(&cam->dev_sem); /* prevent disconnect() to be called */ | |
1549 | ||
1550 | sn9c102_stop_transfer(cam); | |
1551 | ||
1552 | sn9c102_release_buffers(cam); | |
1553 | ||
1554 | if (cam->state & DEV_DISCONNECTED) { | |
1555 | sn9c102_release_resources(cam); | |
1556 | up(&cam->dev_sem); | |
1557 | kfree(cam); | |
1558 | return 0; | |
1559 | } | |
1560 | ||
1561 | cam->users--; | |
1562 | wake_up_interruptible_nr(&cam->open, 1); | |
1563 | ||
a966f3e7 | 1564 | DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor); |
1da177e4 LT |
1565 | |
1566 | up(&cam->dev_sem); | |
1567 | ||
1568 | return 0; | |
1569 | } | |
1570 | ||
1571 | ||
1572 | static ssize_t | |
1573 | sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos) | |
1574 | { | |
1575 | struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); | |
1576 | struct sn9c102_frame_t* f, * i; | |
1577 | unsigned long lock_flags; | |
1578 | int err = 0; | |
1579 | ||
1580 | if (down_interruptible(&cam->fileop_sem)) | |
1581 | return -ERESTARTSYS; | |
1582 | ||
1583 | if (cam->state & DEV_DISCONNECTED) { | |
a966f3e7 | 1584 | DBG(1, "Device not present"); |
1da177e4 LT |
1585 | up(&cam->fileop_sem); |
1586 | return -ENODEV; | |
1587 | } | |
1588 | ||
1589 | if (cam->state & DEV_MISCONFIGURED) { | |
a966f3e7 LR |
1590 | DBG(1, "The camera is misconfigured. Close and open it " |
1591 | "again."); | |
1da177e4 LT |
1592 | up(&cam->fileop_sem); |
1593 | return -EIO; | |
1594 | } | |
1595 | ||
1596 | if (cam->io == IO_MMAP) { | |
1597 | DBG(3, "Close and open the device again to choose " | |
a966f3e7 | 1598 | "the read method"); |
1da177e4 LT |
1599 | up(&cam->fileop_sem); |
1600 | return -EINVAL; | |
1601 | } | |
1602 | ||
1603 | if (cam->io == IO_NONE) { | |
1604 | if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) { | |
a966f3e7 | 1605 | DBG(1, "read() failed, not enough memory"); |
1da177e4 LT |
1606 | up(&cam->fileop_sem); |
1607 | return -ENOMEM; | |
1608 | } | |
1609 | cam->io = IO_READ; | |
1610 | cam->stream = STREAM_ON; | |
a966f3e7 LR |
1611 | } |
1612 | ||
1613 | if (list_empty(&cam->inqueue)) { | |
1614 | if (!list_empty(&cam->outqueue)) | |
1615 | sn9c102_empty_framequeues(cam); | |
1da177e4 LT |
1616 | sn9c102_queue_unusedframes(cam); |
1617 | } | |
1618 | ||
1619 | if (!count) { | |
1620 | up(&cam->fileop_sem); | |
1621 | return 0; | |
1622 | } | |
1623 | ||
1624 | if (list_empty(&cam->outqueue)) { | |
1625 | if (filp->f_flags & O_NONBLOCK) { | |
1626 | up(&cam->fileop_sem); | |
1627 | return -EAGAIN; | |
1628 | } | |
1629 | err = wait_event_interruptible | |
1630 | ( cam->wait_frame, | |
1631 | (!list_empty(&cam->outqueue)) || | |
1632 | (cam->state & DEV_DISCONNECTED) || | |
1633 | (cam->state & DEV_MISCONFIGURED) ); | |
1634 | if (err) { | |
1635 | up(&cam->fileop_sem); | |
1636 | return err; | |
1637 | } | |
1638 | if (cam->state & DEV_DISCONNECTED) { | |
1639 | up(&cam->fileop_sem); | |
1640 | return -ENODEV; | |
1641 | } | |
1642 | if (cam->state & DEV_MISCONFIGURED) { | |
1643 | up(&cam->fileop_sem); | |
1644 | return -EIO; | |
1645 | } | |
1646 | } | |
1647 | ||
1648 | f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame); | |
1649 | ||
a966f3e7 LR |
1650 | if (count > f->buf.bytesused) |
1651 | count = f->buf.bytesused; | |
1652 | ||
1653 | if (copy_to_user(buf, f->bufmem, count)) { | |
1654 | err = -EFAULT; | |
1655 | goto exit; | |
1656 | } | |
1657 | *f_pos += count; | |
1658 | ||
1659 | exit: | |
1da177e4 LT |
1660 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
1661 | list_for_each_entry(i, &cam->outqueue, frame) | |
1662 | i->state = F_UNUSED; | |
1663 | INIT_LIST_HEAD(&cam->outqueue); | |
1664 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
1665 | ||
1666 | sn9c102_queue_unusedframes(cam); | |
1667 | ||
a966f3e7 LR |
1668 | PDBGG("Frame #%lu, bytes read: %zu", |
1669 | (unsigned long)f->buf.index, count); | |
1da177e4 LT |
1670 | |
1671 | up(&cam->fileop_sem); | |
1672 | ||
1673 | return count; | |
1674 | } | |
1675 | ||
1676 | ||
1677 | static unsigned int sn9c102_poll(struct file *filp, poll_table *wait) | |
1678 | { | |
1679 | struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); | |
a966f3e7 LR |
1680 | struct sn9c102_frame_t* f; |
1681 | unsigned long lock_flags; | |
1da177e4 LT |
1682 | unsigned int mask = 0; |
1683 | ||
1684 | if (down_interruptible(&cam->fileop_sem)) | |
1685 | return POLLERR; | |
1686 | ||
1687 | if (cam->state & DEV_DISCONNECTED) { | |
a966f3e7 | 1688 | DBG(1, "Device not present"); |
1da177e4 LT |
1689 | goto error; |
1690 | } | |
1691 | ||
1692 | if (cam->state & DEV_MISCONFIGURED) { | |
a966f3e7 LR |
1693 | DBG(1, "The camera is misconfigured. Close and open it " |
1694 | "again."); | |
1da177e4 LT |
1695 | goto error; |
1696 | } | |
1697 | ||
1698 | if (cam->io == IO_NONE) { | |
1699 | if (!sn9c102_request_buffers(cam, cam->nreadbuffers, | |
1700 | IO_READ)) { | |
a966f3e7 | 1701 | DBG(1, "poll() failed, not enough memory"); |
1da177e4 LT |
1702 | goto error; |
1703 | } | |
1704 | cam->io = IO_READ; | |
1705 | cam->stream = STREAM_ON; | |
1706 | } | |
1707 | ||
a966f3e7 LR |
1708 | if (cam->io == IO_READ) { |
1709 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | |
1710 | list_for_each_entry(f, &cam->outqueue, frame) | |
1711 | f->state = F_UNUSED; | |
1712 | INIT_LIST_HEAD(&cam->outqueue); | |
1713 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
1da177e4 | 1714 | sn9c102_queue_unusedframes(cam); |
a966f3e7 | 1715 | } |
1da177e4 LT |
1716 | |
1717 | poll_wait(filp, &cam->wait_frame, wait); | |
1718 | ||
1719 | if (!list_empty(&cam->outqueue)) | |
1720 | mask |= POLLIN | POLLRDNORM; | |
1721 | ||
1722 | up(&cam->fileop_sem); | |
1723 | ||
1724 | return mask; | |
1725 | ||
1726 | error: | |
1727 | up(&cam->fileop_sem); | |
1728 | return POLLERR; | |
1729 | } | |
1730 | ||
1731 | ||
1732 | static void sn9c102_vm_open(struct vm_area_struct* vma) | |
1733 | { | |
1734 | struct sn9c102_frame_t* f = vma->vm_private_data; | |
1735 | f->vma_use_count++; | |
1736 | } | |
1737 | ||
1738 | ||
1739 | static void sn9c102_vm_close(struct vm_area_struct* vma) | |
1740 | { | |
1741 | /* NOTE: buffers are not freed here */ | |
1742 | struct sn9c102_frame_t* f = vma->vm_private_data; | |
1743 | f->vma_use_count--; | |
1744 | } | |
1745 | ||
1746 | ||
1747 | static struct vm_operations_struct sn9c102_vm_ops = { | |
1748 | .open = sn9c102_vm_open, | |
1749 | .close = sn9c102_vm_close, | |
1750 | }; | |
1751 | ||
1752 | ||
1753 | static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma) | |
1754 | { | |
1755 | struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); | |
1756 | unsigned long size = vma->vm_end - vma->vm_start, | |
1757 | start = vma->vm_start, | |
1758 | pos, | |
1759 | page; | |
1760 | u32 i; | |
1761 | ||
1762 | if (down_interruptible(&cam->fileop_sem)) | |
1763 | return -ERESTARTSYS; | |
1764 | ||
1765 | if (cam->state & DEV_DISCONNECTED) { | |
a966f3e7 | 1766 | DBG(1, "Device not present"); |
1da177e4 LT |
1767 | up(&cam->fileop_sem); |
1768 | return -ENODEV; | |
1769 | } | |
1770 | ||
1771 | if (cam->state & DEV_MISCONFIGURED) { | |
a966f3e7 LR |
1772 | DBG(1, "The camera is misconfigured. Close and open it " |
1773 | "again."); | |
1da177e4 LT |
1774 | up(&cam->fileop_sem); |
1775 | return -EIO; | |
1776 | } | |
1777 | ||
1778 | if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || | |
1779 | size != PAGE_ALIGN(cam->frame[0].buf.length)) { | |
1780 | up(&cam->fileop_sem); | |
1781 | return -EINVAL; | |
1782 | } | |
1783 | ||
1784 | for (i = 0; i < cam->nbuffers; i++) { | |
1785 | if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff) | |
1786 | break; | |
1787 | } | |
1788 | if (i == cam->nbuffers) { | |
1789 | up(&cam->fileop_sem); | |
1790 | return -EINVAL; | |
1791 | } | |
1792 | ||
1793 | /* VM_IO is eventually going to replace PageReserved altogether */ | |
1794 | vma->vm_flags |= VM_IO; | |
1795 | vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ | |
1796 | ||
1797 | pos = (unsigned long)cam->frame[i].bufmem; | |
1798 | while (size > 0) { /* size is page-aligned */ | |
1799 | page = vmalloc_to_pfn((void *)pos); | |
1800 | if (remap_pfn_range(vma, start, page, PAGE_SIZE, | |
1801 | vma->vm_page_prot)) { | |
1802 | up(&cam->fileop_sem); | |
1803 | return -EAGAIN; | |
1804 | } | |
1805 | start += PAGE_SIZE; | |
1806 | pos += PAGE_SIZE; | |
1807 | size -= PAGE_SIZE; | |
1808 | } | |
1809 | ||
1810 | vma->vm_ops = &sn9c102_vm_ops; | |
1811 | vma->vm_private_data = &cam->frame[i]; | |
1812 | ||
1813 | sn9c102_vm_open(vma); | |
1814 | ||
1815 | up(&cam->fileop_sem); | |
1816 | ||
1817 | return 0; | |
1818 | } | |
1819 | ||
a966f3e7 | 1820 | /*****************************************************************************/ |
1da177e4 | 1821 | |
a966f3e7 LR |
1822 | static int |
1823 | sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg) | |
1da177e4 | 1824 | { |
a966f3e7 LR |
1825 | struct v4l2_capability cap = { |
1826 | .driver = "sn9c102", | |
1827 | .version = SN9C102_MODULE_VERSION_CODE, | |
1828 | .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | | |
1829 | V4L2_CAP_STREAMING, | |
1830 | }; | |
1831 | ||
1832 | strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card)); | |
1833 | if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0) | |
1834 | strlcpy(cap.bus_info, cam->dev.bus_id, sizeof(cap.bus_info)); | |
1835 | ||
1836 | if (copy_to_user(arg, &cap, sizeof(cap))) | |
1837 | return -EFAULT; | |
1da177e4 | 1838 | |
a966f3e7 LR |
1839 | return 0; |
1840 | } | |
1da177e4 | 1841 | |
1da177e4 | 1842 | |
a966f3e7 LR |
1843 | static int |
1844 | sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg) | |
1845 | { | |
1846 | struct v4l2_input i; | |
1da177e4 | 1847 | |
a966f3e7 LR |
1848 | if (copy_from_user(&i, arg, sizeof(i))) |
1849 | return -EFAULT; | |
1da177e4 | 1850 | |
a966f3e7 LR |
1851 | if (i.index) |
1852 | return -EINVAL; | |
1da177e4 | 1853 | |
a966f3e7 LR |
1854 | memset(&i, 0, sizeof(i)); |
1855 | strcpy(i.name, "USB"); | |
1da177e4 | 1856 | |
a966f3e7 LR |
1857 | if (copy_to_user(arg, &i, sizeof(i))) |
1858 | return -EFAULT; | |
1da177e4 | 1859 | |
a966f3e7 LR |
1860 | return 0; |
1861 | } | |
1da177e4 | 1862 | |
1da177e4 | 1863 | |
a966f3e7 LR |
1864 | static int |
1865 | sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg) | |
1866 | { | |
1867 | int index; | |
1da177e4 | 1868 | |
a966f3e7 LR |
1869 | if (copy_from_user(&index, arg, sizeof(index))) |
1870 | return -EFAULT; | |
1da177e4 | 1871 | |
a966f3e7 | 1872 | if (index != 0) |
1da177e4 | 1873 | return -EINVAL; |
1da177e4 | 1874 | |
a966f3e7 LR |
1875 | return 0; |
1876 | } | |
1da177e4 | 1877 | |
1da177e4 | 1878 | |
a966f3e7 LR |
1879 | static int |
1880 | sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg) | |
1881 | { | |
1882 | struct sn9c102_sensor* s = cam->sensor; | |
1883 | struct v4l2_queryctrl qc; | |
1884 | u8 i; | |
1da177e4 | 1885 | |
a966f3e7 LR |
1886 | if (copy_from_user(&qc, arg, sizeof(qc))) |
1887 | return -EFAULT; | |
1da177e4 | 1888 | |
a966f3e7 LR |
1889 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
1890 | if (qc.id && qc.id == s->qctrl[i].id) { | |
1891 | memcpy(&qc, &(s->qctrl[i]), sizeof(qc)); | |
1892 | if (copy_to_user(arg, &qc, sizeof(qc))) | |
1893 | return -EFAULT; | |
1894 | return 0; | |
1895 | } | |
1da177e4 | 1896 | |
a966f3e7 LR |
1897 | return -EINVAL; |
1898 | } | |
1da177e4 | 1899 | |
1da177e4 | 1900 | |
a966f3e7 LR |
1901 | static int |
1902 | sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg) | |
1903 | { | |
1904 | struct sn9c102_sensor* s = cam->sensor; | |
1905 | struct v4l2_control ctrl; | |
1906 | int err = 0; | |
1907 | u8 i; | |
1da177e4 | 1908 | |
a966f3e7 LR |
1909 | if (!s->get_ctrl && !s->set_ctrl) |
1910 | return -EINVAL; | |
1da177e4 | 1911 | |
a966f3e7 LR |
1912 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
1913 | return -EFAULT; | |
1914 | ||
1915 | if (!s->get_ctrl) { | |
52950ed4 | 1916 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
a966f3e7 LR |
1917 | if (ctrl.id && ctrl.id == s->qctrl[i].id) { |
1918 | ctrl.value = s->_qctrl[i].default_value; | |
1919 | goto exit; | |
1da177e4 | 1920 | } |
a966f3e7 LR |
1921 | return -EINVAL; |
1922 | } else | |
1923 | err = s->get_ctrl(cam, &ctrl); | |
1da177e4 | 1924 | |
a966f3e7 LR |
1925 | exit: |
1926 | if (copy_to_user(arg, &ctrl, sizeof(ctrl))) | |
1927 | return -EFAULT; | |
1da177e4 | 1928 | |
a966f3e7 LR |
1929 | return err; |
1930 | } | |
1da177e4 | 1931 | |
1da177e4 | 1932 | |
a966f3e7 LR |
1933 | static int |
1934 | sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg) | |
1935 | { | |
1936 | struct sn9c102_sensor* s = cam->sensor; | |
1937 | struct v4l2_control ctrl; | |
1938 | u8 i; | |
1939 | int err = 0; | |
1da177e4 | 1940 | |
a966f3e7 LR |
1941 | if (!s->set_ctrl) |
1942 | return -EINVAL; | |
1da177e4 | 1943 | |
a966f3e7 LR |
1944 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
1945 | return -EFAULT; | |
1da177e4 | 1946 | |
a966f3e7 LR |
1947 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
1948 | if (ctrl.id == s->qctrl[i].id) { | |
1949 | if (ctrl.value < s->qctrl[i].minimum || | |
1950 | ctrl.value > s->qctrl[i].maximum) | |
1951 | return -ERANGE; | |
1952 | ctrl.value -= ctrl.value % s->qctrl[i].step; | |
1953 | break; | |
1954 | } | |
1da177e4 | 1955 | |
a966f3e7 LR |
1956 | if ((err = s->set_ctrl(cam, &ctrl))) |
1957 | return err; | |
1da177e4 | 1958 | |
a966f3e7 | 1959 | s->_qctrl[i].default_value = ctrl.value; |
1da177e4 | 1960 | |
a966f3e7 LR |
1961 | PDBGG("VIDIOC_S_CTRL: id %lu, value %lu", |
1962 | (unsigned long)ctrl.id, (unsigned long)ctrl.value); | |
1da177e4 | 1963 | |
a966f3e7 LR |
1964 | return 0; |
1965 | } | |
1da177e4 | 1966 | |
1da177e4 | 1967 | |
a966f3e7 LR |
1968 | static int |
1969 | sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg) | |
1970 | { | |
1971 | struct v4l2_cropcap* cc = &(cam->sensor->cropcap); | |
1da177e4 | 1972 | |
a966f3e7 LR |
1973 | cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
1974 | cc->pixelaspect.numerator = 1; | |
1975 | cc->pixelaspect.denominator = 1; | |
1da177e4 | 1976 | |
a966f3e7 LR |
1977 | if (copy_to_user(arg, cc, sizeof(*cc))) |
1978 | return -EFAULT; | |
1da177e4 | 1979 | |
a966f3e7 LR |
1980 | return 0; |
1981 | } | |
1da177e4 | 1982 | |
1da177e4 | 1983 | |
a966f3e7 LR |
1984 | static int |
1985 | sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg) | |
1986 | { | |
1987 | struct sn9c102_sensor* s = cam->sensor; | |
1988 | struct v4l2_crop crop = { | |
1989 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
1990 | }; | |
1da177e4 | 1991 | |
a966f3e7 | 1992 | memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect)); |
1da177e4 | 1993 | |
a966f3e7 LR |
1994 | if (copy_to_user(arg, &crop, sizeof(crop))) |
1995 | return -EFAULT; | |
1da177e4 | 1996 | |
a966f3e7 LR |
1997 | return 0; |
1998 | } | |
1da177e4 | 1999 | |
1da177e4 | 2000 | |
a966f3e7 LR |
2001 | static int |
2002 | sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg) | |
2003 | { | |
2004 | struct sn9c102_sensor* s = cam->sensor; | |
2005 | struct v4l2_crop crop; | |
2006 | struct v4l2_rect* rect; | |
2007 | struct v4l2_rect* bounds = &(s->cropcap.bounds); | |
2008 | struct v4l2_pix_format* pix_format = &(s->pix_format); | |
2009 | u8 scale; | |
2010 | const enum sn9c102_stream_state stream = cam->stream; | |
2011 | const u32 nbuffers = cam->nbuffers; | |
2012 | u32 i; | |
2013 | int err = 0; | |
1da177e4 | 2014 | |
a966f3e7 LR |
2015 | if (copy_from_user(&crop, arg, sizeof(crop))) |
2016 | return -EFAULT; | |
1da177e4 | 2017 | |
a966f3e7 | 2018 | rect = &(crop.c); |
1da177e4 | 2019 | |
a966f3e7 LR |
2020 | if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
2021 | return -EINVAL; | |
1da177e4 | 2022 | |
a966f3e7 LR |
2023 | if (cam->module_param.force_munmap) |
2024 | for (i = 0; i < cam->nbuffers; i++) | |
2025 | if (cam->frame[i].vma_use_count) { | |
2026 | DBG(3, "VIDIOC_S_CROP failed. " | |
2027 | "Unmap the buffers first."); | |
2028 | return -EINVAL; | |
2029 | } | |
1da177e4 | 2030 | |
a966f3e7 LR |
2031 | /* Preserve R,G or B origin */ |
2032 | rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L; | |
2033 | rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L; | |
2034 | ||
2035 | if (rect->width < 16) | |
2036 | rect->width = 16; | |
2037 | if (rect->height < 16) | |
2038 | rect->height = 16; | |
2039 | if (rect->width > bounds->width) | |
2040 | rect->width = bounds->width; | |
2041 | if (rect->height > bounds->height) | |
2042 | rect->height = bounds->height; | |
2043 | if (rect->left < bounds->left) | |
2044 | rect->left = bounds->left; | |
2045 | if (rect->top < bounds->top) | |
2046 | rect->top = bounds->top; | |
2047 | if (rect->left + rect->width > bounds->left + bounds->width) | |
2048 | rect->left = bounds->left+bounds->width - rect->width; | |
2049 | if (rect->top + rect->height > bounds->top + bounds->height) | |
2050 | rect->top = bounds->top+bounds->height - rect->height; | |
2051 | ||
2052 | rect->width &= ~15L; | |
2053 | rect->height &= ~15L; | |
2054 | ||
2055 | if (SN9C102_PRESERVE_IMGSCALE) { | |
2056 | /* Calculate the actual scaling factor */ | |
2057 | u32 a, b; | |
2058 | a = rect->width * rect->height; | |
2059 | b = pix_format->width * pix_format->height; | |
2060 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | |
2061 | } else | |
2062 | scale = 1; | |
2063 | ||
2064 | if (cam->stream == STREAM_ON) | |
2065 | if ((err = sn9c102_stream_interrupt(cam))) | |
2066 | return err; | |
1da177e4 | 2067 | |
a966f3e7 LR |
2068 | if (copy_to_user(arg, &crop, sizeof(crop))) { |
2069 | cam->stream = stream; | |
2070 | return -EFAULT; | |
1da177e4 LT |
2071 | } |
2072 | ||
a966f3e7 LR |
2073 | if (cam->module_param.force_munmap || cam->io == IO_READ) |
2074 | sn9c102_release_buffers(cam); | |
1da177e4 | 2075 | |
a966f3e7 LR |
2076 | err = sn9c102_set_crop(cam, rect); |
2077 | if (s->set_crop) | |
2078 | err += s->set_crop(cam, rect); | |
2079 | err += sn9c102_set_scale(cam, scale); | |
1da177e4 | 2080 | |
a966f3e7 LR |
2081 | if (err) { /* atomic, no rollback in ioctl() */ |
2082 | cam->state |= DEV_MISCONFIGURED; | |
2083 | DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To " | |
2084 | "use the camera, close and open /dev/video%d again.", | |
2085 | cam->v4ldev->minor); | |
2086 | return -EIO; | |
2087 | } | |
1da177e4 | 2088 | |
a966f3e7 LR |
2089 | s->pix_format.width = rect->width/scale; |
2090 | s->pix_format.height = rect->height/scale; | |
2091 | memcpy(&(s->_rect), rect, sizeof(*rect)); | |
1da177e4 | 2092 | |
a966f3e7 LR |
2093 | if ((cam->module_param.force_munmap || cam->io == IO_READ) && |
2094 | nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) { | |
2095 | cam->state |= DEV_MISCONFIGURED; | |
2096 | DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To " | |
2097 | "use the camera, close and open /dev/video%d again.", | |
2098 | cam->v4ldev->minor); | |
2099 | return -ENOMEM; | |
1da177e4 LT |
2100 | } |
2101 | ||
a966f3e7 LR |
2102 | if (cam->io == IO_READ) |
2103 | sn9c102_empty_framequeues(cam); | |
2104 | else if (cam->module_param.force_munmap) | |
2105 | sn9c102_requeue_outqueue(cam); | |
1da177e4 | 2106 | |
a966f3e7 | 2107 | cam->stream = stream; |
1da177e4 | 2108 | |
a966f3e7 LR |
2109 | return 0; |
2110 | } | |
1da177e4 | 2111 | |
1da177e4 | 2112 | |
a966f3e7 LR |
2113 | static int |
2114 | sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg) | |
2115 | { | |
2116 | struct v4l2_fmtdesc fmtd; | |
1da177e4 | 2117 | |
a966f3e7 LR |
2118 | if (copy_from_user(&fmtd, arg, sizeof(fmtd))) |
2119 | return -EFAULT; | |
1da177e4 | 2120 | |
a966f3e7 LR |
2121 | if (fmtd.index == 0) { |
2122 | strcpy(fmtd.description, "bayer rgb"); | |
2123 | fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8; | |
2124 | } else if (fmtd.index == 1) { | |
2125 | strcpy(fmtd.description, "compressed"); | |
2126 | fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X; | |
2127 | fmtd.flags = V4L2_FMT_FLAG_COMPRESSED; | |
2128 | } else | |
2129 | return -EINVAL; | |
1da177e4 | 2130 | |
a966f3e7 LR |
2131 | fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
2132 | memset(&fmtd.reserved, 0, sizeof(fmtd.reserved)); | |
1da177e4 | 2133 | |
a966f3e7 LR |
2134 | if (copy_to_user(arg, &fmtd, sizeof(fmtd))) |
2135 | return -EFAULT; | |
1da177e4 | 2136 | |
a966f3e7 LR |
2137 | return 0; |
2138 | } | |
1da177e4 | 2139 | |
1da177e4 | 2140 | |
a966f3e7 LR |
2141 | static int |
2142 | sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg) | |
2143 | { | |
2144 | struct v4l2_format format; | |
2145 | struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format); | |
1da177e4 | 2146 | |
a966f3e7 LR |
2147 | if (copy_from_user(&format, arg, sizeof(format))) |
2148 | return -EFAULT; | |
1da177e4 | 2149 | |
a966f3e7 LR |
2150 | if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
2151 | return -EINVAL; | |
1da177e4 | 2152 | |
a966f3e7 LR |
2153 | pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X) |
2154 | ? 0 : (pfmt->width * pfmt->priv) / 8; | |
2155 | pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8); | |
2156 | pfmt->field = V4L2_FIELD_NONE; | |
2157 | memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt)); | |
1da177e4 | 2158 | |
a966f3e7 LR |
2159 | if (copy_to_user(arg, &format, sizeof(format))) |
2160 | return -EFAULT; | |
1da177e4 | 2161 | |
a966f3e7 LR |
2162 | return 0; |
2163 | } | |
1da177e4 | 2164 | |
1da177e4 | 2165 | |
a966f3e7 LR |
2166 | static int |
2167 | sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd, | |
2168 | void __user * arg) | |
2169 | { | |
2170 | struct sn9c102_sensor* s = cam->sensor; | |
2171 | struct v4l2_format format; | |
2172 | struct v4l2_pix_format* pix; | |
2173 | struct v4l2_pix_format* pfmt = &(s->pix_format); | |
2174 | struct v4l2_rect* bounds = &(s->cropcap.bounds); | |
2175 | struct v4l2_rect rect; | |
2176 | u8 scale; | |
2177 | const enum sn9c102_stream_state stream = cam->stream; | |
2178 | const u32 nbuffers = cam->nbuffers; | |
2179 | u32 i; | |
2180 | int err = 0; | |
1da177e4 | 2181 | |
a966f3e7 LR |
2182 | if (copy_from_user(&format, arg, sizeof(format))) |
2183 | return -EFAULT; | |
1da177e4 | 2184 | |
a966f3e7 | 2185 | pix = &(format.fmt.pix); |
1da177e4 | 2186 | |
a966f3e7 LR |
2187 | if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
2188 | return -EINVAL; | |
1da177e4 | 2189 | |
a966f3e7 | 2190 | memcpy(&rect, &(s->_rect), sizeof(rect)); |
1da177e4 | 2191 | |
a966f3e7 LR |
2192 | { /* calculate the actual scaling factor */ |
2193 | u32 a, b; | |
2194 | a = rect.width * rect.height; | |
2195 | b = pix->width * pix->height; | |
2196 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | |
1da177e4 LT |
2197 | } |
2198 | ||
a966f3e7 LR |
2199 | rect.width = scale * pix->width; |
2200 | rect.height = scale * pix->height; | |
1da177e4 | 2201 | |
a966f3e7 LR |
2202 | if (rect.width < 16) |
2203 | rect.width = 16; | |
2204 | if (rect.height < 16) | |
2205 | rect.height = 16; | |
2206 | if (rect.width > bounds->left + bounds->width - rect.left) | |
2207 | rect.width = bounds->left + bounds->width - rect.left; | |
2208 | if (rect.height > bounds->top + bounds->height - rect.top) | |
2209 | rect.height = bounds->top + bounds->height - rect.top; | |
1da177e4 | 2210 | |
a966f3e7 LR |
2211 | rect.width &= ~15L; |
2212 | rect.height &= ~15L; | |
1da177e4 | 2213 | |
a966f3e7 LR |
2214 | { /* adjust the scaling factor */ |
2215 | u32 a, b; | |
2216 | a = rect.width * rect.height; | |
2217 | b = pix->width * pix->height; | |
2218 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | |
2219 | } | |
2220 | ||
2221 | pix->width = rect.width / scale; | |
2222 | pix->height = rect.height / scale; | |
2223 | ||
2224 | if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X && | |
2225 | pix->pixelformat != V4L2_PIX_FMT_SBGGR8) | |
2226 | pix->pixelformat = pfmt->pixelformat; | |
2227 | pix->priv = pfmt->priv; /* bpp */ | |
2228 | pix->colorspace = pfmt->colorspace; | |
2229 | pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) | |
2230 | ? 0 : (pix->width * pix->priv) / 8; | |
2231 | pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8); | |
2232 | pix->field = V4L2_FIELD_NONE; | |
2233 | ||
2234 | if (cmd == VIDIOC_TRY_FMT) { | |
2235 | if (copy_to_user(arg, &format, sizeof(format))) | |
2236 | return -EFAULT; | |
2237 | return 0; | |
2238 | } | |
1da177e4 | 2239 | |
a966f3e7 | 2240 | if (cam->module_param.force_munmap) |
1da177e4 LT |
2241 | for (i = 0; i < cam->nbuffers; i++) |
2242 | if (cam->frame[i].vma_use_count) { | |
a966f3e7 LR |
2243 | DBG(3, "VIDIOC_S_FMT failed. Unmap the " |
2244 | "buffers first."); | |
1da177e4 LT |
2245 | return -EINVAL; |
2246 | } | |
2247 | ||
a966f3e7 LR |
2248 | if (cam->stream == STREAM_ON) |
2249 | if ((err = sn9c102_stream_interrupt(cam))) | |
2250 | return err; | |
1da177e4 | 2251 | |
a966f3e7 LR |
2252 | if (copy_to_user(arg, &format, sizeof(format))) { |
2253 | cam->stream = stream; | |
2254 | return -EFAULT; | |
2255 | } | |
1da177e4 | 2256 | |
a966f3e7 | 2257 | if (cam->module_param.force_munmap || cam->io == IO_READ) |
1da177e4 | 2258 | sn9c102_release_buffers(cam); |
1da177e4 | 2259 | |
a966f3e7 LR |
2260 | err += sn9c102_set_pix_format(cam, pix); |
2261 | err += sn9c102_set_crop(cam, &rect); | |
2262 | if (s->set_pix_format) | |
2263 | err += s->set_pix_format(cam, pix); | |
2264 | if (s->set_crop) | |
2265 | err += s->set_crop(cam, &rect); | |
2266 | err += sn9c102_set_scale(cam, scale); | |
1da177e4 | 2267 | |
a966f3e7 LR |
2268 | if (err) { /* atomic, no rollback in ioctl() */ |
2269 | cam->state |= DEV_MISCONFIGURED; | |
2270 | DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To " | |
2271 | "use the camera, close and open /dev/video%d again.", | |
2272 | cam->v4ldev->minor); | |
2273 | return -EIO; | |
2274 | } | |
1da177e4 | 2275 | |
a966f3e7 LR |
2276 | memcpy(pfmt, pix, sizeof(*pix)); |
2277 | memcpy(&(s->_rect), &rect, sizeof(rect)); | |
2278 | ||
2279 | if ((cam->module_param.force_munmap || cam->io == IO_READ) && | |
2280 | nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) { | |
2281 | cam->state |= DEV_MISCONFIGURED; | |
2282 | DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To " | |
2283 | "use the camera, close and open /dev/video%d again.", | |
2284 | cam->v4ldev->minor); | |
2285 | return -ENOMEM; | |
1da177e4 LT |
2286 | } |
2287 | ||
a966f3e7 LR |
2288 | if (cam->io == IO_READ) |
2289 | sn9c102_empty_framequeues(cam); | |
2290 | else if (cam->module_param.force_munmap) | |
2291 | sn9c102_requeue_outqueue(cam); | |
1da177e4 | 2292 | |
a966f3e7 | 2293 | cam->stream = stream; |
1da177e4 | 2294 | |
a966f3e7 LR |
2295 | return 0; |
2296 | } | |
2297 | ||
2298 | ||
2299 | static int | |
2300 | sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg) | |
2301 | { | |
2302 | if (copy_to_user(arg, &cam->compression, | |
2303 | sizeof(cam->compression))) | |
2304 | return -EFAULT; | |
1da177e4 | 2305 | |
a966f3e7 LR |
2306 | return 0; |
2307 | } | |
1da177e4 | 2308 | |
1da177e4 | 2309 | |
a966f3e7 LR |
2310 | static int |
2311 | sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg) | |
2312 | { | |
2313 | struct v4l2_jpegcompression jc; | |
2314 | const enum sn9c102_stream_state stream = cam->stream; | |
2315 | int err = 0; | |
1da177e4 | 2316 | |
a966f3e7 LR |
2317 | if (copy_from_user(&jc, arg, sizeof(jc))) |
2318 | return -EFAULT; | |
1da177e4 | 2319 | |
a966f3e7 LR |
2320 | if (jc.quality != 0 && jc.quality != 1) |
2321 | return -EINVAL; | |
2322 | ||
2323 | if (cam->stream == STREAM_ON) | |
2324 | if ((err = sn9c102_stream_interrupt(cam))) | |
2325 | return err; | |
2326 | ||
2327 | err += sn9c102_set_compression(cam, &jc); | |
2328 | if (err) { /* atomic, no rollback in ioctl() */ | |
2329 | cam->state |= DEV_MISCONFIGURED; | |
2330 | DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware " | |
2331 | "problems. To use the camera, close and open " | |
2332 | "/dev/video%d again.", cam->v4ldev->minor); | |
2333 | return -EIO; | |
1da177e4 LT |
2334 | } |
2335 | ||
a966f3e7 | 2336 | cam->compression.quality = jc.quality; |
1da177e4 | 2337 | |
a966f3e7 | 2338 | cam->stream = stream; |
1da177e4 | 2339 | |
a966f3e7 LR |
2340 | return 0; |
2341 | } | |
1da177e4 | 2342 | |
a966f3e7 LR |
2343 | |
2344 | static int | |
2345 | sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg) | |
2346 | { | |
2347 | struct v4l2_requestbuffers rb; | |
2348 | u32 i; | |
2349 | int err; | |
2350 | ||
2351 | if (copy_from_user(&rb, arg, sizeof(rb))) | |
2352 | return -EFAULT; | |
2353 | ||
2354 | if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | |
2355 | rb.memory != V4L2_MEMORY_MMAP) | |
2356 | return -EINVAL; | |
2357 | ||
2358 | if (cam->io == IO_READ) { | |
2359 | DBG(3, "Close and open the device again to choose the mmap " | |
2360 | "I/O method"); | |
2361 | return -EINVAL; | |
2362 | } | |
2363 | ||
2364 | for (i = 0; i < cam->nbuffers; i++) | |
2365 | if (cam->frame[i].vma_use_count) { | |
2366 | DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are " | |
2367 | "still mapped."); | |
1da177e4 | 2368 | return -EINVAL; |
a966f3e7 | 2369 | } |
1da177e4 | 2370 | |
a966f3e7 LR |
2371 | if (cam->stream == STREAM_ON) |
2372 | if ((err = sn9c102_stream_interrupt(cam))) | |
2373 | return err; | |
1da177e4 | 2374 | |
a966f3e7 | 2375 | sn9c102_empty_framequeues(cam); |
1da177e4 | 2376 | |
a966f3e7 LR |
2377 | sn9c102_release_buffers(cam); |
2378 | if (rb.count) | |
2379 | rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP); | |
1da177e4 | 2380 | |
a966f3e7 LR |
2381 | if (copy_to_user(arg, &rb, sizeof(rb))) { |
2382 | sn9c102_release_buffers(cam); | |
2383 | cam->io = IO_NONE; | |
2384 | return -EFAULT; | |
1da177e4 LT |
2385 | } |
2386 | ||
a966f3e7 | 2387 | cam->io = rb.count ? IO_MMAP : IO_NONE; |
1da177e4 | 2388 | |
a966f3e7 LR |
2389 | return 0; |
2390 | } | |
1da177e4 | 2391 | |
1da177e4 | 2392 | |
a966f3e7 LR |
2393 | static int |
2394 | sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg) | |
2395 | { | |
2396 | struct v4l2_buffer b; | |
1da177e4 | 2397 | |
a966f3e7 LR |
2398 | if (copy_from_user(&b, arg, sizeof(b))) |
2399 | return -EFAULT; | |
1da177e4 | 2400 | |
a966f3e7 LR |
2401 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
2402 | b.index >= cam->nbuffers || cam->io != IO_MMAP) | |
2403 | return -EINVAL; | |
1da177e4 | 2404 | |
a966f3e7 | 2405 | memcpy(&b, &cam->frame[b.index].buf, sizeof(b)); |
1da177e4 | 2406 | |
a966f3e7 LR |
2407 | if (cam->frame[b.index].vma_use_count) |
2408 | b.flags |= V4L2_BUF_FLAG_MAPPED; | |
1da177e4 | 2409 | |
a966f3e7 LR |
2410 | if (cam->frame[b.index].state == F_DONE) |
2411 | b.flags |= V4L2_BUF_FLAG_DONE; | |
2412 | else if (cam->frame[b.index].state != F_UNUSED) | |
2413 | b.flags |= V4L2_BUF_FLAG_QUEUED; | |
1da177e4 | 2414 | |
a966f3e7 LR |
2415 | if (copy_to_user(arg, &b, sizeof(b))) |
2416 | return -EFAULT; | |
1da177e4 | 2417 | |
a966f3e7 LR |
2418 | return 0; |
2419 | } | |
1da177e4 | 2420 | |
1da177e4 | 2421 | |
a966f3e7 LR |
2422 | static int |
2423 | sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg) | |
2424 | { | |
2425 | struct v4l2_buffer b; | |
2426 | unsigned long lock_flags; | |
1da177e4 | 2427 | |
a966f3e7 LR |
2428 | if (copy_from_user(&b, arg, sizeof(b))) |
2429 | return -EFAULT; | |
1da177e4 | 2430 | |
a966f3e7 LR |
2431 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
2432 | b.index >= cam->nbuffers || cam->io != IO_MMAP) | |
2433 | return -EINVAL; | |
1da177e4 | 2434 | |
a966f3e7 LR |
2435 | if (cam->frame[b.index].state != F_UNUSED) |
2436 | return -EINVAL; | |
1da177e4 | 2437 | |
a966f3e7 | 2438 | cam->frame[b.index].state = F_QUEUED; |
1da177e4 | 2439 | |
a966f3e7 LR |
2440 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
2441 | list_add_tail(&cam->frame[b.index].frame, &cam->inqueue); | |
2442 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
1da177e4 | 2443 | |
a966f3e7 | 2444 | PDBGG("Frame #%lu queued", (unsigned long)b.index); |
1da177e4 | 2445 | |
a966f3e7 LR |
2446 | return 0; |
2447 | } | |
1da177e4 | 2448 | |
1da177e4 | 2449 | |
a966f3e7 LR |
2450 | static int |
2451 | sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp, | |
2452 | void __user * arg) | |
2453 | { | |
2454 | struct v4l2_buffer b; | |
2455 | struct sn9c102_frame_t *f; | |
2456 | unsigned long lock_flags; | |
2457 | int err = 0; | |
1da177e4 | 2458 | |
a966f3e7 LR |
2459 | if (copy_from_user(&b, arg, sizeof(b))) |
2460 | return -EFAULT; | |
1da177e4 | 2461 | |
a966f3e7 LR |
2462 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) |
2463 | return -EINVAL; | |
2464 | ||
2465 | if (list_empty(&cam->outqueue)) { | |
2466 | if (cam->stream == STREAM_OFF) | |
2467 | return -EINVAL; | |
2468 | if (filp->f_flags & O_NONBLOCK) | |
2469 | return -EAGAIN; | |
2470 | err = wait_event_interruptible | |
2471 | ( cam->wait_frame, | |
2472 | (!list_empty(&cam->outqueue)) || | |
2473 | (cam->state & DEV_DISCONNECTED) || | |
2474 | (cam->state & DEV_MISCONFIGURED) ); | |
2475 | if (err) | |
2476 | return err; | |
2477 | if (cam->state & DEV_DISCONNECTED) | |
2478 | return -ENODEV; | |
2479 | if (cam->state & DEV_MISCONFIGURED) | |
2480 | return -EIO; | |
1da177e4 LT |
2481 | } |
2482 | ||
a966f3e7 LR |
2483 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
2484 | f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame); | |
2485 | list_del(cam->outqueue.next); | |
2486 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
1da177e4 | 2487 | |
a966f3e7 | 2488 | f->state = F_UNUSED; |
1da177e4 | 2489 | |
a966f3e7 LR |
2490 | memcpy(&b, &f->buf, sizeof(b)); |
2491 | if (f->vma_use_count) | |
2492 | b.flags |= V4L2_BUF_FLAG_MAPPED; | |
2493 | ||
2494 | if (copy_to_user(arg, &b, sizeof(b))) | |
2495 | return -EFAULT; | |
2496 | ||
2497 | PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index); | |
2498 | ||
2499 | return 0; | |
2500 | } | |
2501 | ||
2502 | ||
2503 | static int | |
2504 | sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg) | |
2505 | { | |
2506 | int type; | |
2507 | ||
2508 | if (copy_from_user(&type, arg, sizeof(type))) | |
2509 | return -EFAULT; | |
2510 | ||
2511 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) | |
2512 | return -EINVAL; | |
2513 | ||
2514 | if (list_empty(&cam->inqueue)) | |
2515 | return -EINVAL; | |
2516 | ||
2517 | cam->stream = STREAM_ON; | |
2518 | ||
2519 | DBG(3, "Stream on"); | |
2520 | ||
2521 | return 0; | |
2522 | } | |
2523 | ||
2524 | ||
2525 | static int | |
2526 | sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg) | |
2527 | { | |
2528 | int type, err; | |
2529 | ||
2530 | if (copy_from_user(&type, arg, sizeof(type))) | |
2531 | return -EFAULT; | |
2532 | ||
2533 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) | |
2534 | return -EINVAL; | |
2535 | ||
2536 | if (cam->stream == STREAM_ON) | |
2537 | if ((err = sn9c102_stream_interrupt(cam))) | |
2538 | return err; | |
2539 | ||
2540 | sn9c102_empty_framequeues(cam); | |
2541 | ||
2542 | DBG(3, "Stream off"); | |
2543 | ||
2544 | return 0; | |
2545 | } | |
2546 | ||
2547 | ||
2548 | static int | |
2549 | sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg) | |
2550 | { | |
2551 | struct v4l2_streamparm sp; | |
2552 | ||
2553 | if (copy_from_user(&sp, arg, sizeof(sp))) | |
2554 | return -EFAULT; | |
2555 | ||
2556 | if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
2557 | return -EINVAL; | |
2558 | ||
2559 | sp.parm.capture.extendedmode = 0; | |
2560 | sp.parm.capture.readbuffers = cam->nreadbuffers; | |
2561 | ||
2562 | if (copy_to_user(arg, &sp, sizeof(sp))) | |
2563 | return -EFAULT; | |
2564 | ||
2565 | return 0; | |
2566 | } | |
2567 | ||
2568 | ||
2569 | static int | |
2570 | sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg) | |
2571 | { | |
2572 | struct v4l2_streamparm sp; | |
2573 | ||
2574 | if (copy_from_user(&sp, arg, sizeof(sp))) | |
2575 | return -EFAULT; | |
2576 | ||
2577 | if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
2578 | return -EINVAL; | |
2579 | ||
2580 | sp.parm.capture.extendedmode = 0; | |
1da177e4 | 2581 | |
a966f3e7 | 2582 | if (sp.parm.capture.readbuffers == 0) |
1da177e4 LT |
2583 | sp.parm.capture.readbuffers = cam->nreadbuffers; |
2584 | ||
a966f3e7 LR |
2585 | if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES) |
2586 | sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES; | |
1da177e4 | 2587 | |
a966f3e7 LR |
2588 | if (copy_to_user(arg, &sp, sizeof(sp))) |
2589 | return -EFAULT; | |
1da177e4 | 2590 | |
a966f3e7 | 2591 | cam->nreadbuffers = sp.parm.capture.readbuffers; |
1da177e4 | 2592 | |
a966f3e7 LR |
2593 | return 0; |
2594 | } | |
1da177e4 | 2595 | |
1da177e4 | 2596 | |
a966f3e7 LR |
2597 | static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp, |
2598 | unsigned int cmd, void __user * arg) | |
2599 | { | |
2600 | struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); | |
1da177e4 | 2601 | |
a966f3e7 | 2602 | switch (cmd) { |
1da177e4 | 2603 | |
a966f3e7 LR |
2604 | case VIDIOC_QUERYCAP: |
2605 | return sn9c102_vidioc_querycap(cam, arg); | |
1da177e4 | 2606 | |
a966f3e7 LR |
2607 | case VIDIOC_ENUMINPUT: |
2608 | return sn9c102_vidioc_enuminput(cam, arg); | |
1da177e4 | 2609 | |
a966f3e7 LR |
2610 | case VIDIOC_G_INPUT: |
2611 | case VIDIOC_S_INPUT: | |
2612 | return sn9c102_vidioc_gs_input(cam, arg); | |
1da177e4 | 2613 | |
a966f3e7 LR |
2614 | case VIDIOC_QUERYCTRL: |
2615 | return sn9c102_vidioc_query_ctrl(cam, arg); | |
2616 | ||
2617 | case VIDIOC_G_CTRL: | |
2618 | return sn9c102_vidioc_g_ctrl(cam, arg); | |
2619 | ||
2620 | case VIDIOC_S_CTRL_OLD: | |
2621 | case VIDIOC_S_CTRL: | |
2622 | return sn9c102_vidioc_s_ctrl(cam, arg); | |
2623 | ||
2624 | case VIDIOC_CROPCAP_OLD: | |
2625 | case VIDIOC_CROPCAP: | |
2626 | return sn9c102_vidioc_cropcap(cam, arg); | |
2627 | ||
2628 | case VIDIOC_G_CROP: | |
2629 | return sn9c102_vidioc_g_crop(cam, arg); | |
2630 | ||
2631 | case VIDIOC_S_CROP: | |
2632 | return sn9c102_vidioc_s_crop(cam, arg); | |
2633 | ||
2634 | case VIDIOC_ENUM_FMT: | |
2635 | return sn9c102_vidioc_enum_fmt(cam, arg); | |
2636 | ||
2637 | case VIDIOC_G_FMT: | |
2638 | return sn9c102_vidioc_g_fmt(cam, arg); | |
2639 | ||
2640 | case VIDIOC_TRY_FMT: | |
2641 | case VIDIOC_S_FMT: | |
2642 | return sn9c102_vidioc_try_s_fmt(cam, cmd, arg); | |
2643 | ||
2644 | case VIDIOC_G_JPEGCOMP: | |
2645 | return sn9c102_vidioc_g_jpegcomp(cam, arg); | |
2646 | ||
2647 | case VIDIOC_S_JPEGCOMP: | |
2648 | return sn9c102_vidioc_s_jpegcomp(cam, arg); | |
2649 | ||
2650 | case VIDIOC_REQBUFS: | |
2651 | return sn9c102_vidioc_reqbufs(cam, arg); | |
2652 | ||
2653 | case VIDIOC_QUERYBUF: | |
2654 | return sn9c102_vidioc_querybuf(cam, arg); | |
2655 | ||
2656 | case VIDIOC_QBUF: | |
2657 | return sn9c102_vidioc_qbuf(cam, arg); | |
2658 | ||
2659 | case VIDIOC_DQBUF: | |
2660 | return sn9c102_vidioc_dqbuf(cam, filp, arg); | |
2661 | ||
2662 | case VIDIOC_STREAMON: | |
2663 | return sn9c102_vidioc_streamon(cam, arg); | |
2664 | ||
2665 | case VIDIOC_STREAMOFF: | |
2666 | return sn9c102_vidioc_streamoff(cam, arg); | |
2667 | ||
2668 | case VIDIOC_G_PARM: | |
2669 | return sn9c102_vidioc_g_parm(cam, arg); | |
2670 | ||
2671 | case VIDIOC_S_PARM_OLD: | |
2672 | case VIDIOC_S_PARM: | |
2673 | return sn9c102_vidioc_s_parm(cam, arg); | |
1da177e4 LT |
2674 | |
2675 | case VIDIOC_G_STD: | |
2676 | case VIDIOC_S_STD: | |
2677 | case VIDIOC_QUERYSTD: | |
2678 | case VIDIOC_ENUMSTD: | |
2679 | case VIDIOC_QUERYMENU: | |
2680 | return -EINVAL; | |
2681 | ||
2682 | default: | |
2683 | return -EINVAL; | |
2684 | ||
2685 | } | |
2686 | } | |
2687 | ||
2688 | ||
2689 | static int sn9c102_ioctl(struct inode* inode, struct file* filp, | |
2690 | unsigned int cmd, unsigned long arg) | |
2691 | { | |
2692 | struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); | |
2693 | int err = 0; | |
2694 | ||
2695 | if (down_interruptible(&cam->fileop_sem)) | |
2696 | return -ERESTARTSYS; | |
2697 | ||
2698 | if (cam->state & DEV_DISCONNECTED) { | |
a966f3e7 | 2699 | DBG(1, "Device not present"); |
1da177e4 LT |
2700 | up(&cam->fileop_sem); |
2701 | return -ENODEV; | |
2702 | } | |
2703 | ||
2704 | if (cam->state & DEV_MISCONFIGURED) { | |
a966f3e7 LR |
2705 | DBG(1, "The camera is misconfigured. Close and open it " |
2706 | "again."); | |
1da177e4 LT |
2707 | up(&cam->fileop_sem); |
2708 | return -EIO; | |
2709 | } | |
2710 | ||
2711 | err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg); | |
2712 | ||
2713 | up(&cam->fileop_sem); | |
2714 | ||
2715 | return err; | |
2716 | } | |
2717 | ||
a966f3e7 | 2718 | /*****************************************************************************/ |
1da177e4 LT |
2719 | |
2720 | static struct file_operations sn9c102_fops = { | |
a966f3e7 | 2721 | .owner = THIS_MODULE, |
1da177e4 LT |
2722 | .open = sn9c102_open, |
2723 | .release = sn9c102_release, | |
2724 | .ioctl = sn9c102_ioctl, | |
2725 | .read = sn9c102_read, | |
2726 | .poll = sn9c102_poll, | |
2727 | .mmap = sn9c102_mmap, | |
2728 | .llseek = no_llseek, | |
2729 | }; | |
2730 | ||
2731 | /*****************************************************************************/ | |
2732 | ||
2733 | /* It exists a single interface only. We do not need to validate anything. */ | |
2734 | static int | |
2735 | sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id) | |
2736 | { | |
2737 | struct usb_device *udev = interface_to_usbdev(intf); | |
2738 | struct sn9c102_device* cam; | |
2739 | static unsigned int dev_nr = 0; | |
a966f3e7 | 2740 | unsigned int i; |
1da177e4 LT |
2741 | int err = 0, r; |
2742 | ||
1da177e4 LT |
2743 | if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL))) |
2744 | return -ENOMEM; | |
1da177e4 LT |
2745 | |
2746 | cam->usbdev = udev; | |
1da177e4 LT |
2747 | memcpy(&cam->dev, &udev->dev, sizeof(struct device)); |
2748 | ||
2749 | if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) { | |
a966f3e7 | 2750 | DBG(1, "kmalloc() failed"); |
1da177e4 LT |
2751 | err = -ENOMEM; |
2752 | goto fail; | |
2753 | } | |
1da177e4 LT |
2754 | |
2755 | if (!(cam->v4ldev = video_device_alloc())) { | |
a966f3e7 | 2756 | DBG(1, "video_device_alloc() failed"); |
1da177e4 LT |
2757 | err = -ENOMEM; |
2758 | goto fail; | |
2759 | } | |
2760 | ||
2761 | init_MUTEX(&cam->dev_sem); | |
2762 | ||
2763 | r = sn9c102_read_reg(cam, 0x00); | |
2764 | if (r < 0 || r != 0x10) { | |
2765 | DBG(1, "Sorry, this is not a SN9C10x based camera " | |
a966f3e7 | 2766 | "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct); |
1da177e4 LT |
2767 | err = -ENODEV; |
2768 | goto fail; | |
2769 | } | |
2770 | ||
a966f3e7 | 2771 | cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ? |
1da177e4 LT |
2772 | BRIDGE_SN9C103 : BRIDGE_SN9C102; |
2773 | switch (cam->bridge) { | |
2774 | case BRIDGE_SN9C101: | |
2775 | case BRIDGE_SN9C102: | |
2776 | DBG(2, "SN9C10[12] PC Camera Controller detected " | |
a966f3e7 | 2777 | "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct); |
1da177e4 LT |
2778 | break; |
2779 | case BRIDGE_SN9C103: | |
2780 | DBG(2, "SN9C103 PC Camera Controller detected " | |
a966f3e7 | 2781 | "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct); |
1da177e4 LT |
2782 | break; |
2783 | } | |
2784 | ||
2785 | for (i = 0; sn9c102_sensor_table[i]; i++) { | |
2786 | err = sn9c102_sensor_table[i](cam); | |
2787 | if (!err) | |
2788 | break; | |
2789 | } | |
2790 | ||
2791 | if (!err && cam->sensor) { | |
a966f3e7 | 2792 | DBG(2, "%s image sensor detected", cam->sensor->name); |
1da177e4 | 2793 | DBG(3, "Support for %s maintained by %s", |
a966f3e7 | 2794 | cam->sensor->name, cam->sensor->maintainer); |
1da177e4 | 2795 | } else { |
a966f3e7 | 2796 | DBG(1, "No supported image sensor detected"); |
1da177e4 LT |
2797 | err = -ENODEV; |
2798 | goto fail; | |
2799 | } | |
2800 | ||
2801 | if (sn9c102_init(cam)) { | |
a966f3e7 | 2802 | DBG(1, "Initialization failed. I will retry on open()."); |
1da177e4 LT |
2803 | cam->state |= DEV_MISCONFIGURED; |
2804 | } | |
2805 | ||
2806 | strcpy(cam->v4ldev->name, "SN9C10x PC Camera"); | |
2807 | cam->v4ldev->owner = THIS_MODULE; | |
2808 | cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES; | |
2809 | cam->v4ldev->hardware = VID_HARDWARE_SN9C102; | |
2810 | cam->v4ldev->fops = &sn9c102_fops; | |
2811 | cam->v4ldev->minor = video_nr[dev_nr]; | |
2812 | cam->v4ldev->release = video_device_release; | |
2813 | video_set_drvdata(cam->v4ldev, cam); | |
2814 | ||
2815 | down(&cam->dev_sem); | |
2816 | ||
2817 | err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, | |
2818 | video_nr[dev_nr]); | |
2819 | if (err) { | |
a966f3e7 | 2820 | DBG(1, "V4L2 device registration failed"); |
1da177e4 | 2821 | if (err == -ENFILE && video_nr[dev_nr] == -1) |
a966f3e7 | 2822 | DBG(1, "Free /dev/videoX node not found"); |
1da177e4 LT |
2823 | video_nr[dev_nr] = -1; |
2824 | dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; | |
2825 | up(&cam->dev_sem); | |
2826 | goto fail; | |
2827 | } | |
2828 | ||
a966f3e7 | 2829 | DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor); |
1da177e4 LT |
2830 | |
2831 | cam->module_param.force_munmap = force_munmap[dev_nr]; | |
2832 | ||
2833 | dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; | |
2834 | ||
2835 | sn9c102_create_sysfs(cam); | |
a966f3e7 | 2836 | DBG(2, "Optional device control through 'sysfs' interface ready"); |
1da177e4 LT |
2837 | |
2838 | usb_set_intfdata(intf, cam); | |
2839 | ||
2840 | up(&cam->dev_sem); | |
2841 | ||
2842 | return 0; | |
2843 | ||
2844 | fail: | |
2845 | if (cam) { | |
2846 | kfree(cam->control_buffer); | |
2847 | if (cam->v4ldev) | |
2848 | video_device_release(cam->v4ldev); | |
2849 | kfree(cam); | |
2850 | } | |
2851 | return err; | |
2852 | } | |
2853 | ||
2854 | ||
2855 | static void sn9c102_usb_disconnect(struct usb_interface* intf) | |
2856 | { | |
2857 | struct sn9c102_device* cam = usb_get_intfdata(intf); | |
2858 | ||
2859 | if (!cam) | |
2860 | return; | |
2861 | ||
2862 | down_write(&sn9c102_disconnect); | |
2863 | ||
2864 | down(&cam->dev_sem); | |
2865 | ||
a966f3e7 | 2866 | DBG(2, "Disconnecting %s...", cam->v4ldev->name); |
1da177e4 LT |
2867 | |
2868 | wake_up_interruptible_all(&cam->open); | |
2869 | ||
2870 | if (cam->users) { | |
2871 | DBG(2, "Device /dev/video%d is open! Deregistration and " | |
2872 | "memory deallocation are deferred on close.", | |
a966f3e7 | 2873 | cam->v4ldev->minor); |
1da177e4 LT |
2874 | cam->state |= DEV_MISCONFIGURED; |
2875 | sn9c102_stop_transfer(cam); | |
2876 | cam->state |= DEV_DISCONNECTED; | |
2877 | wake_up_interruptible(&cam->wait_frame); | |
2878 | wake_up_interruptible(&cam->wait_stream); | |
2879 | } else { | |
2880 | cam->state |= DEV_DISCONNECTED; | |
2881 | sn9c102_release_resources(cam); | |
2882 | } | |
2883 | ||
2884 | up(&cam->dev_sem); | |
2885 | ||
2886 | if (!cam->users) | |
2887 | kfree(cam); | |
2888 | ||
2889 | up_write(&sn9c102_disconnect); | |
2890 | } | |
2891 | ||
2892 | ||
2893 | static struct usb_driver sn9c102_usb_driver = { | |
1da177e4 LT |
2894 | .name = "sn9c102", |
2895 | .id_table = sn9c102_id_table, | |
2896 | .probe = sn9c102_usb_probe, | |
2897 | .disconnect = sn9c102_usb_disconnect, | |
2898 | }; | |
2899 | ||
2900 | /*****************************************************************************/ | |
2901 | ||
2902 | static int __init sn9c102_module_init(void) | |
2903 | { | |
2904 | int err = 0; | |
2905 | ||
a966f3e7 LR |
2906 | KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION); |
2907 | KDBG(3, SN9C102_MODULE_AUTHOR); | |
1da177e4 LT |
2908 | |
2909 | if ((err = usb_register(&sn9c102_usb_driver))) | |
a966f3e7 | 2910 | KDBG(1, "usb_register() failed"); |
1da177e4 LT |
2911 | |
2912 | return err; | |
2913 | } | |
2914 | ||
2915 | ||
2916 | static void __exit sn9c102_module_exit(void) | |
2917 | { | |
2918 | usb_deregister(&sn9c102_usb_driver); | |
2919 | } | |
2920 | ||
2921 | ||
2922 | module_init(sn9c102_module_init); | |
2923 | module_exit(sn9c102_module_exit); |