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