aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/media/sn9c102_core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/media/sn9c102_core.c')
-rw-r--r--drivers/usb/media/sn9c102_core.c1681
1 files changed, 915 insertions, 766 deletions
diff --git a/drivers/usb/media/sn9c102_core.c b/drivers/usb/media/sn9c102_core.c
index 8d1a1c357d5a..c81397e4714b 100644
--- a/drivers/usb/media/sn9c102_core.c
+++ b/drivers/usb/media/sn9c102_core.c
@@ -1,7 +1,7 @@
1/*************************************************************************** 1/***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers * 2 * V4L2 driver for SN9C10x PC Camera Controllers *
3 * * 3 * *
4 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * * 5 * *
6 * This program is free software; you can redistribute it and/or modify * 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 * 7 * it under the terms of the GNU General Public License as published by *
@@ -45,6 +45,15 @@
45 45
46/*****************************************************************************/ 46/*****************************************************************************/
47 47
48#define SN9C102_MODULE_NAME "V4L2 driver for SN9C10x PC Camera Controllers"
49#define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
50#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
51#define SN9C102_MODULE_LICENSE "GPL"
52#define SN9C102_MODULE_VERSION "1:1.26"
53#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 26)
54
55/*****************************************************************************/
56
48MODULE_DEVICE_TABLE(usb, sn9c102_id_table); 57MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
49 58
50MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL); 59MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
@@ -70,10 +79,10 @@ static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
70 SN9C102_FORCE_MUNMAP}; 79 SN9C102_FORCE_MUNMAP};
71module_param_array(force_munmap, bool, NULL, 0444); 80module_param_array(force_munmap, bool, NULL, 0444);
72MODULE_PARM_DESC(force_munmap, 81MODULE_PARM_DESC(force_munmap,
73 "\n<0|1[,...]> Force the application to unmap previously " 82 "\n<0|1[,...]> Force the application to unmap previously"
74 "\nmapped buffer memory before calling any VIDIOC_S_CROP or " 83 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
75 "\nVIDIOC_S_FMT ioctl's. Not all the applications support " 84 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
76 "\nthis feature. This parameter is specific for each " 85 "\nthis feature. This parameter is specific for each"
77 "\ndetected camera." 86 "\ndetected camera."
78 "\n 0 = do not force memory unmapping" 87 "\n 0 = do not force memory unmapping"
79 "\n 1 = force memory unmapping (save memory)" 88 "\n 1 = force memory unmapping (save memory)"
@@ -102,6 +111,9 @@ static sn9c102_sof_header_t sn9c102_sof_header[] = {
102 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01}, 111 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
103}; 112};
104 113
114static sn9c103_sof_header_t sn9c103_sof_header[] = {
115 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20},
116};
105 117
106static sn9c102_eof_header_t sn9c102_eof_header[] = { 118static sn9c102_eof_header_t sn9c102_eof_header[] = {
107 {0x00, 0x00, 0x00, 0x00}, 119 {0x00, 0x00, 0x00, 0x00},
@@ -112,50 +124,6 @@ static sn9c102_eof_header_t sn9c102_eof_header[] = {
112 124
113/*****************************************************************************/ 125/*****************************************************************************/
114 126
115static void* rvmalloc(size_t size)
116{
117 void* mem;
118 unsigned long adr;
119
120 size = PAGE_ALIGN(size);
121
122 mem = vmalloc_32((unsigned long)size);
123 if (!mem)
124 return NULL;
125
126 memset(mem, 0, size);
127
128 adr = (unsigned long)mem;
129 while (size > 0) {
130 SetPageReserved(vmalloc_to_page((void *)adr));
131 adr += PAGE_SIZE;
132 size -= PAGE_SIZE;
133 }
134
135 return mem;
136}
137
138
139static void rvfree(void* mem, size_t size)
140{
141 unsigned long adr;
142
143 if (!mem)
144 return;
145
146 size = PAGE_ALIGN(size);
147
148 adr = (unsigned long)mem;
149 while (size > 0) {
150 ClearPageReserved(vmalloc_to_page((void *)adr));
151 adr += PAGE_SIZE;
152 size -= PAGE_SIZE;
153 }
154
155 vfree(mem);
156}
157
158
159static u32 127static u32
160sn9c102_request_buffers(struct sn9c102_device* cam, u32 count, 128sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
161 enum sn9c102_io_method io) 129 enum sn9c102_io_method io)
@@ -174,7 +142,7 @@ sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
174 142
175 cam->nbuffers = count; 143 cam->nbuffers = count;
176 while (cam->nbuffers > 0) { 144 while (cam->nbuffers > 0) {
177 if ((buff = rvmalloc(cam->nbuffers * PAGE_ALIGN(imagesize)))) 145 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
178 break; 146 break;
179 cam->nbuffers--; 147 cam->nbuffers--;
180 } 148 }
@@ -198,10 +166,10 @@ sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
198static void sn9c102_release_buffers(struct sn9c102_device* cam) 166static void sn9c102_release_buffers(struct sn9c102_device* cam)
199{ 167{
200 if (cam->nbuffers) { 168 if (cam->nbuffers) {
201 rvfree(cam->frame[0].bufmem, 169 vfree(cam->frame[0].bufmem);
202 cam->nbuffers * PAGE_ALIGN(cam->frame[0].buf.length));
203 cam->nbuffers = 0; 170 cam->nbuffers = 0;
204 } 171 }
172 cam->frame_current = NULL;
205} 173}
206 174
207 175
@@ -219,6 +187,19 @@ static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
219} 187}
220 188
221 189
190static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
191{
192 struct sn9c102_frame_t *i;
193
194 list_for_each_entry(i, &cam->outqueue, frame) {
195 i->state = F_QUEUED;
196 list_add(&i->frame, &cam->inqueue);
197 }
198
199 INIT_LIST_HEAD(&cam->outqueue);
200}
201
202
222static void sn9c102_queue_unusedframes(struct sn9c102_device* cam) 203static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
223{ 204{
224 unsigned long lock_flags; 205 unsigned long lock_flags;
@@ -235,19 +216,46 @@ static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
235 216
236/*****************************************************************************/ 217/*****************************************************************************/
237 218
219int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
220{
221 struct usb_device* udev = cam->usbdev;
222 int i, res;
223
224 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
225 return -1;
226
227 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
228 index, 0, buff, sizeof(buff),
229 SN9C102_CTRL_TIMEOUT*sizeof(buff));
230 if (res < 0) {
231 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
232 index, res);
233 return -1;
234 }
235
236 for (i = 0; i < sizeof(buff); i++)
237 cam->reg[index+i] = buff[i];
238
239 return 0;
240}
241
242
238int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index) 243int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
239{ 244{
240 struct usb_device* udev = cam->usbdev; 245 struct usb_device* udev = cam->usbdev;
241 u8* buff = cam->control_buffer; 246 u8* buff = cam->control_buffer;
242 int res; 247 int res;
243 248
249 if (index >= ARRAY_SIZE(cam->reg))
250 return -1;
251
244 *buff = value; 252 *buff = value;
245 253
246 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, 254 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
247 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); 255 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
248 if (res < 0) { 256 if (res < 0) {
249 DBG(3, "Failed to write a register (value 0x%02X, index " 257 DBG(3, "Failed to write a register (value 0x%02X, index "
250 "0x%02X, error %d)", value, index, res) 258 "0x%02X, error %d)", value, index, res);
251 return -1; 259 return -1;
252 } 260 }
253 261
@@ -268,7 +276,7 @@ static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
268 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); 276 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
269 if (res < 0) 277 if (res < 0)
270 DBG(3, "Failed to read a register (index 0x%02X, error %d)", 278 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
271 index, res) 279 index, res);
272 280
273 return (res >= 0) ? (int)(*buff) : -1; 281 return (res >= 0) ? (int)(*buff) : -1;
274} 282}
@@ -276,8 +284,8 @@ static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
276 284
277int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index) 285int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
278{ 286{
279 if (index > 0x1f) 287 if (index >= ARRAY_SIZE(cam->reg))
280 return -EINVAL; 288 return -1;
281 289
282 return cam->reg[index]; 290 return cam->reg[index];
283} 291}
@@ -367,10 +375,10 @@ sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
367 err += sn9c102_i2c_detect_read_error(cam, sensor); 375 err += sn9c102_i2c_detect_read_error(cam, sensor);
368 376
369 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1, 377 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
370 data[4]) 378 data[4]);
371 379
372 if (err) { 380 if (err) {
373 DBG(3, "I2C read failed for %s image sensor", sensor->name) 381 DBG(3, "I2C read failed for %s image sensor", sensor->name);
374 return -1; 382 return -1;
375 } 383 }
376 384
@@ -410,11 +418,11 @@ sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
410 err += sn9c102_i2c_detect_write_error(cam, sensor); 418 err += sn9c102_i2c_detect_write_error(cam, sensor);
411 419
412 if (err) 420 if (err)
413 DBG(3, "I2C write failed for %s image sensor", sensor->name) 421 DBG(3, "I2C write failed for %s image sensor", sensor->name);
414 422
415 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, " 423 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
416 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X", 424 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
417 n, data0, data1, data2, data3, data4, data5) 425 n, data0, data1, data2, data3, data4, data5);
418 426
419 return err ? -1 : 0; 427 return err ? -1 : 0;
420} 428}
@@ -461,13 +469,27 @@ int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
461static void* 469static void*
462sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len) 470sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
463{ 471{
464 size_t soflen = sizeof(sn9c102_sof_header_t), i; 472 size_t soflen = 0, i;
465 u8 j, n = sizeof(sn9c102_sof_header) / soflen; 473 u8 j, n = 0;
466 474
467 for (i = 0; (len >= soflen) && (i <= len - soflen); i++) 475 switch (cam->bridge) {
476 case BRIDGE_SN9C101:
477 case BRIDGE_SN9C102:
478 soflen = sizeof(sn9c102_sof_header_t);
479 n = sizeof(sn9c102_sof_header) / soflen;
480 break;
481 case BRIDGE_SN9C103:
482 soflen = sizeof(sn9c103_sof_header_t);
483 n = sizeof(sn9c103_sof_header) / soflen;
484 }
485
486 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
468 for (j = 0; j < n; j++) 487 for (j = 0; j < n; j++)
469 /* It's enough to compare 7 bytes */ 488 /* The invariable part of the header is 6 bytes long */
470 if (!memcmp(mem + i, sn9c102_sof_header[j], 7)) { 489 if ((cam->bridge != BRIDGE_SN9C103 &&
490 !memcmp(mem + i, sn9c102_sof_header[j], 6)) ||
491 (cam->bridge == BRIDGE_SN9C103 &&
492 !memcmp(mem + i, sn9c103_sof_header[j], 6))) {
471 memcpy(cam->sof_header, mem + i, soflen); 493 memcpy(cam->sof_header, mem + i, soflen);
472 /* Skip the header */ 494 /* Skip the header */
473 return mem + i + soflen; 495 return mem + i + soflen;
@@ -499,8 +521,7 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
499{ 521{
500 struct sn9c102_device* cam = urb->context; 522 struct sn9c102_device* cam = urb->context;
501 struct sn9c102_frame_t** f; 523 struct sn9c102_frame_t** f;
502 size_t imagesize; 524 size_t imagesize, soflen;
503 unsigned long lock_flags;
504 u8 i; 525 u8 i;
505 int err = 0; 526 int err = 0;
506 527
@@ -513,7 +534,7 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
513 cam->stream = STREAM_OFF; 534 cam->stream = STREAM_OFF;
514 if ((*f)) 535 if ((*f))
515 (*f)->state = F_QUEUED; 536 (*f)->state = F_QUEUED;
516 DBG(3, "Stream interrupted") 537 DBG(3, "Stream interrupted");
517 wake_up_interruptible(&cam->wait_stream); 538 wake_up_interruptible(&cam->wait_stream);
518 } 539 }
519 540
@@ -536,6 +557,10 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
536 cam->sensor->pix_format.height * 557 cam->sensor->pix_format.height *
537 cam->sensor->pix_format.priv) / 8; 558 cam->sensor->pix_format.priv) / 8;
538 559
560 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
561 sizeof(sn9c103_sof_header_t) :
562 sizeof(sn9c102_sof_header_t);
563
539 for (i = 0; i < urb->number_of_packets; i++) { 564 for (i = 0; i < urb->number_of_packets; i++) {
540 unsigned int img, len, status; 565 unsigned int img, len, status;
541 void *pos, *sof, *eof; 566 void *pos, *sof, *eof;
@@ -545,19 +570,12 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
545 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer; 570 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
546 571
547 if (status) { 572 if (status) {
548 DBG(3, "Error in isochronous frame") 573 DBG(3, "Error in isochronous frame");
549 (*f)->state = F_ERROR; 574 (*f)->state = F_ERROR;
550 continue; 575 continue;
551 } 576 }
552 577
553 PDBGG("Isochrnous frame: length %u, #%u i", len, i) 578 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
554
555 /*
556 NOTE: It is probably correct to assume that SOF and EOF
557 headers do not occur between two consecutive packets,
558 but who knows..Whatever is the truth, this assumption
559 doesn't introduce bugs.
560 */
561 579
562redo: 580redo:
563 sof = sn9c102_find_sof_header(cam, pos, len); 581 sof = sn9c102_find_sof_header(cam, pos, len);
@@ -575,10 +593,10 @@ end_of_frame:
575 imagesize; 593 imagesize;
576 img = imagesize - (*f)->buf.bytesused; 594 img = imagesize - (*f)->buf.bytesused;
577 DBG(3, "Expected EOF not found: " 595 DBG(3, "Expected EOF not found: "
578 "video frame cut") 596 "video frame cut");
579 if (eof) 597 if (eof)
580 DBG(3, "Exceeded limit: +%u " 598 DBG(3, "Exceeded limit: +%u "
581 "bytes", (unsigned)(b)) 599 "bytes", (unsigned)(b));
582 } 600 }
583 601
584 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos, 602 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
@@ -595,8 +613,7 @@ end_of_frame:
595 u32 b = (*f)->buf.bytesused; 613 u32 b = (*f)->buf.bytesused;
596 (*f)->state = F_DONE; 614 (*f)->state = F_DONE;
597 (*f)->buf.sequence= ++cam->frame_count; 615 (*f)->buf.sequence= ++cam->frame_count;
598 spin_lock_irqsave(&cam->queue_lock, 616 spin_lock(&cam->queue_lock);
599 lock_flags);
600 list_move_tail(&(*f)->frame, 617 list_move_tail(&(*f)->frame,
601 &cam->outqueue); 618 &cam->outqueue);
602 if (!list_empty(&cam->inqueue)) 619 if (!list_empty(&cam->inqueue))
@@ -606,13 +623,11 @@ end_of_frame:
606 frame ); 623 frame );
607 else 624 else
608 (*f) = NULL; 625 (*f) = NULL;
609 spin_unlock_irqrestore(&cam->queue_lock 626 spin_unlock(&cam->queue_lock);
610 , lock_flags);
611 memcpy(cam->sysfs.frame_header, 627 memcpy(cam->sysfs.frame_header,
612 cam->sof_header, 628 cam->sof_header, soflen);
613 sizeof(sn9c102_sof_header_t)); 629 DBG(3, "Video frame captured: %lu "
614 DBG(3, "Video frame captured: " 630 "bytes", (unsigned long)(b));
615 "%lu bytes", (unsigned long)(b))
616 631
617 if (!(*f)) 632 if (!(*f))
618 goto resubmit_urb; 633 goto resubmit_urb;
@@ -621,18 +636,19 @@ end_of_frame:
621 (*f)->state = F_ERROR; 636 (*f)->state = F_ERROR;
622 DBG(3, "Not expected EOF after %lu " 637 DBG(3, "Not expected EOF after %lu "
623 "bytes of image data", 638 "bytes of image data",
624 (unsigned long)((*f)->buf.bytesused)) 639 (unsigned long)
640 ((*f)->buf.bytesused));
625 } 641 }
626 642
627 if (sof) /* (1) */ 643 if (sof) /* (1) */
628 goto start_of_frame; 644 goto start_of_frame;
629 645
630 } else if (eof) { 646 } else if (eof) {
631 DBG(3, "EOF without SOF") 647 DBG(3, "EOF without SOF");
632 continue; 648 continue;
633 649
634 } else { 650 } else {
635 PDBGG("Ignoring pointless isochronous frame") 651 PDBGG("Ignoring pointless isochronous frame");
636 continue; 652 continue;
637 } 653 }
638 654
@@ -642,7 +658,7 @@ start_of_frame:
642 (*f)->buf.bytesused = 0; 658 (*f)->buf.bytesused = 0;
643 len -= (sof - pos); 659 len -= (sof - pos);
644 pos = sof; 660 pos = sof;
645 DBG(3, "SOF detected: new video frame") 661 DBG(3, "SOF detected: new video frame");
646 if (len) 662 if (len)
647 goto redo; 663 goto redo;
648 664
@@ -653,12 +669,13 @@ start_of_frame:
653 else { 669 else {
654 if (cam->sensor->pix_format.pixelformat == 670 if (cam->sensor->pix_format.pixelformat ==
655 V4L2_PIX_FMT_SN9C10X) { 671 V4L2_PIX_FMT_SN9C10X) {
656 eof = sof-sizeof(sn9c102_sof_header_t); 672 eof = sof - soflen;
657 goto end_of_frame; 673 goto end_of_frame;
658 } else { 674 } else {
659 DBG(3, "SOF before expected EOF after " 675 DBG(3, "SOF before expected EOF after "
660 "%lu bytes of image data", 676 "%lu bytes of image data",
661 (unsigned long)((*f)->buf.bytesused)) 677 (unsigned long)
678 ((*f)->buf.bytesused));
662 goto start_of_frame; 679 goto start_of_frame;
663 } 680 }
664 } 681 }
@@ -670,7 +687,7 @@ resubmit_urb:
670 err = usb_submit_urb(urb, GFP_ATOMIC); 687 err = usb_submit_urb(urb, GFP_ATOMIC);
671 if (err < 0 && err != -EPERM) { 688 if (err < 0 && err != -EPERM) {
672 cam->state |= DEV_MISCONFIGURED; 689 cam->state |= DEV_MISCONFIGURED;
673 DBG(1, "usb_submit_urb() failed") 690 DBG(1, "usb_submit_urb() failed");
674 } 691 }
675 692
676 wake_up_interruptible(&cam->wait_frame); 693 wake_up_interruptible(&cam->wait_frame);
@@ -681,18 +698,22 @@ static int sn9c102_start_transfer(struct sn9c102_device* cam)
681{ 698{
682 struct usb_device *udev = cam->usbdev; 699 struct usb_device *udev = cam->usbdev;
683 struct urb* urb; 700 struct urb* urb;
684 const unsigned int wMaxPacketSize[] = {0, 128, 256, 384, 512, 701 const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512,
685 680, 800, 900, 1023}; 702 680, 800, 900, 1023};
686 const unsigned int psz = wMaxPacketSize[SN9C102_ALTERNATE_SETTING]; 703 const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512,
704 680, 800, 900, 1003};
705 const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ?
706 sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] :
707 sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
687 s8 i, j; 708 s8 i, j;
688 int err = 0; 709 int err = 0;
689 710
690 for (i = 0; i < SN9C102_URBS; i++) { 711 for (i = 0; i < SN9C102_URBS; i++) {
691 cam->transfer_buffer[i] = kmalloc(SN9C102_ISO_PACKETS * psz, 712 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
692 GFP_KERNEL); 713 GFP_KERNEL);
693 if (!cam->transfer_buffer[i]) { 714 if (!cam->transfer_buffer[i]) {
694 err = -ENOMEM; 715 err = -ENOMEM;
695 DBG(1, "Not enough memory") 716 DBG(1, "Not enough memory");
696 goto free_buffers; 717 goto free_buffers;
697 } 718 }
698 } 719 }
@@ -702,7 +723,7 @@ static int sn9c102_start_transfer(struct sn9c102_device* cam)
702 cam->urb[i] = urb; 723 cam->urb[i] = urb;
703 if (!urb) { 724 if (!urb) {
704 err = -ENOMEM; 725 err = -ENOMEM;
705 DBG(1, "usb_alloc_urb() failed") 726 DBG(1, "usb_alloc_urb() failed");
706 goto free_urbs; 727 goto free_urbs;
707 } 728 }
708 urb->dev = udev; 729 urb->dev = udev;
@@ -725,14 +746,14 @@ static int sn9c102_start_transfer(struct sn9c102_device* cam)
725 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01); 746 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
726 if (err) { 747 if (err) {
727 err = -EIO; 748 err = -EIO;
728 DBG(1, "I/O hardware error") 749 DBG(1, "I/O hardware error");
729 goto free_urbs; 750 goto free_urbs;
730 } 751 }
731 } 752 }
732 753
733 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING); 754 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
734 if (err) { 755 if (err) {
735 DBG(1, "usb_set_interface() failed") 756 DBG(1, "usb_set_interface() failed");
736 goto free_urbs; 757 goto free_urbs;
737 } 758 }
738 759
@@ -743,7 +764,7 @@ static int sn9c102_start_transfer(struct sn9c102_device* cam)
743 if (err) { 764 if (err) {
744 for (j = i-1; j >= 0; j--) 765 for (j = i-1; j >= 0; j--)
745 usb_kill_urb(cam->urb[j]); 766 usb_kill_urb(cam->urb[j]);
746 DBG(1, "usb_submit_urb() failed, error %d", err) 767 DBG(1, "usb_submit_urb() failed, error %d", err);
747 goto free_urbs; 768 goto free_urbs;
748 } 769 }
749 } 770 }
@@ -779,7 +800,7 @@ static int sn9c102_stop_transfer(struct sn9c102_device* cam)
779 800
780 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */ 801 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
781 if (err) 802 if (err)
782 DBG(3, "usb_set_interface() failed") 803 DBG(3, "usb_set_interface() failed");
783 804
784 return err; 805 return err;
785} 806}
@@ -799,7 +820,7 @@ static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
799 else if (err) { 820 else if (err) {
800 cam->state |= DEV_MISCONFIGURED; 821 cam->state |= DEV_MISCONFIGURED;
801 DBG(1, "The camera is misconfigured. To use it, close and " 822 DBG(1, "The camera is misconfigured. To use it, close and "
802 "open /dev/video%d again.", cam->v4ldev->minor) 823 "open /dev/video%d again.", cam->v4ldev->minor);
803 return err; 824 return err;
804 } 825 }
805 826
@@ -808,6 +829,7 @@ static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
808 829
809/*****************************************************************************/ 830/*****************************************************************************/
810 831
832#ifdef CONFIG_VIDEO_ADV_DEBUG
811static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count) 833static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
812{ 834{
813 char str[5]; 835 char str[5];
@@ -885,8 +907,8 @@ sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
885 907
886 cam->sysfs.reg = index; 908 cam->sysfs.reg = index;
887 909
888 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg) 910 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
889 DBG(3, "Written bytes: %zd", count) 911 DBG(3, "Written bytes: %zd", count);
890 912
891 up(&sn9c102_sysfs_lock); 913 up(&sn9c102_sysfs_lock);
892 914
@@ -916,7 +938,7 @@ static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
916 938
917 count = sprintf(buf, "%d\n", val); 939 count = sprintf(buf, "%d\n", val);
918 940
919 DBG(3, "Read bytes: %zd", count) 941 DBG(3, "Read bytes: %zd", count);
920 942
921 up(&sn9c102_sysfs_lock); 943 up(&sn9c102_sysfs_lock);
922 944
@@ -954,8 +976,8 @@ sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
954 } 976 }
955 977
956 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X", 978 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
957 cam->sysfs.reg, value) 979 cam->sysfs.reg, value);
958 DBG(3, "Written bytes: %zd", count) 980 DBG(3, "Written bytes: %zd", count);
959 981
960 up(&sn9c102_sysfs_lock); 982 up(&sn9c102_sysfs_lock);
961 983
@@ -979,7 +1001,7 @@ static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
979 1001
980 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg); 1002 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
981 1003
982 DBG(3, "Read bytes: %zd", count) 1004 DBG(3, "Read bytes: %zd", count);
983 1005
984 up(&sn9c102_sysfs_lock); 1006 up(&sn9c102_sysfs_lock);
985 1007
@@ -1011,8 +1033,8 @@ sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1011 1033
1012 cam->sysfs.i2c_reg = index; 1034 cam->sysfs.i2c_reg = index;
1013 1035
1014 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg) 1036 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1015 DBG(3, "Written bytes: %zd", count) 1037 DBG(3, "Written bytes: %zd", count);
1016 1038
1017 up(&sn9c102_sysfs_lock); 1039 up(&sn9c102_sysfs_lock);
1018 1040
@@ -1047,7 +1069,7 @@ static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1047 1069
1048 count = sprintf(buf, "%d\n", val); 1070 count = sprintf(buf, "%d\n", val);
1049 1071
1050 DBG(3, "Read bytes: %zd", count) 1072 DBG(3, "Read bytes: %zd", count);
1051 1073
1052 up(&sn9c102_sysfs_lock); 1074 up(&sn9c102_sysfs_lock);
1053 1075
@@ -1090,8 +1112,8 @@ sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1090 } 1112 }
1091 1113
1092 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X", 1114 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1093 cam->sysfs.i2c_reg, value) 1115 cam->sysfs.i2c_reg, value);
1094 DBG(3, "Written bytes: %zd", count) 1116 DBG(3, "Written bytes: %zd", count);
1095 1117
1096 up(&sn9c102_sysfs_lock); 1118 up(&sn9c102_sysfs_lock);
1097 1119
@@ -1193,7 +1215,7 @@ static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1193 count = sizeof(cam->sysfs.frame_header); 1215 count = sizeof(cam->sysfs.frame_header);
1194 memcpy(buf, cam->sysfs.frame_header, count); 1216 memcpy(buf, cam->sysfs.frame_header, count);
1195 1217
1196 DBG(3, "Frame header, read bytes: %zd", count) 1218 DBG(3, "Frame header, read bytes: %zd", count);
1197 1219
1198 return count; 1220 return count;
1199} 1221}
@@ -1227,11 +1249,12 @@ static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1227 video_device_create_file(v4ldev, &class_device_attr_blue); 1249 video_device_create_file(v4ldev, &class_device_attr_blue);
1228 video_device_create_file(v4ldev, &class_device_attr_red); 1250 video_device_create_file(v4ldev, &class_device_attr_red);
1229 } 1251 }
1230 if (cam->sensor->sysfs_ops) { 1252 if (cam->sensor && cam->sensor->sysfs_ops) {
1231 video_device_create_file(v4ldev, &class_device_attr_i2c_reg); 1253 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1232 video_device_create_file(v4ldev, &class_device_attr_i2c_val); 1254 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1233 } 1255 }
1234} 1256}
1257#endif /* CONFIG_VIDEO_ADV_DEBUG */
1235 1258
1236/*****************************************************************************/ 1259/*****************************************************************************/
1237 1260
@@ -1281,7 +1304,7 @@ static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1281 if (err) 1304 if (err)
1282 return -EIO; 1305 return -EIO;
1283 1306
1284 PDBGG("Scaling factor: %u", scale) 1307 PDBGG("Scaling factor: %u", scale);
1285 1308
1286 return 0; 1309 return 0;
1287} 1310}
@@ -1304,7 +1327,7 @@ static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1304 return -EIO; 1327 return -EIO;
1305 1328
1306 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size " 1329 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1307 "%u %u %u %u", h_start, v_start, h_size, v_size) 1330 "%u %u %u %u", h_start, v_start, h_size, v_size);
1308 1331
1309 return 0; 1332 return 0;
1310} 1333}
@@ -1336,7 +1359,7 @@ static int sn9c102_init(struct sn9c102_device* cam)
1336 if (s->init) { 1359 if (s->init) {
1337 err = s->init(cam); 1360 err = s->init(cam);
1338 if (err) { 1361 if (err) {
1339 DBG(3, "Sensor initialization failed") 1362 DBG(3, "Sensor initialization failed");
1340 return err; 1363 return err;
1341 } 1364 }
1342 } 1365 }
@@ -1353,13 +1376,13 @@ static int sn9c102_init(struct sn9c102_device* cam)
1353 1376
1354 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X) 1377 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1355 DBG(3, "Compressed video format is active, quality %d", 1378 DBG(3, "Compressed video format is active, quality %d",
1356 cam->compression.quality) 1379 cam->compression.quality);
1357 else 1380 else
1358 DBG(3, "Uncompressed video format is active") 1381 DBG(3, "Uncompressed video format is active");
1359 1382
1360 if (s->set_crop) 1383 if (s->set_crop)
1361 if ((err = s->set_crop(cam, rect))) { 1384 if ((err = s->set_crop(cam, rect))) {
1362 DBG(3, "set_crop() failed") 1385 DBG(3, "set_crop() failed");
1363 return err; 1386 return err;
1364 } 1387 }
1365 1388
@@ -1372,11 +1395,11 @@ static int sn9c102_init(struct sn9c102_device* cam)
1372 err = s->set_ctrl(cam, &ctrl); 1395 err = s->set_ctrl(cam, &ctrl);
1373 if (err) { 1396 if (err) {
1374 DBG(3, "Set %s control failed", 1397 DBG(3, "Set %s control failed",
1375 s->qctrl[i].name) 1398 s->qctrl[i].name);
1376 return err; 1399 return err;
1377 } 1400 }
1378 DBG(3, "Image sensor supports '%s' control", 1401 DBG(3, "Image sensor supports '%s' control",
1379 s->qctrl[i].name) 1402 s->qctrl[i].name);
1380 } 1403 }
1381 } 1404 }
1382 1405
@@ -1392,7 +1415,7 @@ static int sn9c102_init(struct sn9c102_device* cam)
1392 cam->state |= DEV_INITIALIZED; 1415 cam->state |= DEV_INITIALIZED;
1393 } 1416 }
1394 1417
1395 DBG(2, "Initialization succeeded") 1418 DBG(2, "Initialization succeeded");
1396 return 0; 1419 return 0;
1397} 1420}
1398 1421
@@ -1401,7 +1424,7 @@ static void sn9c102_release_resources(struct sn9c102_device* cam)
1401{ 1424{
1402 down(&sn9c102_sysfs_lock); 1425 down(&sn9c102_sysfs_lock);
1403 1426
1404 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor) 1427 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1405 video_set_drvdata(cam->v4ldev, NULL); 1428 video_set_drvdata(cam->v4ldev, NULL);
1406 video_unregister_device(cam->v4ldev); 1429 video_unregister_device(cam->v4ldev);
1407 1430
@@ -1432,7 +1455,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1432 } 1455 }
1433 1456
1434 if (cam->users) { 1457 if (cam->users) {
1435 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor) 1458 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1436 if ((filp->f_flags & O_NONBLOCK) || 1459 if ((filp->f_flags & O_NONBLOCK) ||
1437 (filp->f_flags & O_NDELAY)) { 1460 (filp->f_flags & O_NDELAY)) {
1438 err = -EWOULDBLOCK; 1461 err = -EWOULDBLOCK;
@@ -1458,7 +1481,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1458 err = sn9c102_init(cam); 1481 err = sn9c102_init(cam);
1459 if (err) { 1482 if (err) {
1460 DBG(1, "Initialization failed again. " 1483 DBG(1, "Initialization failed again. "
1461 "I will retry on next open().") 1484 "I will retry on next open().");
1462 goto out; 1485 goto out;
1463 } 1486 }
1464 cam->state &= ~DEV_MISCONFIGURED; 1487 cam->state &= ~DEV_MISCONFIGURED;
@@ -1475,7 +1498,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1475 cam->frame_count = 0; 1498 cam->frame_count = 0;
1476 sn9c102_empty_framequeues(cam); 1499 sn9c102_empty_framequeues(cam);
1477 1500
1478 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor) 1501 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1479 1502
1480out: 1503out:
1481 up(&cam->dev_sem); 1504 up(&cam->dev_sem);
@@ -1504,7 +1527,7 @@ static int sn9c102_release(struct inode* inode, struct file* filp)
1504 cam->users--; 1527 cam->users--;
1505 wake_up_interruptible_nr(&cam->open, 1); 1528 wake_up_interruptible_nr(&cam->open, 1);
1506 1529
1507 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor) 1530 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1508 1531
1509 up(&cam->dev_sem); 1532 up(&cam->dev_sem);
1510 1533
@@ -1524,32 +1547,38 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1524 return -ERESTARTSYS; 1547 return -ERESTARTSYS;
1525 1548
1526 if (cam->state & DEV_DISCONNECTED) { 1549 if (cam->state & DEV_DISCONNECTED) {
1527 DBG(1, "Device not present") 1550 DBG(1, "Device not present");
1528 up(&cam->fileop_sem); 1551 up(&cam->fileop_sem);
1529 return -ENODEV; 1552 return -ENODEV;
1530 } 1553 }
1531 1554
1532 if (cam->state & DEV_MISCONFIGURED) { 1555 if (cam->state & DEV_MISCONFIGURED) {
1533 DBG(1, "The camera is misconfigured. Close and open it again.") 1556 DBG(1, "The camera is misconfigured. Close and open it "
1557 "again.");
1534 up(&cam->fileop_sem); 1558 up(&cam->fileop_sem);
1535 return -EIO; 1559 return -EIO;
1536 } 1560 }
1537 1561
1538 if (cam->io == IO_MMAP) { 1562 if (cam->io == IO_MMAP) {
1539 DBG(3, "Close and open the device again to choose " 1563 DBG(3, "Close and open the device again to choose "
1540 "the read method") 1564 "the read method");
1541 up(&cam->fileop_sem); 1565 up(&cam->fileop_sem);
1542 return -EINVAL; 1566 return -EINVAL;
1543 } 1567 }
1544 1568
1545 if (cam->io == IO_NONE) { 1569 if (cam->io == IO_NONE) {
1546 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) { 1570 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1547 DBG(1, "read() failed, not enough memory") 1571 DBG(1, "read() failed, not enough memory");
1548 up(&cam->fileop_sem); 1572 up(&cam->fileop_sem);
1549 return -ENOMEM; 1573 return -ENOMEM;
1550 } 1574 }
1551 cam->io = IO_READ; 1575 cam->io = IO_READ;
1552 cam->stream = STREAM_ON; 1576 cam->stream = STREAM_ON;
1577 }
1578
1579 if (list_empty(&cam->inqueue)) {
1580 if (!list_empty(&cam->outqueue))
1581 sn9c102_empty_framequeues(cam);
1553 sn9c102_queue_unusedframes(cam); 1582 sn9c102_queue_unusedframes(cam);
1554 } 1583 }
1555 1584
@@ -1584,6 +1613,16 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1584 1613
1585 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame); 1614 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1586 1615
1616 if (count > f->buf.bytesused)
1617 count = f->buf.bytesused;
1618
1619 if (copy_to_user(buf, f->bufmem, count)) {
1620 err = -EFAULT;
1621 goto exit;
1622 }
1623 *f_pos += count;
1624
1625exit:
1587 spin_lock_irqsave(&cam->queue_lock, lock_flags); 1626 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1588 list_for_each_entry(i, &cam->outqueue, frame) 1627 list_for_each_entry(i, &cam->outqueue, frame)
1589 i->state = F_UNUSED; 1628 i->state = F_UNUSED;
@@ -1592,16 +1631,8 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1592 1631
1593 sn9c102_queue_unusedframes(cam); 1632 sn9c102_queue_unusedframes(cam);
1594 1633
1595 if (count > f->buf.bytesused) 1634 PDBGG("Frame #%lu, bytes read: %zu",
1596 count = f->buf.bytesused; 1635 (unsigned long)f->buf.index, count);
1597
1598 if (copy_to_user(buf, f->bufmem, count)) {
1599 up(&cam->fileop_sem);
1600 return -EFAULT;
1601 }
1602 *f_pos += count;
1603
1604 PDBGG("Frame #%lu, bytes read: %zu", (unsigned long)f->buf.index,count)
1605 1636
1606 up(&cam->fileop_sem); 1637 up(&cam->fileop_sem);
1607 1638
@@ -1612,33 +1643,42 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1612static unsigned int sn9c102_poll(struct file *filp, poll_table *wait) 1643static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1613{ 1644{
1614 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1645 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1646 struct sn9c102_frame_t* f;
1647 unsigned long lock_flags;
1615 unsigned int mask = 0; 1648 unsigned int mask = 0;
1616 1649
1617 if (down_interruptible(&cam->fileop_sem)) 1650 if (down_interruptible(&cam->fileop_sem))
1618 return POLLERR; 1651 return POLLERR;
1619 1652
1620 if (cam->state & DEV_DISCONNECTED) { 1653 if (cam->state & DEV_DISCONNECTED) {
1621 DBG(1, "Device not present") 1654 DBG(1, "Device not present");
1622 goto error; 1655 goto error;
1623 } 1656 }
1624 1657
1625 if (cam->state & DEV_MISCONFIGURED) { 1658 if (cam->state & DEV_MISCONFIGURED) {
1626 DBG(1, "The camera is misconfigured. Close and open it again.") 1659 DBG(1, "The camera is misconfigured. Close and open it "
1660 "again.");
1627 goto error; 1661 goto error;
1628 } 1662 }
1629 1663
1630 if (cam->io == IO_NONE) { 1664 if (cam->io == IO_NONE) {
1631 if (!sn9c102_request_buffers(cam, cam->nreadbuffers, 1665 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1632 IO_READ)) { 1666 IO_READ)) {
1633 DBG(1, "poll() failed, not enough memory") 1667 DBG(1, "poll() failed, not enough memory");
1634 goto error; 1668 goto error;
1635 } 1669 }
1636 cam->io = IO_READ; 1670 cam->io = IO_READ;
1637 cam->stream = STREAM_ON; 1671 cam->stream = STREAM_ON;
1638 } 1672 }
1639 1673
1640 if (cam->io == IO_READ) 1674 if (cam->io == IO_READ) {
1675 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1676 list_for_each_entry(f, &cam->outqueue, frame)
1677 f->state = F_UNUSED;
1678 INIT_LIST_HEAD(&cam->outqueue);
1679 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1641 sn9c102_queue_unusedframes(cam); 1680 sn9c102_queue_unusedframes(cam);
1681 }
1642 1682
1643 poll_wait(filp, &cam->wait_frame, wait); 1683 poll_wait(filp, &cam->wait_frame, wait);
1644 1684
@@ -1680,22 +1720,22 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1680{ 1720{
1681 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1721 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1682 unsigned long size = vma->vm_end - vma->vm_start, 1722 unsigned long size = vma->vm_end - vma->vm_start,
1683 start = vma->vm_start, 1723 start = vma->vm_start;
1684 pos, 1724 void *pos;
1685 page;
1686 u32 i; 1725 u32 i;
1687 1726
1688 if (down_interruptible(&cam->fileop_sem)) 1727 if (down_interruptible(&cam->fileop_sem))
1689 return -ERESTARTSYS; 1728 return -ERESTARTSYS;
1690 1729
1691 if (cam->state & DEV_DISCONNECTED) { 1730 if (cam->state & DEV_DISCONNECTED) {
1692 DBG(1, "Device not present") 1731 DBG(1, "Device not present");
1693 up(&cam->fileop_sem); 1732 up(&cam->fileop_sem);
1694 return -ENODEV; 1733 return -ENODEV;
1695 } 1734 }
1696 1735
1697 if (cam->state & DEV_MISCONFIGURED) { 1736 if (cam->state & DEV_MISCONFIGURED) {
1698 DBG(1, "The camera is misconfigured. Close and open it again.") 1737 DBG(1, "The camera is misconfigured. Close and open it "
1738 "again.");
1699 up(&cam->fileop_sem); 1739 up(&cam->fileop_sem);
1700 return -EIO; 1740 return -EIO;
1701 } 1741 }
@@ -1715,15 +1755,12 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1715 return -EINVAL; 1755 return -EINVAL;
1716 } 1756 }
1717 1757
1718 /* VM_IO is eventually going to replace PageReserved altogether */
1719 vma->vm_flags |= VM_IO; 1758 vma->vm_flags |= VM_IO;
1720 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ 1759 vma->vm_flags |= VM_RESERVED;
1721 1760
1722 pos = (unsigned long)cam->frame[i].bufmem; 1761 pos = cam->frame[i].bufmem;
1723 while (size > 0) { /* size is page-aligned */ 1762 while (size > 0) { /* size is page-aligned */
1724 page = vmalloc_to_pfn((void *)pos); 1763 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1725 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
1726 vma->vm_page_prot)) {
1727 up(&cam->fileop_sem); 1764 up(&cam->fileop_sem);
1728 return -EAGAIN; 1765 return -EAGAIN;
1729 } 1766 }
@@ -1742,738 +1779,861 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1742 return 0; 1779 return 0;
1743} 1780}
1744 1781
1782/*****************************************************************************/
1745 1783
1746static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp, 1784static int
1747 unsigned int cmd, void __user * arg) 1785sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
1748{ 1786{
1749 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1787 struct v4l2_capability cap = {
1788 .driver = "sn9c102",
1789 .version = SN9C102_MODULE_VERSION_CODE,
1790 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1791 V4L2_CAP_STREAMING,
1792 };
1793
1794 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1795 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1796 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1797 sizeof(cap.bus_info));
1798
1799 if (copy_to_user(arg, &cap, sizeof(cap)))
1800 return -EFAULT;
1750 1801
1751 switch (cmd) { 1802 return 0;
1803}
1752 1804
1753 case VIDIOC_QUERYCAP:
1754 {
1755 struct v4l2_capability cap = {
1756 .driver = "sn9c102",
1757 .version = SN9C102_MODULE_VERSION_CODE,
1758 .capabilities = V4L2_CAP_VIDEO_CAPTURE |
1759 V4L2_CAP_READWRITE |
1760 V4L2_CAP_STREAMING,
1761 };
1762
1763 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1764 if (usb_make_path(cam->usbdev, cap.bus_info,
1765 sizeof(cap.bus_info)) < 0)
1766 strlcpy(cap.bus_info, cam->dev.bus_id,
1767 sizeof(cap.bus_info));
1768
1769 if (copy_to_user(arg, &cap, sizeof(cap)))
1770 return -EFAULT;
1771 1805
1772 return 0; 1806static int
1773 } 1807sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1808{
1809 struct v4l2_input i;
1774 1810
1775 case VIDIOC_ENUMINPUT: 1811 if (copy_from_user(&i, arg, sizeof(i)))
1776 { 1812 return -EFAULT;
1777 struct v4l2_input i;
1778 1813
1779 if (copy_from_user(&i, arg, sizeof(i))) 1814 if (i.index)
1780 return -EFAULT; 1815 return -EINVAL;
1781 1816
1782 if (i.index) 1817 memset(&i, 0, sizeof(i));
1783 return -EINVAL; 1818 strcpy(i.name, "Camera");
1784 1819
1785 memset(&i, 0, sizeof(i)); 1820 if (copy_to_user(arg, &i, sizeof(i)))
1786 strcpy(i.name, "USB"); 1821 return -EFAULT;
1787 1822
1788 if (copy_to_user(arg, &i, sizeof(i))) 1823 return 0;
1789 return -EFAULT; 1824}
1790 1825
1791 return 0;
1792 }
1793 1826
1794 case VIDIOC_G_INPUT: 1827static int
1795 case VIDIOC_S_INPUT: 1828sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg)
1796 { 1829{
1797 int index; 1830 int index;
1798 1831
1799 if (copy_from_user(&index, arg, sizeof(index))) 1832 if (copy_from_user(&index, arg, sizeof(index)))
1800 return -EFAULT; 1833 return -EFAULT;
1801 1834
1802 if (index != 0) 1835 if (index != 0)
1803 return -EINVAL; 1836 return -EINVAL;
1804 1837
1805 return 0; 1838 return 0;
1806 } 1839}
1807 1840
1808 case VIDIOC_QUERYCTRL:
1809 {
1810 struct sn9c102_sensor* s = cam->sensor;
1811 struct v4l2_queryctrl qc;
1812 u8 i;
1813 1841
1814 if (copy_from_user(&qc, arg, sizeof(qc))) 1842static int
1815 return -EFAULT; 1843sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1844{
1845 struct sn9c102_sensor* s = cam->sensor;
1846 struct v4l2_queryctrl qc;
1847 u8 i;
1816 1848
1849 if (copy_from_user(&qc, arg, sizeof(qc)))
1850 return -EFAULT;
1851
1852 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1853 if (qc.id && qc.id == s->qctrl[i].id) {
1854 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1855 if (copy_to_user(arg, &qc, sizeof(qc)))
1856 return -EFAULT;
1857 return 0;
1858 }
1859
1860 return -EINVAL;
1861}
1862
1863
1864static int
1865sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1866{
1867 struct sn9c102_sensor* s = cam->sensor;
1868 struct v4l2_control ctrl;
1869 int err = 0;
1870 u8 i;
1871
1872 if (!s->get_ctrl && !s->set_ctrl)
1873 return -EINVAL;
1874
1875 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1876 return -EFAULT;
1877
1878 if (!s->get_ctrl) {
1817 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) 1879 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1818 if (qc.id && qc.id == s->qctrl[i].id) { 1880 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
1819 memcpy(&qc, &(s->qctrl[i]), sizeof(qc)); 1881 ctrl.value = s->_qctrl[i].default_value;
1820 if (copy_to_user(arg, &qc, sizeof(qc))) 1882 goto exit;
1821 return -EFAULT;
1822 return 0;
1823 } 1883 }
1824
1825 return -EINVAL; 1884 return -EINVAL;
1826 } 1885 } else
1886 err = s->get_ctrl(cam, &ctrl);
1827 1887
1828 case VIDIOC_G_CTRL: 1888exit:
1829 { 1889 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1830 struct sn9c102_sensor* s = cam->sensor; 1890 return -EFAULT;
1831 struct v4l2_control ctrl;
1832 int err = 0;
1833 1891
1834 if (!s->get_ctrl) 1892 return err;
1835 return -EINVAL; 1893}
1836 1894
1837 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1838 return -EFAULT;
1839 1895
1840 err = s->get_ctrl(cam, &ctrl); 1896static int
1897sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1898{
1899 struct sn9c102_sensor* s = cam->sensor;
1900 struct v4l2_control ctrl;
1901 u8 i;
1902 int err = 0;
1841 1903
1842 if (copy_to_user(arg, &ctrl, sizeof(ctrl))) 1904 if (!s->set_ctrl)
1843 return -EFAULT; 1905 return -EINVAL;
1906
1907 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1908 return -EFAULT;
1844 1909
1910 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1911 if (ctrl.id == s->qctrl[i].id) {
1912 if (ctrl.value < s->qctrl[i].minimum ||
1913 ctrl.value > s->qctrl[i].maximum)
1914 return -ERANGE;
1915 ctrl.value -= ctrl.value % s->qctrl[i].step;
1916 break;
1917 }
1918
1919 if ((err = s->set_ctrl(cam, &ctrl)))
1845 return err; 1920 return err;
1846 }
1847 1921
1848 case VIDIOC_S_CTRL_OLD: 1922 s->_qctrl[i].default_value = ctrl.value;
1849 case VIDIOC_S_CTRL:
1850 {
1851 struct sn9c102_sensor* s = cam->sensor;
1852 struct v4l2_control ctrl;
1853 u8 i;
1854 int err = 0;
1855 1923
1856 if (!s->set_ctrl) 1924 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1857 return -EINVAL; 1925 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
1858 1926
1859 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 1927 return 0;
1860 return -EFAULT; 1928}
1861 1929
1862 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) 1930
1863 if (ctrl.id == s->qctrl[i].id) { 1931static int
1864 if (ctrl.value < s->qctrl[i].minimum || 1932sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1865 ctrl.value > s->qctrl[i].maximum) 1933{
1866 return -ERANGE; 1934 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1867 ctrl.value -= ctrl.value % s->qctrl[i].step; 1935
1868 break; 1936 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1937 cc->pixelaspect.numerator = 1;
1938 cc->pixelaspect.denominator = 1;
1939
1940 if (copy_to_user(arg, cc, sizeof(*cc)))
1941 return -EFAULT;
1942
1943 return 0;
1944}
1945
1946
1947static int
1948sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1949{
1950 struct sn9c102_sensor* s = cam->sensor;
1951 struct v4l2_crop crop = {
1952 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1953 };
1954
1955 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1956
1957 if (copy_to_user(arg, &crop, sizeof(crop)))
1958 return -EFAULT;
1959
1960 return 0;
1961}
1962
1963
1964static int
1965sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
1966{
1967 struct sn9c102_sensor* s = cam->sensor;
1968 struct v4l2_crop crop;
1969 struct v4l2_rect* rect;
1970 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1971 struct v4l2_pix_format* pix_format = &(s->pix_format);
1972 u8 scale;
1973 const enum sn9c102_stream_state stream = cam->stream;
1974 const u32 nbuffers = cam->nbuffers;
1975 u32 i;
1976 int err = 0;
1977
1978 if (copy_from_user(&crop, arg, sizeof(crop)))
1979 return -EFAULT;
1980
1981 rect = &(crop.c);
1982
1983 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1984 return -EINVAL;
1985
1986 if (cam->module_param.force_munmap)
1987 for (i = 0; i < cam->nbuffers; i++)
1988 if (cam->frame[i].vma_use_count) {
1989 DBG(3, "VIDIOC_S_CROP failed. "
1990 "Unmap the buffers first.");
1991 return -EINVAL;
1869 } 1992 }
1870 1993
1871 if ((err = s->set_ctrl(cam, &ctrl))) 1994 /* Preserve R,G or B origin */
1995 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1996 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1997
1998 if (rect->width < 16)
1999 rect->width = 16;
2000 if (rect->height < 16)
2001 rect->height = 16;
2002 if (rect->width > bounds->width)
2003 rect->width = bounds->width;
2004 if (rect->height > bounds->height)
2005 rect->height = bounds->height;
2006 if (rect->left < bounds->left)
2007 rect->left = bounds->left;
2008 if (rect->top < bounds->top)
2009 rect->top = bounds->top;
2010 if (rect->left + rect->width > bounds->left + bounds->width)
2011 rect->left = bounds->left+bounds->width - rect->width;
2012 if (rect->top + rect->height > bounds->top + bounds->height)
2013 rect->top = bounds->top+bounds->height - rect->height;
2014
2015 rect->width &= ~15L;
2016 rect->height &= ~15L;
2017
2018 if (SN9C102_PRESERVE_IMGSCALE) {
2019 /* Calculate the actual scaling factor */
2020 u32 a, b;
2021 a = rect->width * rect->height;
2022 b = pix_format->width * pix_format->height;
2023 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2024 } else
2025 scale = 1;
2026
2027 if (cam->stream == STREAM_ON)
2028 if ((err = sn9c102_stream_interrupt(cam)))
1872 return err; 2029 return err;
1873 2030
1874 s->_qctrl[i].default_value = ctrl.value; 2031 if (copy_to_user(arg, &crop, sizeof(crop))) {
2032 cam->stream = stream;
2033 return -EFAULT;
2034 }
2035
2036 if (cam->module_param.force_munmap || cam->io == IO_READ)
2037 sn9c102_release_buffers(cam);
1875 2038
1876 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu", 2039 err = sn9c102_set_crop(cam, rect);
1877 (unsigned long)ctrl.id, (unsigned long)ctrl.value) 2040 if (s->set_crop)
2041 err += s->set_crop(cam, rect);
2042 err += sn9c102_set_scale(cam, scale);
1878 2043
1879 return 0; 2044 if (err) { /* atomic, no rollback in ioctl() */
2045 cam->state |= DEV_MISCONFIGURED;
2046 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2047 "use the camera, close and open /dev/video%d again.",
2048 cam->v4ldev->minor);
2049 return -EIO;
1880 } 2050 }
1881 2051
1882 case VIDIOC_CROPCAP: 2052 s->pix_format.width = rect->width/scale;
1883 { 2053 s->pix_format.height = rect->height/scale;
1884 struct v4l2_cropcap* cc = &(cam->sensor->cropcap); 2054 memcpy(&(s->_rect), rect, sizeof(*rect));
1885 2055
1886 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2056 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1887 cc->pixelaspect.numerator = 1; 2057 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
1888 cc->pixelaspect.denominator = 1; 2058 cam->state |= DEV_MISCONFIGURED;
2059 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2060 "use the camera, close and open /dev/video%d again.",
2061 cam->v4ldev->minor);
2062 return -ENOMEM;
2063 }
1889 2064
1890 if (copy_to_user(arg, cc, sizeof(*cc))) 2065 if (cam->io == IO_READ)
1891 return -EFAULT; 2066 sn9c102_empty_framequeues(cam);
2067 else if (cam->module_param.force_munmap)
2068 sn9c102_requeue_outqueue(cam);
1892 2069
1893 return 0; 2070 cam->stream = stream;
1894 }
1895 2071
1896 case VIDIOC_G_CROP: 2072 return 0;
1897 { 2073}
1898 struct sn9c102_sensor* s = cam->sensor;
1899 struct v4l2_crop crop = {
1900 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1901 };
1902 2074
1903 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1904 2075
1905 if (copy_to_user(arg, &crop, sizeof(crop))) 2076static int
1906 return -EFAULT; 2077sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2078{
2079 struct v4l2_fmtdesc fmtd;
1907 2080
1908 return 0; 2081 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1909 } 2082 return -EFAULT;
1910 2083
1911 case VIDIOC_S_CROP: 2084 if (fmtd.index == 0) {
1912 { 2085 strcpy(fmtd.description, "bayer rgb");
1913 struct sn9c102_sensor* s = cam->sensor; 2086 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1914 struct v4l2_crop crop; 2087 } else if (fmtd.index == 1) {
1915 struct v4l2_rect* rect; 2088 strcpy(fmtd.description, "compressed");
1916 struct v4l2_rect* bounds = &(s->cropcap.bounds); 2089 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
1917 struct v4l2_pix_format* pix_format = &(s->pix_format); 2090 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1918 u8 scale; 2091 } else
1919 const enum sn9c102_stream_state stream = cam->stream; 2092 return -EINVAL;
1920 const u32 nbuffers = cam->nbuffers;
1921 u32 i;
1922 int err = 0;
1923
1924 if (copy_from_user(&crop, arg, sizeof(crop)))
1925 return -EFAULT;
1926 2093
1927 rect = &(crop.c); 2094 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2095 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1928 2096
1929 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2097 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1930 return -EINVAL; 2098 return -EFAULT;
1931 2099
1932 if (cam->module_param.force_munmap) 2100 return 0;
1933 for (i = 0; i < cam->nbuffers; i++) 2101}
1934 if (cam->frame[i].vma_use_count) {
1935 DBG(3, "VIDIOC_S_CROP failed. "
1936 "Unmap the buffers first.")
1937 return -EINVAL;
1938 }
1939 2102
1940 /* Preserve R,G or B origin */
1941 rect->left = (s->_rect.left & 1L) ?
1942 rect->left | 1L : rect->left & ~1L;
1943 rect->top = (s->_rect.top & 1L) ?
1944 rect->top | 1L : rect->top & ~1L;
1945
1946 if (rect->width < 16)
1947 rect->width = 16;
1948 if (rect->height < 16)
1949 rect->height = 16;
1950 if (rect->width > bounds->width)
1951 rect->width = bounds->width;
1952 if (rect->height > bounds->height)
1953 rect->height = bounds->height;
1954 if (rect->left < bounds->left)
1955 rect->left = bounds->left;
1956 if (rect->top < bounds->top)
1957 rect->top = bounds->top;
1958 if (rect->left + rect->width > bounds->left + bounds->width)
1959 rect->left = bounds->left+bounds->width - rect->width;
1960 if (rect->top + rect->height > bounds->top + bounds->height)
1961 rect->top = bounds->top+bounds->height - rect->height;
1962
1963 rect->width &= ~15L;
1964 rect->height &= ~15L;
1965
1966 if (SN9C102_PRESERVE_IMGSCALE) {
1967 /* Calculate the actual scaling factor */
1968 u32 a, b;
1969 a = rect->width * rect->height;
1970 b = pix_format->width * pix_format->height;
1971 scale = b ? (u8)((a / b) < 4 ? 1 :
1972 ((a / b) < 16 ? 2 : 4)) : 1;
1973 } else
1974 scale = 1;
1975
1976 if (cam->stream == STREAM_ON)
1977 if ((err = sn9c102_stream_interrupt(cam)))
1978 return err;
1979
1980 if (copy_to_user(arg, &crop, sizeof(crop))) {
1981 cam->stream = stream;
1982 return -EFAULT;
1983 }
1984 2103
1985 if (cam->module_param.force_munmap || cam->io == IO_READ) 2104static int
1986 sn9c102_release_buffers(cam); 2105sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2106{
2107 struct v4l2_format format;
2108 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
1987 2109
1988 err = sn9c102_set_crop(cam, rect); 2110 if (copy_from_user(&format, arg, sizeof(format)))
1989 if (s->set_crop) 2111 return -EFAULT;
1990 err += s->set_crop(cam, rect);
1991 err += sn9c102_set_scale(cam, scale);
1992 2112
1993 if (err) { /* atomic, no rollback in ioctl() */ 2113 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1994 cam->state |= DEV_MISCONFIGURED; 2114 return -EINVAL;
1995 DBG(1, "VIDIOC_S_CROP failed because of hardware "
1996 "problems. To use the camera, close and open "
1997 "/dev/video%d again.", cam->v4ldev->minor)
1998 return -EIO;
1999 }
2000 2115
2001 s->pix_format.width = rect->width/scale; 2116 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
2002 s->pix_format.height = rect->height/scale; 2117 ? 0 : (pfmt->width * pfmt->priv) / 8;
2003 memcpy(&(s->_rect), rect, sizeof(*rect)); 2118 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2004 2119 pfmt->field = V4L2_FIELD_NONE;
2005 if ((cam->module_param.force_munmap || cam->io == IO_READ) && 2120 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2006 nbuffers != sn9c102_request_buffers(cam, nbuffers,
2007 cam->io)) {
2008 cam->state |= DEV_MISCONFIGURED;
2009 DBG(1, "VIDIOC_S_CROP failed because of not enough "
2010 "memory. To use the camera, close and open "
2011 "/dev/video%d again.", cam->v4ldev->minor)
2012 return -ENOMEM;
2013 }
2014 2121
2015 cam->stream = stream; 2122 if (copy_to_user(arg, &format, sizeof(format)))
2123 return -EFAULT;
2016 2124
2017 return 0; 2125 return 0;
2018 } 2126}
2019 2127
2020 case VIDIOC_ENUM_FMT:
2021 {
2022 struct v4l2_fmtdesc fmtd;
2023 2128
2024 if (copy_from_user(&fmtd, arg, sizeof(fmtd))) 2129static int
2025 return -EFAULT; 2130sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2131 void __user * arg)
2132{
2133 struct sn9c102_sensor* s = cam->sensor;
2134 struct v4l2_format format;
2135 struct v4l2_pix_format* pix;
2136 struct v4l2_pix_format* pfmt = &(s->pix_format);
2137 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2138 struct v4l2_rect rect;
2139 u8 scale;
2140 const enum sn9c102_stream_state stream = cam->stream;
2141 const u32 nbuffers = cam->nbuffers;
2142 u32 i;
2143 int err = 0;
2026 2144
2027 if (fmtd.index == 0) { 2145 if (copy_from_user(&format, arg, sizeof(format)))
2028 strcpy(fmtd.description, "bayer rgb"); 2146 return -EFAULT;
2029 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2030 } else if (fmtd.index == 1) {
2031 strcpy(fmtd.description, "compressed");
2032 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2033 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2034 } else
2035 return -EINVAL;
2036 2147
2037 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2148 pix = &(format.fmt.pix);
2038 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2039 2149
2040 if (copy_to_user(arg, &fmtd, sizeof(fmtd))) 2150 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2041 return -EFAULT; 2151 return -EINVAL;
2042 2152
2043 return 0; 2153 memcpy(&rect, &(s->_rect), sizeof(rect));
2154
2155 { /* calculate the actual scaling factor */
2156 u32 a, b;
2157 a = rect.width * rect.height;
2158 b = pix->width * pix->height;
2159 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2044 } 2160 }
2045 2161
2046 case VIDIOC_G_FMT: 2162 rect.width = scale * pix->width;
2047 { 2163 rect.height = scale * pix->height;
2048 struct v4l2_format format;
2049 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
2050 2164
2051 if (copy_from_user(&format, arg, sizeof(format))) 2165 if (rect.width < 16)
2052 return -EFAULT; 2166 rect.width = 16;
2167 if (rect.height < 16)
2168 rect.height = 16;
2169 if (rect.width > bounds->left + bounds->width - rect.left)
2170 rect.width = bounds->left + bounds->width - rect.left;
2171 if (rect.height > bounds->top + bounds->height - rect.top)
2172 rect.height = bounds->top + bounds->height - rect.top;
2053 2173
2054 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2174 rect.width &= ~15L;
2055 return -EINVAL; 2175 rect.height &= ~15L;
2176
2177 { /* adjust the scaling factor */
2178 u32 a, b;
2179 a = rect.width * rect.height;
2180 b = pix->width * pix->height;
2181 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2182 }
2056 2183
2057 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X) 2184 pix->width = rect.width / scale;
2058 ? 0 : (pfmt->width * pfmt->priv) / 8; 2185 pix->height = rect.height / scale;
2059 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2060 pfmt->field = V4L2_FIELD_NONE;
2061 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2062 2186
2187 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2188 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2189 pix->pixelformat = pfmt->pixelformat;
2190 pix->priv = pfmt->priv; /* bpp */
2191 pix->colorspace = pfmt->colorspace;
2192 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2193 ? 0 : (pix->width * pix->priv) / 8;
2194 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2195 pix->field = V4L2_FIELD_NONE;
2196
2197 if (cmd == VIDIOC_TRY_FMT) {
2063 if (copy_to_user(arg, &format, sizeof(format))) 2198 if (copy_to_user(arg, &format, sizeof(format)))
2064 return -EFAULT; 2199 return -EFAULT;
2065
2066 return 0; 2200 return 0;
2067 } 2201 }
2068 2202
2069 case VIDIOC_TRY_FMT: 2203 if (cam->module_param.force_munmap)
2070 case VIDIOC_S_FMT: 2204 for (i = 0; i < cam->nbuffers; i++)
2071 { 2205 if (cam->frame[i].vma_use_count) {
2072 struct sn9c102_sensor* s = cam->sensor; 2206 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2073 struct v4l2_format format; 2207 "buffers first.");
2074 struct v4l2_pix_format* pix; 2208 return -EINVAL;
2075 struct v4l2_pix_format* pfmt = &(s->pix_format); 2209 }
2076 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2077 struct v4l2_rect rect;
2078 u8 scale;
2079 const enum sn9c102_stream_state stream = cam->stream;
2080 const u32 nbuffers = cam->nbuffers;
2081 u32 i;
2082 int err = 0;
2083
2084 if (copy_from_user(&format, arg, sizeof(format)))
2085 return -EFAULT;
2086 2210
2087 pix = &(format.fmt.pix); 2211 if (cam->stream == STREAM_ON)
2212 if ((err = sn9c102_stream_interrupt(cam)))
2213 return err;
2088 2214
2089 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2215 if (copy_to_user(arg, &format, sizeof(format))) {
2090 return -EINVAL; 2216 cam->stream = stream;
2217 return -EFAULT;
2218 }
2091 2219
2092 memcpy(&rect, &(s->_rect), sizeof(rect)); 2220 if (cam->module_param.force_munmap || cam->io == IO_READ)
2221 sn9c102_release_buffers(cam);
2093 2222
2094 { /* calculate the actual scaling factor */ 2223 err += sn9c102_set_pix_format(cam, pix);
2095 u32 a, b; 2224 err += sn9c102_set_crop(cam, &rect);
2096 a = rect.width * rect.height; 2225 if (s->set_pix_format)
2097 b = pix->width * pix->height; 2226 err += s->set_pix_format(cam, pix);
2098 scale = b ? (u8)((a / b) < 4 ? 1 : 2227 if (s->set_crop)
2099 ((a / b) < 16 ? 2 : 4)) : 1; 2228 err += s->set_crop(cam, &rect);
2100 } 2229 err += sn9c102_set_scale(cam, scale);
2101 2230
2102 rect.width = scale * pix->width; 2231 if (err) { /* atomic, no rollback in ioctl() */
2103 rect.height = scale * pix->height; 2232 cam->state |= DEV_MISCONFIGURED;
2104 2233 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2105 if (rect.width < 16) 2234 "use the camera, close and open /dev/video%d again.",
2106 rect.width = 16; 2235 cam->v4ldev->minor);
2107 if (rect.height < 16) 2236 return -EIO;
2108 rect.height = 16; 2237 }
2109 if (rect.width > bounds->left + bounds->width - rect.left)
2110 rect.width = bounds->left + bounds->width - rect.left;
2111 if (rect.height > bounds->top + bounds->height - rect.top)
2112 rect.height = bounds->top + bounds->height - rect.top;
2113
2114 rect.width &= ~15L;
2115 rect.height &= ~15L;
2116
2117 { /* adjust the scaling factor */
2118 u32 a, b;
2119 a = rect.width * rect.height;
2120 b = pix->width * pix->height;
2121 scale = b ? (u8)((a / b) < 4 ? 1 :
2122 ((a / b) < 16 ? 2 : 4)) : 1;
2123 }
2124 2238
2125 pix->width = rect.width / scale; 2239 memcpy(pfmt, pix, sizeof(*pix));
2126 pix->height = rect.height / scale; 2240 memcpy(&(s->_rect), &rect, sizeof(rect));
2127
2128 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2129 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2130 pix->pixelformat = pfmt->pixelformat;
2131 pix->priv = pfmt->priv; /* bpp */
2132 pix->colorspace = pfmt->colorspace;
2133 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2134 ? 0 : (pix->width * pix->priv) / 8;
2135 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2136 pix->field = V4L2_FIELD_NONE;
2137
2138 if (cmd == VIDIOC_TRY_FMT) {
2139 if (copy_to_user(arg, &format, sizeof(format)))
2140 return -EFAULT;
2141 return 0;
2142 }
2143 2241
2144 if (cam->module_param.force_munmap) 2242 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2145 for (i = 0; i < cam->nbuffers; i++) 2243 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2146 if (cam->frame[i].vma_use_count) { 2244 cam->state |= DEV_MISCONFIGURED;
2147 DBG(3, "VIDIOC_S_FMT failed. " 2245 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2148 "Unmap the buffers first.") 2246 "use the camera, close and open /dev/video%d again.",
2149 return -EINVAL; 2247 cam->v4ldev->minor);
2150 } 2248 return -ENOMEM;
2249 }
2151 2250
2152 if (cam->stream == STREAM_ON) 2251 if (cam->io == IO_READ)
2153 if ((err = sn9c102_stream_interrupt(cam))) 2252 sn9c102_empty_framequeues(cam);
2154 return err; 2253 else if (cam->module_param.force_munmap)
2254 sn9c102_requeue_outqueue(cam);
2155 2255
2156 if (copy_to_user(arg, &format, sizeof(format))) { 2256 cam->stream = stream;
2157 cam->stream = stream;
2158 return -EFAULT;
2159 }
2160 2257
2161 if (cam->module_param.force_munmap || cam->io == IO_READ) 2258 return 0;
2162 sn9c102_release_buffers(cam); 2259}
2163
2164 err += sn9c102_set_pix_format(cam, pix);
2165 err += sn9c102_set_crop(cam, &rect);
2166 if (s->set_pix_format)
2167 err += s->set_pix_format(cam, pix);
2168 if (s->set_crop)
2169 err += s->set_crop(cam, &rect);
2170 err += sn9c102_set_scale(cam, scale);
2171
2172 if (err) { /* atomic, no rollback in ioctl() */
2173 cam->state |= DEV_MISCONFIGURED;
2174 DBG(1, "VIDIOC_S_FMT failed because of hardware "
2175 "problems. To use the camera, close and open "
2176 "/dev/video%d again.", cam->v4ldev->minor)
2177 return -EIO;
2178 }
2179 2260
2180 memcpy(pfmt, pix, sizeof(*pix));
2181 memcpy(&(s->_rect), &rect, sizeof(rect));
2182 2261
2183 if ((cam->module_param.force_munmap || cam->io == IO_READ) && 2262static int
2184 nbuffers != sn9c102_request_buffers(cam, nbuffers, 2263sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2185 cam->io)) { 2264{
2186 cam->state |= DEV_MISCONFIGURED; 2265 if (copy_to_user(arg, &cam->compression,
2187 DBG(1, "VIDIOC_S_FMT failed because of not enough " 2266 sizeof(cam->compression)))
2188 "memory. To use the camera, close and open " 2267 return -EFAULT;
2189 "/dev/video%d again.", cam->v4ldev->minor)
2190 return -ENOMEM;
2191 }
2192 2268
2193 cam->stream = stream; 2269 return 0;
2270}
2194 2271
2195 return 0;
2196 }
2197 2272
2198 case VIDIOC_G_JPEGCOMP: 2273static int
2199 { 2274sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2200 if (copy_to_user(arg, &cam->compression, 2275{
2201 sizeof(cam->compression))) 2276 struct v4l2_jpegcompression jc;
2202 return -EFAULT; 2277 const enum sn9c102_stream_state stream = cam->stream;
2278 int err = 0;
2203 2279
2204 return 0; 2280 if (copy_from_user(&jc, arg, sizeof(jc)))
2205 } 2281 return -EFAULT;
2206 2282
2207 case VIDIOC_S_JPEGCOMP: 2283 if (jc.quality != 0 && jc.quality != 1)
2208 { 2284 return -EINVAL;
2209 struct v4l2_jpegcompression jc;
2210 const enum sn9c102_stream_state stream = cam->stream;
2211 int err = 0;
2212 2285
2213 if (copy_from_user(&jc, arg, sizeof(jc))) 2286 if (cam->stream == STREAM_ON)
2214 return -EFAULT; 2287 if ((err = sn9c102_stream_interrupt(cam)))
2288 return err;
2215 2289
2216 if (jc.quality != 0 && jc.quality != 1) 2290 err += sn9c102_set_compression(cam, &jc);
2217 return -EINVAL; 2291 if (err) { /* atomic, no rollback in ioctl() */
2292 cam->state |= DEV_MISCONFIGURED;
2293 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2294 "problems. To use the camera, close and open "
2295 "/dev/video%d again.", cam->v4ldev->minor);
2296 return -EIO;
2297 }
2218 2298
2219 if (cam->stream == STREAM_ON) 2299 cam->compression.quality = jc.quality;
2220 if ((err = sn9c102_stream_interrupt(cam)))
2221 return err;
2222 2300
2223 err += sn9c102_set_compression(cam, &jc); 2301 cam->stream = stream;
2224 if (err) { /* atomic, no rollback in ioctl() */
2225 cam->state |= DEV_MISCONFIGURED;
2226 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2227 "problems. To use the camera, close and open "
2228 "/dev/video%d again.", cam->v4ldev->minor)
2229 return -EIO;
2230 }
2231 2302
2232 cam->compression.quality = jc.quality; 2303 return 0;
2304}
2233 2305
2234 cam->stream = stream;
2235 2306
2236 return 0; 2307static int
2237 } 2308sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2309{
2310 struct v4l2_requestbuffers rb;
2311 u32 i;
2312 int err;
2238 2313
2239 case VIDIOC_REQBUFS: 2314 if (copy_from_user(&rb, arg, sizeof(rb)))
2240 { 2315 return -EFAULT;
2241 struct v4l2_requestbuffers rb;
2242 u32 i;
2243 int err;
2244 2316
2245 if (copy_from_user(&rb, arg, sizeof(rb))) 2317 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2246 return -EFAULT; 2318 rb.memory != V4L2_MEMORY_MMAP)
2319 return -EINVAL;
2247 2320
2248 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 2321 if (cam->io == IO_READ) {
2249 rb.memory != V4L2_MEMORY_MMAP) 2322 DBG(3, "Close and open the device again to choose the mmap "
2250 return -EINVAL; 2323 "I/O method");
2324 return -EINVAL;
2325 }
2251 2326
2252 if (cam->io == IO_READ) { 2327 for (i = 0; i < cam->nbuffers; i++)
2253 DBG(3, "Close and open the device again to choose " 2328 if (cam->frame[i].vma_use_count) {
2254 "the mmap I/O method") 2329 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2330 "still mapped.");
2255 return -EINVAL; 2331 return -EINVAL;
2256 } 2332 }
2257 2333
2258 for (i = 0; i < cam->nbuffers; i++) 2334 if (cam->stream == STREAM_ON)
2259 if (cam->frame[i].vma_use_count) { 2335 if ((err = sn9c102_stream_interrupt(cam)))
2260 DBG(3, "VIDIOC_REQBUFS failed. " 2336 return err;
2261 "Previous buffers are still mapped.")
2262 return -EINVAL;
2263 }
2264 2337
2265 if (cam->stream == STREAM_ON) 2338 sn9c102_empty_framequeues(cam);
2266 if ((err = sn9c102_stream_interrupt(cam)))
2267 return err;
2268 2339
2269 sn9c102_empty_framequeues(cam); 2340 sn9c102_release_buffers(cam);
2341 if (rb.count)
2342 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2270 2343
2344 if (copy_to_user(arg, &rb, sizeof(rb))) {
2271 sn9c102_release_buffers(cam); 2345 sn9c102_release_buffers(cam);
2272 if (rb.count) 2346 cam->io = IO_NONE;
2273 rb.count = sn9c102_request_buffers(cam, rb.count, 2347 return -EFAULT;
2274 IO_MMAP); 2348 }
2275 2349
2276 if (copy_to_user(arg, &rb, sizeof(rb))) { 2350 cam->io = rb.count ? IO_MMAP : IO_NONE;
2277 sn9c102_release_buffers(cam);
2278 cam->io = IO_NONE;
2279 return -EFAULT;
2280 }
2281 2351
2282 cam->io = rb.count ? IO_MMAP : IO_NONE; 2352 return 0;
2353}
2283 2354
2284 return 0;
2285 }
2286 2355
2287 case VIDIOC_QUERYBUF: 2356static int
2288 { 2357sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2289 struct v4l2_buffer b; 2358{
2359 struct v4l2_buffer b;
2290 2360
2291 if (copy_from_user(&b, arg, sizeof(b))) 2361 if (copy_from_user(&b, arg, sizeof(b)))
2292 return -EFAULT; 2362 return -EFAULT;
2293 2363
2294 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 2364 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2295 b.index >= cam->nbuffers || cam->io != IO_MMAP) 2365 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2296 return -EINVAL; 2366 return -EINVAL;
2297 2367
2298 memcpy(&b, &cam->frame[b.index].buf, sizeof(b)); 2368 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2299 2369
2300 if (cam->frame[b.index].vma_use_count) 2370 if (cam->frame[b.index].vma_use_count)
2301 b.flags |= V4L2_BUF_FLAG_MAPPED; 2371 b.flags |= V4L2_BUF_FLAG_MAPPED;
2302 2372
2303 if (cam->frame[b.index].state == F_DONE) 2373 if (cam->frame[b.index].state == F_DONE)
2304 b.flags |= V4L2_BUF_FLAG_DONE; 2374 b.flags |= V4L2_BUF_FLAG_DONE;
2305 else if (cam->frame[b.index].state != F_UNUSED) 2375 else if (cam->frame[b.index].state != F_UNUSED)
2306 b.flags |= V4L2_BUF_FLAG_QUEUED; 2376 b.flags |= V4L2_BUF_FLAG_QUEUED;
2307 2377
2308 if (copy_to_user(arg, &b, sizeof(b))) 2378 if (copy_to_user(arg, &b, sizeof(b)))
2309 return -EFAULT; 2379 return -EFAULT;
2310 2380
2311 return 0; 2381 return 0;
2312 } 2382}
2313 2383
2314 case VIDIOC_QBUF:
2315 {
2316 struct v4l2_buffer b;
2317 unsigned long lock_flags;
2318 2384
2319 if (copy_from_user(&b, arg, sizeof(b))) 2385static int
2320 return -EFAULT; 2386sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2387{
2388 struct v4l2_buffer b;
2389 unsigned long lock_flags;
2321 2390
2322 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 2391 if (copy_from_user(&b, arg, sizeof(b)))
2323 b.index >= cam->nbuffers || cam->io != IO_MMAP) 2392 return -EFAULT;
2324 return -EINVAL;
2325 2393
2326 if (cam->frame[b.index].state != F_UNUSED) 2394 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2327 return -EINVAL; 2395 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2396 return -EINVAL;
2328 2397
2329 cam->frame[b.index].state = F_QUEUED; 2398 if (cam->frame[b.index].state != F_UNUSED)
2399 return -EINVAL;
2330 2400
2331 spin_lock_irqsave(&cam->queue_lock, lock_flags); 2401 cam->frame[b.index].state = F_QUEUED;
2332 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2333 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2334 2402
2335 PDBGG("Frame #%lu queued", (unsigned long)b.index) 2403 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2404 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2405 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2336 2406
2337 return 0; 2407 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2338 }
2339 2408
2340 case VIDIOC_DQBUF: 2409 return 0;
2341 { 2410}
2342 struct v4l2_buffer b;
2343 struct sn9c102_frame_t *f;
2344 unsigned long lock_flags;
2345 int err = 0;
2346 2411
2347 if (copy_from_user(&b, arg, sizeof(b)))
2348 return -EFAULT;
2349 2412
2350 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP) 2413static int
2414sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2415 void __user * arg)
2416{
2417 struct v4l2_buffer b;
2418 struct sn9c102_frame_t *f;
2419 unsigned long lock_flags;
2420 int err = 0;
2421
2422 if (copy_from_user(&b, arg, sizeof(b)))
2423 return -EFAULT;
2424
2425 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2426 return -EINVAL;
2427
2428 if (list_empty(&cam->outqueue)) {
2429 if (cam->stream == STREAM_OFF)
2351 return -EINVAL; 2430 return -EINVAL;
2431 if (filp->f_flags & O_NONBLOCK)
2432 return -EAGAIN;
2433 err = wait_event_interruptible
2434 ( cam->wait_frame,
2435 (!list_empty(&cam->outqueue)) ||
2436 (cam->state & DEV_DISCONNECTED) ||
2437 (cam->state & DEV_MISCONFIGURED) );
2438 if (err)
2439 return err;
2440 if (cam->state & DEV_DISCONNECTED)
2441 return -ENODEV;
2442 if (cam->state & DEV_MISCONFIGURED)
2443 return -EIO;
2444 }
2352 2445
2353 if (list_empty(&cam->outqueue)) { 2446 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2354 if (cam->stream == STREAM_OFF) 2447 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2355 return -EINVAL; 2448 list_del(cam->outqueue.next);
2356 if (filp->f_flags & O_NONBLOCK) 2449 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2357 return -EAGAIN;
2358 err = wait_event_interruptible
2359 ( cam->wait_frame,
2360 (!list_empty(&cam->outqueue)) ||
2361 (cam->state & DEV_DISCONNECTED) ||
2362 (cam->state & DEV_MISCONFIGURED) );
2363 if (err)
2364 return err;
2365 if (cam->state & DEV_DISCONNECTED)
2366 return -ENODEV;
2367 if (cam->state & DEV_MISCONFIGURED)
2368 return -EIO;
2369 }
2370 2450
2371 spin_lock_irqsave(&cam->queue_lock, lock_flags); 2451 f->state = F_UNUSED;
2372 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t,
2373 frame);
2374 list_del(cam->outqueue.next);
2375 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2376 2452
2377 f->state = F_UNUSED; 2453 memcpy(&b, &f->buf, sizeof(b));
2454 if (f->vma_use_count)
2455 b.flags |= V4L2_BUF_FLAG_MAPPED;
2378 2456
2379 memcpy(&b, &f->buf, sizeof(b)); 2457 if (copy_to_user(arg, &b, sizeof(b)))
2380 if (f->vma_use_count) 2458 return -EFAULT;
2381 b.flags |= V4L2_BUF_FLAG_MAPPED;
2382 2459
2383 if (copy_to_user(arg, &b, sizeof(b))) 2460 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2384 return -EFAULT;
2385 2461
2386 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index) 2462 return 0;
2463}
2387 2464
2388 return 0;
2389 }
2390 2465
2391 case VIDIOC_STREAMON: 2466static int
2392 { 2467sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2393 int type; 2468{
2469 int type;
2394 2470
2395 if (copy_from_user(&type, arg, sizeof(type))) 2471 if (copy_from_user(&type, arg, sizeof(type)))
2396 return -EFAULT; 2472 return -EFAULT;
2397 2473
2398 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) 2474 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2399 return -EINVAL; 2475 return -EINVAL;
2400 2476
2401 if (list_empty(&cam->inqueue)) 2477 if (list_empty(&cam->inqueue))
2402 return -EINVAL; 2478 return -EINVAL;
2403 2479
2404 cam->stream = STREAM_ON; 2480 cam->stream = STREAM_ON;
2405 2481
2406 DBG(3, "Stream on") 2482 DBG(3, "Stream on");
2407 2483
2408 return 0; 2484 return 0;
2409 } 2485}
2410 2486
2411 case VIDIOC_STREAMOFF:
2412 {
2413 int type, err;
2414 2487
2415 if (copy_from_user(&type, arg, sizeof(type))) 2488static int
2416 return -EFAULT; 2489sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2490{
2491 int type, err;
2417 2492
2418 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) 2493 if (copy_from_user(&type, arg, sizeof(type)))
2419 return -EINVAL; 2494 return -EFAULT;
2420 2495
2421 if (cam->stream == STREAM_ON) 2496 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2422 if ((err = sn9c102_stream_interrupt(cam))) 2497 return -EINVAL;
2423 return err;
2424 2498
2425 sn9c102_empty_framequeues(cam); 2499 if (cam->stream == STREAM_ON)
2500 if ((err = sn9c102_stream_interrupt(cam)))
2501 return err;
2426 2502
2427 DBG(3, "Stream off") 2503 sn9c102_empty_framequeues(cam);
2428 2504
2429 return 0; 2505 DBG(3, "Stream off");
2430 }
2431 2506
2432 case VIDIOC_G_PARM: 2507 return 0;
2433 { 2508}
2434 struct v4l2_streamparm sp;
2435 2509
2436 if (copy_from_user(&sp, arg, sizeof(sp)))
2437 return -EFAULT;
2438 2510
2439 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2511static int
2440 return -EINVAL; 2512sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2513{
2514 struct v4l2_streamparm sp;
2515
2516 if (copy_from_user(&sp, arg, sizeof(sp)))
2517 return -EFAULT;
2518
2519 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2520 return -EINVAL;
2521
2522 sp.parm.capture.extendedmode = 0;
2523 sp.parm.capture.readbuffers = cam->nreadbuffers;
2524
2525 if (copy_to_user(arg, &sp, sizeof(sp)))
2526 return -EFAULT;
2527
2528 return 0;
2529}
2530
2531
2532static int
2533sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2534{
2535 struct v4l2_streamparm sp;
2536
2537 if (copy_from_user(&sp, arg, sizeof(sp)))
2538 return -EFAULT;
2539
2540 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2541 return -EINVAL;
2542
2543 sp.parm.capture.extendedmode = 0;
2441 2544
2442 sp.parm.capture.extendedmode = 0; 2545 if (sp.parm.capture.readbuffers == 0)
2443 sp.parm.capture.readbuffers = cam->nreadbuffers; 2546 sp.parm.capture.readbuffers = cam->nreadbuffers;
2444 2547
2445 if (copy_to_user(arg, &sp, sizeof(sp))) 2548 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2446 return -EFAULT; 2549 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2447 2550
2448 return 0; 2551 if (copy_to_user(arg, &sp, sizeof(sp)))
2449 } 2552 return -EFAULT;
2450 2553
2451 case VIDIOC_S_PARM_OLD: 2554 cam->nreadbuffers = sp.parm.capture.readbuffers;
2452 case VIDIOC_S_PARM:
2453 {
2454 struct v4l2_streamparm sp;
2455 2555
2456 if (copy_from_user(&sp, arg, sizeof(sp))) 2556 return 0;
2457 return -EFAULT; 2557}
2458 2558
2459 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2460 return -EINVAL;
2461 2559
2462 sp.parm.capture.extendedmode = 0; 2560static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2561 unsigned int cmd, void __user * arg)
2562{
2563 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2564
2565 switch (cmd) {
2566
2567 case VIDIOC_QUERYCAP:
2568 return sn9c102_vidioc_querycap(cam, arg);
2463 2569
2464 if (sp.parm.capture.readbuffers == 0) 2570 case VIDIOC_ENUMINPUT:
2465 sp.parm.capture.readbuffers = cam->nreadbuffers; 2571 return sn9c102_vidioc_enuminput(cam, arg);
2466 2572
2467 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES) 2573 case VIDIOC_G_INPUT:
2468 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES; 2574 case VIDIOC_S_INPUT:
2575 return sn9c102_vidioc_gs_input(cam, arg);
2469 2576
2470 if (copy_to_user(arg, &sp, sizeof(sp))) 2577 case VIDIOC_QUERYCTRL:
2471 return -EFAULT; 2578 return sn9c102_vidioc_query_ctrl(cam, arg);
2472 2579
2473 cam->nreadbuffers = sp.parm.capture.readbuffers; 2580 case VIDIOC_G_CTRL:
2581 return sn9c102_vidioc_g_ctrl(cam, arg);
2474 2582
2475 return 0; 2583 case VIDIOC_S_CTRL_OLD:
2476 } 2584 case VIDIOC_S_CTRL:
2585 return sn9c102_vidioc_s_ctrl(cam, arg);
2586
2587 case VIDIOC_CROPCAP_OLD:
2588 case VIDIOC_CROPCAP:
2589 return sn9c102_vidioc_cropcap(cam, arg);
2590
2591 case VIDIOC_G_CROP:
2592 return sn9c102_vidioc_g_crop(cam, arg);
2593
2594 case VIDIOC_S_CROP:
2595 return sn9c102_vidioc_s_crop(cam, arg);
2596
2597 case VIDIOC_ENUM_FMT:
2598 return sn9c102_vidioc_enum_fmt(cam, arg);
2599
2600 case VIDIOC_G_FMT:
2601 return sn9c102_vidioc_g_fmt(cam, arg);
2602
2603 case VIDIOC_TRY_FMT:
2604 case VIDIOC_S_FMT:
2605 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
2606
2607 case VIDIOC_G_JPEGCOMP:
2608 return sn9c102_vidioc_g_jpegcomp(cam, arg);
2609
2610 case VIDIOC_S_JPEGCOMP:
2611 return sn9c102_vidioc_s_jpegcomp(cam, arg);
2612
2613 case VIDIOC_REQBUFS:
2614 return sn9c102_vidioc_reqbufs(cam, arg);
2615
2616 case VIDIOC_QUERYBUF:
2617 return sn9c102_vidioc_querybuf(cam, arg);
2618
2619 case VIDIOC_QBUF:
2620 return sn9c102_vidioc_qbuf(cam, arg);
2621
2622 case VIDIOC_DQBUF:
2623 return sn9c102_vidioc_dqbuf(cam, filp, arg);
2624
2625 case VIDIOC_STREAMON:
2626 return sn9c102_vidioc_streamon(cam, arg);
2627
2628 case VIDIOC_STREAMOFF:
2629 return sn9c102_vidioc_streamoff(cam, arg);
2630
2631 case VIDIOC_G_PARM:
2632 return sn9c102_vidioc_g_parm(cam, arg);
2633
2634 case VIDIOC_S_PARM_OLD:
2635 case VIDIOC_S_PARM:
2636 return sn9c102_vidioc_s_parm(cam, arg);
2477 2637
2478 case VIDIOC_G_STD: 2638 case VIDIOC_G_STD:
2479 case VIDIOC_S_STD: 2639 case VIDIOC_S_STD:
@@ -2499,17 +2659,20 @@ static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2499 return -ERESTARTSYS; 2659 return -ERESTARTSYS;
2500 2660
2501 if (cam->state & DEV_DISCONNECTED) { 2661 if (cam->state & DEV_DISCONNECTED) {
2502 DBG(1, "Device not present") 2662 DBG(1, "Device not present");
2503 up(&cam->fileop_sem); 2663 up(&cam->fileop_sem);
2504 return -ENODEV; 2664 return -ENODEV;
2505 } 2665 }
2506 2666
2507 if (cam->state & DEV_MISCONFIGURED) { 2667 if (cam->state & DEV_MISCONFIGURED) {
2508 DBG(1, "The camera is misconfigured. Close and open it again.") 2668 DBG(1, "The camera is misconfigured. Close and open it "
2669 "again.");
2509 up(&cam->fileop_sem); 2670 up(&cam->fileop_sem);
2510 return -EIO; 2671 return -EIO;
2511 } 2672 }
2512 2673
2674 V4LDBG(3, "sn9c102", cmd);
2675
2513 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg); 2676 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2514 2677
2515 up(&cam->fileop_sem); 2678 up(&cam->fileop_sem);
@@ -2517,9 +2680,10 @@ static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2517 return err; 2680 return err;
2518} 2681}
2519 2682
2683/*****************************************************************************/
2520 2684
2521static struct file_operations sn9c102_fops = { 2685static struct file_operations sn9c102_fops = {
2522 .owner = THIS_MODULE, 2686 .owner = THIS_MODULE,
2523 .open = sn9c102_open, 2687 .open = sn9c102_open,
2524 .release = sn9c102_release, 2688 .release = sn9c102_release,
2525 .ioctl = sn9c102_ioctl, 2689 .ioctl = sn9c102_ioctl,
@@ -2538,36 +2702,22 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2538 struct usb_device *udev = interface_to_usbdev(intf); 2702 struct usb_device *udev = interface_to_usbdev(intf);
2539 struct sn9c102_device* cam; 2703 struct sn9c102_device* cam;
2540 static unsigned int dev_nr = 0; 2704 static unsigned int dev_nr = 0;
2541 unsigned int i, n; 2705 unsigned int i;
2542 int err = 0, r; 2706 int err = 0, r;
2543 2707
2544 n = ARRAY_SIZE(sn9c102_id_table); 2708 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2545 for (i = 0; i < n-1; i++)
2546 if (le16_to_cpu(udev->descriptor.idVendor) ==
2547 sn9c102_id_table[i].idVendor &&
2548 le16_to_cpu(udev->descriptor.idProduct) ==
2549 sn9c102_id_table[i].idProduct)
2550 break;
2551 if (i == n-1)
2552 return -ENODEV;
2553
2554 if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2555 return -ENOMEM; 2709 return -ENOMEM;
2556 memset(cam, 0, sizeof(*cam));
2557 2710
2558 cam->usbdev = udev; 2711 cam->usbdev = udev;
2559 2712
2560 memcpy(&cam->dev, &udev->dev, sizeof(struct device)); 2713 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2561 2714 DBG(1, "kmalloc() failed");
2562 if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) {
2563 DBG(1, "kmalloc() failed")
2564 err = -ENOMEM; 2715 err = -ENOMEM;
2565 goto fail; 2716 goto fail;
2566 } 2717 }
2567 memset(cam->control_buffer, 0, 8);
2568 2718
2569 if (!(cam->v4ldev = video_device_alloc())) { 2719 if (!(cam->v4ldev = video_device_alloc())) {
2570 DBG(1, "video_device_alloc() failed") 2720 DBG(1, "video_device_alloc() failed");
2571 err = -ENOMEM; 2721 err = -ENOMEM;
2572 goto fail; 2722 goto fail;
2573 } 2723 }
@@ -2577,25 +2727,22 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2577 r = sn9c102_read_reg(cam, 0x00); 2727 r = sn9c102_read_reg(cam, 0x00);
2578 if (r < 0 || r != 0x10) { 2728 if (r < 0 || r != 0x10) {
2579 DBG(1, "Sorry, this is not a SN9C10x based camera " 2729 DBG(1, "Sorry, this is not a SN9C10x based camera "
2580 "(vid/pid 0x%04X/0x%04X)", 2730 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2581 sn9c102_id_table[i].idVendor,sn9c102_id_table[i].idProduct)
2582 err = -ENODEV; 2731 err = -ENODEV;
2583 goto fail; 2732 goto fail;
2584 } 2733 }
2585 2734
2586 cam->bridge = (sn9c102_id_table[i].idProduct & 0xffc0) == 0x6080 ? 2735 cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ?
2587 BRIDGE_SN9C103 : BRIDGE_SN9C102; 2736 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2588 switch (cam->bridge) { 2737 switch (cam->bridge) {
2589 case BRIDGE_SN9C101: 2738 case BRIDGE_SN9C101:
2590 case BRIDGE_SN9C102: 2739 case BRIDGE_SN9C102:
2591 DBG(2, "SN9C10[12] PC Camera Controller detected " 2740 DBG(2, "SN9C10[12] PC Camera Controller detected "
2592 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor, 2741 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2593 sn9c102_id_table[i].idProduct)
2594 break; 2742 break;
2595 case BRIDGE_SN9C103: 2743 case BRIDGE_SN9C103:
2596 DBG(2, "SN9C103 PC Camera Controller detected " 2744 DBG(2, "SN9C103 PC Camera Controller detected "
2597 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor, 2745 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2598 sn9c102_id_table[i].idProduct)
2599 break; 2746 break;
2600 } 2747 }
2601 2748
@@ -2606,24 +2753,24 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2606 } 2753 }
2607 2754
2608 if (!err && cam->sensor) { 2755 if (!err && cam->sensor) {
2609 DBG(2, "%s image sensor detected", cam->sensor->name) 2756 DBG(2, "%s image sensor detected", cam->sensor->name);
2610 DBG(3, "Support for %s maintained by %s", 2757 DBG(3, "Support for %s maintained by %s",
2611 cam->sensor->name, cam->sensor->maintainer) 2758 cam->sensor->name, cam->sensor->maintainer);
2612 } else { 2759 } else {
2613 DBG(1, "No supported image sensor detected") 2760 DBG(1, "No supported image sensor detected");
2614 err = -ENODEV; 2761 err = -ENODEV;
2615 goto fail; 2762 goto fail;
2616 } 2763 }
2617 2764
2618 if (sn9c102_init(cam)) { 2765 if (sn9c102_init(cam)) {
2619 DBG(1, "Initialization failed. I will retry on open().") 2766 DBG(1, "Initialization failed. I will retry on open().");
2620 cam->state |= DEV_MISCONFIGURED; 2767 cam->state |= DEV_MISCONFIGURED;
2621 } 2768 }
2622 2769
2623 strcpy(cam->v4ldev->name, "SN9C10x PC Camera"); 2770 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2624 cam->v4ldev->owner = THIS_MODULE; 2771 cam->v4ldev->owner = THIS_MODULE;
2625 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES; 2772 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2626 cam->v4ldev->hardware = VID_HARDWARE_SN9C102; 2773 cam->v4ldev->hardware = 0;
2627 cam->v4ldev->fops = &sn9c102_fops; 2774 cam->v4ldev->fops = &sn9c102_fops;
2628 cam->v4ldev->minor = video_nr[dev_nr]; 2775 cam->v4ldev->minor = video_nr[dev_nr];
2629 cam->v4ldev->release = video_device_release; 2776 cam->v4ldev->release = video_device_release;
@@ -2634,23 +2781,25 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2634 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, 2781 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2635 video_nr[dev_nr]); 2782 video_nr[dev_nr]);
2636 if (err) { 2783 if (err) {
2637 DBG(1, "V4L2 device registration failed") 2784 DBG(1, "V4L2 device registration failed");
2638 if (err == -ENFILE && video_nr[dev_nr] == -1) 2785 if (err == -ENFILE && video_nr[dev_nr] == -1)
2639 DBG(1, "Free /dev/videoX node not found") 2786 DBG(1, "Free /dev/videoX node not found");
2640 video_nr[dev_nr] = -1; 2787 video_nr[dev_nr] = -1;
2641 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; 2788 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2642 up(&cam->dev_sem); 2789 up(&cam->dev_sem);
2643 goto fail; 2790 goto fail;
2644 } 2791 }
2645 2792
2646 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor) 2793 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2647 2794
2648 cam->module_param.force_munmap = force_munmap[dev_nr]; 2795 cam->module_param.force_munmap = force_munmap[dev_nr];
2649 2796
2650 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; 2797 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2651 2798
2799#ifdef CONFIG_VIDEO_ADV_DEBUG
2652 sn9c102_create_sysfs(cam); 2800 sn9c102_create_sysfs(cam);
2653 DBG(2, "Optional device control through 'sysfs' interface ready") 2801 DBG(2, "Optional device control through 'sysfs' interface ready");
2802#endif
2654 2803
2655 usb_set_intfdata(intf, cam); 2804 usb_set_intfdata(intf, cam);
2656 2805
@@ -2680,14 +2829,14 @@ static void sn9c102_usb_disconnect(struct usb_interface* intf)
2680 2829
2681 down(&cam->dev_sem); 2830 down(&cam->dev_sem);
2682 2831
2683 DBG(2, "Disconnecting %s...", cam->v4ldev->name) 2832 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2684 2833
2685 wake_up_interruptible_all(&cam->open); 2834 wake_up_interruptible_all(&cam->open);
2686 2835
2687 if (cam->users) { 2836 if (cam->users) {
2688 DBG(2, "Device /dev/video%d is open! Deregistration and " 2837 DBG(2, "Device /dev/video%d is open! Deregistration and "
2689 "memory deallocation are deferred on close.", 2838 "memory deallocation are deferred on close.",
2690 cam->v4ldev->minor) 2839 cam->v4ldev->minor);
2691 cam->state |= DEV_MISCONFIGURED; 2840 cam->state |= DEV_MISCONFIGURED;
2692 sn9c102_stop_transfer(cam); 2841 sn9c102_stop_transfer(cam);
2693 cam->state |= DEV_DISCONNECTED; 2842 cam->state |= DEV_DISCONNECTED;
@@ -2720,11 +2869,11 @@ static int __init sn9c102_module_init(void)
2720{ 2869{
2721 int err = 0; 2870 int err = 0;
2722 2871
2723 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION) 2872 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
2724 KDBG(3, SN9C102_MODULE_AUTHOR) 2873 KDBG(3, SN9C102_MODULE_AUTHOR);
2725 2874
2726 if ((err = usb_register(&sn9c102_usb_driver))) 2875 if ((err = usb_register(&sn9c102_usb_driver)))
2727 KDBG(1, "usb_register() failed") 2876 KDBG(1, "usb_register() failed");
2728 2877
2729 return err; 2878 return err;
2730} 2879}