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