Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /*************************************************************************** |
f327ebbd | 2 | * V4L2 driver for SN9C1xx PC Camera Controllers * |
1da177e4 | 3 | * * |
f327ebbd | 4 | * Copyright (C) 2004-2007 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> | |
1da177e4 LT |
25 | #include <linux/errno.h> |
26 | #include <linux/slab.h> | |
1da177e4 LT |
27 | #include <linux/device.h> |
28 | #include <linux/fs.h> | |
29 | #include <linux/delay.h> | |
1da177e4 LT |
30 | #include <linux/compiler.h> |
31 | #include <linux/ioctl.h> | |
32 | #include <linux/poll.h> | |
33 | #include <linux/stat.h> | |
34 | #include <linux/mm.h> | |
35 | #include <linux/vmalloc.h> | |
36 | #include <linux/page-flags.h> | |
9a6ab769 | 37 | #include <asm/byteorder.h> |
1da177e4 LT |
38 | #include <asm/page.h> |
39 | #include <asm/uaccess.h> | |
40 | ||
41 | #include "sn9c102.h" | |
42 | ||
43 | /*****************************************************************************/ | |
44 | ||
f327ebbd | 45 | #define SN9C102_MODULE_NAME "V4L2 driver for SN9C1xx PC Camera Controllers" |
f423b9a8 LR |
46 | #define SN9C102_MODULE_ALIAS "sn9c1xx" |
47 | #define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia" | |
cd6fcc55 LR |
48 | #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>" |
49 | #define SN9C102_MODULE_LICENSE "GPL" | |
ddef2dcc | 50 | #define SN9C102_MODULE_VERSION "1:1.47pre49" |
3770be34 | 51 | #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 47) |
cd6fcc55 LR |
52 | |
53 | /*****************************************************************************/ | |
54 | ||
1da177e4 LT |
55 | MODULE_DEVICE_TABLE(usb, sn9c102_id_table); |
56 | ||
57 | MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL); | |
58 | MODULE_DESCRIPTION(SN9C102_MODULE_NAME); | |
f423b9a8 | 59 | MODULE_ALIAS(SN9C102_MODULE_ALIAS); |
1da177e4 LT |
60 | MODULE_VERSION(SN9C102_MODULE_VERSION); |
61 | MODULE_LICENSE(SN9C102_MODULE_LICENSE); | |
62 | ||
63 | static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1}; | |
64 | module_param_array(video_nr, short, NULL, 0444); | |
65 | MODULE_PARM_DESC(video_nr, | |
3770be34 LR |
66 | " <-1|n[,...]>" |
67 | "\nSpecify V4L2 minor mode number." | |
68 | "\n-1 = use next available (default)" | |
69 | "\n n = use minor number n (integer >= 0)" | |
d56410e0 MCC |
70 | "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES) |
71 | " cameras this way." | |
72 | "\nFor example:" | |
73 | "\nvideo_nr=-1,2,-1 would assign minor number 2 to" | |
74 | "\nthe second camera and use auto for the first" | |
75 | "\none and for every other camera." | |
76 | "\n"); | |
77 | ||
78 | static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] = | |
79 | SN9C102_FORCE_MUNMAP}; | |
1da177e4 LT |
80 | module_param_array(force_munmap, bool, NULL, 0444); |
81 | MODULE_PARM_DESC(force_munmap, | |
3770be34 LR |
82 | " <0|1[,...]>" |
83 | "\nForce the application to unmap previously" | |
d56410e0 MCC |
84 | "\nmapped buffer memory before calling any VIDIOC_S_CROP or" |
85 | "\nVIDIOC_S_FMT ioctl's. Not all the applications support" | |
86 | "\nthis feature. This parameter is specific for each" | |
87 | "\ndetected camera." | |
3770be34 LR |
88 | "\n0 = do not force memory unmapping" |
89 | "\n1 = force memory unmapping (save memory)" | |
d56410e0 MCC |
90 | "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"." |
91 | "\n"); | |
1da177e4 | 92 | |
2ffab02f | 93 | static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] = |
d56410e0 | 94 | SN9C102_FRAME_TIMEOUT}; |
2ffab02f LR |
95 | module_param_array(frame_timeout, uint, NULL, 0644); |
96 | MODULE_PARM_DESC(frame_timeout, | |
3770be34 LR |
97 | " <0|n[,...]>" |
98 | "\nTimeout for a video frame in seconds before" | |
f327ebbd | 99 | "\nreturning an I/O error; 0 for infinity." |
d56410e0 MCC |
100 | "\nThis parameter is specific for each detected camera." |
101 | "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"." | |
102 | "\n"); | |
2ffab02f | 103 | |
1da177e4 LT |
104 | #ifdef SN9C102_DEBUG |
105 | static unsigned short debug = SN9C102_DEBUG_LEVEL; | |
106 | module_param(debug, ushort, 0644); | |
107 | MODULE_PARM_DESC(debug, | |
3770be34 LR |
108 | " <n>" |
109 | "\nDebugging information level, from 0 to 3:" | |
d56410e0 MCC |
110 | "\n0 = none (use carefully)" |
111 | "\n1 = critical errors" | |
112 | "\n2 = significant informations" | |
113 | "\n3 = more verbose messages" | |
f423b9a8 | 114 | "\nLevel 3 is useful for testing only." |
d56410e0 MCC |
115 | "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"." |
116 | "\n"); | |
1da177e4 LT |
117 | #endif |
118 | ||
d45b9b8a HV |
119 | /* |
120 | Add the probe entries to this table. Be sure to add the entry in the right | |
121 | place, since, on failure, the next probing routine is called according to | |
122 | the order of the list below, from top to bottom. | |
123 | */ | |
124 | static int (*sn9c102_sensor_table[])(struct sn9c102_device *) = { | |
125 | &sn9c102_probe_hv7131d, /* strong detection based on SENSOR ids */ | |
126 | &sn9c102_probe_hv7131r, /* strong detection based on SENSOR ids */ | |
127 | &sn9c102_probe_mi0343, /* strong detection based on SENSOR ids */ | |
128 | &sn9c102_probe_mi0360, /* strong detection based on SENSOR ids */ | |
129 | &sn9c102_probe_mt9v111, /* strong detection based on SENSOR ids */ | |
130 | &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */ | |
131 | &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */ | |
132 | &sn9c102_probe_ov7630, /* strong detection based on SENSOR ids */ | |
133 | &sn9c102_probe_ov7660, /* strong detection based on SENSOR ids */ | |
134 | &sn9c102_probe_tas5110c1b, /* detection based on USB pid/vid */ | |
135 | &sn9c102_probe_tas5110d, /* detection based on USB pid/vid */ | |
136 | &sn9c102_probe_tas5130d1b, /* detection based on USB pid/vid */ | |
137 | }; | |
138 | ||
1da177e4 LT |
139 | /*****************************************************************************/ |
140 | ||
d56410e0 MCC |
141 | static u32 |
142 | sn9c102_request_buffers(struct sn9c102_device* cam, u32 count, | |
143 | enum sn9c102_io_method io) | |
1da177e4 | 144 | { |
2ffab02f LR |
145 | struct v4l2_pix_format* p = &(cam->sensor.pix_format); |
146 | struct v4l2_rect* r = &(cam->sensor.cropcap.bounds); | |
f327ebbd | 147 | size_t imagesize = cam->module_param.force_munmap || io == IO_READ ? |
f423b9a8 LR |
148 | (p->width * p->height * p->priv) / 8 : |
149 | (r->width * r->height * p->priv) / 8; | |
1da177e4 LT |
150 | void* buff = NULL; |
151 | u32 i; | |
152 | ||
153 | if (count > SN9C102_MAX_FRAMES) | |
154 | count = SN9C102_MAX_FRAMES; | |
155 | ||
f327ebbd LR |
156 | if (cam->bridge == BRIDGE_SN9C105 || cam->bridge == BRIDGE_SN9C120) |
157 | imagesize += 589 + 2; /* length of JPEG header + EOI marker */ | |
158 | ||
1da177e4 LT |
159 | cam->nbuffers = count; |
160 | while (cam->nbuffers > 0) { | |
f327ebbd LR |
161 | if ((buff = vmalloc_32_user(cam->nbuffers * |
162 | PAGE_ALIGN(imagesize)))) | |
1da177e4 LT |
163 | break; |
164 | cam->nbuffers--; | |
165 | } | |
166 | ||
167 | for (i = 0; i < cam->nbuffers; i++) { | |
168 | cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize); | |
169 | cam->frame[i].buf.index = i; | |
170 | cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize); | |
171 | cam->frame[i].buf.length = imagesize; | |
172 | cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
173 | cam->frame[i].buf.sequence = 0; | |
174 | cam->frame[i].buf.field = V4L2_FIELD_NONE; | |
175 | cam->frame[i].buf.memory = V4L2_MEMORY_MMAP; | |
176 | cam->frame[i].buf.flags = 0; | |
177 | } | |
178 | ||
179 | return cam->nbuffers; | |
180 | } | |
181 | ||
182 | ||
183 | static void sn9c102_release_buffers(struct sn9c102_device* cam) | |
184 | { | |
185 | if (cam->nbuffers) { | |
cd6fcc55 | 186 | vfree(cam->frame[0].bufmem); |
1da177e4 LT |
187 | cam->nbuffers = 0; |
188 | } | |
a966f3e7 | 189 | cam->frame_current = NULL; |
1da177e4 LT |
190 | } |
191 | ||
192 | ||
193 | static void sn9c102_empty_framequeues(struct sn9c102_device* cam) | |
194 | { | |
195 | u32 i; | |
196 | ||
197 | INIT_LIST_HEAD(&cam->inqueue); | |
198 | INIT_LIST_HEAD(&cam->outqueue); | |
199 | ||
200 | for (i = 0; i < SN9C102_MAX_FRAMES; i++) { | |
201 | cam->frame[i].state = F_UNUSED; | |
202 | cam->frame[i].buf.bytesused = 0; | |
203 | } | |
204 | } | |
205 | ||
206 | ||
a966f3e7 LR |
207 | static void sn9c102_requeue_outqueue(struct sn9c102_device* cam) |
208 | { | |
209 | struct sn9c102_frame_t *i; | |
210 | ||
211 | list_for_each_entry(i, &cam->outqueue, frame) { | |
212 | i->state = F_QUEUED; | |
213 | list_add(&i->frame, &cam->inqueue); | |
214 | } | |
215 | ||
216 | INIT_LIST_HEAD(&cam->outqueue); | |
217 | } | |
218 | ||
219 | ||
1da177e4 LT |
220 | static void sn9c102_queue_unusedframes(struct sn9c102_device* cam) |
221 | { | |
222 | unsigned long lock_flags; | |
223 | u32 i; | |
224 | ||
225 | for (i = 0; i < cam->nbuffers; i++) | |
226 | if (cam->frame[i].state == F_UNUSED) { | |
227 | cam->frame[i].state = F_QUEUED; | |
228 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | |
229 | list_add_tail(&cam->frame[i].frame, &cam->inqueue); | |
230 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
231 | } | |
232 | } | |
233 | ||
234 | /*****************************************************************************/ | |
480b55c2 | 235 | |
c680dd60 | 236 | /* |
480b55c2 LR |
237 | Write a sequence of count value/register pairs. Returns -1 after the first |
238 | failed write, or 0 for no errors. | |
239 | */ | |
c680dd60 TP |
240 | int sn9c102_write_regs(struct sn9c102_device* cam, const u8 valreg[][2], |
241 | int count) | |
a966f3e7 LR |
242 | { |
243 | struct usb_device* udev = cam->usbdev; | |
480b55c2 | 244 | u8* buff = cam->control_buffer; |
a966f3e7 LR |
245 | int i, res; |
246 | ||
c680dd60 TP |
247 | for (i = 0; i < count; i++) { |
248 | u8 index = valreg[i][1]; | |
249 | ||
250 | /* | |
480b55c2 LR |
251 | index is a u8, so it must be <256 and can't be out of range. |
252 | If we put in a check anyway, gcc annoys us with a warning | |
253 | hat our check is useless. People get all uppity when they | |
254 | see warnings in the kernel compile. | |
255 | */ | |
256 | ||
257 | *buff = valreg[i][0]; | |
258 | ||
259 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, | |
260 | 0x41, index, 0, buff, 1, | |
261 | SN9C102_CTRL_TIMEOUT); | |
262 | ||
c680dd60 TP |
263 | if (res < 0) { |
264 | DBG(3, "Failed to write a register (value 0x%02X, " | |
480b55c2 | 265 | "index 0x%02X, error %d)", *buff, index, res); |
c680dd60 TP |
266 | return -1; |
267 | } | |
a966f3e7 | 268 | |
480b55c2 | 269 | cam->reg[index] = *buff; |
a966f3e7 LR |
270 | } |
271 | ||
a966f3e7 LR |
272 | return 0; |
273 | } | |
274 | ||
275 | ||
1da177e4 LT |
276 | int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index) |
277 | { | |
278 | struct usb_device* udev = cam->usbdev; | |
279 | u8* buff = cam->control_buffer; | |
280 | int res; | |
281 | ||
a966f3e7 LR |
282 | if (index >= ARRAY_SIZE(cam->reg)) |
283 | return -1; | |
284 | ||
1da177e4 LT |
285 | *buff = value; |
286 | ||
287 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | |
d56410e0 | 288 | index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); |
1da177e4 LT |
289 | if (res < 0) { |
290 | DBG(3, "Failed to write a register (value 0x%02X, index " | |
a966f3e7 | 291 | "0x%02X, error %d)", value, index, res); |
1da177e4 LT |
292 | return -1; |
293 | } | |
294 | ||
295 | cam->reg[index] = value; | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | ||
480b55c2 LR |
301 | /* NOTE: with the SN9C10[123] reading some registers always returns 0 */ |
302 | int sn9c102_read_reg(struct sn9c102_device* cam, u16 index) | |
1da177e4 LT |
303 | { |
304 | struct usb_device* udev = cam->usbdev; | |
305 | u8* buff = cam->control_buffer; | |
306 | int res; | |
307 | ||
308 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1, | |
d56410e0 | 309 | index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); |
1da177e4 LT |
310 | if (res < 0) |
311 | DBG(3, "Failed to read a register (index 0x%02X, error %d)", | |
a966f3e7 | 312 | index, res); |
1da177e4 LT |
313 | |
314 | return (res >= 0) ? (int)(*buff) : -1; | |
315 | } | |
316 | ||
317 | ||
318 | int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index) | |
319 | { | |
a966f3e7 LR |
320 | if (index >= ARRAY_SIZE(cam->reg)) |
321 | return -1; | |
1da177e4 LT |
322 | |
323 | return cam->reg[index]; | |
324 | } | |
325 | ||
326 | ||
327 | static int | |
480b55c2 LR |
328 | sn9c102_i2c_wait(struct sn9c102_device* cam, |
329 | const struct sn9c102_sensor* sensor) | |
1da177e4 LT |
330 | { |
331 | int i, r; | |
332 | ||
333 | for (i = 1; i <= 5; i++) { | |
334 | r = sn9c102_read_reg(cam, 0x08); | |
335 | if (r < 0) | |
336 | return -EIO; | |
337 | if (r & 0x04) | |
338 | return 0; | |
339 | if (sensor->frequency & SN9C102_I2C_400KHZ) | |
340 | udelay(5*16); | |
341 | else | |
342 | udelay(16*16); | |
343 | } | |
344 | return -EBUSY; | |
345 | } | |
346 | ||
347 | ||
348 | static int | |
d56410e0 | 349 | sn9c102_i2c_detect_read_error(struct sn9c102_device* cam, |
480b55c2 | 350 | const struct sn9c102_sensor* sensor) |
1da177e4 | 351 | { |
f327ebbd LR |
352 | int r , err = 0; |
353 | ||
1da177e4 | 354 | r = sn9c102_read_reg(cam, 0x08); |
f327ebbd LR |
355 | if (r < 0) |
356 | err += r; | |
357 | ||
358 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) { | |
359 | if (!(r & 0x08)) | |
360 | err += -1; | |
361 | } else { | |
362 | if (r & 0x08) | |
363 | err += -1; | |
364 | } | |
365 | ||
366 | return err ? -EIO : 0; | |
1da177e4 LT |
367 | } |
368 | ||
369 | ||
370 | static int | |
d56410e0 | 371 | sn9c102_i2c_detect_write_error(struct sn9c102_device* cam, |
480b55c2 | 372 | const struct sn9c102_sensor* sensor) |
1da177e4 LT |
373 | { |
374 | int r; | |
375 | r = sn9c102_read_reg(cam, 0x08); | |
376 | return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0; | |
377 | } | |
378 | ||
379 | ||
d56410e0 | 380 | int |
1da177e4 | 381 | sn9c102_i2c_try_raw_read(struct sn9c102_device* cam, |
480b55c2 LR |
382 | const struct sn9c102_sensor* sensor, u8 data0, |
383 | u8 data1, u8 n, u8 buffer[]) | |
1da177e4 LT |
384 | { |
385 | struct usb_device* udev = cam->usbdev; | |
386 | u8* data = cam->control_buffer; | |
480b55c2 | 387 | int i = 0, err = 0, res; |
1da177e4 LT |
388 | |
389 | /* Write cycle */ | |
390 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | |
d56410e0 | 391 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10; |
1da177e4 LT |
392 | data[1] = data0; /* I2C slave id */ |
393 | data[2] = data1; /* address */ | |
394 | data[7] = 0x10; | |
395 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | |
d56410e0 | 396 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); |
1da177e4 LT |
397 | if (res < 0) |
398 | err += res; | |
399 | ||
400 | err += sn9c102_i2c_wait(cam, sensor); | |
401 | ||
402 | /* Read cycle - n bytes */ | |
403 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | |
d56410e0 MCC |
404 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | |
405 | (n << 4) | 0x02; | |
1da177e4 LT |
406 | data[1] = data0; |
407 | data[7] = 0x10; | |
408 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | |
d56410e0 | 409 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); |
1da177e4 LT |
410 | if (res < 0) |
411 | err += res; | |
412 | ||
413 | err += sn9c102_i2c_wait(cam, sensor); | |
414 | ||
415 | /* The first read byte will be placed in data[4] */ | |
416 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1, | |
d56410e0 | 417 | 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT); |
1da177e4 LT |
418 | if (res < 0) |
419 | err += res; | |
420 | ||
421 | err += sn9c102_i2c_detect_read_error(cam, sensor); | |
422 | ||
423 | PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1, | |
a966f3e7 | 424 | data[4]); |
1da177e4 LT |
425 | |
426 | if (err) { | |
a966f3e7 | 427 | DBG(3, "I2C read failed for %s image sensor", sensor->name); |
1da177e4 LT |
428 | return -1; |
429 | } | |
430 | ||
431 | if (buffer) | |
480b55c2 LR |
432 | for (i = 0; i < n && i < 5; i++) |
433 | buffer[n-i-1] = data[4-i]; | |
1da177e4 LT |
434 | |
435 | return (int)data[4]; | |
436 | } | |
437 | ||
438 | ||
d56410e0 | 439 | int |
1da177e4 | 440 | sn9c102_i2c_try_raw_write(struct sn9c102_device* cam, |
480b55c2 | 441 | const struct sn9c102_sensor* sensor, u8 n, u8 data0, |
d56410e0 | 442 | u8 data1, u8 data2, u8 data3, u8 data4, u8 data5) |
1da177e4 LT |
443 | { |
444 | struct usb_device* udev = cam->usbdev; | |
445 | u8* data = cam->control_buffer; | |
446 | int err = 0, res; | |
447 | ||
448 | /* Write cycle. It usually is address + value */ | |
449 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | |
d56410e0 MCC |
450 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
451 | | ((n - 1) << 4); | |
1da177e4 LT |
452 | data[1] = data0; |
453 | data[2] = data1; | |
454 | data[3] = data2; | |
455 | data[4] = data3; | |
456 | data[5] = data4; | |
457 | data[6] = data5; | |
f327ebbd | 458 | data[7] = 0x17; |
1da177e4 | 459 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, |
d56410e0 | 460 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); |
1da177e4 LT |
461 | if (res < 0) |
462 | err += res; | |
463 | ||
464 | err += sn9c102_i2c_wait(cam, sensor); | |
465 | err += sn9c102_i2c_detect_write_error(cam, sensor); | |
466 | ||
467 | if (err) | |
a966f3e7 | 468 | DBG(3, "I2C write failed for %s image sensor", sensor->name); |
1da177e4 LT |
469 | |
470 | PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, " | |
471 | "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X", | |
a966f3e7 | 472 | n, data0, data1, data2, data3, data4, data5); |
1da177e4 LT |
473 | |
474 | return err ? -1 : 0; | |
475 | } | |
476 | ||
477 | ||
478 | int | |
479 | sn9c102_i2c_try_read(struct sn9c102_device* cam, | |
480b55c2 | 480 | const struct sn9c102_sensor* sensor, u8 address) |
1da177e4 LT |
481 | { |
482 | return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id, | |
d56410e0 | 483 | address, 1, NULL); |
1da177e4 LT |
484 | } |
485 | ||
486 | ||
762250f8 AB |
487 | static int sn9c102_i2c_try_write(struct sn9c102_device* cam, |
488 | const struct sn9c102_sensor* sensor, | |
489 | u8 address, u8 value) | |
1da177e4 | 490 | { |
d56410e0 MCC |
491 | return sn9c102_i2c_try_raw_write(cam, sensor, 3, |
492 | sensor->i2c_slave_id, address, | |
493 | value, 0, 0, 0); | |
1da177e4 LT |
494 | } |
495 | ||
496 | ||
497 | int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address) | |
498 | { | |
2ffab02f | 499 | return sn9c102_i2c_try_read(cam, &cam->sensor, address); |
1da177e4 LT |
500 | } |
501 | ||
502 | ||
503 | int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value) | |
504 | { | |
2ffab02f | 505 | return sn9c102_i2c_try_write(cam, &cam->sensor, address, value); |
1da177e4 LT |
506 | } |
507 | ||
508 | /*****************************************************************************/ | |
509 | ||
f327ebbd | 510 | static size_t sn9c102_sof_length(struct sn9c102_device* cam) |
1da177e4 | 511 | { |
a966f3e7 LR |
512 | switch (cam->bridge) { |
513 | case BRIDGE_SN9C101: | |
514 | case BRIDGE_SN9C102: | |
f327ebbd | 515 | return 12; |
a966f3e7 | 516 | case BRIDGE_SN9C103: |
f327ebbd LR |
517 | return 18; |
518 | case BRIDGE_SN9C105: | |
519 | case BRIDGE_SN9C120: | |
520 | return 62; | |
a966f3e7 | 521 | } |
1da177e4 | 522 | |
f327ebbd LR |
523 | return 0; |
524 | } | |
525 | ||
526 | ||
527 | static void* | |
528 | sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len) | |
529 | { | |
7e81d825 | 530 | static const char marker[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96}; |
f423b9a8 LR |
531 | const char *m = mem; |
532 | size_t soflen = 0, i, j; | |
f327ebbd LR |
533 | |
534 | soflen = sn9c102_sof_length(cam); | |
535 | ||
f423b9a8 LR |
536 | for (i = 0; i < len; i++) { |
537 | size_t b; | |
538 | ||
539 | /* Read the variable part of the header */ | |
540 | if (unlikely(cam->sof.bytesread >= sizeof(marker))) { | |
541 | cam->sof.header[cam->sof.bytesread] = *(m+i); | |
542 | if (++cam->sof.bytesread == soflen) { | |
543 | cam->sof.bytesread = 0; | |
544 | return mem + i; | |
545 | } | |
546 | continue; | |
547 | } | |
548 | ||
549 | /* Search for the SOF marker (fixed part) in the header */ | |
550 | for (j = 0, b=cam->sof.bytesread; j+b < sizeof(marker); j++) { | |
05b20792 | 551 | if (unlikely(i+j == len)) |
f423b9a8 LR |
552 | return NULL; |
553 | if (*(m+i+j) == marker[cam->sof.bytesread]) { | |
554 | cam->sof.header[cam->sof.bytesread] = *(m+i+j); | |
555 | if (++cam->sof.bytesread == sizeof(marker)) { | |
556 | PDBGG("Bytes to analyze: %zd. SOF " | |
557 | "starts at byte #%zd", len, i); | |
558 | i += j+1; | |
559 | break; | |
560 | } | |
561 | } else { | |
562 | cam->sof.bytesread = 0; | |
563 | break; | |
1da177e4 | 564 | } |
f423b9a8 LR |
565 | } |
566 | } | |
1da177e4 LT |
567 | |
568 | return NULL; | |
569 | } | |
570 | ||
571 | ||
572 | static void* | |
573 | sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len) | |
574 | { | |
7e81d825 | 575 | static const u8 eof_header[4][4] = { |
f327ebbd LR |
576 | {0x00, 0x00, 0x00, 0x00}, |
577 | {0x40, 0x00, 0x00, 0x00}, | |
578 | {0x80, 0x00, 0x00, 0x00}, | |
579 | {0xc0, 0x00, 0x00, 0x00}, | |
580 | }; | |
581 | size_t i, j; | |
1da177e4 | 582 | |
f423b9a8 | 583 | /* The EOF header does not exist in compressed data */ |
f327ebbd LR |
584 | if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X || |
585 | cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG) | |
f423b9a8 | 586 | return NULL; |
1da177e4 | 587 | |
f423b9a8 LR |
588 | /* |
589 | The EOF header might cross the packet boundary, but this is not a | |
590 | problem, since the end of a frame is determined by checking its size | |
591 | in the first place. | |
592 | */ | |
f327ebbd LR |
593 | for (i = 0; (len >= 4) && (i <= len - 4); i++) |
594 | for (j = 0; j < ARRAY_SIZE(eof_header); j++) | |
595 | if (!memcmp(mem + i, eof_header[j], 4)) | |
1da177e4 LT |
596 | return mem + i; |
597 | ||
598 | return NULL; | |
599 | } | |
600 | ||
601 | ||
f327ebbd LR |
602 | static void |
603 | sn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f) | |
604 | { | |
7e81d825 | 605 | static const u8 jpeg_header[589] = { |
f327ebbd LR |
606 | 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05, |
607 | 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06, | |
608 | 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e, | |
609 | 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16, | |
610 | 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16, | |
611 | 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29, | |
612 | 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29, | |
613 | 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a, | |
614 | 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28, | |
615 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, | |
616 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, | |
617 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, | |
618 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, | |
619 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2, | |
620 | 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, | |
621 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, | |
622 | 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01, | |
623 | 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, | |
624 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, | |
625 | 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00, | |
626 | 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, | |
627 | 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04, | |
628 | 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, | |
629 | 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, | |
630 | 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, | |
631 | 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, | |
632 | 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, | |
633 | 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, | |
634 | 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, | |
635 | 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, | |
636 | 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, | |
637 | 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, | |
638 | 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, | |
639 | 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, | |
640 | 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, | |
641 | 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, | |
642 | 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, | |
643 | 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02, | |
644 | 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, | |
645 | 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, | |
646 | 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, | |
647 | 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, | |
648 | 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, | |
649 | 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, | |
650 | 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, | |
651 | 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, | |
652 | 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, | |
653 | 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, | |
654 | 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, | |
655 | 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, | |
656 | 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, | |
657 | 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, | |
658 | 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, | |
659 | 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, | |
660 | 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, | |
661 | 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11, | |
662 | 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02, | |
663 | 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03, | |
664 | 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00 | |
665 | }; | |
666 | u8 *pos = f->bufmem; | |
667 | ||
668 | memcpy(pos, jpeg_header, sizeof(jpeg_header)); | |
669 | *(pos + 6) = 0x00; | |
670 | *(pos + 7 + 64) = 0x01; | |
671 | if (cam->compression.quality == 0) { | |
672 | memcpy(pos + 7, SN9C102_Y_QTABLE0, 64); | |
673 | memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64); | |
674 | } else if (cam->compression.quality == 1) { | |
675 | memcpy(pos + 7, SN9C102_Y_QTABLE1, 64); | |
676 | memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64); | |
677 | } | |
678 | *(pos + 564) = cam->sensor.pix_format.width & 0xFF; | |
679 | *(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF; | |
680 | *(pos + 562) = cam->sensor.pix_format.height & 0xFF; | |
681 | *(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF; | |
682 | *(pos + 567) = 0x21; | |
683 | ||
684 | f->buf.bytesused += sizeof(jpeg_header); | |
685 | } | |
686 | ||
687 | ||
7d12e780 | 688 | static void sn9c102_urb_complete(struct urb *urb) |
1da177e4 LT |
689 | { |
690 | struct sn9c102_device* cam = urb->context; | |
691 | struct sn9c102_frame_t** f; | |
a966f3e7 | 692 | size_t imagesize, soflen; |
1da177e4 LT |
693 | u8 i; |
694 | int err = 0; | |
695 | ||
696 | if (urb->status == -ENOENT) | |
697 | return; | |
698 | ||
699 | f = &cam->frame_current; | |
700 | ||
701 | if (cam->stream == STREAM_INTERRUPT) { | |
702 | cam->stream = STREAM_OFF; | |
703 | if ((*f)) | |
704 | (*f)->state = F_QUEUED; | |
f423b9a8 | 705 | cam->sof.bytesread = 0; |
f327ebbd | 706 | DBG(3, "Stream interrupted by application"); |
2ffab02f | 707 | wake_up(&cam->wait_stream); |
1da177e4 LT |
708 | } |
709 | ||
710 | if (cam->state & DEV_DISCONNECTED) | |
711 | return; | |
712 | ||
713 | if (cam->state & DEV_MISCONFIGURED) { | |
714 | wake_up_interruptible(&cam->wait_frame); | |
715 | return; | |
716 | } | |
717 | ||
718 | if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue)) | |
719 | goto resubmit_urb; | |
720 | ||
721 | if (!(*f)) | |
722 | (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t, | |
d56410e0 | 723 | frame); |
1da177e4 | 724 | |
2ffab02f | 725 | imagesize = (cam->sensor.pix_format.width * |
d56410e0 MCC |
726 | cam->sensor.pix_format.height * |
727 | cam->sensor.pix_format.priv) / 8; | |
f327ebbd LR |
728 | if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG) |
729 | imagesize += 589; /* length of jpeg header */ | |
730 | soflen = sn9c102_sof_length(cam); | |
a966f3e7 | 731 | |
1da177e4 LT |
732 | for (i = 0; i < urb->number_of_packets; i++) { |
733 | unsigned int img, len, status; | |
734 | void *pos, *sof, *eof; | |
735 | ||
736 | len = urb->iso_frame_desc[i].actual_length; | |
737 | status = urb->iso_frame_desc[i].status; | |
738 | pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer; | |
739 | ||
740 | if (status) { | |
a966f3e7 | 741 | DBG(3, "Error in isochronous frame"); |
1da177e4 | 742 | (*f)->state = F_ERROR; |
f423b9a8 | 743 | cam->sof.bytesread = 0; |
1da177e4 LT |
744 | continue; |
745 | } | |
746 | ||
a966f3e7 | 747 | PDBGG("Isochrnous frame: length %u, #%u i", len, i); |
1da177e4 LT |
748 | |
749 | redo: | |
750 | sof = sn9c102_find_sof_header(cam, pos, len); | |
2ffab02f | 751 | if (likely(!sof)) { |
1da177e4 LT |
752 | eof = sn9c102_find_eof_header(cam, pos, len); |
753 | if ((*f)->state == F_GRABBING) { | |
754 | end_of_frame: | |
755 | img = len; | |
756 | ||
757 | if (eof) | |
758 | img = (eof > pos) ? eof - pos - 1 : 0; | |
759 | ||
f423b9a8 | 760 | if ((*f)->buf.bytesused + img > imagesize) { |
2ffab02f LR |
761 | u32 b; |
762 | b = (*f)->buf.bytesused + img - | |
763 | imagesize; | |
1da177e4 | 764 | img = imagesize - (*f)->buf.bytesused; |
f423b9a8 LR |
765 | PDBGG("Expected EOF not found: video " |
766 | "frame cut"); | |
1da177e4 LT |
767 | if (eof) |
768 | DBG(3, "Exceeded limit: +%u " | |
a966f3e7 | 769 | "bytes", (unsigned)(b)); |
1da177e4 LT |
770 | } |
771 | ||
772 | memcpy((*f)->bufmem + (*f)->buf.bytesused, pos, | |
773 | img); | |
774 | ||
775 | if ((*f)->buf.bytesused == 0) | |
776 | do_gettimeofday(&(*f)->buf.timestamp); | |
777 | ||
778 | (*f)->buf.bytesused += img; | |
779 | ||
780 | if ((*f)->buf.bytesused == imagesize || | |
f327ebbd LR |
781 | ((cam->sensor.pix_format.pixelformat == |
782 | V4L2_PIX_FMT_SN9C10X || | |
783 | cam->sensor.pix_format.pixelformat == | |
784 | V4L2_PIX_FMT_JPEG) && eof)) { | |
2ffab02f | 785 | u32 b; |
f327ebbd | 786 | |
2ffab02f | 787 | b = (*f)->buf.bytesused; |
1da177e4 LT |
788 | (*f)->state = F_DONE; |
789 | (*f)->buf.sequence= ++cam->frame_count; | |
f327ebbd | 790 | |
a966f3e7 | 791 | spin_lock(&cam->queue_lock); |
1da177e4 | 792 | list_move_tail(&(*f)->frame, |
d56410e0 | 793 | &cam->outqueue); |
1da177e4 LT |
794 | if (!list_empty(&cam->inqueue)) |
795 | (*f) = list_entry( | |
d56410e0 MCC |
796 | cam->inqueue.next, |
797 | struct sn9c102_frame_t, | |
798 | frame ); | |
1da177e4 LT |
799 | else |
800 | (*f) = NULL; | |
a966f3e7 | 801 | spin_unlock(&cam->queue_lock); |
f327ebbd | 802 | |
1da177e4 | 803 | memcpy(cam->sysfs.frame_header, |
f423b9a8 | 804 | cam->sof.header, soflen); |
f327ebbd | 805 | |
a966f3e7 LR |
806 | DBG(3, "Video frame captured: %lu " |
807 | "bytes", (unsigned long)(b)); | |
1da177e4 LT |
808 | |
809 | if (!(*f)) | |
810 | goto resubmit_urb; | |
811 | ||
812 | } else if (eof) { | |
813 | (*f)->state = F_ERROR; | |
814 | DBG(3, "Not expected EOF after %lu " | |
d56410e0 | 815 | "bytes of image data", |
a966f3e7 LR |
816 | (unsigned long) |
817 | ((*f)->buf.bytesused)); | |
1da177e4 LT |
818 | } |
819 | ||
820 | if (sof) /* (1) */ | |
821 | goto start_of_frame; | |
822 | ||
823 | } else if (eof) { | |
a966f3e7 | 824 | DBG(3, "EOF without SOF"); |
1da177e4 LT |
825 | continue; |
826 | ||
827 | } else { | |
a966f3e7 | 828 | PDBGG("Ignoring pointless isochronous frame"); |
1da177e4 LT |
829 | continue; |
830 | } | |
831 | ||
832 | } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) { | |
833 | start_of_frame: | |
834 | (*f)->state = F_GRABBING; | |
835 | (*f)->buf.bytesused = 0; | |
836 | len -= (sof - pos); | |
837 | pos = sof; | |
f327ebbd LR |
838 | if (cam->sensor.pix_format.pixelformat == |
839 | V4L2_PIX_FMT_JPEG) | |
840 | sn9c102_write_jpegheader(cam, (*f)); | |
a966f3e7 | 841 | DBG(3, "SOF detected: new video frame"); |
1da177e4 LT |
842 | if (len) |
843 | goto redo; | |
844 | ||
845 | } else if ((*f)->state == F_GRABBING) { | |
846 | eof = sn9c102_find_eof_header(cam, pos, len); | |
847 | if (eof && eof < sof) | |
848 | goto end_of_frame; /* (1) */ | |
849 | else { | |
2ffab02f | 850 | if (cam->sensor.pix_format.pixelformat == |
f327ebbd LR |
851 | V4L2_PIX_FMT_SN9C10X || |
852 | cam->sensor.pix_format.pixelformat == | |
853 | V4L2_PIX_FMT_JPEG) { | |
f423b9a8 LR |
854 | if (sof - pos >= soflen) { |
855 | eof = sof - soflen; | |
856 | } else { /* remove header */ | |
857 | eof = pos; | |
858 | (*f)->buf.bytesused -= | |
859 | (soflen - (sof - pos)); | |
860 | } | |
1da177e4 LT |
861 | goto end_of_frame; |
862 | } else { | |
863 | DBG(3, "SOF before expected EOF after " | |
d56410e0 | 864 | "%lu bytes of image data", |
a966f3e7 LR |
865 | (unsigned long) |
866 | ((*f)->buf.bytesused)); | |
1da177e4 LT |
867 | goto start_of_frame; |
868 | } | |
869 | } | |
870 | } | |
871 | } | |
872 | ||
873 | resubmit_urb: | |
874 | urb->dev = cam->usbdev; | |
875 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
876 | if (err < 0 && err != -EPERM) { | |
877 | cam->state |= DEV_MISCONFIGURED; | |
a966f3e7 | 878 | DBG(1, "usb_submit_urb() failed"); |
1da177e4 LT |
879 | } |
880 | ||
881 | wake_up_interruptible(&cam->wait_frame); | |
882 | } | |
883 | ||
884 | ||
885 | static int sn9c102_start_transfer(struct sn9c102_device* cam) | |
886 | { | |
887 | struct usb_device *udev = cam->usbdev; | |
888 | struct urb* urb; | |
f327ebbd LR |
889 | struct usb_host_interface* altsetting = usb_altnum_to_altsetting( |
890 | usb_ifnum_to_if(udev, 0), | |
891 | SN9C102_ALTERNATE_SETTING); | |
892 | const unsigned int psz = le16_to_cpu(altsetting-> | |
893 | endpoint[0].desc.wMaxPacketSize); | |
1da177e4 LT |
894 | s8 i, j; |
895 | int err = 0; | |
896 | ||
897 | for (i = 0; i < SN9C102_URBS; i++) { | |
cd6fcc55 | 898 | cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz, |
d56410e0 | 899 | GFP_KERNEL); |
1da177e4 LT |
900 | if (!cam->transfer_buffer[i]) { |
901 | err = -ENOMEM; | |
a966f3e7 | 902 | DBG(1, "Not enough memory"); |
1da177e4 LT |
903 | goto free_buffers; |
904 | } | |
905 | } | |
906 | ||
907 | for (i = 0; i < SN9C102_URBS; i++) { | |
908 | urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL); | |
909 | cam->urb[i] = urb; | |
910 | if (!urb) { | |
911 | err = -ENOMEM; | |
a966f3e7 | 912 | DBG(1, "usb_alloc_urb() failed"); |
1da177e4 LT |
913 | goto free_urbs; |
914 | } | |
915 | urb->dev = udev; | |
916 | urb->context = cam; | |
917 | urb->pipe = usb_rcvisocpipe(udev, 1); | |
918 | urb->transfer_flags = URB_ISO_ASAP; | |
919 | urb->number_of_packets = SN9C102_ISO_PACKETS; | |
920 | urb->complete = sn9c102_urb_complete; | |
921 | urb->transfer_buffer = cam->transfer_buffer[i]; | |
922 | urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS; | |
923 | urb->interval = 1; | |
924 | for (j = 0; j < SN9C102_ISO_PACKETS; j++) { | |
925 | urb->iso_frame_desc[j].offset = psz * j; | |
926 | urb->iso_frame_desc[j].length = psz; | |
927 | } | |
928 | } | |
929 | ||
930 | /* Enable video */ | |
931 | if (!(cam->reg[0x01] & 0x04)) { | |
932 | err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01); | |
933 | if (err) { | |
934 | err = -EIO; | |
a966f3e7 | 935 | DBG(1, "I/O hardware error"); |
1da177e4 LT |
936 | goto free_urbs; |
937 | } | |
938 | } | |
939 | ||
940 | err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING); | |
941 | if (err) { | |
a966f3e7 | 942 | DBG(1, "usb_set_interface() failed"); |
1da177e4 LT |
943 | goto free_urbs; |
944 | } | |
945 | ||
946 | cam->frame_current = NULL; | |
f423b9a8 | 947 | cam->sof.bytesread = 0; |
1da177e4 LT |
948 | |
949 | for (i = 0; i < SN9C102_URBS; i++) { | |
950 | err = usb_submit_urb(cam->urb[i], GFP_KERNEL); | |
951 | if (err) { | |
952 | for (j = i-1; j >= 0; j--) | |
953 | usb_kill_urb(cam->urb[j]); | |
a966f3e7 | 954 | DBG(1, "usb_submit_urb() failed, error %d", err); |
1da177e4 LT |
955 | goto free_urbs; |
956 | } | |
957 | } | |
958 | ||
959 | return 0; | |
960 | ||
961 | free_urbs: | |
f327ebbd | 962 | for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++) |
1da177e4 LT |
963 | usb_free_urb(cam->urb[i]); |
964 | ||
965 | free_buffers: | |
966 | for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++) | |
967 | kfree(cam->transfer_buffer[i]); | |
968 | ||
969 | return err; | |
970 | } | |
971 | ||
972 | ||
973 | static int sn9c102_stop_transfer(struct sn9c102_device* cam) | |
974 | { | |
975 | struct usb_device *udev = cam->usbdev; | |
976 | s8 i; | |
977 | int err = 0; | |
978 | ||
979 | if (cam->state & DEV_DISCONNECTED) | |
980 | return 0; | |
981 | ||
982 | for (i = SN9C102_URBS-1; i >= 0; i--) { | |
983 | usb_kill_urb(cam->urb[i]); | |
984 | usb_free_urb(cam->urb[i]); | |
985 | kfree(cam->transfer_buffer[i]); | |
986 | } | |
987 | ||
988 | err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */ | |
989 | if (err) | |
a966f3e7 | 990 | DBG(3, "usb_set_interface() failed"); |
1da177e4 LT |
991 | |
992 | return err; | |
993 | } | |
994 | ||
995 | ||
7107627b | 996 | static int sn9c102_stream_interrupt(struct sn9c102_device* cam) |
1da177e4 | 997 | { |
2ffab02f | 998 | long timeout; |
1da177e4 LT |
999 | |
1000 | cam->stream = STREAM_INTERRUPT; | |
2ffab02f | 1001 | timeout = wait_event_timeout(cam->wait_stream, |
d56410e0 MCC |
1002 | (cam->stream == STREAM_OFF) || |
1003 | (cam->state & DEV_DISCONNECTED), | |
1004 | SN9C102_URB_TIMEOUT); | |
1da177e4 LT |
1005 | if (cam->state & DEV_DISCONNECTED) |
1006 | return -ENODEV; | |
2ffab02f | 1007 | else if (cam->stream != STREAM_OFF) { |
1da177e4 | 1008 | cam->state |= DEV_MISCONFIGURED; |
2ffab02f LR |
1009 | DBG(1, "URB timeout reached. The camera is misconfigured. " |
1010 | "To use it, close and open /dev/video%d again.", | |
c6330fb8 | 1011 | cam->v4ldev->num); |
2ffab02f | 1012 | return -EIO; |
1da177e4 LT |
1013 | } |
1014 | ||
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | /*****************************************************************************/ | |
1019 | ||
cd6fcc55 | 1020 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
f327ebbd | 1021 | static u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count) |
1da177e4 | 1022 | { |
f327ebbd | 1023 | char str[7]; |
1da177e4 LT |
1024 | char* endp; |
1025 | unsigned long val; | |
1026 | ||
f327ebbd | 1027 | if (len < 6) { |
1da177e4 | 1028 | strncpy(str, buff, len); |
f423b9a8 | 1029 | str[len] = '\0'; |
1da177e4 | 1030 | } else { |
f423b9a8 | 1031 | strncpy(str, buff, 6); |
f327ebbd | 1032 | str[6] = '\0'; |
1da177e4 LT |
1033 | } |
1034 | ||
1035 | val = simple_strtoul(str, &endp, 0); | |
1036 | ||
1037 | *count = 0; | |
f327ebbd | 1038 | if (val <= 0xffff) |
1da177e4 LT |
1039 | *count = (ssize_t)(endp - str); |
1040 | if ((*count) && (len == *count+1) && (buff[*count] == '\n')) | |
1041 | *count += 1; | |
1042 | ||
f327ebbd | 1043 | return (u16)val; |
1da177e4 LT |
1044 | } |
1045 | ||
1046 | /* | |
1047 | NOTE 1: being inside one of the following methods implies that the v4l | |
d56410e0 | 1048 | device exists for sure (see kobjects and reference counters) |
1da177e4 LT |
1049 | NOTE 2: buffers are PAGE_SIZE long |
1050 | */ | |
1051 | ||
54bd5b66 KS |
1052 | static ssize_t sn9c102_show_reg(struct device* cd, |
1053 | struct device_attribute *attr, char* buf) | |
1da177e4 LT |
1054 | { |
1055 | struct sn9c102_device* cam; | |
1056 | ssize_t count; | |
1057 | ||
4186ecf8 | 1058 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1059 | return -ERESTARTSYS; |
1060 | ||
22a04f10 | 1061 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1062 | if (!cam) { |
4186ecf8 | 1063 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1064 | return -ENODEV; |
1065 | } | |
1066 | ||
1067 | count = sprintf(buf, "%u\n", cam->sysfs.reg); | |
1068 | ||
4186ecf8 | 1069 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1070 | |
1071 | return count; | |
d56410e0 | 1072 | } |
1da177e4 LT |
1073 | |
1074 | ||
d56410e0 | 1075 | static ssize_t |
54bd5b66 KS |
1076 | sn9c102_store_reg(struct device* cd, struct device_attribute *attr, |
1077 | const char* buf, size_t len) | |
1da177e4 LT |
1078 | { |
1079 | struct sn9c102_device* cam; | |
f327ebbd | 1080 | u16 index; |
1da177e4 LT |
1081 | ssize_t count; |
1082 | ||
4186ecf8 | 1083 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1084 | return -ERESTARTSYS; |
1085 | ||
22a04f10 | 1086 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1087 | if (!cam) { |
4186ecf8 | 1088 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1089 | return -ENODEV; |
1090 | } | |
1091 | ||
f327ebbd LR |
1092 | index = sn9c102_strtou16(buf, len, &count); |
1093 | if (index >= ARRAY_SIZE(cam->reg) || !count) { | |
4186ecf8 | 1094 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1095 | return -EINVAL; |
1096 | } | |
1097 | ||
1098 | cam->sysfs.reg = index; | |
1099 | ||
f327ebbd | 1100 | DBG(2, "Moved SN9C1XX register index to 0x%02X", cam->sysfs.reg); |
a966f3e7 | 1101 | DBG(3, "Written bytes: %zd", count); |
1da177e4 | 1102 | |
4186ecf8 | 1103 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1104 | |
1105 | return count; | |
1106 | } | |
1107 | ||
1108 | ||
54bd5b66 KS |
1109 | static ssize_t sn9c102_show_val(struct device* cd, |
1110 | struct device_attribute *attr, char* buf) | |
1da177e4 LT |
1111 | { |
1112 | struct sn9c102_device* cam; | |
1113 | ssize_t count; | |
1114 | int val; | |
1115 | ||
4186ecf8 | 1116 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1117 | return -ERESTARTSYS; |
1118 | ||
22a04f10 | 1119 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1120 | if (!cam) { |
4186ecf8 | 1121 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1122 | return -ENODEV; |
1123 | } | |
1124 | ||
1125 | if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) { | |
4186ecf8 | 1126 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1127 | return -EIO; |
1128 | } | |
1129 | ||
1130 | count = sprintf(buf, "%d\n", val); | |
1131 | ||
f423b9a8 | 1132 | DBG(3, "Read bytes: %zd, value: %d", count, val); |
1da177e4 | 1133 | |
4186ecf8 | 1134 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1135 | |
1136 | return count; | |
d56410e0 | 1137 | } |
1da177e4 LT |
1138 | |
1139 | ||
1140 | static ssize_t | |
54bd5b66 KS |
1141 | sn9c102_store_val(struct device* cd, struct device_attribute *attr, |
1142 | const char* buf, size_t len) | |
1da177e4 LT |
1143 | { |
1144 | struct sn9c102_device* cam; | |
f327ebbd | 1145 | u16 value; |
1da177e4 LT |
1146 | ssize_t count; |
1147 | int err; | |
1148 | ||
4186ecf8 | 1149 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1150 | return -ERESTARTSYS; |
1151 | ||
22a04f10 | 1152 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1153 | if (!cam) { |
4186ecf8 | 1154 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1155 | return -ENODEV; |
1156 | } | |
1157 | ||
f327ebbd | 1158 | value = sn9c102_strtou16(buf, len, &count); |
1da177e4 | 1159 | if (!count) { |
4186ecf8 | 1160 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1161 | return -EINVAL; |
1162 | } | |
1163 | ||
1164 | err = sn9c102_write_reg(cam, value, cam->sysfs.reg); | |
1165 | if (err) { | |
4186ecf8 | 1166 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1167 | return -EIO; |
1168 | } | |
1169 | ||
f327ebbd | 1170 | DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X", |
a966f3e7 LR |
1171 | cam->sysfs.reg, value); |
1172 | DBG(3, "Written bytes: %zd", count); | |
1da177e4 | 1173 | |
4186ecf8 | 1174 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1175 | |
1176 | return count; | |
1177 | } | |
1178 | ||
1179 | ||
54bd5b66 KS |
1180 | static ssize_t sn9c102_show_i2c_reg(struct device* cd, |
1181 | struct device_attribute *attr, char* buf) | |
1da177e4 LT |
1182 | { |
1183 | struct sn9c102_device* cam; | |
1184 | ssize_t count; | |
1185 | ||
4186ecf8 | 1186 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1187 | return -ERESTARTSYS; |
1188 | ||
22a04f10 | 1189 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1190 | if (!cam) { |
4186ecf8 | 1191 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1192 | return -ENODEV; |
1193 | } | |
1194 | ||
1195 | count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg); | |
1196 | ||
a966f3e7 | 1197 | DBG(3, "Read bytes: %zd", count); |
1da177e4 | 1198 | |
4186ecf8 | 1199 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1200 | |
1201 | return count; | |
1202 | } | |
1203 | ||
1204 | ||
d56410e0 | 1205 | static ssize_t |
54bd5b66 KS |
1206 | sn9c102_store_i2c_reg(struct device* cd, struct device_attribute *attr, |
1207 | const char* buf, size_t len) | |
1da177e4 LT |
1208 | { |
1209 | struct sn9c102_device* cam; | |
f327ebbd | 1210 | u16 index; |
1da177e4 LT |
1211 | ssize_t count; |
1212 | ||
4186ecf8 | 1213 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1214 | return -ERESTARTSYS; |
1215 | ||
22a04f10 | 1216 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1217 | if (!cam) { |
4186ecf8 | 1218 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1219 | return -ENODEV; |
1220 | } | |
1221 | ||
f327ebbd | 1222 | index = sn9c102_strtou16(buf, len, &count); |
1da177e4 | 1223 | if (!count) { |
4186ecf8 | 1224 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1225 | return -EINVAL; |
1226 | } | |
1227 | ||
1228 | cam->sysfs.i2c_reg = index; | |
1229 | ||
a966f3e7 LR |
1230 | DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg); |
1231 | DBG(3, "Written bytes: %zd", count); | |
1da177e4 | 1232 | |
4186ecf8 | 1233 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1234 | |
1235 | return count; | |
1236 | } | |
1237 | ||
1238 | ||
54bd5b66 KS |
1239 | static ssize_t sn9c102_show_i2c_val(struct device* cd, |
1240 | struct device_attribute *attr, char* buf) | |
1da177e4 LT |
1241 | { |
1242 | struct sn9c102_device* cam; | |
1243 | ssize_t count; | |
1244 | int val; | |
1245 | ||
4186ecf8 | 1246 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1247 | return -ERESTARTSYS; |
1248 | ||
22a04f10 | 1249 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1250 | if (!cam) { |
4186ecf8 | 1251 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1252 | return -ENODEV; |
1253 | } | |
1254 | ||
2ffab02f | 1255 | if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) { |
4186ecf8 | 1256 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1257 | return -ENOSYS; |
1258 | } | |
1259 | ||
1260 | if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) { | |
4186ecf8 | 1261 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1262 | return -EIO; |
1263 | } | |
1264 | ||
1265 | count = sprintf(buf, "%d\n", val); | |
1266 | ||
f423b9a8 | 1267 | DBG(3, "Read bytes: %zd, value: %d", count, val); |
1da177e4 | 1268 | |
4186ecf8 | 1269 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1270 | |
1271 | return count; | |
d56410e0 | 1272 | } |
1da177e4 LT |
1273 | |
1274 | ||
1275 | static ssize_t | |
54bd5b66 KS |
1276 | sn9c102_store_i2c_val(struct device* cd, struct device_attribute *attr, |
1277 | const char* buf, size_t len) | |
1da177e4 LT |
1278 | { |
1279 | struct sn9c102_device* cam; | |
f327ebbd | 1280 | u16 value; |
1da177e4 LT |
1281 | ssize_t count; |
1282 | int err; | |
1283 | ||
4186ecf8 | 1284 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1285 | return -ERESTARTSYS; |
1286 | ||
22a04f10 | 1287 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1288 | if (!cam) { |
4186ecf8 | 1289 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1290 | return -ENODEV; |
1291 | } | |
1292 | ||
2ffab02f | 1293 | if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) { |
4186ecf8 | 1294 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1295 | return -ENOSYS; |
1296 | } | |
1297 | ||
f327ebbd | 1298 | value = sn9c102_strtou16(buf, len, &count); |
1da177e4 | 1299 | if (!count) { |
4186ecf8 | 1300 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1301 | return -EINVAL; |
1302 | } | |
1303 | ||
1304 | err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value); | |
1305 | if (err) { | |
4186ecf8 | 1306 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1307 | return -EIO; |
1308 | } | |
1309 | ||
1310 | DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X", | |
a966f3e7 LR |
1311 | cam->sysfs.i2c_reg, value); |
1312 | DBG(3, "Written bytes: %zd", count); | |
1da177e4 | 1313 | |
4186ecf8 | 1314 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1315 | |
1316 | return count; | |
1317 | } | |
1318 | ||
1319 | ||
1320 | static ssize_t | |
54bd5b66 KS |
1321 | sn9c102_store_green(struct device* cd, struct device_attribute *attr, |
1322 | const char* buf, size_t len) | |
1da177e4 LT |
1323 | { |
1324 | struct sn9c102_device* cam; | |
1325 | enum sn9c102_bridge bridge; | |
1326 | ssize_t res = 0; | |
f327ebbd | 1327 | u16 value; |
1da177e4 LT |
1328 | ssize_t count; |
1329 | ||
4186ecf8 | 1330 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) |
1da177e4 LT |
1331 | return -ERESTARTSYS; |
1332 | ||
22a04f10 | 1333 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 | 1334 | if (!cam) { |
4186ecf8 | 1335 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 LT |
1336 | return -ENODEV; |
1337 | } | |
1338 | ||
1339 | bridge = cam->bridge; | |
1340 | ||
4186ecf8 | 1341 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 | 1342 | |
f327ebbd | 1343 | value = sn9c102_strtou16(buf, len, &count); |
1da177e4 LT |
1344 | if (!count) |
1345 | return -EINVAL; | |
1346 | ||
1347 | switch (bridge) { | |
1348 | case BRIDGE_SN9C101: | |
1349 | case BRIDGE_SN9C102: | |
1350 | if (value > 0x0f) | |
1351 | return -EINVAL; | |
54bd5b66 KS |
1352 | if ((res = sn9c102_store_reg(cd, attr, "0x11", 4)) >= 0) |
1353 | res = sn9c102_store_val(cd, attr, buf, len); | |
1da177e4 LT |
1354 | break; |
1355 | case BRIDGE_SN9C103: | |
f327ebbd LR |
1356 | case BRIDGE_SN9C105: |
1357 | case BRIDGE_SN9C120: | |
1da177e4 LT |
1358 | if (value > 0x7f) |
1359 | return -EINVAL; | |
54bd5b66 KS |
1360 | if ((res = sn9c102_store_reg(cd, attr, "0x07", 4)) >= 0) |
1361 | res = sn9c102_store_val(cd, attr, buf, len); | |
1da177e4 LT |
1362 | break; |
1363 | } | |
1364 | ||
1365 | return res; | |
1366 | } | |
1367 | ||
1368 | ||
1369 | static ssize_t | |
54bd5b66 KS |
1370 | sn9c102_store_blue(struct device* cd, struct device_attribute *attr, |
1371 | const char* buf, size_t len) | |
1da177e4 LT |
1372 | { |
1373 | ssize_t res = 0; | |
f327ebbd | 1374 | u16 value; |
1da177e4 LT |
1375 | ssize_t count; |
1376 | ||
f327ebbd | 1377 | value = sn9c102_strtou16(buf, len, &count); |
1da177e4 LT |
1378 | if (!count || value > 0x7f) |
1379 | return -EINVAL; | |
1380 | ||
54bd5b66 KS |
1381 | if ((res = sn9c102_store_reg(cd, attr, "0x06", 4)) >= 0) |
1382 | res = sn9c102_store_val(cd, attr, buf, len); | |
1da177e4 LT |
1383 | |
1384 | return res; | |
1385 | } | |
1386 | ||
1387 | ||
1388 | static ssize_t | |
54bd5b66 KS |
1389 | sn9c102_store_red(struct device* cd, struct device_attribute *attr, |
1390 | const char* buf, size_t len) | |
1da177e4 LT |
1391 | { |
1392 | ssize_t res = 0; | |
f327ebbd | 1393 | u16 value; |
1da177e4 LT |
1394 | ssize_t count; |
1395 | ||
f327ebbd | 1396 | value = sn9c102_strtou16(buf, len, &count); |
1da177e4 LT |
1397 | if (!count || value > 0x7f) |
1398 | return -EINVAL; | |
1399 | ||
54bd5b66 KS |
1400 | if ((res = sn9c102_store_reg(cd, attr, "0x05", 4)) >= 0) |
1401 | res = sn9c102_store_val(cd, attr, buf, len); | |
1da177e4 LT |
1402 | |
1403 | return res; | |
1404 | } | |
1405 | ||
1406 | ||
54bd5b66 KS |
1407 | static ssize_t sn9c102_show_frame_header(struct device* cd, |
1408 | struct device_attribute *attr, | |
1409 | char* buf) | |
1da177e4 LT |
1410 | { |
1411 | struct sn9c102_device* cam; | |
1412 | ssize_t count; | |
1413 | ||
22a04f10 | 1414 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); |
1da177e4 LT |
1415 | if (!cam) |
1416 | return -ENODEV; | |
1417 | ||
1418 | count = sizeof(cam->sysfs.frame_header); | |
1419 | memcpy(buf, cam->sysfs.frame_header, count); | |
1420 | ||
a966f3e7 | 1421 | DBG(3, "Frame header, read bytes: %zd", count); |
1da177e4 LT |
1422 | |
1423 | return count; | |
d56410e0 | 1424 | } |
1da177e4 LT |
1425 | |
1426 | ||
54bd5b66 KS |
1427 | static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR, sn9c102_show_reg, sn9c102_store_reg); |
1428 | static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, sn9c102_show_val, sn9c102_store_val); | |
1429 | static DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR, | |
1430 | sn9c102_show_i2c_reg, sn9c102_store_i2c_reg); | |
1431 | static DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR, | |
1432 | sn9c102_show_i2c_val, sn9c102_store_i2c_val); | |
1433 | static DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green); | |
1434 | static DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue); | |
1435 | static DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red); | |
1436 | static DEVICE_ATTR(frame_header, S_IRUGO, sn9c102_show_frame_header, NULL); | |
1da177e4 LT |
1437 | |
1438 | ||
c12e3be0 | 1439 | static int sn9c102_create_sysfs(struct sn9c102_device* cam) |
1da177e4 | 1440 | { |
22a04f10 | 1441 | struct device *dev = &(cam->v4ldev->dev); |
f327ebbd | 1442 | int err = 0; |
c12e3be0 | 1443 | |
22a04f10 | 1444 | if ((err = device_create_file(dev, &dev_attr_reg))) |
f327ebbd | 1445 | goto err_out; |
22a04f10 | 1446 | if ((err = device_create_file(dev, &dev_attr_val))) |
f327ebbd | 1447 | goto err_reg; |
22a04f10 | 1448 | if ((err = device_create_file(dev, &dev_attr_frame_header))) |
f327ebbd | 1449 | goto err_val; |
1da177e4 | 1450 | |
2ffab02f | 1451 | if (cam->sensor.sysfs_ops) { |
22a04f10 | 1452 | if ((err = device_create_file(dev, &dev_attr_i2c_reg))) |
f327ebbd | 1453 | goto err_frame_header; |
22a04f10 | 1454 | if ((err = device_create_file(dev, &dev_attr_i2c_val))) |
f327ebbd | 1455 | goto err_i2c_reg; |
c12e3be0 JG |
1456 | } |
1457 | ||
1458 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) { | |
22a04f10 | 1459 | if ((err = device_create_file(dev, &dev_attr_green))) |
f327ebbd LR |
1460 | goto err_i2c_val; |
1461 | } else { | |
22a04f10 | 1462 | if ((err = device_create_file(dev, &dev_attr_blue))) |
f327ebbd | 1463 | goto err_i2c_val; |
22a04f10 | 1464 | if ((err = device_create_file(dev, &dev_attr_red))) |
f327ebbd | 1465 | goto err_blue; |
1da177e4 | 1466 | } |
c12e3be0 JG |
1467 | |
1468 | return 0; | |
1469 | ||
1470 | err_blue: | |
22a04f10 | 1471 | device_remove_file(dev, &dev_attr_blue); |
c12e3be0 JG |
1472 | err_i2c_val: |
1473 | if (cam->sensor.sysfs_ops) | |
22a04f10 | 1474 | device_remove_file(dev, &dev_attr_i2c_val); |
c12e3be0 JG |
1475 | err_i2c_reg: |
1476 | if (cam->sensor.sysfs_ops) | |
22a04f10 | 1477 | device_remove_file(dev, &dev_attr_i2c_reg); |
f327ebbd | 1478 | err_frame_header: |
22a04f10 | 1479 | device_remove_file(dev, &dev_attr_frame_header); |
c12e3be0 | 1480 | err_val: |
22a04f10 | 1481 | device_remove_file(dev, &dev_attr_val); |
c12e3be0 | 1482 | err_reg: |
22a04f10 | 1483 | device_remove_file(dev, &dev_attr_reg); |
f327ebbd LR |
1484 | err_out: |
1485 | return err; | |
1da177e4 | 1486 | } |
cd6fcc55 | 1487 | #endif /* CONFIG_VIDEO_ADV_DEBUG */ |
1da177e4 LT |
1488 | |
1489 | /*****************************************************************************/ | |
1490 | ||
1491 | static int | |
1492 | sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix) | |
1493 | { | |
1494 | int err = 0; | |
1495 | ||
f327ebbd LR |
1496 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X || |
1497 | pix->pixelformat == V4L2_PIX_FMT_JPEG) { | |
1498 | switch (cam->bridge) { | |
1499 | case BRIDGE_SN9C101: | |
1500 | case BRIDGE_SN9C102: | |
1501 | case BRIDGE_SN9C103: | |
1502 | err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, | |
1503 | 0x18); | |
1504 | break; | |
1505 | case BRIDGE_SN9C105: | |
1506 | case BRIDGE_SN9C120: | |
1507 | err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, | |
1508 | 0x18); | |
1509 | break; | |
1510 | } | |
1511 | } else { | |
1512 | switch (cam->bridge) { | |
1513 | case BRIDGE_SN9C101: | |
1514 | case BRIDGE_SN9C102: | |
1515 | case BRIDGE_SN9C103: | |
1516 | err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, | |
1517 | 0x18); | |
1518 | break; | |
1519 | case BRIDGE_SN9C105: | |
1520 | case BRIDGE_SN9C120: | |
1521 | err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, | |
1522 | 0x18); | |
1523 | break; | |
1524 | } | |
1525 | } | |
1da177e4 LT |
1526 | |
1527 | return err ? -EIO : 0; | |
1528 | } | |
1529 | ||
1530 | ||
1531 | static int | |
1532 | sn9c102_set_compression(struct sn9c102_device* cam, | |
d56410e0 | 1533 | struct v4l2_jpegcompression* compression) |
1da177e4 | 1534 | { |
f327ebbd | 1535 | int i, err = 0; |
1da177e4 | 1536 | |
f327ebbd LR |
1537 | switch (cam->bridge) { |
1538 | case BRIDGE_SN9C101: | |
1539 | case BRIDGE_SN9C102: | |
1540 | case BRIDGE_SN9C103: | |
f423b9a8 | 1541 | if (compression->quality == 0) |
f327ebbd LR |
1542 | err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, |
1543 | 0x17); | |
f423b9a8 | 1544 | else if (compression->quality == 1) |
f327ebbd LR |
1545 | err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, |
1546 | 0x17); | |
1547 | break; | |
1548 | case BRIDGE_SN9C105: | |
1549 | case BRIDGE_SN9C120: | |
1550 | if (compression->quality == 0) { | |
1551 | for (i = 0; i <= 63; i++) { | |
1552 | err += sn9c102_write_reg(cam, | |
f423b9a8 | 1553 | SN9C102_Y_QTABLE1[i], |
f327ebbd LR |
1554 | 0x100 + i); |
1555 | err += sn9c102_write_reg(cam, | |
f423b9a8 | 1556 | SN9C102_UV_QTABLE1[i], |
f327ebbd LR |
1557 | 0x140 + i); |
1558 | } | |
1559 | err += sn9c102_write_reg(cam, cam->reg[0x18] & 0xbf, | |
1560 | 0x18); | |
1561 | } else if (compression->quality == 1) { | |
1562 | for (i = 0; i <= 63; i++) { | |
1563 | err += sn9c102_write_reg(cam, | |
1564 | SN9C102_Y_QTABLE1[i], | |
1565 | 0x100 + i); | |
1566 | err += sn9c102_write_reg(cam, | |
1567 | SN9C102_UV_QTABLE1[i], | |
1568 | 0x140 + i); | |
1569 | } | |
1570 | err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x40, | |
1571 | 0x18); | |
1572 | } | |
1573 | break; | |
1574 | } | |
1da177e4 LT |
1575 | |
1576 | return err ? -EIO : 0; | |
1577 | } | |
1578 | ||
1579 | ||
1580 | static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale) | |
1581 | { | |
1582 | u8 r = 0; | |
1583 | int err = 0; | |
1584 | ||
1585 | if (scale == 1) | |
1586 | r = cam->reg[0x18] & 0xcf; | |
1587 | else if (scale == 2) { | |
1588 | r = cam->reg[0x18] & 0xcf; | |
1589 | r |= 0x10; | |
1590 | } else if (scale == 4) | |
1591 | r = cam->reg[0x18] | 0x20; | |
1592 | ||
1593 | err += sn9c102_write_reg(cam, r, 0x18); | |
1594 | if (err) | |
1595 | return -EIO; | |
1596 | ||
a966f3e7 | 1597 | PDBGG("Scaling factor: %u", scale); |
1da177e4 LT |
1598 | |
1599 | return 0; | |
1600 | } | |
1601 | ||
1602 | ||
1603 | static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect) | |
1604 | { | |
2ffab02f | 1605 | struct sn9c102_sensor* s = &cam->sensor; |
1da177e4 LT |
1606 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left), |
1607 | v_start = (u8)(rect->top - s->cropcap.bounds.top), | |
1608 | h_size = (u8)(rect->width / 16), | |
1609 | v_size = (u8)(rect->height / 16); | |
1610 | int err = 0; | |
1611 | ||
1612 | err += sn9c102_write_reg(cam, h_start, 0x12); | |
1613 | err += sn9c102_write_reg(cam, v_start, 0x13); | |
1614 | err += sn9c102_write_reg(cam, h_size, 0x15); | |
1615 | err += sn9c102_write_reg(cam, v_size, 0x16); | |
1616 | if (err) | |
1617 | return -EIO; | |
1618 | ||
1619 | PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size " | |
a966f3e7 | 1620 | "%u %u %u %u", h_start, v_start, h_size, v_size); |
1da177e4 LT |
1621 | |
1622 | return 0; | |
1623 | } | |
1624 | ||
1625 | ||
1626 | static int sn9c102_init(struct sn9c102_device* cam) | |
1627 | { | |
2ffab02f | 1628 | struct sn9c102_sensor* s = &cam->sensor; |
1da177e4 LT |
1629 | struct v4l2_control ctrl; |
1630 | struct v4l2_queryctrl *qctrl; | |
1631 | struct v4l2_rect* rect; | |
52950ed4 | 1632 | u8 i = 0; |
1da177e4 LT |
1633 | int err = 0; |
1634 | ||
1635 | if (!(cam->state & DEV_INITIALIZED)) { | |
3770be34 LR |
1636 | mutex_init(&cam->open_mutex); |
1637 | init_waitqueue_head(&cam->wait_open); | |
1da177e4 LT |
1638 | qctrl = s->qctrl; |
1639 | rect = &(s->cropcap.defrect); | |
1640 | } else { /* use current values */ | |
1641 | qctrl = s->_qctrl; | |
1642 | rect = &(s->_rect); | |
1643 | } | |
1644 | ||
1645 | err += sn9c102_set_scale(cam, rect->width / s->pix_format.width); | |
1646 | err += sn9c102_set_crop(cam, rect); | |
1647 | if (err) | |
1648 | return err; | |
1649 | ||
1650 | if (s->init) { | |
1651 | err = s->init(cam); | |
1652 | if (err) { | |
a966f3e7 | 1653 | DBG(3, "Sensor initialization failed"); |
1da177e4 LT |
1654 | return err; |
1655 | } | |
1656 | } | |
1657 | ||
1658 | if (!(cam->state & DEV_INITIALIZED)) | |
f327ebbd LR |
1659 | if (cam->bridge == BRIDGE_SN9C101 || |
1660 | cam->bridge == BRIDGE_SN9C102 || | |
1661 | cam->bridge == BRIDGE_SN9C103) { | |
f423b9a8 LR |
1662 | if (s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG) |
1663 | s->pix_format.pixelformat= V4L2_PIX_FMT_SBGGR8; | |
f327ebbd LR |
1664 | cam->compression.quality = cam->reg[0x17] & 0x01 ? |
1665 | 0 : 1; | |
1666 | } else { | |
f423b9a8 LR |
1667 | if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X) |
1668 | s->pix_format.pixelformat = V4L2_PIX_FMT_JPEG; | |
f327ebbd LR |
1669 | cam->compression.quality = cam->reg[0x18] & 0x40 ? |
1670 | 0 : 1; | |
1671 | err += sn9c102_set_compression(cam, &cam->compression); | |
1672 | } | |
1da177e4 LT |
1673 | else |
1674 | err += sn9c102_set_compression(cam, &cam->compression); | |
1675 | err += sn9c102_set_pix_format(cam, &s->pix_format); | |
1676 | if (s->set_pix_format) | |
1677 | err += s->set_pix_format(cam, &s->pix_format); | |
1678 | if (err) | |
1679 | return err; | |
1680 | ||
f327ebbd LR |
1681 | if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X || |
1682 | s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG) | |
52950ed4 | 1683 | DBG(3, "Compressed video format is active, quality %d", |
a966f3e7 | 1684 | cam->compression.quality); |
1da177e4 | 1685 | else |
a966f3e7 | 1686 | DBG(3, "Uncompressed video format is active"); |
1da177e4 LT |
1687 | |
1688 | if (s->set_crop) | |
1689 | if ((err = s->set_crop(cam, rect))) { | |
a966f3e7 | 1690 | DBG(3, "set_crop() failed"); |
1da177e4 LT |
1691 | return err; |
1692 | } | |
1693 | ||
1694 | if (s->set_ctrl) { | |
52950ed4 TK |
1695 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
1696 | if (s->qctrl[i].id != 0 && | |
1da177e4 LT |
1697 | !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) { |
1698 | ctrl.id = s->qctrl[i].id; | |
1699 | ctrl.value = qctrl[i].default_value; | |
1700 | err = s->set_ctrl(cam, &ctrl); | |
1701 | if (err) { | |
1702 | DBG(3, "Set %s control failed", | |
a966f3e7 | 1703 | s->qctrl[i].name); |
1da177e4 LT |
1704 | return err; |
1705 | } | |
1706 | DBG(3, "Image sensor supports '%s' control", | |
a966f3e7 | 1707 | s->qctrl[i].name); |
1da177e4 LT |
1708 | } |
1709 | } | |
1710 | ||
1711 | if (!(cam->state & DEV_INITIALIZED)) { | |
4186ecf8 | 1712 | mutex_init(&cam->fileop_mutex); |
1da177e4 LT |
1713 | spin_lock_init(&cam->queue_lock); |
1714 | init_waitqueue_head(&cam->wait_frame); | |
1715 | init_waitqueue_head(&cam->wait_stream); | |
1716 | cam->nreadbuffers = 2; | |
1717 | memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl)); | |
52950ed4 | 1718 | memcpy(&(s->_rect), &(s->cropcap.defrect), |
1da177e4 LT |
1719 | sizeof(struct v4l2_rect)); |
1720 | cam->state |= DEV_INITIALIZED; | |
1721 | } | |
1722 | ||
a966f3e7 | 1723 | DBG(2, "Initialization succeeded"); |
1da177e4 LT |
1724 | return 0; |
1725 | } | |
1726 | ||
3770be34 | 1727 | /*****************************************************************************/ |
1da177e4 | 1728 | |
3770be34 | 1729 | static void sn9c102_release_resources(struct kref *kref) |
1da177e4 | 1730 | { |
3770be34 LR |
1731 | struct sn9c102_device *cam; |
1732 | ||
4186ecf8 | 1733 | mutex_lock(&sn9c102_sysfs_lock); |
1da177e4 | 1734 | |
3770be34 LR |
1735 | cam = container_of(kref, struct sn9c102_device, kref); |
1736 | ||
c6330fb8 | 1737 | DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->num); |
1da177e4 LT |
1738 | video_set_drvdata(cam->v4ldev, NULL); |
1739 | video_unregister_device(cam->v4ldev); | |
3770be34 LR |
1740 | usb_put_dev(cam->usbdev); |
1741 | kfree(cam->control_buffer); | |
1742 | kfree(cam); | |
1da177e4 | 1743 | |
4186ecf8 | 1744 | mutex_unlock(&sn9c102_sysfs_lock); |
1da177e4 | 1745 | |
1da177e4 LT |
1746 | } |
1747 | ||
1da177e4 | 1748 | |
bec43661 | 1749 | static int sn9c102_open(struct file *filp) |
1da177e4 LT |
1750 | { |
1751 | struct sn9c102_device* cam; | |
1752 | int err = 0; | |
1753 | ||
1754 | /* | |
3770be34 LR |
1755 | A read_trylock() in open() is the only safe way to prevent race |
1756 | conditions with disconnect(), one close() and multiple (not | |
1757 | necessarily simultaneous) attempts to open(). For example, it | |
1758 | prevents from waiting for a second access, while the device | |
1759 | structure is being deallocated, after a possible disconnect() and | |
1760 | during a following close() holding the write lock: given that, after | |
1761 | this deallocation, no access will be possible anymore, using the | |
1762 | non-trylock version would have let open() gain the access to the | |
1763 | device structure improperly. | |
1764 | For this reason the lock must also not be per-device. | |
1da177e4 | 1765 | */ |
3770be34 | 1766 | if (!down_read_trylock(&sn9c102_dev_lock)) |
1da177e4 LT |
1767 | return -ERESTARTSYS; |
1768 | ||
c170ecf4 | 1769 | cam = video_drvdata(filp); |
1da177e4 | 1770 | |
3770be34 LR |
1771 | if (wait_for_completion_interruptible(&cam->probe)) { |
1772 | up_read(&sn9c102_dev_lock); | |
1773 | return -ERESTARTSYS; | |
1774 | } | |
1775 | ||
1776 | kref_get(&cam->kref); | |
1777 | ||
1778 | /* | |
1779 | Make sure to isolate all the simultaneous opens. | |
1780 | */ | |
1781 | if (mutex_lock_interruptible(&cam->open_mutex)) { | |
1782 | kref_put(&cam->kref, sn9c102_release_resources); | |
1783 | up_read(&sn9c102_dev_lock); | |
1da177e4 LT |
1784 | return -ERESTARTSYS; |
1785 | } | |
1786 | ||
3770be34 LR |
1787 | if (cam->state & DEV_DISCONNECTED) { |
1788 | DBG(1, "Device not present"); | |
1789 | err = -ENODEV; | |
1790 | goto out; | |
1791 | } | |
1792 | ||
1da177e4 | 1793 | if (cam->users) { |
3770be34 | 1794 | DBG(2, "Device /dev/video%d is already in use", |
c6330fb8 | 1795 | cam->v4ldev->num); |
f327ebbd | 1796 | DBG(3, "Simultaneous opens are not supported"); |
3770be34 LR |
1797 | /* |
1798 | open() must follow the open flags and should block | |
1799 | eventually while the device is in use. | |
1800 | */ | |
1da177e4 LT |
1801 | if ((filp->f_flags & O_NONBLOCK) || |
1802 | (filp->f_flags & O_NDELAY)) { | |
1803 | err = -EWOULDBLOCK; | |
1804 | goto out; | |
1805 | } | |
3770be34 LR |
1806 | DBG(2, "A blocking open() has been requested. Wait for the " |
1807 | "device to be released..."); | |
1808 | up_read(&sn9c102_dev_lock); | |
1809 | /* | |
1810 | We will not release the "open_mutex" lock, so that only one | |
1811 | process can be in the wait queue below. This way the process | |
1812 | will be sleeping while holding the lock, without loosing its | |
1813 | priority after any wake_up(). | |
1814 | */ | |
1815 | err = wait_event_interruptible_exclusive(cam->wait_open, | |
1816 | (cam->state & DEV_DISCONNECTED) | |
d56410e0 | 1817 | || !cam->users); |
3770be34 LR |
1818 | down_read(&sn9c102_dev_lock); |
1819 | if (err) | |
1820 | goto out; | |
1da177e4 | 1821 | if (cam->state & DEV_DISCONNECTED) { |
3770be34 LR |
1822 | err = -ENODEV; |
1823 | goto out; | |
1da177e4 | 1824 | } |
1da177e4 LT |
1825 | } |
1826 | ||
1da177e4 LT |
1827 | if (cam->state & DEV_MISCONFIGURED) { |
1828 | err = sn9c102_init(cam); | |
1829 | if (err) { | |
1830 | DBG(1, "Initialization failed again. " | |
a966f3e7 | 1831 | "I will retry on next open()."); |
1da177e4 LT |
1832 | goto out; |
1833 | } | |
1834 | cam->state &= ~DEV_MISCONFIGURED; | |
1835 | } | |
1836 | ||
1837 | if ((err = sn9c102_start_transfer(cam))) | |
1838 | goto out; | |
1839 | ||
1840 | filp->private_data = cam; | |
1841 | cam->users++; | |
1842 | cam->io = IO_NONE; | |
1843 | cam->stream = STREAM_OFF; | |
1844 | cam->nbuffers = 0; | |
1845 | cam->frame_count = 0; | |
1846 | sn9c102_empty_framequeues(cam); | |
1847 | ||
c6330fb8 | 1848 | DBG(3, "Video device /dev/video%d is open", cam->v4ldev->num); |
1da177e4 LT |
1849 | |
1850 | out: | |
3770be34 LR |
1851 | mutex_unlock(&cam->open_mutex); |
1852 | if (err) | |
1853 | kref_put(&cam->kref, sn9c102_release_resources); | |
1854 | ||
1855 | up_read(&sn9c102_dev_lock); | |
1da177e4 LT |
1856 | return err; |
1857 | } | |
1858 | ||
1859 | ||
bec43661 | 1860 | static int sn9c102_release(struct file *filp) |
1da177e4 | 1861 | { |
3770be34 | 1862 | struct sn9c102_device* cam; |
1da177e4 | 1863 | |
3770be34 | 1864 | down_write(&sn9c102_dev_lock); |
1da177e4 | 1865 | |
c170ecf4 | 1866 | cam = video_drvdata(filp); |
1da177e4 | 1867 | |
3770be34 | 1868 | sn9c102_stop_transfer(cam); |
1da177e4 | 1869 | sn9c102_release_buffers(cam); |
1da177e4 | 1870 | cam->users--; |
3770be34 | 1871 | wake_up_interruptible_nr(&cam->wait_open, 1); |
1da177e4 | 1872 | |
c6330fb8 | 1873 | DBG(3, "Video device /dev/video%d closed", cam->v4ldev->num); |
1da177e4 | 1874 | |
3770be34 LR |
1875 | kref_put(&cam->kref, sn9c102_release_resources); |
1876 | ||
1877 | up_write(&sn9c102_dev_lock); | |
1da177e4 LT |
1878 | |
1879 | return 0; | |
1880 | } | |
1881 | ||
1882 | ||
1883 | static ssize_t | |
1884 | sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos) | |
1885 | { | |
c170ecf4 | 1886 | struct sn9c102_device *cam = video_drvdata(filp); |
1da177e4 LT |
1887 | struct sn9c102_frame_t* f, * i; |
1888 | unsigned long lock_flags; | |
2ffab02f | 1889 | long timeout; |
1da177e4 LT |
1890 | int err = 0; |
1891 | ||
4186ecf8 | 1892 | if (mutex_lock_interruptible(&cam->fileop_mutex)) |
1da177e4 LT |
1893 | return -ERESTARTSYS; |
1894 | ||
1895 | if (cam->state & DEV_DISCONNECTED) { | |
a966f3e7 | 1896 | DBG(1, "Device not present"); |
4186ecf8 | 1897 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
1898 | return -ENODEV; |
1899 | } | |
1900 | ||
1901 | if (cam->state & DEV_MISCONFIGURED) { | |
a966f3e7 LR |
1902 | DBG(1, "The camera is misconfigured. Close and open it " |
1903 | "again."); | |
4186ecf8 | 1904 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
1905 | return -EIO; |
1906 | } | |
1907 | ||
1908 | if (cam->io == IO_MMAP) { | |
1909 | DBG(3, "Close and open the device again to choose " | |
a966f3e7 | 1910 | "the read method"); |
4186ecf8 | 1911 | mutex_unlock(&cam->fileop_mutex); |
f423b9a8 | 1912 | return -EBUSY; |
1da177e4 LT |
1913 | } |
1914 | ||
1915 | if (cam->io == IO_NONE) { | |
1916 | if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) { | |
a966f3e7 | 1917 | DBG(1, "read() failed, not enough memory"); |
4186ecf8 | 1918 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
1919 | return -ENOMEM; |
1920 | } | |
1921 | cam->io = IO_READ; | |
1922 | cam->stream = STREAM_ON; | |
a966f3e7 LR |
1923 | } |
1924 | ||
1925 | if (list_empty(&cam->inqueue)) { | |
1926 | if (!list_empty(&cam->outqueue)) | |
1927 | sn9c102_empty_framequeues(cam); | |
1da177e4 LT |
1928 | sn9c102_queue_unusedframes(cam); |
1929 | } | |
1930 | ||
1931 | if (!count) { | |
4186ecf8 | 1932 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
1933 | return 0; |
1934 | } | |
1935 | ||
1936 | if (list_empty(&cam->outqueue)) { | |
1937 | if (filp->f_flags & O_NONBLOCK) { | |
4186ecf8 | 1938 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
1939 | return -EAGAIN; |
1940 | } | |
f327ebbd LR |
1941 | if (!cam->module_param.frame_timeout) { |
1942 | err = wait_event_interruptible | |
1943 | ( cam->wait_frame, | |
1944 | (!list_empty(&cam->outqueue)) || | |
1945 | (cam->state & DEV_DISCONNECTED) || | |
1946 | (cam->state & DEV_MISCONFIGURED) ); | |
1947 | if (err) { | |
1948 | mutex_unlock(&cam->fileop_mutex); | |
1949 | return err; | |
1950 | } | |
1951 | } else { | |
f423b9a8 LR |
1952 | timeout = wait_event_interruptible_timeout |
1953 | ( cam->wait_frame, | |
1954 | (!list_empty(&cam->outqueue)) || | |
1955 | (cam->state & DEV_DISCONNECTED) || | |
1956 | (cam->state & DEV_MISCONFIGURED), | |
1957 | cam->module_param.frame_timeout * | |
1958 | 1000 * msecs_to_jiffies(1) ); | |
1959 | if (timeout < 0) { | |
1960 | mutex_unlock(&cam->fileop_mutex); | |
1961 | return timeout; | |
f327ebbd LR |
1962 | } else if (timeout == 0 && |
1963 | !(cam->state & DEV_DISCONNECTED)) { | |
1964 | DBG(1, "Video frame timeout elapsed"); | |
1965 | mutex_unlock(&cam->fileop_mutex); | |
1966 | return -EIO; | |
1967 | } | |
1da177e4 LT |
1968 | } |
1969 | if (cam->state & DEV_DISCONNECTED) { | |
4186ecf8 | 1970 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
1971 | return -ENODEV; |
1972 | } | |
f327ebbd | 1973 | if (cam->state & DEV_MISCONFIGURED) { |
4186ecf8 | 1974 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
1975 | return -EIO; |
1976 | } | |
1977 | } | |
1978 | ||
1979 | f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame); | |
1980 | ||
a966f3e7 LR |
1981 | if (count > f->buf.bytesused) |
1982 | count = f->buf.bytesused; | |
1983 | ||
1984 | if (copy_to_user(buf, f->bufmem, count)) { | |
1985 | err = -EFAULT; | |
1986 | goto exit; | |
1987 | } | |
1988 | *f_pos += count; | |
1989 | ||
1990 | exit: | |
1da177e4 LT |
1991 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
1992 | list_for_each_entry(i, &cam->outqueue, frame) | |
1993 | i->state = F_UNUSED; | |
1994 | INIT_LIST_HEAD(&cam->outqueue); | |
1995 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
1996 | ||
1997 | sn9c102_queue_unusedframes(cam); | |
1998 | ||
a966f3e7 LR |
1999 | PDBGG("Frame #%lu, bytes read: %zu", |
2000 | (unsigned long)f->buf.index, count); | |
1da177e4 | 2001 | |
4186ecf8 | 2002 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2003 | |
2004 | return count; | |
2005 | } | |
2006 | ||
2007 | ||
2008 | static unsigned int sn9c102_poll(struct file *filp, poll_table *wait) | |
2009 | { | |
c170ecf4 | 2010 | struct sn9c102_device *cam = video_drvdata(filp); |
a966f3e7 LR |
2011 | struct sn9c102_frame_t* f; |
2012 | unsigned long lock_flags; | |
1da177e4 LT |
2013 | unsigned int mask = 0; |
2014 | ||
4186ecf8 | 2015 | if (mutex_lock_interruptible(&cam->fileop_mutex)) |
1da177e4 LT |
2016 | return POLLERR; |
2017 | ||
2018 | if (cam->state & DEV_DISCONNECTED) { | |
a966f3e7 | 2019 | DBG(1, "Device not present"); |
1da177e4 LT |
2020 | goto error; |
2021 | } | |
2022 | ||
2023 | if (cam->state & DEV_MISCONFIGURED) { | |
a966f3e7 LR |
2024 | DBG(1, "The camera is misconfigured. Close and open it " |
2025 | "again."); | |
1da177e4 LT |
2026 | goto error; |
2027 | } | |
2028 | ||
2029 | if (cam->io == IO_NONE) { | |
2030 | if (!sn9c102_request_buffers(cam, cam->nreadbuffers, | |
d56410e0 | 2031 | IO_READ)) { |
a966f3e7 | 2032 | DBG(1, "poll() failed, not enough memory"); |
1da177e4 LT |
2033 | goto error; |
2034 | } | |
2035 | cam->io = IO_READ; | |
2036 | cam->stream = STREAM_ON; | |
2037 | } | |
2038 | ||
a966f3e7 LR |
2039 | if (cam->io == IO_READ) { |
2040 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | |
2041 | list_for_each_entry(f, &cam->outqueue, frame) | |
2042 | f->state = F_UNUSED; | |
2043 | INIT_LIST_HEAD(&cam->outqueue); | |
2044 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
1da177e4 | 2045 | sn9c102_queue_unusedframes(cam); |
a966f3e7 | 2046 | } |
1da177e4 LT |
2047 | |
2048 | poll_wait(filp, &cam->wait_frame, wait); | |
2049 | ||
2050 | if (!list_empty(&cam->outqueue)) | |
2051 | mask |= POLLIN | POLLRDNORM; | |
2052 | ||
4186ecf8 | 2053 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2054 | |
2055 | return mask; | |
2056 | ||
2057 | error: | |
4186ecf8 | 2058 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2059 | return POLLERR; |
2060 | } | |
2061 | ||
2062 | ||
2063 | static void sn9c102_vm_open(struct vm_area_struct* vma) | |
2064 | { | |
2065 | struct sn9c102_frame_t* f = vma->vm_private_data; | |
2066 | f->vma_use_count++; | |
2067 | } | |
2068 | ||
2069 | ||
2070 | static void sn9c102_vm_close(struct vm_area_struct* vma) | |
2071 | { | |
2072 | /* NOTE: buffers are not freed here */ | |
2073 | struct sn9c102_frame_t* f = vma->vm_private_data; | |
2074 | f->vma_use_count--; | |
2075 | } | |
2076 | ||
2077 | ||
2078 | static struct vm_operations_struct sn9c102_vm_ops = { | |
2079 | .open = sn9c102_vm_open, | |
2080 | .close = sn9c102_vm_close, | |
2081 | }; | |
2082 | ||
2083 | ||
2084 | static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma) | |
2085 | { | |
c170ecf4 | 2086 | struct sn9c102_device *cam = video_drvdata(filp); |
1da177e4 | 2087 | unsigned long size = vma->vm_end - vma->vm_start, |
d56410e0 | 2088 | start = vma->vm_start; |
cd6fcc55 | 2089 | void *pos; |
1da177e4 LT |
2090 | u32 i; |
2091 | ||
4186ecf8 | 2092 | if (mutex_lock_interruptible(&cam->fileop_mutex)) |
1da177e4 LT |
2093 | return -ERESTARTSYS; |
2094 | ||
2095 | if (cam->state & DEV_DISCONNECTED) { | |
a966f3e7 | 2096 | DBG(1, "Device not present"); |
4186ecf8 | 2097 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2098 | return -ENODEV; |
2099 | } | |
2100 | ||
2101 | if (cam->state & DEV_MISCONFIGURED) { | |
a966f3e7 LR |
2102 | DBG(1, "The camera is misconfigured. Close and open it " |
2103 | "again."); | |
4186ecf8 | 2104 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2105 | return -EIO; |
2106 | } | |
2107 | ||
f423b9a8 LR |
2108 | if (!(vma->vm_flags & (VM_WRITE | VM_READ))) { |
2109 | mutex_unlock(&cam->fileop_mutex); | |
2110 | return -EACCES; | |
2111 | } | |
2112 | ||
2113 | if (cam->io != IO_MMAP || | |
1da177e4 | 2114 | size != PAGE_ALIGN(cam->frame[0].buf.length)) { |
4186ecf8 | 2115 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2116 | return -EINVAL; |
2117 | } | |
2118 | ||
2119 | for (i = 0; i < cam->nbuffers; i++) { | |
2120 | if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff) | |
2121 | break; | |
2122 | } | |
2123 | if (i == cam->nbuffers) { | |
4186ecf8 | 2124 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2125 | return -EINVAL; |
2126 | } | |
2127 | ||
1da177e4 | 2128 | vma->vm_flags |= VM_IO; |
cd6fcc55 | 2129 | vma->vm_flags |= VM_RESERVED; |
1da177e4 | 2130 | |
cd6fcc55 | 2131 | pos = cam->frame[i].bufmem; |
1da177e4 | 2132 | while (size > 0) { /* size is page-aligned */ |
cd6fcc55 | 2133 | if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { |
4186ecf8 | 2134 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2135 | return -EAGAIN; |
2136 | } | |
2137 | start += PAGE_SIZE; | |
2138 | pos += PAGE_SIZE; | |
2139 | size -= PAGE_SIZE; | |
2140 | } | |
2141 | ||
2142 | vma->vm_ops = &sn9c102_vm_ops; | |
2143 | vma->vm_private_data = &cam->frame[i]; | |
1da177e4 LT |
2144 | sn9c102_vm_open(vma); |
2145 | ||
4186ecf8 | 2146 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
2147 | |
2148 | return 0; | |
2149 | } | |
2150 | ||
a966f3e7 | 2151 | /*****************************************************************************/ |
1da177e4 | 2152 | |
a966f3e7 LR |
2153 | static int |
2154 | sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg) | |
1da177e4 | 2155 | { |
a966f3e7 LR |
2156 | struct v4l2_capability cap = { |
2157 | .driver = "sn9c102", | |
2158 | .version = SN9C102_MODULE_VERSION_CODE, | |
2159 | .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | | |
d56410e0 | 2160 | V4L2_CAP_STREAMING, |
a966f3e7 LR |
2161 | }; |
2162 | ||
2163 | strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card)); | |
2164 | if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0) | |
af128a10 | 2165 | strlcpy(cap.bus_info, dev_name(&cam->usbdev->dev), |
d56410e0 | 2166 | sizeof(cap.bus_info)); |
a966f3e7 LR |
2167 | |
2168 | if (copy_to_user(arg, &cap, sizeof(cap))) | |
2169 | return -EFAULT; | |
1da177e4 | 2170 | |
a966f3e7 LR |
2171 | return 0; |
2172 | } | |
1da177e4 | 2173 | |
1da177e4 | 2174 | |
a966f3e7 LR |
2175 | static int |
2176 | sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg) | |
2177 | { | |
2178 | struct v4l2_input i; | |
1da177e4 | 2179 | |
a966f3e7 LR |
2180 | if (copy_from_user(&i, arg, sizeof(i))) |
2181 | return -EFAULT; | |
1da177e4 | 2182 | |
a966f3e7 LR |
2183 | if (i.index) |
2184 | return -EINVAL; | |
1da177e4 | 2185 | |
a966f3e7 | 2186 | memset(&i, 0, sizeof(i)); |
cd6fcc55 | 2187 | strcpy(i.name, "Camera"); |
2ffab02f | 2188 | i.type = V4L2_INPUT_TYPE_CAMERA; |
1da177e4 | 2189 | |
a966f3e7 LR |
2190 | if (copy_to_user(arg, &i, sizeof(i))) |
2191 | return -EFAULT; | |
1da177e4 | 2192 | |
a966f3e7 LR |
2193 | return 0; |
2194 | } | |
1da177e4 | 2195 | |
1da177e4 | 2196 | |
a966f3e7 | 2197 | static int |
2ffab02f LR |
2198 | sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg) |
2199 | { | |
2200 | int index = 0; | |
2201 | ||
2202 | if (copy_to_user(arg, &index, sizeof(index))) | |
2203 | return -EFAULT; | |
2204 | ||
2205 | return 0; | |
2206 | } | |
2207 | ||
2208 | ||
2209 | static int | |
2210 | sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg) | |
a966f3e7 LR |
2211 | { |
2212 | int index; | |
1da177e4 | 2213 | |
a966f3e7 LR |
2214 | if (copy_from_user(&index, arg, sizeof(index))) |
2215 | return -EFAULT; | |
1da177e4 | 2216 | |
a966f3e7 | 2217 | if (index != 0) |
1da177e4 | 2218 | return -EINVAL; |
1da177e4 | 2219 | |
a966f3e7 LR |
2220 | return 0; |
2221 | } | |
1da177e4 | 2222 | |
1da177e4 | 2223 | |
a966f3e7 LR |
2224 | static int |
2225 | sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg) | |
2226 | { | |
2ffab02f | 2227 | struct sn9c102_sensor* s = &cam->sensor; |
a966f3e7 LR |
2228 | struct v4l2_queryctrl qc; |
2229 | u8 i; | |
1da177e4 | 2230 | |
a966f3e7 LR |
2231 | if (copy_from_user(&qc, arg, sizeof(qc))) |
2232 | return -EFAULT; | |
1da177e4 | 2233 | |
a966f3e7 LR |
2234 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
2235 | if (qc.id && qc.id == s->qctrl[i].id) { | |
2236 | memcpy(&qc, &(s->qctrl[i]), sizeof(qc)); | |
2237 | if (copy_to_user(arg, &qc, sizeof(qc))) | |
2238 | return -EFAULT; | |
2239 | return 0; | |
2240 | } | |
1da177e4 | 2241 | |
a966f3e7 LR |
2242 | return -EINVAL; |
2243 | } | |
1da177e4 | 2244 | |
1da177e4 | 2245 | |
a966f3e7 LR |
2246 | static int |
2247 | sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg) | |
2248 | { | |
2ffab02f | 2249 | struct sn9c102_sensor* s = &cam->sensor; |
a966f3e7 LR |
2250 | struct v4l2_control ctrl; |
2251 | int err = 0; | |
2252 | u8 i; | |
1da177e4 | 2253 | |
a966f3e7 LR |
2254 | if (!s->get_ctrl && !s->set_ctrl) |
2255 | return -EINVAL; | |
1da177e4 | 2256 | |
a966f3e7 LR |
2257 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
2258 | return -EFAULT; | |
2259 | ||
2260 | if (!s->get_ctrl) { | |
52950ed4 | 2261 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
a966f3e7 LR |
2262 | if (ctrl.id && ctrl.id == s->qctrl[i].id) { |
2263 | ctrl.value = s->_qctrl[i].default_value; | |
2264 | goto exit; | |
1da177e4 | 2265 | } |
a966f3e7 LR |
2266 | return -EINVAL; |
2267 | } else | |
2268 | err = s->get_ctrl(cam, &ctrl); | |
1da177e4 | 2269 | |
a966f3e7 LR |
2270 | exit: |
2271 | if (copy_to_user(arg, &ctrl, sizeof(ctrl))) | |
2272 | return -EFAULT; | |
1da177e4 | 2273 | |
f327ebbd LR |
2274 | PDBGG("VIDIOC_G_CTRL: id %lu, value %lu", |
2275 | (unsigned long)ctrl.id, (unsigned long)ctrl.value); | |
2276 | ||
a966f3e7 LR |
2277 | return err; |
2278 | } | |
1da177e4 | 2279 | |
1da177e4 | 2280 | |
a966f3e7 LR |
2281 | static int |
2282 | sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg) | |
2283 | { | |
2ffab02f | 2284 | struct sn9c102_sensor* s = &cam->sensor; |
a966f3e7 LR |
2285 | struct v4l2_control ctrl; |
2286 | u8 i; | |
2287 | int err = 0; | |
1da177e4 | 2288 | |
a966f3e7 LR |
2289 | if (!s->set_ctrl) |
2290 | return -EINVAL; | |
1da177e4 | 2291 | |
a966f3e7 LR |
2292 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
2293 | return -EFAULT; | |
1da177e4 | 2294 | |
a966f3e7 LR |
2295 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
2296 | if (ctrl.id == s->qctrl[i].id) { | |
2ffab02f LR |
2297 | if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED) |
2298 | return -EINVAL; | |
a966f3e7 LR |
2299 | if (ctrl.value < s->qctrl[i].minimum || |
2300 | ctrl.value > s->qctrl[i].maximum) | |
2301 | return -ERANGE; | |
2302 | ctrl.value -= ctrl.value % s->qctrl[i].step; | |
2303 | break; | |
2304 | } | |
1da177e4 | 2305 | |
a966f3e7 LR |
2306 | if ((err = s->set_ctrl(cam, &ctrl))) |
2307 | return err; | |
1da177e4 | 2308 | |
a966f3e7 | 2309 | s->_qctrl[i].default_value = ctrl.value; |
1da177e4 | 2310 | |
a966f3e7 LR |
2311 | PDBGG("VIDIOC_S_CTRL: id %lu, value %lu", |
2312 | (unsigned long)ctrl.id, (unsigned long)ctrl.value); | |
1da177e4 | 2313 | |
a966f3e7 LR |
2314 | return 0; |
2315 | } | |
1da177e4 | 2316 | |
1da177e4 | 2317 | |
a966f3e7 LR |
2318 | static int |
2319 | sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg) | |
2320 | { | |
2ffab02f | 2321 | struct v4l2_cropcap* cc = &(cam->sensor.cropcap); |
1da177e4 | 2322 | |
a966f3e7 LR |
2323 | cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
2324 | cc->pixelaspect.numerator = 1; | |
2325 | cc->pixelaspect.denominator = 1; | |
1da177e4 | 2326 | |
a966f3e7 LR |
2327 | if (copy_to_user(arg, cc, sizeof(*cc))) |
2328 | return -EFAULT; | |
1da177e4 | 2329 | |
a966f3e7 LR |
2330 | return 0; |
2331 | } | |
1da177e4 | 2332 | |
1da177e4 | 2333 | |
a966f3e7 LR |
2334 | static int |
2335 | sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg) | |
2336 | { | |
2ffab02f | 2337 | struct sn9c102_sensor* s = &cam->sensor; |
a966f3e7 LR |
2338 | struct v4l2_crop crop = { |
2339 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
2340 | }; | |
1da177e4 | 2341 | |
a966f3e7 | 2342 | memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect)); |
1da177e4 | 2343 | |
a966f3e7 LR |
2344 | if (copy_to_user(arg, &crop, sizeof(crop))) |
2345 | return -EFAULT; | |
1da177e4 | 2346 | |
a966f3e7 LR |
2347 | return 0; |
2348 | } | |
1da177e4 | 2349 | |
1da177e4 | 2350 | |
a966f3e7 LR |
2351 | static int |
2352 | sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg) | |
2353 | { | |
2ffab02f | 2354 | struct sn9c102_sensor* s = &cam->sensor; |
a966f3e7 LR |
2355 | struct v4l2_crop crop; |
2356 | struct v4l2_rect* rect; | |
2357 | struct v4l2_rect* bounds = &(s->cropcap.bounds); | |
2358 | struct v4l2_pix_format* pix_format = &(s->pix_format); | |
2359 | u8 scale; | |
2360 | const enum sn9c102_stream_state stream = cam->stream; | |
2361 | const u32 nbuffers = cam->nbuffers; | |
2362 | u32 i; | |
2363 | int err = 0; | |
1da177e4 | 2364 | |
a966f3e7 LR |
2365 | if (copy_from_user(&crop, arg, sizeof(crop))) |
2366 | return -EFAULT; | |
1da177e4 | 2367 | |
a966f3e7 | 2368 | rect = &(crop.c); |
1da177e4 | 2369 | |
a966f3e7 LR |
2370 | if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
2371 | return -EINVAL; | |
1da177e4 | 2372 | |
a966f3e7 LR |
2373 | if (cam->module_param.force_munmap) |
2374 | for (i = 0; i < cam->nbuffers; i++) | |
2375 | if (cam->frame[i].vma_use_count) { | |
2376 | DBG(3, "VIDIOC_S_CROP failed. " | |
2377 | "Unmap the buffers first."); | |
f423b9a8 | 2378 | return -EBUSY; |
a966f3e7 | 2379 | } |
1da177e4 | 2380 | |
a966f3e7 LR |
2381 | /* Preserve R,G or B origin */ |
2382 | rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L; | |
2383 | rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L; | |
2384 | ||
2385 | if (rect->width < 16) | |
2386 | rect->width = 16; | |
2387 | if (rect->height < 16) | |
2388 | rect->height = 16; | |
2389 | if (rect->width > bounds->width) | |
2390 | rect->width = bounds->width; | |
2391 | if (rect->height > bounds->height) | |
2392 | rect->height = bounds->height; | |
2393 | if (rect->left < bounds->left) | |
2394 | rect->left = bounds->left; | |
2395 | if (rect->top < bounds->top) | |
2396 | rect->top = bounds->top; | |
2397 | if (rect->left + rect->width > bounds->left + bounds->width) | |
2398 | rect->left = bounds->left+bounds->width - rect->width; | |
2399 | if (rect->top + rect->height > bounds->top + bounds->height) | |
2400 | rect->top = bounds->top+bounds->height - rect->height; | |
2401 | ||
2402 | rect->width &= ~15L; | |
2403 | rect->height &= ~15L; | |
2404 | ||
2405 | if (SN9C102_PRESERVE_IMGSCALE) { | |
2406 | /* Calculate the actual scaling factor */ | |
2407 | u32 a, b; | |
2408 | a = rect->width * rect->height; | |
2409 | b = pix_format->width * pix_format->height; | |
2410 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | |
2411 | } else | |
2412 | scale = 1; | |
2413 | ||
2414 | if (cam->stream == STREAM_ON) | |
2415 | if ((err = sn9c102_stream_interrupt(cam))) | |
2416 | return err; | |
1da177e4 | 2417 | |
a966f3e7 LR |
2418 | if (copy_to_user(arg, &crop, sizeof(crop))) { |
2419 | cam->stream = stream; | |
2420 | return -EFAULT; | |
1da177e4 LT |
2421 | } |
2422 | ||
a966f3e7 LR |
2423 | if (cam->module_param.force_munmap || cam->io == IO_READ) |
2424 | sn9c102_release_buffers(cam); | |
1da177e4 | 2425 | |
a966f3e7 LR |
2426 | err = sn9c102_set_crop(cam, rect); |
2427 | if (s->set_crop) | |
2428 | err += s->set_crop(cam, rect); | |
2429 | err += sn9c102_set_scale(cam, scale); | |
1da177e4 | 2430 | |
a966f3e7 LR |
2431 | if (err) { /* atomic, no rollback in ioctl() */ |
2432 | cam->state |= DEV_MISCONFIGURED; | |
2433 | DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To " | |
2434 | "use the camera, close and open /dev/video%d again.", | |
c6330fb8 | 2435 | cam->v4ldev->num); |
a966f3e7 LR |
2436 | return -EIO; |
2437 | } | |
1da177e4 | 2438 | |
a966f3e7 LR |
2439 | s->pix_format.width = rect->width/scale; |
2440 | s->pix_format.height = rect->height/scale; | |
2441 | memcpy(&(s->_rect), rect, sizeof(*rect)); | |
1da177e4 | 2442 | |
a966f3e7 LR |
2443 | if ((cam->module_param.force_munmap || cam->io == IO_READ) && |
2444 | nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) { | |
2445 | cam->state |= DEV_MISCONFIGURED; | |
2446 | DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To " | |
2447 | "use the camera, close and open /dev/video%d again.", | |
c6330fb8 | 2448 | cam->v4ldev->num); |
a966f3e7 | 2449 | return -ENOMEM; |
1da177e4 LT |
2450 | } |
2451 | ||
a966f3e7 LR |
2452 | if (cam->io == IO_READ) |
2453 | sn9c102_empty_framequeues(cam); | |
2454 | else if (cam->module_param.force_munmap) | |
2455 | sn9c102_requeue_outqueue(cam); | |
1da177e4 | 2456 | |
a966f3e7 | 2457 | cam->stream = stream; |
1da177e4 | 2458 | |
a966f3e7 LR |
2459 | return 0; |
2460 | } | |
1da177e4 | 2461 | |
1da177e4 | 2462 | |
f327ebbd LR |
2463 | static int |
2464 | sn9c102_vidioc_enum_framesizes(struct sn9c102_device* cam, void __user * arg) | |
2465 | { | |
2466 | struct v4l2_frmsizeenum frmsize; | |
2467 | ||
2468 | if (copy_from_user(&frmsize, arg, sizeof(frmsize))) | |
2469 | return -EFAULT; | |
2470 | ||
2471 | if (frmsize.index != 0) | |
2472 | return -EINVAL; | |
2473 | ||
2474 | switch (cam->bridge) { | |
2475 | case BRIDGE_SN9C101: | |
2476 | case BRIDGE_SN9C102: | |
2477 | case BRIDGE_SN9C103: | |
2478 | if (frmsize.pixel_format != V4L2_PIX_FMT_SN9C10X && | |
2479 | frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8) | |
2480 | return -EINVAL; | |
2481 | case BRIDGE_SN9C105: | |
2482 | case BRIDGE_SN9C120: | |
2483 | if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG && | |
2484 | frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8) | |
2485 | return -EINVAL; | |
2486 | } | |
2487 | ||
2488 | frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE; | |
2489 | frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16; | |
2490 | frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16; | |
2491 | frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width; | |
2492 | frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height; | |
2493 | memset(&frmsize.reserved, 0, sizeof(frmsize.reserved)); | |
2494 | ||
2495 | if (copy_to_user(arg, &frmsize, sizeof(frmsize))) | |
2496 | return -EFAULT; | |
2497 | ||
2498 | return 0; | |
2499 | } | |
2500 | ||
2501 | ||
a966f3e7 LR |
2502 | static int |
2503 | sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg) | |
2504 | { | |
2505 | struct v4l2_fmtdesc fmtd; | |
1da177e4 | 2506 | |
a966f3e7 LR |
2507 | if (copy_from_user(&fmtd, arg, sizeof(fmtd))) |
2508 | return -EFAULT; | |
1da177e4 | 2509 | |
f327ebbd LR |
2510 | if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
2511 | return -EINVAL; | |
2512 | ||
a966f3e7 LR |
2513 | if (fmtd.index == 0) { |
2514 | strcpy(fmtd.description, "bayer rgb"); | |
2515 | fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8; | |
2516 | } else if (fmtd.index == 1) { | |
f327ebbd LR |
2517 | switch (cam->bridge) { |
2518 | case BRIDGE_SN9C101: | |
2519 | case BRIDGE_SN9C102: | |
2520 | case BRIDGE_SN9C103: | |
f423b9a8 LR |
2521 | strcpy(fmtd.description, "compressed"); |
2522 | fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X; | |
f327ebbd LR |
2523 | break; |
2524 | case BRIDGE_SN9C105: | |
2525 | case BRIDGE_SN9C120: | |
2526 | strcpy(fmtd.description, "JPEG"); | |
2527 | fmtd.pixelformat = V4L2_PIX_FMT_JPEG; | |
2528 | break; | |
2529 | } | |
a966f3e7 LR |
2530 | fmtd.flags = V4L2_FMT_FLAG_COMPRESSED; |
2531 | } else | |
2532 | return -EINVAL; | |
1da177e4 | 2533 | |
a966f3e7 LR |
2534 | fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
2535 | memset(&fmtd.reserved, 0, sizeof(fmtd.reserved)); | |
1da177e4 | 2536 | |
a966f3e7 LR |
2537 | if (copy_to_user(arg, &fmtd, sizeof(fmtd))) |
2538 | return -EFAULT; | |
1da177e4 | 2539 | |
a966f3e7 LR |
2540 | return 0; |
2541 | } | |
1da177e4 | 2542 | |
1da177e4 | 2543 | |
a966f3e7 LR |
2544 | static int |
2545 | sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg) | |
2546 | { | |
2547 | struct v4l2_format format; | |
2ffab02f | 2548 | struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format); |
1da177e4 | 2549 | |
a966f3e7 LR |
2550 | if (copy_from_user(&format, arg, sizeof(format))) |
2551 | return -EFAULT; | |
1da177e4 | 2552 | |
a966f3e7 LR |
2553 | if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
2554 | return -EINVAL; | |
1da177e4 | 2555 | |
f423b9a8 LR |
2556 | pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_JPEG) ? |
2557 | V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB; | |
2558 | pfmt->bytesperline = (pfmt->pixelformat == V4L2_PIX_FMT_SN9C10X || | |
2559 | pfmt->pixelformat == V4L2_PIX_FMT_JPEG) | |
d56410e0 | 2560 | ? 0 : (pfmt->width * pfmt->priv) / 8; |
a966f3e7 LR |
2561 | pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8); |
2562 | pfmt->field = V4L2_FIELD_NONE; | |
2563 | memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt)); | |
1da177e4 | 2564 | |
a966f3e7 LR |
2565 | if (copy_to_user(arg, &format, sizeof(format))) |
2566 | return -EFAULT; | |
1da177e4 | 2567 | |
a966f3e7 LR |
2568 | return 0; |
2569 | } | |
1da177e4 | 2570 | |
1da177e4 | 2571 | |
a966f3e7 LR |
2572 | static int |
2573 | sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd, | |
d56410e0 | 2574 | void __user * arg) |
a966f3e7 | 2575 | { |
2ffab02f | 2576 | struct sn9c102_sensor* s = &cam->sensor; |
a966f3e7 LR |
2577 | struct v4l2_format format; |
2578 | struct v4l2_pix_format* pix; | |
2579 | struct v4l2_pix_format* pfmt = &(s->pix_format); | |
2580 | struct v4l2_rect* bounds = &(s->cropcap.bounds); | |
2581 | struct v4l2_rect rect; | |
2582 | u8 scale; | |
2583 | const enum sn9c102_stream_state stream = cam->stream; | |
2584 | const u32 nbuffers = cam->nbuffers; | |
2585 | u32 i; | |
2586 | int err = 0; | |
1da177e4 | 2587 | |
a966f3e7 LR |
2588 | if (copy_from_user(&format, arg, sizeof(format))) |
2589 | return -EFAULT; | |
1da177e4 | 2590 | |
a966f3e7 | 2591 | pix = &(format.fmt.pix); |
1da177e4 | 2592 | |
a966f3e7 LR |
2593 | if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
2594 | return -EINVAL; | |
1da177e4 | 2595 | |
a966f3e7 | 2596 | memcpy(&rect, &(s->_rect), sizeof(rect)); |
1da177e4 | 2597 | |
a966f3e7 LR |
2598 | { /* calculate the actual scaling factor */ |
2599 | u32 a, b; | |
2600 | a = rect.width * rect.height; | |
2601 | b = pix->width * pix->height; | |
2602 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | |
1da177e4 LT |
2603 | } |
2604 | ||
a966f3e7 LR |
2605 | rect.width = scale * pix->width; |
2606 | rect.height = scale * pix->height; | |
1da177e4 | 2607 | |
a966f3e7 LR |
2608 | if (rect.width < 16) |
2609 | rect.width = 16; | |
2610 | if (rect.height < 16) | |
2611 | rect.height = 16; | |
2612 | if (rect.width > bounds->left + bounds->width - rect.left) | |
2613 | rect.width = bounds->left + bounds->width - rect.left; | |
2614 | if (rect.height > bounds->top + bounds->height - rect.top) | |
2615 | rect.height = bounds->top + bounds->height - rect.top; | |
1da177e4 | 2616 | |
a966f3e7 LR |
2617 | rect.width &= ~15L; |
2618 | rect.height &= ~15L; | |
1da177e4 | 2619 | |
a966f3e7 LR |
2620 | { /* adjust the scaling factor */ |
2621 | u32 a, b; | |
2622 | a = rect.width * rect.height; | |
2623 | b = pix->width * pix->height; | |
2624 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | |
2625 | } | |
2626 | ||
2627 | pix->width = rect.width / scale; | |
2628 | pix->height = rect.height / scale; | |
2629 | ||
f327ebbd LR |
2630 | switch (cam->bridge) { |
2631 | case BRIDGE_SN9C101: | |
2632 | case BRIDGE_SN9C102: | |
2633 | case BRIDGE_SN9C103: | |
f423b9a8 LR |
2634 | if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X && |
2635 | pix->pixelformat != V4L2_PIX_FMT_SBGGR8) | |
2636 | pix->pixelformat = pfmt->pixelformat; | |
f327ebbd LR |
2637 | break; |
2638 | case BRIDGE_SN9C105: | |
2639 | case BRIDGE_SN9C120: | |
2640 | if (pix->pixelformat != V4L2_PIX_FMT_JPEG && | |
2641 | pix->pixelformat != V4L2_PIX_FMT_SBGGR8) | |
2642 | pix->pixelformat = pfmt->pixelformat; | |
2643 | break; | |
2644 | } | |
a966f3e7 | 2645 | pix->priv = pfmt->priv; /* bpp */ |
f423b9a8 LR |
2646 | pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_JPEG) ? |
2647 | V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB; | |
f327ebbd LR |
2648 | pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X || |
2649 | pix->pixelformat == V4L2_PIX_FMT_JPEG) | |
d56410e0 | 2650 | ? 0 : (pix->width * pix->priv) / 8; |
a966f3e7 LR |
2651 | pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8); |
2652 | pix->field = V4L2_FIELD_NONE; | |
2653 | ||
2654 | if (cmd == VIDIOC_TRY_FMT) { | |
2655 | if (copy_to_user(arg, &format, sizeof(format))) | |
2656 | return -EFAULT; | |
2657 | return 0; | |
2658 | } | |
1da177e4 | 2659 | |
a966f3e7 | 2660 | if (cam->module_param.force_munmap) |
1da177e4 LT |
2661 | for (i = 0; i < cam->nbuffers; i++) |
2662 | if (cam->frame[i].vma_use_count) { | |
a966f3e7 LR |
2663 | DBG(3, "VIDIOC_S_FMT failed. Unmap the " |
2664 | "buffers first."); | |
f423b9a8 | 2665 | return -EBUSY; |
1da177e4 LT |
2666 | } |
2667 | ||
a966f3e7 LR |
2668 | if (cam->stream == STREAM_ON) |
2669 | if ((err = sn9c102_stream_interrupt(cam))) | |
2670 | return err; | |
1da177e4 | 2671 | |
a966f3e7 LR |
2672 | if (copy_to_user(arg, &format, sizeof(format))) { |
2673 | cam->stream = stream; | |
2674 | return -EFAULT; | |
2675 | } | |
1da177e4 | 2676 | |
a966f3e7 | 2677 | if (cam->module_param.force_munmap || cam->io == IO_READ) |
1da177e4 | 2678 | sn9c102_release_buffers(cam); |
1da177e4 | 2679 | |
a966f3e7 LR |
2680 | err += sn9c102_set_pix_format(cam, pix); |
2681 | err += sn9c102_set_crop(cam, &rect); | |
2682 | if (s->set_pix_format) | |
2683 | err += s->set_pix_format(cam, pix); | |
2684 | if (s->set_crop) | |
2685 | err += s->set_crop(cam, &rect); | |
2686 | err += sn9c102_set_scale(cam, scale); | |
1da177e4 | 2687 | |
a966f3e7 LR |
2688 | if (err) { /* atomic, no rollback in ioctl() */ |
2689 | cam->state |= DEV_MISCONFIGURED; | |
2690 | DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To " | |
2691 | "use the camera, close and open /dev/video%d again.", | |
c6330fb8 | 2692 | cam->v4ldev->num); |
a966f3e7 LR |
2693 | return -EIO; |
2694 | } | |
1da177e4 | 2695 | |
a966f3e7 LR |
2696 | memcpy(pfmt, pix, sizeof(*pix)); |
2697 | memcpy(&(s->_rect), &rect, sizeof(rect)); | |
2698 | ||
2699 | if ((cam->module_param.force_munmap || cam->io == IO_READ) && | |
2700 | nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) { | |
2701 | cam->state |= DEV_MISCONFIGURED; | |
2702 | DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To " | |
2703 | "use the camera, close and open /dev/video%d again.", | |
c6330fb8 | 2704 | cam->v4ldev->num); |
a966f3e7 | 2705 | return -ENOMEM; |
1da177e4 LT |
2706 | } |
2707 | ||
a966f3e7 LR |
2708 | if (cam->io == IO_READ) |
2709 | sn9c102_empty_framequeues(cam); | |
2710 | else if (cam->module_param.force_munmap) | |
2711 | sn9c102_requeue_outqueue(cam); | |
1da177e4 | 2712 | |
a966f3e7 | 2713 | cam->stream = stream; |
1da177e4 | 2714 | |
a966f3e7 LR |
2715 | return 0; |
2716 | } | |
2717 | ||
2718 | ||
2719 | static int | |
2720 | sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg) | |
2721 | { | |
f327ebbd | 2722 | if (copy_to_user(arg, &cam->compression, sizeof(cam->compression))) |
a966f3e7 | 2723 | return -EFAULT; |
1da177e4 | 2724 | |
a966f3e7 LR |
2725 | return 0; |
2726 | } | |
1da177e4 | 2727 | |
1da177e4 | 2728 | |
a966f3e7 LR |
2729 | static int |
2730 | sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg) | |
2731 | { | |
2732 | struct v4l2_jpegcompression jc; | |
2733 | const enum sn9c102_stream_state stream = cam->stream; | |
2734 | int err = 0; | |
1da177e4 | 2735 | |
a966f3e7 LR |
2736 | if (copy_from_user(&jc, arg, sizeof(jc))) |
2737 | return -EFAULT; | |
1da177e4 | 2738 | |
a966f3e7 LR |
2739 | if (jc.quality != 0 && jc.quality != 1) |
2740 | return -EINVAL; | |
2741 | ||
2742 | if (cam->stream == STREAM_ON) | |
2743 | if ((err = sn9c102_stream_interrupt(cam))) | |
2744 | return err; | |
2745 | ||
2746 | err += sn9c102_set_compression(cam, &jc); | |
2747 | if (err) { /* atomic, no rollback in ioctl() */ | |
2748 | cam->state |= DEV_MISCONFIGURED; | |
2749 | DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware " | |
2750 | "problems. To use the camera, close and open " | |
c6330fb8 | 2751 | "/dev/video%d again.", cam->v4ldev->num); |
a966f3e7 | 2752 | return -EIO; |
1da177e4 LT |
2753 | } |
2754 | ||
a966f3e7 | 2755 | cam->compression.quality = jc.quality; |
1da177e4 | 2756 | |
a966f3e7 | 2757 | cam->stream = stream; |
1da177e4 | 2758 | |
a966f3e7 LR |
2759 | return 0; |
2760 | } | |
1da177e4 | 2761 | |
a966f3e7 LR |
2762 | |
2763 | static int | |
2764 | sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg) | |
2765 | { | |
2766 | struct v4l2_requestbuffers rb; | |
2767 | u32 i; | |
2768 | int err; | |
2769 | ||
2770 | if (copy_from_user(&rb, arg, sizeof(rb))) | |
2771 | return -EFAULT; | |
2772 | ||
2773 | if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | |
2774 | rb.memory != V4L2_MEMORY_MMAP) | |
2775 | return -EINVAL; | |
2776 | ||
2777 | if (cam->io == IO_READ) { | |
2778 | DBG(3, "Close and open the device again to choose the mmap " | |
2779 | "I/O method"); | |
f423b9a8 | 2780 | return -EBUSY; |
a966f3e7 LR |
2781 | } |
2782 | ||
2783 | for (i = 0; i < cam->nbuffers; i++) | |
2784 | if (cam->frame[i].vma_use_count) { | |
2785 | DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are " | |
2786 | "still mapped."); | |
f423b9a8 | 2787 | return -EBUSY; |
a966f3e7 | 2788 | } |
1da177e4 | 2789 | |
a966f3e7 LR |
2790 | if (cam->stream == STREAM_ON) |
2791 | if ((err = sn9c102_stream_interrupt(cam))) | |
2792 | return err; | |
1da177e4 | 2793 | |
a966f3e7 | 2794 | sn9c102_empty_framequeues(cam); |
1da177e4 | 2795 | |
a966f3e7 LR |
2796 | sn9c102_release_buffers(cam); |
2797 | if (rb.count) | |
2798 | rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP); | |
1da177e4 | 2799 | |
a966f3e7 LR |
2800 | if (copy_to_user(arg, &rb, sizeof(rb))) { |
2801 | sn9c102_release_buffers(cam); | |
2802 | cam->io = IO_NONE; | |
2803 | return -EFAULT; | |
1da177e4 LT |
2804 | } |
2805 | ||
a966f3e7 | 2806 | cam->io = rb.count ? IO_MMAP : IO_NONE; |
1da177e4 | 2807 | |
a966f3e7 LR |
2808 | return 0; |
2809 | } | |
1da177e4 | 2810 | |
1da177e4 | 2811 | |
a966f3e7 LR |
2812 | static int |
2813 | sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg) | |
2814 | { | |
2815 | struct v4l2_buffer b; | |
1da177e4 | 2816 | |
a966f3e7 LR |
2817 | if (copy_from_user(&b, arg, sizeof(b))) |
2818 | return -EFAULT; | |
1da177e4 | 2819 | |
a966f3e7 LR |
2820 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
2821 | b.index >= cam->nbuffers || cam->io != IO_MMAP) | |
2822 | return -EINVAL; | |
1da177e4 | 2823 | |
a966f3e7 | 2824 | memcpy(&b, &cam->frame[b.index].buf, sizeof(b)); |
1da177e4 | 2825 | |
a966f3e7 LR |
2826 | if (cam->frame[b.index].vma_use_count) |
2827 | b.flags |= V4L2_BUF_FLAG_MAPPED; | |
1da177e4 | 2828 | |
a966f3e7 LR |
2829 | if (cam->frame[b.index].state == F_DONE) |
2830 | b.flags |= V4L2_BUF_FLAG_DONE; | |
2831 | else if (cam->frame[b.index].state != F_UNUSED) | |
2832 | b.flags |= V4L2_BUF_FLAG_QUEUED; | |
1da177e4 | 2833 | |
a966f3e7 LR |
2834 | if (copy_to_user(arg, &b, sizeof(b))) |
2835 | return -EFAULT; | |
1da177e4 | 2836 | |
a966f3e7 LR |
2837 | return 0; |
2838 | } | |
1da177e4 | 2839 | |
1da177e4 | 2840 | |
a966f3e7 LR |
2841 | static int |
2842 | sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg) | |
2843 | { | |
2844 | struct v4l2_buffer b; | |
2845 | unsigned long lock_flags; | |
1da177e4 | 2846 | |
a966f3e7 LR |
2847 | if (copy_from_user(&b, arg, sizeof(b))) |
2848 | return -EFAULT; | |
1da177e4 | 2849 | |
a966f3e7 LR |
2850 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
2851 | b.index >= cam->nbuffers || cam->io != IO_MMAP) | |
2852 | return -EINVAL; | |
1da177e4 | 2853 | |
a966f3e7 LR |
2854 | if (cam->frame[b.index].state != F_UNUSED) |
2855 | return -EINVAL; | |
1da177e4 | 2856 | |
a966f3e7 | 2857 | cam->frame[b.index].state = F_QUEUED; |
1da177e4 | 2858 | |
a966f3e7 LR |
2859 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
2860 | list_add_tail(&cam->frame[b.index].frame, &cam->inqueue); | |
2861 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
1da177e4 | 2862 | |
a966f3e7 | 2863 | PDBGG("Frame #%lu queued", (unsigned long)b.index); |
1da177e4 | 2864 | |
a966f3e7 LR |
2865 | return 0; |
2866 | } | |
1da177e4 | 2867 | |
1da177e4 | 2868 | |
a966f3e7 LR |
2869 | static int |
2870 | sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp, | |
d56410e0 | 2871 | void __user * arg) |
a966f3e7 LR |
2872 | { |
2873 | struct v4l2_buffer b; | |
2874 | struct sn9c102_frame_t *f; | |
2875 | unsigned long lock_flags; | |
2ffab02f | 2876 | long timeout; |
f327ebbd | 2877 | int err = 0; |
1da177e4 | 2878 | |
a966f3e7 LR |
2879 | if (copy_from_user(&b, arg, sizeof(b))) |
2880 | return -EFAULT; | |
1da177e4 | 2881 | |
a966f3e7 LR |
2882 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) |
2883 | return -EINVAL; | |
2884 | ||
2885 | if (list_empty(&cam->outqueue)) { | |
2886 | if (cam->stream == STREAM_OFF) | |
2887 | return -EINVAL; | |
2888 | if (filp->f_flags & O_NONBLOCK) | |
2889 | return -EAGAIN; | |
f327ebbd LR |
2890 | if (!cam->module_param.frame_timeout) { |
2891 | err = wait_event_interruptible | |
2892 | ( cam->wait_frame, | |
2893 | (!list_empty(&cam->outqueue)) || | |
2894 | (cam->state & DEV_DISCONNECTED) || | |
2895 | (cam->state & DEV_MISCONFIGURED) ); | |
2896 | if (err) | |
2897 | return err; | |
2898 | } else { | |
f423b9a8 LR |
2899 | timeout = wait_event_interruptible_timeout |
2900 | ( cam->wait_frame, | |
2901 | (!list_empty(&cam->outqueue)) || | |
2902 | (cam->state & DEV_DISCONNECTED) || | |
2903 | (cam->state & DEV_MISCONFIGURED), | |
2904 | cam->module_param.frame_timeout * | |
2905 | 1000 * msecs_to_jiffies(1) ); | |
2906 | if (timeout < 0) | |
2907 | return timeout; | |
f327ebbd LR |
2908 | else if (timeout == 0 && |
2909 | !(cam->state & DEV_DISCONNECTED)) { | |
2910 | DBG(1, "Video frame timeout elapsed"); | |
2911 | return -EIO; | |
2912 | } | |
2913 | } | |
a966f3e7 LR |
2914 | if (cam->state & DEV_DISCONNECTED) |
2915 | return -ENODEV; | |
f327ebbd | 2916 | if (cam->state & DEV_MISCONFIGURED) |
a966f3e7 | 2917 | return -EIO; |
1da177e4 LT |
2918 | } |
2919 | ||
a966f3e7 LR |
2920 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
2921 | f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame); | |
2922 | list_del(cam->outqueue.next); | |
2923 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | |
1da177e4 | 2924 | |
a966f3e7 | 2925 | f->state = F_UNUSED; |
1da177e4 | 2926 | |
a966f3e7 LR |
2927 | memcpy(&b, &f->buf, sizeof(b)); |
2928 | if (f->vma_use_count) | |
2929 | b.flags |= V4L2_BUF_FLAG_MAPPED; | |
2930 | ||
2931 | if (copy_to_user(arg, &b, sizeof(b))) | |
2932 | return -EFAULT; | |
2933 | ||
2934 | PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index); | |
2935 | ||
2936 | return 0; | |
2937 | } | |
2938 | ||
2939 | ||
2940 | static int | |
2941 | sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg) | |
2942 | { | |
2943 | int type; | |
2944 | ||
2945 | if (copy_from_user(&type, arg, sizeof(type))) | |
2946 | return -EFAULT; | |
2947 | ||
2948 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) | |
2949 | return -EINVAL; | |
2950 | ||
a966f3e7 LR |
2951 | cam->stream = STREAM_ON; |
2952 | ||
2953 | DBG(3, "Stream on"); | |
2954 | ||
2955 | return 0; | |
2956 | } | |
2957 | ||
2958 | ||
2959 | static int | |
2960 | sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg) | |
2961 | { | |
2962 | int type, err; | |
2963 | ||
2964 | if (copy_from_user(&type, arg, sizeof(type))) | |
2965 | return -EFAULT; | |
2966 | ||
2967 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) | |
2968 | return -EINVAL; | |
2969 | ||
2970 | if (cam->stream == STREAM_ON) | |
2971 | if ((err = sn9c102_stream_interrupt(cam))) | |
2972 | return err; | |
2973 | ||
2974 | sn9c102_empty_framequeues(cam); | |
2975 | ||
2976 | DBG(3, "Stream off"); | |
2977 | ||
2978 | return 0; | |
2979 | } | |
2980 | ||
2981 | ||
2982 | static int | |
2983 | sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg) | |
2984 | { | |
2985 | struct v4l2_streamparm sp; | |
2986 | ||
2987 | if (copy_from_user(&sp, arg, sizeof(sp))) | |
2988 | return -EFAULT; | |
2989 | ||
2990 | if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
2991 | return -EINVAL; | |
2992 | ||
2993 | sp.parm.capture.extendedmode = 0; | |
2994 | sp.parm.capture.readbuffers = cam->nreadbuffers; | |
2995 | ||
2996 | if (copy_to_user(arg, &sp, sizeof(sp))) | |
2997 | return -EFAULT; | |
2998 | ||
2999 | return 0; | |
3000 | } | |
3001 | ||
3002 | ||
3003 | static int | |
3004 | sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg) | |
3005 | { | |
3006 | struct v4l2_streamparm sp; | |
3007 | ||
3008 | if (copy_from_user(&sp, arg, sizeof(sp))) | |
3009 | return -EFAULT; | |
3010 | ||
3011 | if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
3012 | return -EINVAL; | |
3013 | ||
3014 | sp.parm.capture.extendedmode = 0; | |
1da177e4 | 3015 | |
a966f3e7 | 3016 | if (sp.parm.capture.readbuffers == 0) |
1da177e4 LT |
3017 | sp.parm.capture.readbuffers = cam->nreadbuffers; |
3018 | ||
a966f3e7 LR |
3019 | if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES) |
3020 | sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES; | |
1da177e4 | 3021 | |
a966f3e7 LR |
3022 | if (copy_to_user(arg, &sp, sizeof(sp))) |
3023 | return -EFAULT; | |
1da177e4 | 3024 | |
a966f3e7 | 3025 | cam->nreadbuffers = sp.parm.capture.readbuffers; |
1da177e4 | 3026 | |
a966f3e7 LR |
3027 | return 0; |
3028 | } | |
1da177e4 | 3029 | |
1da177e4 | 3030 | |
f327ebbd LR |
3031 | static int |
3032 | sn9c102_vidioc_enumaudio(struct sn9c102_device* cam, void __user * arg) | |
3033 | { | |
3034 | struct v4l2_audio audio; | |
3035 | ||
3036 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) | |
3037 | return -EINVAL; | |
3038 | ||
3039 | if (copy_from_user(&audio, arg, sizeof(audio))) | |
3040 | return -EFAULT; | |
3041 | ||
3042 | if (audio.index != 0) | |
3043 | return -EINVAL; | |
3044 | ||
3045 | strcpy(audio.name, "Microphone"); | |
3046 | audio.capability = 0; | |
3047 | audio.mode = 0; | |
3048 | ||
3049 | if (copy_to_user(arg, &audio, sizeof(audio))) | |
3050 | return -EFAULT; | |
3051 | ||
3052 | return 0; | |
3053 | } | |
3054 | ||
3055 | ||
3056 | static int | |
3057 | sn9c102_vidioc_g_audio(struct sn9c102_device* cam, void __user * arg) | |
3058 | { | |
3059 | struct v4l2_audio audio; | |
3060 | ||
3061 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) | |
3062 | return -EINVAL; | |
3063 | ||
3064 | if (copy_from_user(&audio, arg, sizeof(audio))) | |
3065 | return -EFAULT; | |
3066 | ||
3067 | memset(&audio, 0, sizeof(audio)); | |
3068 | strcpy(audio.name, "Microphone"); | |
3069 | ||
3070 | if (copy_to_user(arg, &audio, sizeof(audio))) | |
3071 | return -EFAULT; | |
3072 | ||
3073 | return 0; | |
3074 | } | |
3075 | ||
3076 | ||
3077 | static int | |
3078 | sn9c102_vidioc_s_audio(struct sn9c102_device* cam, void __user * arg) | |
3079 | { | |
3080 | struct v4l2_audio audio; | |
3081 | ||
3082 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) | |
3083 | return -EINVAL; | |
3084 | ||
3085 | if (copy_from_user(&audio, arg, sizeof(audio))) | |
3086 | return -EFAULT; | |
3087 | ||
3088 | if (audio.index != 0) | |
3089 | return -EINVAL; | |
3090 | ||
3091 | return 0; | |
3092 | } | |
3093 | ||
3094 | ||
bec43661 HV |
3095 | static int sn9c102_ioctl_v4l2(struct file *filp, |
3096 | unsigned int cmd, void __user *arg) | |
a966f3e7 | 3097 | { |
c170ecf4 | 3098 | struct sn9c102_device *cam = video_drvdata(filp); |
1da177e4 | 3099 | |
a966f3e7 | 3100 | switch (cmd) { |
1da177e4 | 3101 | |
a966f3e7 LR |
3102 | case VIDIOC_QUERYCAP: |
3103 | return sn9c102_vidioc_querycap(cam, arg); | |
1da177e4 | 3104 | |
a966f3e7 LR |
3105 | case VIDIOC_ENUMINPUT: |
3106 | return sn9c102_vidioc_enuminput(cam, arg); | |
1da177e4 | 3107 | |
a966f3e7 | 3108 | case VIDIOC_G_INPUT: |
2ffab02f LR |
3109 | return sn9c102_vidioc_g_input(cam, arg); |
3110 | ||
a966f3e7 | 3111 | case VIDIOC_S_INPUT: |
2ffab02f | 3112 | return sn9c102_vidioc_s_input(cam, arg); |
1da177e4 | 3113 | |
a966f3e7 LR |
3114 | case VIDIOC_QUERYCTRL: |
3115 | return sn9c102_vidioc_query_ctrl(cam, arg); | |
3116 | ||
3117 | case VIDIOC_G_CTRL: | |
3118 | return sn9c102_vidioc_g_ctrl(cam, arg); | |
3119 | ||
a966f3e7 LR |
3120 | case VIDIOC_S_CTRL: |
3121 | return sn9c102_vidioc_s_ctrl(cam, arg); | |
3122 | ||
a966f3e7 LR |
3123 | case VIDIOC_CROPCAP: |
3124 | return sn9c102_vidioc_cropcap(cam, arg); | |
3125 | ||
3126 | case VIDIOC_G_CROP: | |
3127 | return sn9c102_vidioc_g_crop(cam, arg); | |
3128 | ||
3129 | case VIDIOC_S_CROP: | |
3130 | return sn9c102_vidioc_s_crop(cam, arg); | |
3131 | ||
f327ebbd LR |
3132 | case VIDIOC_ENUM_FRAMESIZES: |
3133 | return sn9c102_vidioc_enum_framesizes(cam, arg); | |
3134 | ||
a966f3e7 LR |
3135 | case VIDIOC_ENUM_FMT: |
3136 | return sn9c102_vidioc_enum_fmt(cam, arg); | |
3137 | ||
3138 | case VIDIOC_G_FMT: | |
3139 | return sn9c102_vidioc_g_fmt(cam, arg); | |
3140 | ||
3141 | case VIDIOC_TRY_FMT: | |
3142 | case VIDIOC_S_FMT: | |
3143 | return sn9c102_vidioc_try_s_fmt(cam, cmd, arg); | |
3144 | ||
3145 | case VIDIOC_G_JPEGCOMP: | |
3146 | return sn9c102_vidioc_g_jpegcomp(cam, arg); | |
3147 | ||
3148 | case VIDIOC_S_JPEGCOMP: | |
3149 | return sn9c102_vidioc_s_jpegcomp(cam, arg); | |
3150 | ||
3151 | case VIDIOC_REQBUFS: | |
3152 | return sn9c102_vidioc_reqbufs(cam, arg); | |
3153 | ||
3154 | case VIDIOC_QUERYBUF: | |
3155 | return sn9c102_vidioc_querybuf(cam, arg); | |
3156 | ||
3157 | case VIDIOC_QBUF: | |
3158 | return sn9c102_vidioc_qbuf(cam, arg); | |
3159 | ||
3160 | case VIDIOC_DQBUF: | |
3161 | return sn9c102_vidioc_dqbuf(cam, filp, arg); | |
3162 | ||
3163 | case VIDIOC_STREAMON: | |
3164 | return sn9c102_vidioc_streamon(cam, arg); | |
3165 | ||
3166 | case VIDIOC_STREAMOFF: | |
3167 | return sn9c102_vidioc_streamoff(cam, arg); | |
3168 | ||
3169 | case VIDIOC_G_PARM: | |
3170 | return sn9c102_vidioc_g_parm(cam, arg); | |
3171 | ||
a966f3e7 LR |
3172 | case VIDIOC_S_PARM: |
3173 | return sn9c102_vidioc_s_parm(cam, arg); | |
1da177e4 | 3174 | |
f327ebbd LR |
3175 | case VIDIOC_ENUMAUDIO: |
3176 | return sn9c102_vidioc_enumaudio(cam, arg); | |
3177 | ||
3178 | case VIDIOC_G_AUDIO: | |
3179 | return sn9c102_vidioc_g_audio(cam, arg); | |
3180 | ||
3181 | case VIDIOC_S_AUDIO: | |
3182 | return sn9c102_vidioc_s_audio(cam, arg); | |
3183 | ||
1da177e4 LT |
3184 | case VIDIOC_G_STD: |
3185 | case VIDIOC_S_STD: | |
3186 | case VIDIOC_QUERYSTD: | |
3187 | case VIDIOC_ENUMSTD: | |
3188 | case VIDIOC_QUERYMENU: | |
f327ebbd | 3189 | case VIDIOC_ENUM_FRAMEINTERVALS: |
1da177e4 LT |
3190 | return -EINVAL; |
3191 | ||
3192 | default: | |
3193 | return -EINVAL; | |
3194 | ||
3195 | } | |
3196 | } | |
3197 | ||
3198 | ||
bec43661 | 3199 | static int sn9c102_ioctl(struct file *filp, |
d56410e0 | 3200 | unsigned int cmd, unsigned long arg) |
1da177e4 | 3201 | { |
c170ecf4 | 3202 | struct sn9c102_device *cam = video_drvdata(filp); |
1da177e4 LT |
3203 | int err = 0; |
3204 | ||
4186ecf8 | 3205 | if (mutex_lock_interruptible(&cam->fileop_mutex)) |
1da177e4 LT |
3206 | return -ERESTARTSYS; |
3207 | ||
3208 | if (cam->state & DEV_DISCONNECTED) { | |
a966f3e7 | 3209 | DBG(1, "Device not present"); |
4186ecf8 | 3210 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
3211 | return -ENODEV; |
3212 | } | |
3213 | ||
3214 | if (cam->state & DEV_MISCONFIGURED) { | |
a966f3e7 LR |
3215 | DBG(1, "The camera is misconfigured. Close and open it " |
3216 | "again."); | |
4186ecf8 | 3217 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
3218 | return -EIO; |
3219 | } | |
3220 | ||
cd6fcc55 LR |
3221 | V4LDBG(3, "sn9c102", cmd); |
3222 | ||
bec43661 | 3223 | err = sn9c102_ioctl_v4l2(filp, cmd, (void __user *)arg); |
1da177e4 | 3224 | |
4186ecf8 | 3225 | mutex_unlock(&cam->fileop_mutex); |
1da177e4 LT |
3226 | |
3227 | return err; | |
3228 | } | |
3229 | ||
a966f3e7 | 3230 | /*****************************************************************************/ |
1da177e4 | 3231 | |
bec43661 | 3232 | static const struct v4l2_file_operations sn9c102_fops = { |
a966f3e7 | 3233 | .owner = THIS_MODULE, |
480b55c2 | 3234 | .open = sn9c102_open, |
1da177e4 | 3235 | .release = sn9c102_release, |
480b55c2 | 3236 | .ioctl = sn9c102_ioctl, |
480b55c2 LR |
3237 | .read = sn9c102_read, |
3238 | .poll = sn9c102_poll, | |
3239 | .mmap = sn9c102_mmap, | |
1da177e4 LT |
3240 | }; |
3241 | ||
3242 | /*****************************************************************************/ | |
3243 | ||
3244 | /* It exists a single interface only. We do not need to validate anything. */ | |
3245 | static int | |
3246 | sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id) | |
3247 | { | |
3248 | struct usb_device *udev = interface_to_usbdev(intf); | |
3249 | struct sn9c102_device* cam; | |
ff699e6b | 3250 | static unsigned int dev_nr; |
a966f3e7 | 3251 | unsigned int i; |
1da177e4 LT |
3252 | int err = 0, r; |
3253 | ||
cd6fcc55 | 3254 | if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL))) |
1da177e4 | 3255 | return -ENOMEM; |
1da177e4 LT |
3256 | |
3257 | cam->usbdev = udev; | |
1da177e4 | 3258 | |
cd6fcc55 | 3259 | if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) { |
f327ebbd | 3260 | DBG(1, "kzalloc() failed"); |
1da177e4 LT |
3261 | err = -ENOMEM; |
3262 | goto fail; | |
3263 | } | |
1da177e4 LT |
3264 | |
3265 | if (!(cam->v4ldev = video_device_alloc())) { | |
a966f3e7 | 3266 | DBG(1, "video_device_alloc() failed"); |
1da177e4 LT |
3267 | err = -ENOMEM; |
3268 | goto fail; | |
3269 | } | |
3270 | ||
1da177e4 | 3271 | r = sn9c102_read_reg(cam, 0x00); |
f327ebbd | 3272 | if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) { |
f423b9a8 LR |
3273 | DBG(1, "Sorry, this is not a SN9C1xx-based camera " |
3274 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); | |
1da177e4 LT |
3275 | err = -ENODEV; |
3276 | goto fail; | |
3277 | } | |
3278 | ||
f327ebbd | 3279 | cam->bridge = id->driver_info; |
1da177e4 LT |
3280 | switch (cam->bridge) { |
3281 | case BRIDGE_SN9C101: | |
3282 | case BRIDGE_SN9C102: | |
3283 | DBG(2, "SN9C10[12] PC Camera Controller detected " | |
f423b9a8 | 3284 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); |
1da177e4 LT |
3285 | break; |
3286 | case BRIDGE_SN9C103: | |
3287 | DBG(2, "SN9C103 PC Camera Controller detected " | |
f423b9a8 | 3288 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); |
f327ebbd LR |
3289 | break; |
3290 | case BRIDGE_SN9C105: | |
3291 | DBG(2, "SN9C105 PC Camera Controller detected " | |
f423b9a8 | 3292 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); |
f327ebbd LR |
3293 | break; |
3294 | case BRIDGE_SN9C120: | |
3295 | DBG(2, "SN9C120 PC Camera Controller detected " | |
f423b9a8 | 3296 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); |
1da177e4 LT |
3297 | break; |
3298 | } | |
3299 | ||
480b55c2 | 3300 | for (i = 0; i < ARRAY_SIZE(sn9c102_sensor_table); i++) { |
1da177e4 LT |
3301 | err = sn9c102_sensor_table[i](cam); |
3302 | if (!err) | |
3303 | break; | |
3304 | } | |
3305 | ||
2ffab02f LR |
3306 | if (!err) { |
3307 | DBG(2, "%s image sensor detected", cam->sensor.name); | |
1da177e4 | 3308 | DBG(3, "Support for %s maintained by %s", |
2ffab02f | 3309 | cam->sensor.name, cam->sensor.maintainer); |
1da177e4 | 3310 | } else { |
480b55c2 | 3311 | DBG(1, "No supported image sensor detected for this bridge"); |
1da177e4 LT |
3312 | err = -ENODEV; |
3313 | goto fail; | |
3314 | } | |
3315 | ||
f327ebbd LR |
3316 | if (!(cam->bridge & cam->sensor.supported_bridge)) { |
3317 | DBG(1, "Bridge not supported"); | |
3318 | err = -ENODEV; | |
3319 | goto fail; | |
3320 | } | |
3321 | ||
1da177e4 | 3322 | if (sn9c102_init(cam)) { |
a966f3e7 | 3323 | DBG(1, "Initialization failed. I will retry on open()."); |
1da177e4 LT |
3324 | cam->state |= DEV_MISCONFIGURED; |
3325 | } | |
3326 | ||
f327ebbd | 3327 | strcpy(cam->v4ldev->name, "SN9C1xx PC Camera"); |
1da177e4 LT |
3328 | cam->v4ldev->fops = &sn9c102_fops; |
3329 | cam->v4ldev->minor = video_nr[dev_nr]; | |
3330 | cam->v4ldev->release = video_device_release; | |
748c7f80 | 3331 | cam->v4ldev->parent = &udev->dev; |
1da177e4 | 3332 | |
3770be34 | 3333 | init_completion(&cam->probe); |
1da177e4 LT |
3334 | |
3335 | err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, | |
d56410e0 | 3336 | video_nr[dev_nr]); |
1da177e4 | 3337 | if (err) { |
a966f3e7 | 3338 | DBG(1, "V4L2 device registration failed"); |
1da177e4 | 3339 | if (err == -ENFILE && video_nr[dev_nr] == -1) |
a966f3e7 | 3340 | DBG(1, "Free /dev/videoX node not found"); |
f327ebbd LR |
3341 | video_nr[dev_nr] = -1; |
3342 | dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; | |
3770be34 | 3343 | complete_all(&cam->probe); |
f327ebbd | 3344 | goto fail; |
1da177e4 LT |
3345 | } |
3346 | ||
c6330fb8 | 3347 | DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->num); |
1da177e4 | 3348 | |
ddef2dcc | 3349 | video_set_drvdata(cam->v4ldev, cam); |
1da177e4 | 3350 | cam->module_param.force_munmap = force_munmap[dev_nr]; |
2ffab02f | 3351 | cam->module_param.frame_timeout = frame_timeout[dev_nr]; |
1da177e4 LT |
3352 | |
3353 | dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; | |
3354 | ||
cd6fcc55 | 3355 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
c12e3be0 | 3356 | err = sn9c102_create_sysfs(cam); |
f327ebbd LR |
3357 | if (!err) |
3358 | DBG(2, "Optional device control through 'sysfs' " | |
3359 | "interface ready"); | |
3360 | else | |
3361 | DBG(2, "Failed to create optional 'sysfs' interface for " | |
3362 | "device controlling. Error #%d", err); | |
3363 | #else | |
3364 | DBG(2, "Optional device control through 'sysfs' interface disabled"); | |
f423b9a8 LR |
3365 | DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' " |
3366 | "configuration option to enable it."); | |
cd6fcc55 | 3367 | #endif |
1da177e4 LT |
3368 | |
3369 | usb_set_intfdata(intf, cam); | |
3770be34 LR |
3370 | kref_init(&cam->kref); |
3371 | usb_get_dev(cam->usbdev); | |
1da177e4 | 3372 | |
3770be34 | 3373 | complete_all(&cam->probe); |
1da177e4 LT |
3374 | |
3375 | return 0; | |
3376 | ||
3377 | fail: | |
3378 | if (cam) { | |
3379 | kfree(cam->control_buffer); | |
3380 | if (cam->v4ldev) | |
3381 | video_device_release(cam->v4ldev); | |
3382 | kfree(cam); | |
3383 | } | |
3384 | return err; | |
3385 | } | |
3386 | ||
3387 | ||
3388 | static void sn9c102_usb_disconnect(struct usb_interface* intf) | |
3389 | { | |
3770be34 | 3390 | struct sn9c102_device* cam; |
1da177e4 | 3391 | |
3770be34 | 3392 | down_write(&sn9c102_dev_lock); |
1da177e4 | 3393 | |
3770be34 | 3394 | cam = usb_get_intfdata(intf); |
1da177e4 | 3395 | |
a966f3e7 | 3396 | DBG(2, "Disconnecting %s...", cam->v4ldev->name); |
1da177e4 | 3397 | |
1da177e4 LT |
3398 | if (cam->users) { |
3399 | DBG(2, "Device /dev/video%d is open! Deregistration and " | |
3770be34 | 3400 | "memory deallocation are deferred.", |
c6330fb8 | 3401 | cam->v4ldev->num); |
1da177e4 LT |
3402 | cam->state |= DEV_MISCONFIGURED; |
3403 | sn9c102_stop_transfer(cam); | |
3404 | cam->state |= DEV_DISCONNECTED; | |
3405 | wake_up_interruptible(&cam->wait_frame); | |
2ffab02f | 3406 | wake_up(&cam->wait_stream); |
3770be34 | 3407 | } else |
1da177e4 | 3408 | cam->state |= DEV_DISCONNECTED; |
1da177e4 | 3409 | |
3770be34 | 3410 | wake_up_interruptible_all(&cam->wait_open); |
1da177e4 | 3411 | |
3770be34 | 3412 | kref_put(&cam->kref, sn9c102_release_resources); |
1da177e4 | 3413 | |
3770be34 | 3414 | up_write(&sn9c102_dev_lock); |
1da177e4 LT |
3415 | } |
3416 | ||
3417 | ||
3418 | static struct usb_driver sn9c102_usb_driver = { | |
1da177e4 LT |
3419 | .name = "sn9c102", |
3420 | .id_table = sn9c102_id_table, | |
3421 | .probe = sn9c102_usb_probe, | |
3422 | .disconnect = sn9c102_usb_disconnect, | |
3423 | }; | |
3424 | ||
3425 | /*****************************************************************************/ | |
3426 | ||
3427 | static int __init sn9c102_module_init(void) | |
3428 | { | |
3429 | int err = 0; | |
3430 | ||
a966f3e7 LR |
3431 | KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION); |
3432 | KDBG(3, SN9C102_MODULE_AUTHOR); | |
1da177e4 LT |
3433 | |
3434 | if ((err = usb_register(&sn9c102_usb_driver))) | |
a966f3e7 | 3435 | KDBG(1, "usb_register() failed"); |
1da177e4 LT |
3436 | |
3437 | return err; | |
3438 | } | |
3439 | ||
3440 | ||
3441 | static void __exit sn9c102_module_exit(void) | |
3442 | { | |
3443 | usb_deregister(&sn9c102_usb_driver); | |
3444 | } | |
3445 | ||
3446 | ||
3447 | module_init(sn9c102_module_init); | |
3448 | module_exit(sn9c102_module_exit); |