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