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