Merge git://git.infradead.org/mtd-2.6
[linux-2.6] / 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 device* cd,
710                                  struct device_attribute *attr, char* buf)
711 {
712         struct et61x251_device* cam;
713         ssize_t count;
714
715         if (mutex_lock_interruptible(&et61x251_sysfs_lock))
716                 return -ERESTARTSYS;
717
718         cam = video_get_drvdata(to_video_device(cd));
719         if (!cam) {
720                 mutex_unlock(&et61x251_sysfs_lock);
721                 return -ENODEV;
722         }
723
724         count = sprintf(buf, "%u\n", cam->sysfs.reg);
725
726         mutex_unlock(&et61x251_sysfs_lock);
727
728         return count;
729 }
730
731
732 static ssize_t
733 et61x251_store_reg(struct device* cd,
734                    struct device_attribute *attr, const char* buf, size_t len)
735 {
736         struct et61x251_device* cam;
737         u8 index;
738         ssize_t count;
739
740         if (mutex_lock_interruptible(&et61x251_sysfs_lock))
741                 return -ERESTARTSYS;
742
743         cam = video_get_drvdata(to_video_device(cd));
744         if (!cam) {
745                 mutex_unlock(&et61x251_sysfs_lock);
746                 return -ENODEV;
747         }
748
749         index = et61x251_strtou8(buf, len, &count);
750         if (index > 0x8e || !count) {
751                 mutex_unlock(&et61x251_sysfs_lock);
752                 return -EINVAL;
753         }
754
755         cam->sysfs.reg = index;
756
757         DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
758         DBG(3, "Written bytes: %zd", count);
759
760         mutex_unlock(&et61x251_sysfs_lock);
761
762         return count;
763 }
764
765
766 static ssize_t et61x251_show_val(struct device* cd,
767                                  struct device_attribute *attr, char* buf)
768 {
769         struct et61x251_device* cam;
770         ssize_t count;
771         int val;
772
773         if (mutex_lock_interruptible(&et61x251_sysfs_lock))
774                 return -ERESTARTSYS;
775
776         cam = video_get_drvdata(to_video_device(cd));
777         if (!cam) {
778                 mutex_unlock(&et61x251_sysfs_lock);
779                 return -ENODEV;
780         }
781
782         if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
783                 mutex_unlock(&et61x251_sysfs_lock);
784                 return -EIO;
785         }
786
787         count = sprintf(buf, "%d\n", val);
788
789         DBG(3, "Read bytes: %zd", count);
790
791         mutex_unlock(&et61x251_sysfs_lock);
792
793         return count;
794 }
795
796
797 static ssize_t
798 et61x251_store_val(struct device* cd, struct device_attribute *attr,
799                    const char* buf, size_t len)
800 {
801         struct et61x251_device* cam;
802         u8 value;
803         ssize_t count;
804         int err;
805
806         if (mutex_lock_interruptible(&et61x251_sysfs_lock))
807                 return -ERESTARTSYS;
808
809         cam = video_get_drvdata(to_video_device(cd));
810         if (!cam) {
811                 mutex_unlock(&et61x251_sysfs_lock);
812                 return -ENODEV;
813         }
814
815         value = et61x251_strtou8(buf, len, &count);
816         if (!count) {
817                 mutex_unlock(&et61x251_sysfs_lock);
818                 return -EINVAL;
819         }
820
821         err = et61x251_write_reg(cam, value, cam->sysfs.reg);
822         if (err) {
823                 mutex_unlock(&et61x251_sysfs_lock);
824                 return -EIO;
825         }
826
827         DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
828             cam->sysfs.reg, value);
829         DBG(3, "Written bytes: %zd", count);
830
831         mutex_unlock(&et61x251_sysfs_lock);
832
833         return count;
834 }
835
836
837 static ssize_t et61x251_show_i2c_reg(struct device* cd,
838                                      struct device_attribute *attr, char* buf)
839 {
840         struct et61x251_device* cam;
841         ssize_t count;
842
843         if (mutex_lock_interruptible(&et61x251_sysfs_lock))
844                 return -ERESTARTSYS;
845
846         cam = video_get_drvdata(to_video_device(cd));
847         if (!cam) {
848                 mutex_unlock(&et61x251_sysfs_lock);
849                 return -ENODEV;
850         }
851
852         count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
853
854         DBG(3, "Read bytes: %zd", count);
855
856         mutex_unlock(&et61x251_sysfs_lock);
857
858         return count;
859 }
860
861
862 static ssize_t
863 et61x251_store_i2c_reg(struct device* cd, struct device_attribute *attr,
864                        const char* buf, size_t len)
865 {
866         struct et61x251_device* cam;
867         u8 index;
868         ssize_t count;
869
870         if (mutex_lock_interruptible(&et61x251_sysfs_lock))
871                 return -ERESTARTSYS;
872
873         cam = video_get_drvdata(to_video_device(cd));
874         if (!cam) {
875                 mutex_unlock(&et61x251_sysfs_lock);
876                 return -ENODEV;
877         }
878
879         index = et61x251_strtou8(buf, len, &count);
880         if (!count) {
881                 mutex_unlock(&et61x251_sysfs_lock);
882                 return -EINVAL;
883         }
884
885         cam->sysfs.i2c_reg = index;
886
887         DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
888         DBG(3, "Written bytes: %zd", count);
889
890         mutex_unlock(&et61x251_sysfs_lock);
891
892         return count;
893 }
894
895
896 static ssize_t et61x251_show_i2c_val(struct device* cd,
897                                      struct device_attribute *attr, char* buf)
898 {
899         struct et61x251_device* cam;
900         ssize_t count;
901         int val;
902
903         if (mutex_lock_interruptible(&et61x251_sysfs_lock))
904                 return -ERESTARTSYS;
905
906         cam = video_get_drvdata(to_video_device(cd));
907         if (!cam) {
908                 mutex_unlock(&et61x251_sysfs_lock);
909                 return -ENODEV;
910         }
911
912         if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
913                 mutex_unlock(&et61x251_sysfs_lock);
914                 return -ENOSYS;
915         }
916
917         if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
918                 mutex_unlock(&et61x251_sysfs_lock);
919                 return -EIO;
920         }
921
922         count = sprintf(buf, "%d\n", val);
923
924         DBG(3, "Read bytes: %zd", count);
925
926         mutex_unlock(&et61x251_sysfs_lock);
927
928         return count;
929 }
930
931
932 static ssize_t
933 et61x251_store_i2c_val(struct device* cd, struct device_attribute *attr,
934                        const char* buf, size_t len)
935 {
936         struct et61x251_device* cam;
937         u8 value;
938         ssize_t count;
939         int err;
940
941         if (mutex_lock_interruptible(&et61x251_sysfs_lock))
942                 return -ERESTARTSYS;
943
944         cam = video_get_drvdata(to_video_device(cd));
945         if (!cam) {
946                 mutex_unlock(&et61x251_sysfs_lock);
947                 return -ENODEV;
948         }
949
950         if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
951                 mutex_unlock(&et61x251_sysfs_lock);
952                 return -ENOSYS;
953         }
954
955         value = et61x251_strtou8(buf, len, &count);
956         if (!count) {
957                 mutex_unlock(&et61x251_sysfs_lock);
958                 return -EINVAL;
959         }
960
961         err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
962         if (err) {
963                 mutex_unlock(&et61x251_sysfs_lock);
964                 return -EIO;
965         }
966
967         DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
968             cam->sysfs.i2c_reg, value);
969         DBG(3, "Written bytes: %zd", count);
970
971         mutex_unlock(&et61x251_sysfs_lock);
972
973         return count;
974 }
975
976
977 static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
978                    et61x251_show_reg, et61x251_store_reg);
979 static DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
980                    et61x251_show_val, et61x251_store_val);
981 static DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
982                    et61x251_show_i2c_reg, et61x251_store_i2c_reg);
983 static DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
984                    et61x251_show_i2c_val, et61x251_store_i2c_val);
985
986
987 static int et61x251_create_sysfs(struct et61x251_device* cam)
988 {
989         struct device *classdev = &(cam->v4ldev->class_dev);
990         int err = 0;
991
992         if ((err = device_create_file(classdev, &dev_attr_reg)))
993                 goto err_out;
994         if ((err = device_create_file(classdev, &dev_attr_val)))
995                 goto err_reg;
996
997         if (cam->sensor.sysfs_ops) {
998                 if ((err = device_create_file(classdev, &dev_attr_i2c_reg)))
999                         goto err_val;
1000                 if ((err = device_create_file(classdev, &dev_attr_i2c_val)))
1001                         goto err_i2c_reg;
1002         }
1003
1004 err_i2c_reg:
1005         if (cam->sensor.sysfs_ops)
1006                 device_remove_file(classdev, &dev_attr_i2c_reg);
1007 err_val:
1008         device_remove_file(classdev, &dev_attr_val);
1009 err_reg:
1010         device_remove_file(classdev, &dev_attr_reg);
1011 err_out:
1012         return err;
1013 }
1014 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1015
1016 /*****************************************************************************/
1017
1018 static int
1019 et61x251_set_pix_format(struct et61x251_device* cam,
1020                         struct v4l2_pix_format* pix)
1021 {
1022         int r, err = 0;
1023
1024         if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1025                 err += r;
1026         if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1027                 err += et61x251_write_reg(cam, r & 0xfd, 0x12);
1028         else
1029                 err += et61x251_write_reg(cam, r | 0x02, 0x12);
1030
1031         return err ? -EIO : 0;
1032 }
1033
1034
1035 static int
1036 et61x251_set_compression(struct et61x251_device* cam,
1037                          struct v4l2_jpegcompression* compression)
1038 {
1039         int r, err = 0;
1040
1041         if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1042                 err += r;
1043         if (compression->quality == 0)
1044                 err += et61x251_write_reg(cam, r & 0xfb, 0x12);
1045         else
1046                 err += et61x251_write_reg(cam, r | 0x04, 0x12);
1047
1048         return err ? -EIO : 0;
1049 }
1050
1051
1052 static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1053 {
1054         int r = 0, err = 0;
1055
1056         r = et61x251_read_reg(cam, 0x12);
1057         if (r < 0)
1058                 err += r;
1059
1060         if (scale == 1)
1061                 err += et61x251_write_reg(cam, r & ~0x01, 0x12);
1062         else if (scale == 2)
1063                 err += et61x251_write_reg(cam, r | 0x01, 0x12);
1064
1065         if (err)
1066                 return -EIO;
1067
1068         PDBGG("Scaling factor: %u", scale);
1069
1070         return 0;
1071 }
1072
1073
1074 static int
1075 et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1076 {
1077         struct et61x251_sensor* s = &cam->sensor;
1078         u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1079                            s->active_pixel.left),
1080             fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
1081                            s->active_pixel.top),
1082             fmw_length = (u16)(rect->width),
1083             fmw_height = (u16)(rect->height);
1084         int err = 0;
1085
1086         err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
1087         err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
1088         err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
1089         err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
1090         err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
1091                                        | ((fmw_length & 0x300) >> 4)
1092                                        | ((fmw_height & 0x300) >> 2), 0x6d);
1093         if (err)
1094                 return -EIO;
1095
1096         PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1097               fmw_sx, fmw_sy, fmw_length, fmw_height);
1098
1099         return 0;
1100 }
1101
1102
1103 static int et61x251_init(struct et61x251_device* cam)
1104 {
1105         struct et61x251_sensor* s = &cam->sensor;
1106         struct v4l2_control ctrl;
1107         struct v4l2_queryctrl *qctrl;
1108         struct v4l2_rect* rect;
1109         u8 i = 0;
1110         int err = 0;
1111
1112         if (!(cam->state & DEV_INITIALIZED)) {
1113                 mutex_init(&cam->open_mutex);
1114                 init_waitqueue_head(&cam->wait_open);
1115                 qctrl = s->qctrl;
1116                 rect = &(s->cropcap.defrect);
1117                 cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
1118         } else { /* use current values */
1119                 qctrl = s->_qctrl;
1120                 rect = &(s->_rect);
1121         }
1122
1123         err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
1124         err += et61x251_set_crop(cam, rect);
1125         if (err)
1126                 return err;
1127
1128         if (s->init) {
1129                 err = s->init(cam);
1130                 if (err) {
1131                         DBG(3, "Sensor initialization failed");
1132                         return err;
1133                 }
1134         }
1135
1136         err += et61x251_set_compression(cam, &cam->compression);
1137         err += et61x251_set_pix_format(cam, &s->pix_format);
1138         if (s->set_pix_format)
1139                 err += s->set_pix_format(cam, &s->pix_format);
1140         if (err)
1141                 return err;
1142
1143         if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
1144                 DBG(3, "Compressed video format is active, quality %d",
1145                     cam->compression.quality);
1146         else
1147                 DBG(3, "Uncompressed video format is active");
1148
1149         if (s->set_crop)
1150                 if ((err = s->set_crop(cam, rect))) {
1151                         DBG(3, "set_crop() failed");
1152                         return err;
1153                 }
1154
1155         if (s->set_ctrl) {
1156                 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1157                         if (s->qctrl[i].id != 0 &&
1158                             !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1159                                 ctrl.id = s->qctrl[i].id;
1160                                 ctrl.value = qctrl[i].default_value;
1161                                 err = s->set_ctrl(cam, &ctrl);
1162                                 if (err) {
1163                                         DBG(3, "Set %s control failed",
1164                                             s->qctrl[i].name);
1165                                         return err;
1166                                 }
1167                                 DBG(3, "Image sensor supports '%s' control",
1168                                     s->qctrl[i].name);
1169                         }
1170         }
1171
1172         if (!(cam->state & DEV_INITIALIZED)) {
1173                 mutex_init(&cam->fileop_mutex);
1174                 spin_lock_init(&cam->queue_lock);
1175                 init_waitqueue_head(&cam->wait_frame);
1176                 init_waitqueue_head(&cam->wait_stream);
1177                 cam->nreadbuffers = 2;
1178                 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1179                 memcpy(&(s->_rect), &(s->cropcap.defrect),
1180                        sizeof(struct v4l2_rect));
1181                 cam->state |= DEV_INITIALIZED;
1182         }
1183
1184         DBG(2, "Initialization succeeded");
1185         return 0;
1186 }
1187
1188 /*****************************************************************************/
1189
1190 static void et61x251_release_resources(struct kref *kref)
1191 {
1192         struct et61x251_device *cam;
1193
1194         mutex_lock(&et61x251_sysfs_lock);
1195
1196         cam = container_of(kref, struct et61x251_device, kref);
1197
1198         DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1199         video_set_drvdata(cam->v4ldev, NULL);
1200         video_unregister_device(cam->v4ldev);
1201         usb_put_dev(cam->usbdev);
1202         kfree(cam->control_buffer);
1203         kfree(cam);
1204
1205         mutex_unlock(&et61x251_sysfs_lock);
1206 }
1207
1208
1209 static int et61x251_open(struct inode* inode, struct file* filp)
1210 {
1211         struct et61x251_device* cam;
1212         int err = 0;
1213
1214         if (!down_read_trylock(&et61x251_dev_lock))
1215                 return -ERESTARTSYS;
1216
1217         cam = video_get_drvdata(video_devdata(filp));
1218
1219         if (wait_for_completion_interruptible(&cam->probe)) {
1220                 up_read(&et61x251_dev_lock);
1221                 return -ERESTARTSYS;
1222         }
1223
1224         kref_get(&cam->kref);
1225
1226         if (mutex_lock_interruptible(&cam->open_mutex)) {
1227                 kref_put(&cam->kref, et61x251_release_resources);
1228                 up_read(&et61x251_dev_lock);
1229                 return -ERESTARTSYS;
1230         }
1231
1232         if (cam->state & DEV_DISCONNECTED) {
1233                 DBG(1, "Device not present");
1234                 err = -ENODEV;
1235                 goto out;
1236         }
1237
1238         if (cam->users) {
1239                 DBG(2, "Device /dev/video%d is already in use",
1240                        cam->v4ldev->minor);
1241                 DBG(3, "Simultaneous opens are not supported");
1242                 if ((filp->f_flags & O_NONBLOCK) ||
1243                     (filp->f_flags & O_NDELAY)) {
1244                         err = -EWOULDBLOCK;
1245                         goto out;
1246                 }
1247                 DBG(2, "A blocking open() has been requested. Wait for the "
1248                        "device to be released...");
1249                 up_read(&et61x251_dev_lock);
1250                 err = wait_event_interruptible_exclusive(cam->wait_open,
1251                                                 (cam->state & DEV_DISCONNECTED)
1252                                                          || !cam->users);
1253                 down_read(&et61x251_dev_lock);
1254                 if (err)
1255                         goto out;
1256                 if (cam->state & DEV_DISCONNECTED) {
1257                         err = -ENODEV;
1258                         goto out;
1259                 }
1260         }
1261
1262         if (cam->state & DEV_MISCONFIGURED) {
1263                 err = et61x251_init(cam);
1264                 if (err) {
1265                         DBG(1, "Initialization failed again. "
1266                                "I will retry on next open().");
1267                         goto out;
1268                 }
1269                 cam->state &= ~DEV_MISCONFIGURED;
1270         }
1271
1272         if ((err = et61x251_start_transfer(cam)))
1273                 goto out;
1274
1275         filp->private_data = cam;
1276         cam->users++;
1277         cam->io = IO_NONE;
1278         cam->stream = STREAM_OFF;
1279         cam->nbuffers = 0;
1280         cam->frame_count = 0;
1281         et61x251_empty_framequeues(cam);
1282
1283         DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1284
1285 out:
1286         mutex_unlock(&cam->open_mutex);
1287         if (err)
1288                 kref_put(&cam->kref, et61x251_release_resources);
1289         up_read(&et61x251_dev_lock);
1290         return err;
1291 }
1292
1293
1294 static int et61x251_release(struct inode* inode, struct file* filp)
1295 {
1296         struct et61x251_device* cam;
1297
1298         down_write(&et61x251_dev_lock);
1299
1300         cam = video_get_drvdata(video_devdata(filp));
1301
1302         et61x251_stop_transfer(cam);
1303         et61x251_release_buffers(cam);
1304         cam->users--;
1305         wake_up_interruptible_nr(&cam->wait_open, 1);
1306
1307         DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1308
1309         kref_put(&cam->kref, et61x251_release_resources);
1310
1311         up_write(&et61x251_dev_lock);
1312
1313         return 0;
1314 }
1315
1316
1317 static ssize_t
1318 et61x251_read(struct file* filp, char __user * buf,
1319               size_t count, loff_t* f_pos)
1320 {
1321         struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1322         struct et61x251_frame_t* f, * i;
1323         unsigned long lock_flags;
1324         long timeout;
1325         int err = 0;
1326
1327         if (mutex_lock_interruptible(&cam->fileop_mutex))
1328                 return -ERESTARTSYS;
1329
1330         if (cam->state & DEV_DISCONNECTED) {
1331                 DBG(1, "Device not present");
1332                 mutex_unlock(&cam->fileop_mutex);
1333                 return -ENODEV;
1334         }
1335
1336         if (cam->state & DEV_MISCONFIGURED) {
1337                 DBG(1, "The camera is misconfigured. Close and open it "
1338                        "again.");
1339                 mutex_unlock(&cam->fileop_mutex);
1340                 return -EIO;
1341         }
1342
1343         if (cam->io == IO_MMAP) {
1344                 DBG(3, "Close and open the device again to choose the read "
1345                        "method");
1346                 mutex_unlock(&cam->fileop_mutex);
1347                 return -EBUSY;
1348         }
1349
1350         if (cam->io == IO_NONE) {
1351                 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1352                                               IO_READ)) {
1353                         DBG(1, "read() failed, not enough memory");
1354                         mutex_unlock(&cam->fileop_mutex);
1355                         return -ENOMEM;
1356                 }
1357                 cam->io = IO_READ;
1358                 cam->stream = STREAM_ON;
1359         }
1360
1361         if (list_empty(&cam->inqueue)) {
1362                 if (!list_empty(&cam->outqueue))
1363                         et61x251_empty_framequeues(cam);
1364                 et61x251_queue_unusedframes(cam);
1365         }
1366
1367         if (!count) {
1368                 mutex_unlock(&cam->fileop_mutex);
1369                 return 0;
1370         }
1371
1372         if (list_empty(&cam->outqueue)) {
1373                 if (filp->f_flags & O_NONBLOCK) {
1374                         mutex_unlock(&cam->fileop_mutex);
1375                         return -EAGAIN;
1376                 }
1377                 timeout = wait_event_interruptible_timeout
1378                           ( cam->wait_frame,
1379                             (!list_empty(&cam->outqueue)) ||
1380                             (cam->state & DEV_DISCONNECTED) ||
1381                             (cam->state & DEV_MISCONFIGURED),
1382                             cam->module_param.frame_timeout *
1383                             1000 * msecs_to_jiffies(1) );
1384                 if (timeout < 0) {
1385                         mutex_unlock(&cam->fileop_mutex);
1386                         return timeout;
1387                 }
1388                 if (cam->state & DEV_DISCONNECTED) {
1389                         mutex_unlock(&cam->fileop_mutex);
1390                         return -ENODEV;
1391                 }
1392                 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1393                         mutex_unlock(&cam->fileop_mutex);
1394                         return -EIO;
1395                 }
1396         }
1397
1398         f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
1399
1400         if (count > f->buf.bytesused)
1401                 count = f->buf.bytesused;
1402
1403         if (copy_to_user(buf, f->bufmem, count)) {
1404                 err = -EFAULT;
1405                 goto exit;
1406         }
1407         *f_pos += count;
1408
1409 exit:
1410         spin_lock_irqsave(&cam->queue_lock, lock_flags);
1411         list_for_each_entry(i, &cam->outqueue, frame)
1412                 i->state = F_UNUSED;
1413         INIT_LIST_HEAD(&cam->outqueue);
1414         spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1415
1416         et61x251_queue_unusedframes(cam);
1417
1418         PDBGG("Frame #%lu, bytes read: %zu",
1419               (unsigned long)f->buf.index, count);
1420
1421         mutex_unlock(&cam->fileop_mutex);
1422
1423         return err ? err : count;
1424 }
1425
1426
1427 static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1428 {
1429         struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1430         struct et61x251_frame_t* f;
1431         unsigned long lock_flags;
1432         unsigned int mask = 0;
1433
1434         if (mutex_lock_interruptible(&cam->fileop_mutex))
1435                 return POLLERR;
1436
1437         if (cam->state & DEV_DISCONNECTED) {
1438                 DBG(1, "Device not present");
1439                 goto error;
1440         }
1441
1442         if (cam->state & DEV_MISCONFIGURED) {
1443                 DBG(1, "The camera is misconfigured. Close and open it "
1444                        "again.");
1445                 goto error;
1446         }
1447
1448         if (cam->io == IO_NONE) {
1449                 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1450                                               IO_READ)) {
1451                         DBG(1, "poll() failed, not enough memory");
1452                         goto error;
1453                 }
1454                 cam->io = IO_READ;
1455                 cam->stream = STREAM_ON;
1456         }
1457
1458         if (cam->io == IO_READ) {
1459                 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1460                 list_for_each_entry(f, &cam->outqueue, frame)
1461                         f->state = F_UNUSED;
1462                 INIT_LIST_HEAD(&cam->outqueue);
1463                 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1464                 et61x251_queue_unusedframes(cam);
1465         }
1466
1467         poll_wait(filp, &cam->wait_frame, wait);
1468
1469         if (!list_empty(&cam->outqueue))
1470                 mask |= POLLIN | POLLRDNORM;
1471
1472         mutex_unlock(&cam->fileop_mutex);
1473
1474         return mask;
1475
1476 error:
1477         mutex_unlock(&cam->fileop_mutex);
1478         return POLLERR;
1479 }
1480
1481
1482 static void et61x251_vm_open(struct vm_area_struct* vma)
1483 {
1484         struct et61x251_frame_t* f = vma->vm_private_data;
1485         f->vma_use_count++;
1486 }
1487
1488
1489 static void et61x251_vm_close(struct vm_area_struct* vma)
1490 {
1491         /* NOTE: buffers are not freed here */
1492         struct et61x251_frame_t* f = vma->vm_private_data;
1493         f->vma_use_count--;
1494 }
1495
1496
1497 static struct vm_operations_struct et61x251_vm_ops = {
1498         .open = et61x251_vm_open,
1499         .close = et61x251_vm_close,
1500 };
1501
1502
1503 static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1504 {
1505         struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1506         unsigned long size = vma->vm_end - vma->vm_start,
1507                       start = vma->vm_start;
1508         void *pos;
1509         u32 i;
1510
1511         if (mutex_lock_interruptible(&cam->fileop_mutex))
1512                 return -ERESTARTSYS;
1513
1514         if (cam->state & DEV_DISCONNECTED) {
1515                 DBG(1, "Device not present");
1516                 mutex_unlock(&cam->fileop_mutex);
1517                 return -ENODEV;
1518         }
1519
1520         if (cam->state & DEV_MISCONFIGURED) {
1521                 DBG(1, "The camera is misconfigured. Close and open it "
1522                        "again.");
1523                 mutex_unlock(&cam->fileop_mutex);
1524                 return -EIO;
1525         }
1526
1527         if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
1528                 mutex_unlock(&cam->fileop_mutex);
1529                 return -EACCES;
1530         }
1531
1532         if (cam->io != IO_MMAP ||
1533             size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1534                 mutex_unlock(&cam->fileop_mutex);
1535                 return -EINVAL;
1536         }
1537
1538         for (i = 0; i < cam->nbuffers; i++) {
1539                 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1540                         break;
1541         }
1542         if (i == cam->nbuffers) {
1543                 mutex_unlock(&cam->fileop_mutex);
1544                 return -EINVAL;
1545         }
1546
1547         vma->vm_flags |= VM_IO;
1548         vma->vm_flags |= VM_RESERVED;
1549
1550         pos = cam->frame[i].bufmem;
1551         while (size > 0) { /* size is page-aligned */
1552                 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1553                         mutex_unlock(&cam->fileop_mutex);
1554                         return -EAGAIN;
1555                 }
1556                 start += PAGE_SIZE;
1557                 pos += PAGE_SIZE;
1558                 size -= PAGE_SIZE;
1559         }
1560
1561         vma->vm_ops = &et61x251_vm_ops;
1562         vma->vm_private_data = &cam->frame[i];
1563         et61x251_vm_open(vma);
1564
1565         mutex_unlock(&cam->fileop_mutex);
1566
1567         return 0;
1568 }
1569
1570 /*****************************************************************************/
1571
1572 static int
1573 et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
1574 {
1575         struct v4l2_capability cap = {
1576                 .driver = "et61x251",
1577                 .version = ET61X251_MODULE_VERSION_CODE,
1578                 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1579                                 V4L2_CAP_STREAMING,
1580         };
1581
1582         strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1583         if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1584                 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1585                         sizeof(cap.bus_info));
1586
1587         if (copy_to_user(arg, &cap, sizeof(cap)))
1588                 return -EFAULT;
1589
1590         return 0;
1591 }
1592
1593
1594 static int
1595 et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1596 {
1597         struct v4l2_input i;
1598
1599         if (copy_from_user(&i, arg, sizeof(i)))
1600                 return -EFAULT;
1601
1602         if (i.index)
1603                 return -EINVAL;
1604
1605         memset(&i, 0, sizeof(i));
1606         strcpy(i.name, "Camera");
1607         i.type = V4L2_INPUT_TYPE_CAMERA;
1608
1609         if (copy_to_user(arg, &i, sizeof(i)))
1610                 return -EFAULT;
1611
1612         return 0;
1613 }
1614
1615
1616 static int
1617 et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
1618 {
1619         int index = 0;
1620
1621         if (copy_to_user(arg, &index, sizeof(index)))
1622                 return -EFAULT;
1623
1624         return 0;
1625 }
1626
1627
1628 static int
1629 et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
1630 {
1631         int index;
1632
1633         if (copy_from_user(&index, arg, sizeof(index)))
1634                 return -EFAULT;
1635
1636         if (index != 0)
1637                 return -EINVAL;
1638
1639         return 0;
1640 }
1641
1642
1643 static int
1644 et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1645 {
1646         struct et61x251_sensor* s = &cam->sensor;
1647         struct v4l2_queryctrl qc;
1648         u8 i;
1649
1650         if (copy_from_user(&qc, arg, sizeof(qc)))
1651                 return -EFAULT;
1652
1653         for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1654                 if (qc.id && qc.id == s->qctrl[i].id) {
1655                         memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1656                         if (copy_to_user(arg, &qc, sizeof(qc)))
1657                                 return -EFAULT;
1658                         return 0;
1659                 }
1660
1661         return -EINVAL;
1662 }
1663
1664
1665 static int
1666 et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1667 {
1668         struct et61x251_sensor* s = &cam->sensor;
1669         struct v4l2_control ctrl;
1670         int err = 0;
1671         u8 i;
1672
1673         if (!s->get_ctrl && !s->set_ctrl)
1674                 return -EINVAL;
1675
1676         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1677                 return -EFAULT;
1678
1679         if (!s->get_ctrl) {
1680                 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1681                         if (ctrl.id == s->qctrl[i].id) {
1682                                 ctrl.value = s->_qctrl[i].default_value;
1683                                 goto exit;
1684                         }
1685                 return -EINVAL;
1686         } else
1687                 err = s->get_ctrl(cam, &ctrl);
1688
1689 exit:
1690         if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1691                 return -EFAULT;
1692
1693         return err;
1694 }
1695
1696
1697 static int
1698 et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1699 {
1700         struct et61x251_sensor* s = &cam->sensor;
1701         struct v4l2_control ctrl;
1702         u8 i;
1703         int err = 0;
1704
1705         if (!s->set_ctrl)
1706                 return -EINVAL;
1707
1708         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1709                 return -EFAULT;
1710
1711         for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1712                 if (ctrl.id == s->qctrl[i].id) {
1713                         if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1714                                 return -EINVAL;
1715                         if (ctrl.value < s->qctrl[i].minimum ||
1716                             ctrl.value > s->qctrl[i].maximum)
1717                                 return -ERANGE;
1718                         ctrl.value -= ctrl.value % s->qctrl[i].step;
1719                         break;
1720                 }
1721
1722         if ((err = s->set_ctrl(cam, &ctrl)))
1723                 return err;
1724
1725         s->_qctrl[i].default_value = ctrl.value;
1726
1727         return 0;
1728 }
1729
1730
1731 static int
1732 et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1733 {
1734         struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1735
1736         cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1737         cc->pixelaspect.numerator = 1;
1738         cc->pixelaspect.denominator = 1;
1739
1740         if (copy_to_user(arg, cc, sizeof(*cc)))
1741                 return -EFAULT;
1742
1743         return 0;
1744 }
1745
1746
1747 static int
1748 et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1749 {
1750         struct et61x251_sensor* s = &cam->sensor;
1751         struct v4l2_crop crop = {
1752                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1753         };
1754
1755         memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1756
1757         if (copy_to_user(arg, &crop, sizeof(crop)))
1758                 return -EFAULT;
1759
1760         return 0;
1761 }
1762
1763
1764 static int
1765 et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1766 {
1767         struct et61x251_sensor* s = &cam->sensor;
1768         struct v4l2_crop crop;
1769         struct v4l2_rect* rect;
1770         struct v4l2_rect* bounds = &(s->cropcap.bounds);
1771         struct v4l2_pix_format* pix_format = &(s->pix_format);
1772         u8 scale;
1773         const enum et61x251_stream_state stream = cam->stream;
1774         const u32 nbuffers = cam->nbuffers;
1775         u32 i;
1776         int err = 0;
1777
1778         if (copy_from_user(&crop, arg, sizeof(crop)))
1779                 return -EFAULT;
1780
1781         rect = &(crop.c);
1782
1783         if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1784                 return -EINVAL;
1785
1786         if (cam->module_param.force_munmap)
1787                 for (i = 0; i < cam->nbuffers; i++)
1788                         if (cam->frame[i].vma_use_count) {
1789                                 DBG(3, "VIDIOC_S_CROP failed. "
1790                                        "Unmap the buffers first.");
1791                                 return -EBUSY;
1792                         }
1793
1794         /* Preserve R,G or B origin */
1795         rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1796         rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1797
1798         if (rect->width < 16)
1799                 rect->width = 16;
1800         if (rect->height < 16)
1801                 rect->height = 16;
1802         if (rect->width > bounds->width)
1803                 rect->width = bounds->width;
1804         if (rect->height > bounds->height)
1805                 rect->height = bounds->height;
1806         if (rect->left < bounds->left)
1807                 rect->left = bounds->left;
1808         if (rect->top < bounds->top)
1809                 rect->top = bounds->top;
1810         if (rect->left + rect->width > bounds->left + bounds->width)
1811                 rect->left = bounds->left+bounds->width - rect->width;
1812         if (rect->top + rect->height > bounds->top + bounds->height)
1813                 rect->top = bounds->top+bounds->height - rect->height;
1814
1815         rect->width &= ~15L;
1816         rect->height &= ~15L;
1817
1818         if (ET61X251_PRESERVE_IMGSCALE) {
1819                 /* Calculate the actual scaling factor */
1820                 u32 a, b;
1821                 a = rect->width * rect->height;
1822                 b = pix_format->width * pix_format->height;
1823                 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1824         } else
1825                 scale = 1;
1826
1827         if (cam->stream == STREAM_ON)
1828                 if ((err = et61x251_stream_interrupt(cam)))
1829                         return err;
1830
1831         if (copy_to_user(arg, &crop, sizeof(crop))) {
1832                 cam->stream = stream;
1833                 return -EFAULT;
1834         }
1835
1836         if (cam->module_param.force_munmap || cam->io == IO_READ)
1837                 et61x251_release_buffers(cam);
1838
1839         err = et61x251_set_crop(cam, rect);
1840         if (s->set_crop)
1841                 err += s->set_crop(cam, rect);
1842         err += et61x251_set_scale(cam, scale);
1843
1844         if (err) { /* atomic, no rollback in ioctl() */
1845                 cam->state |= DEV_MISCONFIGURED;
1846                 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1847                        "use the camera, close and open /dev/video%d again.",
1848                     cam->v4ldev->minor);
1849                 return -EIO;
1850         }
1851
1852         s->pix_format.width = rect->width/scale;
1853         s->pix_format.height = rect->height/scale;
1854         memcpy(&(s->_rect), rect, sizeof(*rect));
1855
1856         if ((cam->module_param.force_munmap  || cam->io == IO_READ) &&
1857             nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1858                 cam->state |= DEV_MISCONFIGURED;
1859                 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1860                        "use the camera, close and open /dev/video%d again.",
1861                     cam->v4ldev->minor);
1862                 return -ENOMEM;
1863         }
1864
1865         if (cam->io == IO_READ)
1866                 et61x251_empty_framequeues(cam);
1867         else if (cam->module_param.force_munmap)
1868                 et61x251_requeue_outqueue(cam);
1869
1870         cam->stream = stream;
1871
1872         return 0;
1873 }
1874
1875
1876 static int
1877 et61x251_vidioc_enum_framesizes(struct et61x251_device* cam, void __user * arg)
1878 {
1879         struct v4l2_frmsizeenum frmsize;
1880
1881         if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
1882                 return -EFAULT;
1883
1884         if (frmsize.index != 0)
1885                 return -EINVAL;
1886
1887         if (frmsize.pixel_format != V4L2_PIX_FMT_ET61X251 &&
1888             frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
1889                 return -EINVAL;
1890
1891         frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
1892         frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
1893         frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
1894         frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
1895         frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
1896         memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
1897
1898         if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
1899                 return -EFAULT;
1900
1901         return 0;
1902 }
1903
1904
1905 static int
1906 et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
1907 {
1908         struct v4l2_fmtdesc fmtd;
1909
1910         if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1911                 return -EFAULT;
1912
1913         if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1914                 return -EINVAL;
1915
1916         if (fmtd.index == 0) {
1917                 strcpy(fmtd.description, "bayer rgb");
1918                 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1919         } else if (fmtd.index == 1) {
1920                 strcpy(fmtd.description, "compressed");
1921                 fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
1922                 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1923         } else
1924                 return -EINVAL;
1925
1926         fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1927         memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1928
1929         if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1930                 return -EFAULT;
1931
1932         return 0;
1933 }
1934
1935
1936 static int
1937 et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1938 {
1939         struct v4l2_format format;
1940         struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1941
1942         if (copy_from_user(&format, arg, sizeof(format)))
1943                 return -EFAULT;
1944
1945         if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1946                 return -EINVAL;
1947
1948         pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_ET61X251) ?
1949                            0 : V4L2_COLORSPACE_SRGB;
1950         pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
1951                              ? 0 : (pfmt->width * pfmt->priv) / 8;
1952         pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1953         pfmt->field = V4L2_FIELD_NONE;
1954         memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1955
1956         if (copy_to_user(arg, &format, sizeof(format)))
1957                 return -EFAULT;
1958
1959         return 0;
1960 }
1961
1962
1963 static int
1964 et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1965                           void __user * arg)
1966 {
1967         struct et61x251_sensor* s = &cam->sensor;
1968         struct v4l2_format format;
1969         struct v4l2_pix_format* pix;
1970         struct v4l2_pix_format* pfmt = &(s->pix_format);
1971         struct v4l2_rect* bounds = &(s->cropcap.bounds);
1972         struct v4l2_rect rect;
1973         u8 scale;
1974         const enum et61x251_stream_state stream = cam->stream;
1975         const u32 nbuffers = cam->nbuffers;
1976         u32 i;
1977         int err = 0;
1978
1979         if (copy_from_user(&format, arg, sizeof(format)))
1980                 return -EFAULT;
1981
1982         pix = &(format.fmt.pix);
1983
1984         if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1985                 return -EINVAL;
1986
1987         memcpy(&rect, &(s->_rect), sizeof(rect));
1988
1989         { /* calculate the actual scaling factor */
1990                 u32 a, b;
1991                 a = rect.width * rect.height;
1992                 b = pix->width * pix->height;
1993                 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1994         }
1995
1996         rect.width = scale * pix->width;
1997         rect.height = scale * pix->height;
1998
1999         if (rect.width < 16)
2000                 rect.width = 16;
2001         if (rect.height < 16)
2002                 rect.height = 16;
2003         if (rect.width > bounds->left + bounds->width - rect.left)
2004                 rect.width = bounds->left + bounds->width - rect.left;
2005         if (rect.height > bounds->top + bounds->height - rect.top)
2006                 rect.height = bounds->top + bounds->height - rect.top;
2007
2008         rect.width &= ~15L;
2009         rect.height &= ~15L;
2010
2011         { /* adjust the scaling factor */
2012                 u32 a, b;
2013                 a = rect.width * rect.height;
2014                 b = pix->width * pix->height;
2015                 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
2016         }
2017
2018         pix->width = rect.width / scale;
2019         pix->height = rect.height / scale;
2020
2021         if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
2022             pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2023                 pix->pixelformat = pfmt->pixelformat;
2024         pix->priv = pfmt->priv; /* bpp */
2025         pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_ET61X251) ?
2026                           0 : V4L2_COLORSPACE_SRGB;
2027         pix->colorspace = pfmt->colorspace;
2028         pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
2029                             ? 0 : (pix->width * pix->priv) / 8;
2030         pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2031         pix->field = V4L2_FIELD_NONE;
2032
2033         if (cmd == VIDIOC_TRY_FMT) {
2034                 if (copy_to_user(arg, &format, sizeof(format)))
2035                         return -EFAULT;
2036                 return 0;
2037         }
2038
2039         if (cam->module_param.force_munmap)
2040                 for (i = 0; i < cam->nbuffers; i++)
2041                         if (cam->frame[i].vma_use_count) {
2042                                 DBG(3, "VIDIOC_S_FMT failed. "
2043                                        "Unmap the buffers first.");
2044                                 return -EBUSY;
2045                         }
2046
2047         if (cam->stream == STREAM_ON)
2048                 if ((err = et61x251_stream_interrupt(cam)))
2049                         return err;
2050
2051         if (copy_to_user(arg, &format, sizeof(format))) {
2052                 cam->stream = stream;
2053                 return -EFAULT;
2054         }
2055
2056         if (cam->module_param.force_munmap || cam->io == IO_READ)
2057                 et61x251_release_buffers(cam);
2058
2059         err += et61x251_set_pix_format(cam, pix);
2060         err += et61x251_set_crop(cam, &rect);
2061         if (s->set_pix_format)
2062                 err += s->set_pix_format(cam, pix);
2063         if (s->set_crop)
2064                 err += s->set_crop(cam, &rect);
2065         err += et61x251_set_scale(cam, scale);
2066
2067         if (err) { /* atomic, no rollback in ioctl() */
2068                 cam->state |= DEV_MISCONFIGURED;
2069                 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2070                        "use the camera, close and open /dev/video%d again.",
2071                     cam->v4ldev->minor);
2072                 return -EIO;
2073         }
2074
2075         memcpy(pfmt, pix, sizeof(*pix));
2076         memcpy(&(s->_rect), &rect, sizeof(rect));
2077
2078         if ((cam->module_param.force_munmap  || cam->io == IO_READ) &&
2079             nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
2080                 cam->state |= DEV_MISCONFIGURED;
2081                 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2082                        "use the camera, close and open /dev/video%d again.",
2083                     cam->v4ldev->minor);
2084                 return -ENOMEM;
2085         }
2086
2087         if (cam->io == IO_READ)
2088                 et61x251_empty_framequeues(cam);
2089         else if (cam->module_param.force_munmap)
2090                 et61x251_requeue_outqueue(cam);
2091
2092         cam->stream = stream;
2093
2094         return 0;
2095 }
2096
2097
2098 static int
2099 et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
2100 {
2101         if (copy_to_user(arg, &cam->compression,
2102                          sizeof(cam->compression)))
2103                 return -EFAULT;
2104
2105         return 0;
2106 }
2107
2108
2109 static int
2110 et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
2111 {
2112         struct v4l2_jpegcompression jc;
2113         const enum et61x251_stream_state stream = cam->stream;
2114         int err = 0;
2115
2116         if (copy_from_user(&jc, arg, sizeof(jc)))
2117                 return -EFAULT;
2118
2119         if (jc.quality != 0 && jc.quality != 1)
2120                 return -EINVAL;
2121
2122         if (cam->stream == STREAM_ON)
2123                 if ((err = et61x251_stream_interrupt(cam)))
2124                         return err;
2125
2126         err += et61x251_set_compression(cam, &jc);
2127         if (err) { /* atomic, no rollback in ioctl() */
2128                 cam->state |= DEV_MISCONFIGURED;
2129                 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2130                        "problems. To use the camera, close and open "
2131                        "/dev/video%d again.", cam->v4ldev->minor);
2132                 return -EIO;
2133         }
2134
2135         cam->compression.quality = jc.quality;
2136
2137         cam->stream = stream;
2138
2139         return 0;
2140 }
2141
2142
2143 static int
2144 et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
2145 {
2146         struct v4l2_requestbuffers rb;
2147         u32 i;
2148         int err;
2149
2150         if (copy_from_user(&rb, arg, sizeof(rb)))
2151                 return -EFAULT;
2152
2153         if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2154             rb.memory != V4L2_MEMORY_MMAP)
2155                 return -EINVAL;
2156
2157         if (cam->io == IO_READ) {
2158                 DBG(3, "Close and open the device again to choose the mmap "
2159                        "I/O method");
2160                 return -EBUSY;
2161         }
2162
2163         for (i = 0; i < cam->nbuffers; i++)
2164                 if (cam->frame[i].vma_use_count) {
2165                         DBG(3, "VIDIOC_REQBUFS failed. "
2166                                "Previous buffers are still mapped.");
2167                         return -EBUSY;
2168                 }
2169
2170         if (cam->stream == STREAM_ON)
2171                 if ((err = et61x251_stream_interrupt(cam)))
2172                         return err;
2173
2174         et61x251_empty_framequeues(cam);
2175
2176         et61x251_release_buffers(cam);
2177         if (rb.count)
2178                 rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
2179
2180         if (copy_to_user(arg, &rb, sizeof(rb))) {
2181                 et61x251_release_buffers(cam);
2182                 cam->io = IO_NONE;
2183                 return -EFAULT;
2184         }
2185
2186         cam->io = rb.count ? IO_MMAP : IO_NONE;
2187
2188         return 0;
2189 }
2190
2191
2192 static int
2193 et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
2194 {
2195         struct v4l2_buffer b;
2196
2197         if (copy_from_user(&b, arg, sizeof(b)))
2198                 return -EFAULT;
2199
2200         if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2201             b.index >= cam->nbuffers || cam->io != IO_MMAP)
2202                 return -EINVAL;
2203
2204         memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2205
2206         if (cam->frame[b.index].vma_use_count)
2207                 b.flags |= V4L2_BUF_FLAG_MAPPED;
2208
2209         if (cam->frame[b.index].state == F_DONE)
2210                 b.flags |= V4L2_BUF_FLAG_DONE;
2211         else if (cam->frame[b.index].state != F_UNUSED)
2212                 b.flags |= V4L2_BUF_FLAG_QUEUED;
2213
2214         if (copy_to_user(arg, &b, sizeof(b)))
2215                 return -EFAULT;
2216
2217         return 0;
2218 }
2219
2220
2221 static int
2222 et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
2223 {
2224         struct v4l2_buffer b;
2225         unsigned long lock_flags;
2226
2227         if (copy_from_user(&b, arg, sizeof(b)))
2228                 return -EFAULT;
2229
2230         if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2231             b.index >= cam->nbuffers || cam->io != IO_MMAP)
2232                 return -EINVAL;
2233
2234         if (cam->frame[b.index].state != F_UNUSED)
2235                 return -EINVAL;
2236
2237         cam->frame[b.index].state = F_QUEUED;
2238
2239         spin_lock_irqsave(&cam->queue_lock, lock_flags);
2240         list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2241         spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2242
2243         PDBGG("Frame #%lu queued", (unsigned long)b.index);
2244
2245         return 0;
2246 }
2247
2248
2249 static int
2250 et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2251                       void __user * arg)
2252 {
2253         struct v4l2_buffer b;
2254         struct et61x251_frame_t *f;
2255         unsigned long lock_flags;
2256         long timeout;
2257
2258         if (copy_from_user(&b, arg, sizeof(b)))
2259                 return -EFAULT;
2260
2261         if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2262                 return -EINVAL;
2263
2264         if (list_empty(&cam->outqueue)) {
2265                 if (cam->stream == STREAM_OFF)
2266                         return -EINVAL;
2267                 if (filp->f_flags & O_NONBLOCK)
2268                         return -EAGAIN;
2269                 timeout = wait_event_interruptible_timeout
2270                           ( cam->wait_frame,
2271                             (!list_empty(&cam->outqueue)) ||
2272                             (cam->state & DEV_DISCONNECTED) ||
2273                             (cam->state & DEV_MISCONFIGURED),
2274                             cam->module_param.frame_timeout *
2275                             1000 * msecs_to_jiffies(1) );
2276                 if (timeout < 0)
2277                         return timeout;
2278                 if (cam->state & DEV_DISCONNECTED)
2279                         return -ENODEV;
2280                 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2281                         return -EIO;
2282         }
2283
2284         spin_lock_irqsave(&cam->queue_lock, lock_flags);
2285         f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
2286         list_del(cam->outqueue.next);
2287         spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2288
2289         f->state = F_UNUSED;
2290
2291         memcpy(&b, &f->buf, sizeof(b));
2292         if (f->vma_use_count)
2293                 b.flags |= V4L2_BUF_FLAG_MAPPED;
2294
2295         if (copy_to_user(arg, &b, sizeof(b)))
2296                 return -EFAULT;
2297
2298         PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2299
2300         return 0;
2301 }
2302
2303
2304 static int
2305 et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
2306 {
2307         int type;
2308
2309         if (copy_from_user(&type, arg, sizeof(type)))
2310                 return -EFAULT;
2311
2312         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2313                 return -EINVAL;
2314
2315         cam->stream = STREAM_ON;
2316
2317         DBG(3, "Stream on");
2318
2319         return 0;
2320 }
2321
2322
2323 static int
2324 et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
2325 {
2326         int type, err;
2327
2328         if (copy_from_user(&type, arg, sizeof(type)))
2329                 return -EFAULT;
2330
2331         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2332                 return -EINVAL;
2333
2334         if (cam->stream == STREAM_ON)
2335                 if ((err = et61x251_stream_interrupt(cam)))
2336                         return err;
2337
2338         et61x251_empty_framequeues(cam);
2339
2340         DBG(3, "Stream off");
2341
2342         return 0;
2343 }
2344
2345
2346 static int
2347 et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
2348 {
2349         struct v4l2_streamparm sp;
2350
2351         if (copy_from_user(&sp, arg, sizeof(sp)))
2352                 return -EFAULT;
2353
2354         if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2355                 return -EINVAL;
2356
2357         sp.parm.capture.extendedmode = 0;
2358         sp.parm.capture.readbuffers = cam->nreadbuffers;
2359
2360         if (copy_to_user(arg, &sp, sizeof(sp)))
2361                 return -EFAULT;
2362
2363         return 0;
2364 }
2365
2366
2367 static int
2368 et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
2369 {
2370         struct v4l2_streamparm sp;
2371
2372         if (copy_from_user(&sp, arg, sizeof(sp)))
2373                 return -EFAULT;
2374
2375         if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2376                 return -EINVAL;
2377
2378         sp.parm.capture.extendedmode = 0;
2379
2380         if (sp.parm.capture.readbuffers == 0)
2381                 sp.parm.capture.readbuffers = cam->nreadbuffers;
2382
2383         if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
2384                 sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
2385
2386         if (copy_to_user(arg, &sp, sizeof(sp)))
2387                 return -EFAULT;
2388
2389         cam->nreadbuffers = sp.parm.capture.readbuffers;
2390
2391         return 0;
2392 }
2393
2394
2395 static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2396                                unsigned int cmd, void __user * arg)
2397 {
2398         struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2399
2400         switch (cmd) {
2401
2402         case VIDIOC_QUERYCAP:
2403                 return et61x251_vidioc_querycap(cam, arg);
2404
2405         case VIDIOC_ENUMINPUT:
2406                 return et61x251_vidioc_enuminput(cam, arg);
2407
2408         case VIDIOC_G_INPUT:
2409                 return et61x251_vidioc_g_input(cam, arg);
2410
2411         case VIDIOC_S_INPUT:
2412                 return et61x251_vidioc_s_input(cam, arg);
2413
2414         case VIDIOC_QUERYCTRL:
2415                 return et61x251_vidioc_query_ctrl(cam, arg);
2416
2417         case VIDIOC_G_CTRL:
2418                 return et61x251_vidioc_g_ctrl(cam, arg);
2419
2420         case VIDIOC_S_CTRL:
2421                 return et61x251_vidioc_s_ctrl(cam, arg);
2422
2423         case VIDIOC_CROPCAP:
2424                 return et61x251_vidioc_cropcap(cam, arg);
2425
2426         case VIDIOC_G_CROP:
2427                 return et61x251_vidioc_g_crop(cam, arg);
2428
2429         case VIDIOC_S_CROP:
2430                 return et61x251_vidioc_s_crop(cam, arg);
2431
2432         case VIDIOC_ENUM_FMT:
2433                 return et61x251_vidioc_enum_fmt(cam, arg);
2434
2435         case VIDIOC_G_FMT:
2436                 return et61x251_vidioc_g_fmt(cam, arg);
2437
2438         case VIDIOC_TRY_FMT:
2439         case VIDIOC_S_FMT:
2440                 return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
2441
2442         case VIDIOC_ENUM_FRAMESIZES:
2443                 return et61x251_vidioc_enum_framesizes(cam, arg);
2444
2445         case VIDIOC_G_JPEGCOMP:
2446                 return et61x251_vidioc_g_jpegcomp(cam, arg);
2447
2448         case VIDIOC_S_JPEGCOMP:
2449                 return et61x251_vidioc_s_jpegcomp(cam, arg);
2450
2451         case VIDIOC_REQBUFS:
2452                 return et61x251_vidioc_reqbufs(cam, arg);
2453
2454         case VIDIOC_QUERYBUF:
2455                 return et61x251_vidioc_querybuf(cam, arg);
2456
2457         case VIDIOC_QBUF:
2458                 return et61x251_vidioc_qbuf(cam, arg);
2459
2460         case VIDIOC_DQBUF:
2461                 return et61x251_vidioc_dqbuf(cam, filp, arg);
2462
2463         case VIDIOC_STREAMON:
2464                 return et61x251_vidioc_streamon(cam, arg);
2465
2466         case VIDIOC_STREAMOFF:
2467                 return et61x251_vidioc_streamoff(cam, arg);
2468
2469         case VIDIOC_G_PARM:
2470                 return et61x251_vidioc_g_parm(cam, arg);
2471
2472         case VIDIOC_S_PARM:
2473                 return et61x251_vidioc_s_parm(cam, arg);
2474
2475         case VIDIOC_G_STD:
2476         case VIDIOC_S_STD:
2477         case VIDIOC_QUERYSTD:
2478         case VIDIOC_ENUMSTD:
2479         case VIDIOC_QUERYMENU:
2480         case VIDIOC_ENUM_FRAMEINTERVALS:
2481                 return -EINVAL;
2482
2483         default:
2484                 return -EINVAL;
2485
2486         }
2487 }
2488
2489
2490 static int et61x251_ioctl(struct inode* inode, struct file* filp,
2491                          unsigned int cmd, unsigned long arg)
2492 {
2493         struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2494         int err = 0;
2495
2496         if (mutex_lock_interruptible(&cam->fileop_mutex))
2497                 return -ERESTARTSYS;
2498
2499         if (cam->state & DEV_DISCONNECTED) {
2500                 DBG(1, "Device not present");
2501                 mutex_unlock(&cam->fileop_mutex);
2502                 return -ENODEV;
2503         }
2504
2505         if (cam->state & DEV_MISCONFIGURED) {
2506                 DBG(1, "The camera is misconfigured. Close and open it "
2507                        "again.");
2508                 mutex_unlock(&cam->fileop_mutex);
2509                 return -EIO;
2510         }
2511
2512         V4LDBG(3, "et61x251", cmd);
2513
2514         err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2515
2516         mutex_unlock(&cam->fileop_mutex);
2517
2518         return err;
2519 }
2520
2521
2522 static const struct file_operations et61x251_fops = {
2523         .owner = THIS_MODULE,
2524         .open =    et61x251_open,
2525         .release = et61x251_release,
2526         .ioctl =   et61x251_ioctl,
2527         .compat_ioctl = v4l_compat_ioctl32,
2528         .read =    et61x251_read,
2529         .poll =    et61x251_poll,
2530         .mmap =    et61x251_mmap,
2531         .llseek =  no_llseek,
2532 };
2533
2534 /*****************************************************************************/
2535
2536 /* It exists a single interface only. We do not need to validate anything. */
2537 static int
2538 et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2539 {
2540         struct usb_device *udev = interface_to_usbdev(intf);
2541         struct et61x251_device* cam;
2542         static unsigned int dev_nr = 0;
2543         unsigned int i;
2544         int err = 0;
2545
2546         if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
2547                 return -ENOMEM;
2548
2549         cam->usbdev = udev;
2550
2551         if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2552                 DBG(1, "kmalloc() failed");
2553                 err = -ENOMEM;
2554                 goto fail;
2555         }
2556
2557         if (!(cam->v4ldev = video_device_alloc())) {
2558                 DBG(1, "video_device_alloc() failed");
2559                 err = -ENOMEM;
2560                 goto fail;
2561         }
2562
2563         DBG(2, "ET61X[12]51 PC Camera Controller detected "
2564                "(vid/pid 0x%04X:0x%04X)",id->idVendor, id->idProduct);
2565
2566         for  (i = 0; et61x251_sensor_table[i]; i++) {
2567                 err = et61x251_sensor_table[i](cam);
2568                 if (!err)
2569                         break;
2570         }
2571
2572         if (!err)
2573                 DBG(2, "%s image sensor detected", cam->sensor.name);
2574         else {
2575                 DBG(1, "No supported image sensor detected");
2576                 err = -ENODEV;
2577                 goto fail;
2578         }
2579
2580         if (et61x251_init(cam)) {
2581                 DBG(1, "Initialization failed. I will retry on open().");
2582                 cam->state |= DEV_MISCONFIGURED;
2583         }
2584
2585         strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
2586         cam->v4ldev->owner = THIS_MODULE;
2587         cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2588         cam->v4ldev->fops = &et61x251_fops;
2589         cam->v4ldev->minor = video_nr[dev_nr];
2590         cam->v4ldev->release = video_device_release;
2591         video_set_drvdata(cam->v4ldev, cam);
2592
2593         init_completion(&cam->probe);
2594
2595         err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2596                                     video_nr[dev_nr]);
2597         if (err) {
2598                 DBG(1, "V4L2 device registration failed");
2599                 if (err == -ENFILE && video_nr[dev_nr] == -1)
2600                         DBG(1, "Free /dev/videoX node not found");
2601                 video_nr[dev_nr] = -1;
2602                 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2603                 complete_all(&cam->probe);
2604                 goto fail;
2605         }
2606
2607         DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2608
2609         cam->module_param.force_munmap = force_munmap[dev_nr];
2610         cam->module_param.frame_timeout = frame_timeout[dev_nr];
2611
2612         dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2613
2614 #ifdef CONFIG_VIDEO_ADV_DEBUG
2615         err = et61x251_create_sysfs(cam);
2616         if (!err)
2617                 DBG(2, "Optional device control through 'sysfs' "
2618                        "interface ready");
2619         else
2620                 DBG(2, "Failed to create 'sysfs' interface for optional "
2621                        "device controlling. Error #%d", err);
2622 #else
2623         DBG(2, "Optional device control through 'sysfs' interface disabled");
2624         DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
2625                "configuration option to enable it.");
2626 #endif
2627
2628         usb_set_intfdata(intf, cam);
2629         kref_init(&cam->kref);
2630         usb_get_dev(cam->usbdev);
2631
2632         complete_all(&cam->probe);
2633
2634         return 0;
2635
2636 fail:
2637         if (cam) {
2638                 kfree(cam->control_buffer);
2639                 if (cam->v4ldev)
2640                         video_device_release(cam->v4ldev);
2641                 kfree(cam);
2642         }
2643         return err;
2644 }
2645
2646
2647 static void et61x251_usb_disconnect(struct usb_interface* intf)
2648 {
2649         struct et61x251_device* cam;
2650
2651         down_write(&et61x251_dev_lock);
2652
2653         cam = usb_get_intfdata(intf);
2654
2655         DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2656
2657         if (cam->users) {
2658                 DBG(2, "Device /dev/video%d is open! Deregistration and "
2659                        "memory deallocation are deferred.",
2660                     cam->v4ldev->minor);
2661                 cam->state |= DEV_MISCONFIGURED;
2662                 et61x251_stop_transfer(cam);
2663                 cam->state |= DEV_DISCONNECTED;
2664                 wake_up_interruptible(&cam->wait_frame);
2665                 wake_up(&cam->wait_stream);
2666         } else
2667                 cam->state |= DEV_DISCONNECTED;
2668
2669         wake_up_interruptible_all(&cam->wait_open);
2670
2671         kref_put(&cam->kref, et61x251_release_resources);
2672
2673         up_write(&et61x251_dev_lock);
2674 }
2675
2676
2677 static struct usb_driver et61x251_usb_driver = {
2678         .name =       "et61x251",
2679         .id_table =   et61x251_id_table,
2680         .probe =      et61x251_usb_probe,
2681         .disconnect = et61x251_usb_disconnect,
2682 };
2683
2684 /*****************************************************************************/
2685
2686 static int __init et61x251_module_init(void)
2687 {
2688         int err = 0;
2689
2690         KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
2691         KDBG(3, ET61X251_MODULE_AUTHOR);
2692
2693         if ((err = usb_register(&et61x251_usb_driver)))
2694                 KDBG(1, "usb_register() failed");
2695
2696         return err;
2697 }
2698
2699
2700 static void __exit et61x251_module_exit(void)
2701 {
2702         usb_deregister(&et61x251_usb_driver);
2703 }
2704
2705
2706 module_init(et61x251_module_init);
2707 module_exit(et61x251_module_exit);