const: mark struct vm_struct_operations
[linux-2.6.git] / drivers / media / video / zc0301 / zc0301_core.c
1 /***************************************************************************
2  * Video4Linux2 driver for ZC0301[P] Image Processor and Control Chip      *
3  *                                                                         *
4  * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it>  *
5  *                                                                         *
6  * Informations about the chip internals needed to enable the I2C protocol *
7  * have been taken from the documentation of the ZC030x Video4Linux1       *
8  * driver written by Andrew Birkett <andy@nobugs.org>                      *
9  *                                                                         *
10  * This program is free software; you can redistribute it and/or modify    *
11  * it under the terms of the GNU General Public License as published by    *
12  * the Free Software Foundation; either version 2 of the License, or       *
13  * (at your option) any later version.                                     *
14  *                                                                         *
15  * This program is distributed in the hope that it will be useful,         *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
18  * GNU General Public License for more details.                            *
19  *                                                                         *
20  * You should have received a copy of the GNU General Public License       *
21  * along with this program; if not, write to the Free Software             *
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.               *
23  ***************************************************************************/
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/param.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/device.h>
32 #include <linux/fs.h>
33 #include <linux/delay.h>
34 #include <linux/compiler.h>
35 #include <linux/ioctl.h>
36 #include <linux/poll.h>
37 #include <linux/stat.h>
38 #include <linux/mm.h>
39 #include <linux/vmalloc.h>
40 #include <linux/page-flags.h>
41 #include <asm/byteorder.h>
42 #include <asm/page.h>
43 #include <asm/uaccess.h>
44
45 #include "zc0301.h"
46
47 /*****************************************************************************/
48
49 #define ZC0301_MODULE_NAME    "V4L2 driver for ZC0301[P] "                    \
50                               "Image Processor and Control Chip"
51 #define ZC0301_MODULE_AUTHOR  "(C) 2006-2007 Luca Risolia"
52 #define ZC0301_AUTHOR_EMAIL   "<luca.risolia@studio.unibo.it>"
53 #define ZC0301_MODULE_LICENSE "GPL"
54 #define ZC0301_MODULE_VERSION "1:1.10"
55 #define ZC0301_MODULE_VERSION_CODE  KERNEL_VERSION(1, 1, 10)
56
57 /*****************************************************************************/
58
59 MODULE_DEVICE_TABLE(usb, zc0301_id_table);
60
61 MODULE_AUTHOR(ZC0301_MODULE_AUTHOR " " ZC0301_AUTHOR_EMAIL);
62 MODULE_DESCRIPTION(ZC0301_MODULE_NAME);
63 MODULE_VERSION(ZC0301_MODULE_VERSION);
64 MODULE_LICENSE(ZC0301_MODULE_LICENSE);
65
66 static short video_nr[] = {[0 ... ZC0301_MAX_DEVICES-1] = -1};
67 module_param_array(video_nr, short, NULL, 0444);
68 MODULE_PARM_DESC(video_nr,
69                  "\n<-1|n[,...]> Specify V4L2 minor mode number."
70                  "\n -1 = use next available (default)"
71                  "\n  n = use minor number n (integer >= 0)"
72                  "\nYou can specify up to "
73                  __MODULE_STRING(ZC0301_MAX_DEVICES) " cameras this way."
74                  "\nFor example:"
75                  "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
76                  "\nthe second registered camera and use auto for the first"
77                  "\none and for every other camera."
78                  "\n");
79
80 static short force_munmap[] = {[0 ... ZC0301_MAX_DEVICES-1] =
81                                ZC0301_FORCE_MUNMAP};
82 module_param_array(force_munmap, bool, NULL, 0444);
83 MODULE_PARM_DESC(force_munmap,
84                  "\n<0|1[,...]> Force the application to unmap previously"
85                  "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
86                  "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
87                  "\nthis feature. This parameter is specific for each"
88                  "\ndetected camera."
89                  "\n 0 = do not force memory unmapping"
90                  "\n 1 = force memory unmapping (save memory)"
91                  "\nDefault value is "__MODULE_STRING(ZC0301_FORCE_MUNMAP)"."
92                  "\n");
93
94 static unsigned int frame_timeout[] = {[0 ... ZC0301_MAX_DEVICES-1] =
95                                        ZC0301_FRAME_TIMEOUT};
96 module_param_array(frame_timeout, uint, NULL, 0644);
97 MODULE_PARM_DESC(frame_timeout,
98                  "\n<n[,...]> Timeout for a video frame in seconds."
99                  "\nThis parameter is specific for each detected camera."
100                  "\nDefault value is "__MODULE_STRING(ZC0301_FRAME_TIMEOUT)"."
101                  "\n");
102
103 #ifdef ZC0301_DEBUG
104 static unsigned short debug = ZC0301_DEBUG_LEVEL;
105 module_param(debug, ushort, 0644);
106 MODULE_PARM_DESC(debug,
107                  "\n<n> Debugging information level, from 0 to 3:"
108                  "\n0 = none (use carefully)"
109                  "\n1 = critical errors"
110                  "\n2 = significant informations"
111                  "\n3 = more verbose messages"
112                  "\nLevel 3 is useful for testing only, when only "
113                  "one device is used."
114                  "\nDefault value is "__MODULE_STRING(ZC0301_DEBUG_LEVEL)"."
115                  "\n");
116 #endif
117
118 /*****************************************************************************/
119
120 static u32
121 zc0301_request_buffers(struct zc0301_device* cam, u32 count,
122                        enum zc0301_io_method io)
123 {
124         struct v4l2_pix_format* p = &(cam->sensor.pix_format);
125         struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
126         const size_t imagesize = cam->module_param.force_munmap ||
127                                  io == IO_READ ?
128                                  (p->width * p->height * p->priv) / 8 :
129                                  (r->width * r->height * p->priv) / 8;
130         void* buff = NULL;
131         u32 i;
132
133         if (count > ZC0301_MAX_FRAMES)
134                 count = ZC0301_MAX_FRAMES;
135
136         cam->nbuffers = count;
137         while (cam->nbuffers > 0) {
138                 if ((buff = vmalloc_32_user(cam->nbuffers *
139                                             PAGE_ALIGN(imagesize))))
140                         break;
141                 cam->nbuffers--;
142         }
143
144         for (i = 0; i < cam->nbuffers; i++) {
145                 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
146                 cam->frame[i].buf.index = i;
147                 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
148                 cam->frame[i].buf.length = imagesize;
149                 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
150                 cam->frame[i].buf.sequence = 0;
151                 cam->frame[i].buf.field = V4L2_FIELD_NONE;
152                 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
153                 cam->frame[i].buf.flags = 0;
154         }
155
156         return cam->nbuffers;
157 }
158
159
160 static void zc0301_release_buffers(struct zc0301_device* cam)
161 {
162         if (cam->nbuffers) {
163                 vfree(cam->frame[0].bufmem);
164                 cam->nbuffers = 0;
165         }
166         cam->frame_current = NULL;
167 }
168
169
170 static void zc0301_empty_framequeues(struct zc0301_device* cam)
171 {
172         u32 i;
173
174         INIT_LIST_HEAD(&cam->inqueue);
175         INIT_LIST_HEAD(&cam->outqueue);
176
177         for (i = 0; i < ZC0301_MAX_FRAMES; i++) {
178                 cam->frame[i].state = F_UNUSED;
179                 cam->frame[i].buf.bytesused = 0;
180         }
181 }
182
183
184 static void zc0301_requeue_outqueue(struct zc0301_device* cam)
185 {
186         struct zc0301_frame_t *i;
187
188         list_for_each_entry(i, &cam->outqueue, frame) {
189                 i->state = F_QUEUED;
190                 list_add(&i->frame, &cam->inqueue);
191         }
192
193         INIT_LIST_HEAD(&cam->outqueue);
194 }
195
196
197 static void zc0301_queue_unusedframes(struct zc0301_device* cam)
198 {
199         unsigned long lock_flags;
200         u32 i;
201
202         for (i = 0; i < cam->nbuffers; i++)
203                 if (cam->frame[i].state == F_UNUSED) {
204                         cam->frame[i].state = F_QUEUED;
205                         spin_lock_irqsave(&cam->queue_lock, lock_flags);
206                         list_add_tail(&cam->frame[i].frame, &cam->inqueue);
207                         spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
208                 }
209 }
210
211 /*****************************************************************************/
212
213 int zc0301_write_reg(struct zc0301_device* cam, u16 index, u16 value)
214 {
215         struct usb_device* udev = cam->usbdev;
216         int res;
217
218         res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0xa0, 0x40,
219                               value, index, NULL, 0, ZC0301_CTRL_TIMEOUT);
220         if (res < 0) {
221                 DBG(3, "Failed to write a register (index 0x%04X, "
222                        "value 0x%02X, error %d)",index, value, res);
223                 return -1;
224         }
225
226         return 0;
227 }
228
229
230 int zc0301_read_reg(struct zc0301_device* cam, u16 index)
231 {
232         struct usb_device* udev = cam->usbdev;
233         u8* buff = cam->control_buffer;
234         int res;
235
236         res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0xa1, 0xc0,
237                               0x0001, index, buff, 1, ZC0301_CTRL_TIMEOUT);
238         if (res < 0)
239                 DBG(3, "Failed to read a register (index 0x%04X, error %d)",
240                     index, res);
241
242         PDBGG("Read: index 0x%04X, value: 0x%04X", index, (int)(*buff));
243
244         return (res >= 0) ? (int)(*buff) : -1;
245 }
246
247
248 int zc0301_i2c_read(struct zc0301_device* cam, u16 address, u8 length)
249 {
250         int err = 0, res, r0, r1;
251
252         err += zc0301_write_reg(cam, 0x0092, address);
253         err += zc0301_write_reg(cam, 0x0090, 0x02);
254
255         msleep(1);
256
257         res = zc0301_read_reg(cam, 0x0091);
258         if (res < 0)
259                 err += res;
260         r0 = zc0301_read_reg(cam, 0x0095);
261         if (r0 < 0)
262                 err += r0;
263         r1 = zc0301_read_reg(cam, 0x0096);
264         if (r1 < 0)
265                 err += r1;
266
267         res = (length <= 1) ? r0 : r0 | (r1 << 8);
268
269         if (err)
270                 DBG(3, "I2C read failed at address 0x%04X, value: 0x%04X",
271                     address, res);
272
273
274         PDBGG("I2C read: address 0x%04X, value: 0x%04X", address, res);
275
276         return err ? -1 : res;
277 }
278
279
280 int zc0301_i2c_write(struct zc0301_device* cam, u16 address, u16 value)
281 {
282         int err = 0, res;
283
284         err += zc0301_write_reg(cam, 0x0092, address);
285         err += zc0301_write_reg(cam, 0x0093, value & 0xff);
286         err += zc0301_write_reg(cam, 0x0094, value >> 8);
287         err += zc0301_write_reg(cam, 0x0090, 0x01);
288
289         msleep(1);
290
291         res = zc0301_read_reg(cam, 0x0091);
292         if (res < 0)
293                 err += res;
294
295         if (err)
296                 DBG(3, "I2C write failed at address 0x%04X, value: 0x%04X",
297                     address, value);
298
299         PDBGG("I2C write: address 0x%04X, value: 0x%04X", address, value);
300
301         return err ? -1 : 0;
302 }
303
304 /*****************************************************************************/
305
306 static void zc0301_urb_complete(struct urb *urb)
307 {
308         struct zc0301_device* cam = urb->context;
309         struct zc0301_frame_t** f;
310         size_t imagesize;
311         u8 i;
312         int err = 0;
313
314         if (urb->status == -ENOENT)
315                 return;
316
317         f = &cam->frame_current;
318
319         if (cam->stream == STREAM_INTERRUPT) {
320                 cam->stream = STREAM_OFF;
321                 if ((*f))
322                         (*f)->state = F_QUEUED;
323                 DBG(3, "Stream interrupted");
324                 wake_up(&cam->wait_stream);
325         }
326
327         if (cam->state & DEV_DISCONNECTED)
328                 return;
329
330         if (cam->state & DEV_MISCONFIGURED) {
331                 wake_up_interruptible(&cam->wait_frame);
332                 return;
333         }
334
335         if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
336                 goto resubmit_urb;
337
338         if (!(*f))
339                 (*f) = list_entry(cam->inqueue.next, struct zc0301_frame_t,
340                                   frame);
341
342         imagesize = (cam->sensor.pix_format.width *
343                      cam->sensor.pix_format.height *
344                      cam->sensor.pix_format.priv) / 8;
345
346         for (i = 0; i < urb->number_of_packets; i++) {
347                 unsigned int len, status;
348                 void *pos;
349                 u16* soi;
350                 u8 sof;
351
352                 len = urb->iso_frame_desc[i].actual_length;
353                 status = urb->iso_frame_desc[i].status;
354                 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
355
356                 if (status) {
357                         DBG(3, "Error in isochronous frame");
358                         (*f)->state = F_ERROR;
359                         continue;
360                 }
361
362                 sof = (*(soi = pos) == 0xd8ff);
363
364                 PDBGG("Isochrnous frame: length %u, #%u i,", len, i);
365
366                 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
367 start_of_frame:
368                         if (sof) {
369                                 (*f)->state = F_GRABBING;
370                                 (*f)->buf.bytesused = 0;
371                                 do_gettimeofday(&(*f)->buf.timestamp);
372                                 DBG(3, "SOF detected: new video frame");
373                         }
374
375                 if ((*f)->state == F_GRABBING) {
376                         if (sof && (*f)->buf.bytesused)
377                                         goto end_of_frame;
378
379                         if ((*f)->buf.bytesused + len > imagesize) {
380                                 DBG(3, "Video frame size exceeded");
381                                 (*f)->state = F_ERROR;
382                                 continue;
383                         }
384
385                         memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, len);
386                         (*f)->buf.bytesused += len;
387
388                         if ((*f)->buf.bytesused == imagesize) {
389                                 u32 b;
390 end_of_frame:
391                                 b = (*f)->buf.bytesused;
392                                 (*f)->state = F_DONE;
393                                 (*f)->buf.sequence= ++cam->frame_count;
394                                 spin_lock(&cam->queue_lock);
395                                 list_move_tail(&(*f)->frame, &cam->outqueue);
396                                 if (!list_empty(&cam->inqueue))
397                                         (*f) = list_entry(cam->inqueue.next,
398                                                        struct zc0301_frame_t,
399                                                           frame);
400                                 else
401                                         (*f) = NULL;
402                                 spin_unlock(&cam->queue_lock);
403                                 DBG(3, "Video frame captured: : %lu bytes",
404                                        (unsigned long)(b));
405
406                                 if (!(*f))
407                                         goto resubmit_urb;
408
409                                 if (sof)
410                                         goto start_of_frame;
411                         }
412                 }
413         }
414
415 resubmit_urb:
416         urb->dev = cam->usbdev;
417         err = usb_submit_urb(urb, GFP_ATOMIC);
418         if (err < 0 && err != -EPERM) {
419                 cam->state |= DEV_MISCONFIGURED;
420                 DBG(1, "usb_submit_urb() failed");
421         }
422
423         wake_up_interruptible(&cam->wait_frame);
424 }
425
426
427 static int zc0301_start_transfer(struct zc0301_device* cam)
428 {
429         struct usb_device *udev = cam->usbdev;
430         struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
431                                                      usb_ifnum_to_if(udev, 0),
432                                                      ZC0301_ALTERNATE_SETTING);
433         const unsigned int psz = le16_to_cpu(altsetting->
434                                              endpoint[0].desc.wMaxPacketSize);
435         struct urb* urb;
436         s8 i, j;
437         int err = 0;
438
439         for (i = 0; i < ZC0301_URBS; i++) {
440                 cam->transfer_buffer[i] = kzalloc(ZC0301_ISO_PACKETS * psz,
441                                                   GFP_KERNEL);
442                 if (!cam->transfer_buffer[i]) {
443                         err = -ENOMEM;
444                         DBG(1, "Not enough memory");
445                         goto free_buffers;
446                 }
447         }
448
449         for (i = 0; i < ZC0301_URBS; i++) {
450                 urb = usb_alloc_urb(ZC0301_ISO_PACKETS, GFP_KERNEL);
451                 cam->urb[i] = urb;
452                 if (!urb) {
453                         err = -ENOMEM;
454                         DBG(1, "usb_alloc_urb() failed");
455                         goto free_urbs;
456                 }
457                 urb->dev = udev;
458                 urb->context = cam;
459                 urb->pipe = usb_rcvisocpipe(udev, 1);
460                 urb->transfer_flags = URB_ISO_ASAP;
461                 urb->number_of_packets = ZC0301_ISO_PACKETS;
462                 urb->complete = zc0301_urb_complete;
463                 urb->transfer_buffer = cam->transfer_buffer[i];
464                 urb->transfer_buffer_length = psz * ZC0301_ISO_PACKETS;
465                 urb->interval = 1;
466                 for (j = 0; j < ZC0301_ISO_PACKETS; j++) {
467                         urb->iso_frame_desc[j].offset = psz * j;
468                         urb->iso_frame_desc[j].length = psz;
469                 }
470         }
471
472         err = usb_set_interface(udev, 0, ZC0301_ALTERNATE_SETTING);
473         if (err) {
474                 DBG(1, "usb_set_interface() failed");
475                 goto free_urbs;
476         }
477
478         cam->frame_current = NULL;
479
480         for (i = 0; i < ZC0301_URBS; i++) {
481                 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
482                 if (err) {
483                         for (j = i-1; j >= 0; j--)
484                                 usb_kill_urb(cam->urb[j]);
485                         DBG(1, "usb_submit_urb() failed, error %d", err);
486                         goto free_urbs;
487                 }
488         }
489
490         return 0;
491
492 free_urbs:
493         for (i = 0; (i < ZC0301_URBS) && cam->urb[i]; i++)
494                 usb_free_urb(cam->urb[i]);
495
496 free_buffers:
497         for (i = 0; (i < ZC0301_URBS) && cam->transfer_buffer[i]; i++)
498                 kfree(cam->transfer_buffer[i]);
499
500         return err;
501 }
502
503
504 static int zc0301_stop_transfer(struct zc0301_device* cam)
505 {
506         struct usb_device *udev = cam->usbdev;
507         s8 i;
508         int err = 0;
509
510         if (cam->state & DEV_DISCONNECTED)
511                 return 0;
512
513         for (i = ZC0301_URBS-1; i >= 0; i--) {
514                 usb_kill_urb(cam->urb[i]);
515                 usb_free_urb(cam->urb[i]);
516                 kfree(cam->transfer_buffer[i]);
517         }
518
519         err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
520         if (err)
521                 DBG(3, "usb_set_interface() failed");
522
523         return err;
524 }
525
526
527 static int zc0301_stream_interrupt(struct zc0301_device* cam)
528 {
529         long timeout;
530
531         cam->stream = STREAM_INTERRUPT;
532         timeout = wait_event_timeout(cam->wait_stream,
533                                      (cam->stream == STREAM_OFF) ||
534                                      (cam->state & DEV_DISCONNECTED),
535                                      ZC0301_URB_TIMEOUT);
536         if (cam->state & DEV_DISCONNECTED)
537                 return -ENODEV;
538         else if (cam->stream != STREAM_OFF) {
539                 cam->state |= DEV_MISCONFIGURED;
540                 DBG(1, "URB timeout reached. The camera is misconfigured. To "
541                        "use it, close and open /dev/video%d again.",
542                     cam->v4ldev->num);
543                 return -EIO;
544         }
545
546         return 0;
547 }
548
549 /*****************************************************************************/
550
551 static int
552 zc0301_set_compression(struct zc0301_device* cam,
553                        struct v4l2_jpegcompression* compression)
554 {
555         int r, err = 0;
556
557         if ((r = zc0301_read_reg(cam, 0x0008)) < 0)
558                 err += r;
559         err += zc0301_write_reg(cam, 0x0008, r | 0x11 | compression->quality);
560
561         return err ? -EIO : 0;
562 }
563
564
565 static int zc0301_init(struct zc0301_device* cam)
566 {
567         struct zc0301_sensor* s = &cam->sensor;
568         struct v4l2_control ctrl;
569         struct v4l2_queryctrl *qctrl;
570         struct v4l2_rect* rect;
571         u8 i = 0;
572         int err = 0;
573
574         if (!(cam->state & DEV_INITIALIZED)) {
575                 mutex_init(&cam->open_mutex);
576                 init_waitqueue_head(&cam->wait_open);
577                 qctrl = s->qctrl;
578                 rect = &(s->cropcap.defrect);
579                 cam->compression.quality = ZC0301_COMPRESSION_QUALITY;
580         } else { /* use current values */
581                 qctrl = s->_qctrl;
582                 rect = &(s->_rect);
583         }
584
585         if (s->init) {
586                 err = s->init(cam);
587                 if (err) {
588                         DBG(3, "Sensor initialization failed");
589                         return err;
590                 }
591         }
592
593         if ((err = zc0301_set_compression(cam, &cam->compression))) {
594                 DBG(3, "set_compression() failed");
595                 return err;
596         }
597
598         if (s->set_crop)
599                 if ((err = s->set_crop(cam, rect))) {
600                         DBG(3, "set_crop() failed");
601                         return err;
602                 }
603
604         if (s->set_ctrl) {
605                 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
606                         if (s->qctrl[i].id != 0 &&
607                             !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
608                                 ctrl.id = s->qctrl[i].id;
609                                 ctrl.value = qctrl[i].default_value;
610                                 err = s->set_ctrl(cam, &ctrl);
611                                 if (err) {
612                                         DBG(3, "Set %s control failed",
613                                             s->qctrl[i].name);
614                                         return err;
615                                 }
616                                 DBG(3, "Image sensor supports '%s' control",
617                                     s->qctrl[i].name);
618                         }
619         }
620
621         if (!(cam->state & DEV_INITIALIZED)) {
622                 mutex_init(&cam->fileop_mutex);
623                 spin_lock_init(&cam->queue_lock);
624                 init_waitqueue_head(&cam->wait_frame);
625                 init_waitqueue_head(&cam->wait_stream);
626                 cam->nreadbuffers = 2;
627                 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
628                 memcpy(&(s->_rect), &(s->cropcap.defrect),
629                        sizeof(struct v4l2_rect));
630                 cam->state |= DEV_INITIALIZED;
631         }
632
633         DBG(2, "Initialization succeeded");
634         return 0;
635 }
636
637 /*****************************************************************************/
638
639 static void zc0301_release_resources(struct kref *kref)
640 {
641         struct zc0301_device *cam = container_of(kref, struct zc0301_device,
642                                                  kref);
643         DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->num);
644         video_set_drvdata(cam->v4ldev, NULL);
645         video_unregister_device(cam->v4ldev);
646         usb_put_dev(cam->usbdev);
647         kfree(cam->control_buffer);
648         kfree(cam);
649 }
650
651
652 static int zc0301_open(struct file *filp)
653 {
654         struct zc0301_device* cam;
655         int err = 0;
656
657         if (!down_read_trylock(&zc0301_dev_lock))
658                 return -EAGAIN;
659
660         cam = video_drvdata(filp);
661
662         if (wait_for_completion_interruptible(&cam->probe)) {
663                 up_read(&zc0301_dev_lock);
664                 return -ERESTARTSYS;
665         }
666
667         kref_get(&cam->kref);
668
669         if (mutex_lock_interruptible(&cam->open_mutex)) {
670                 kref_put(&cam->kref, zc0301_release_resources);
671                 up_read(&zc0301_dev_lock);
672                 return -ERESTARTSYS;
673         }
674
675         if (cam->state & DEV_DISCONNECTED) {
676                 DBG(1, "Device not present");
677                 err = -ENODEV;
678                 goto out;
679         }
680
681         if (cam->users) {
682                 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->num);
683                 DBG(3, "Simultaneous opens are not supported");
684                 if ((filp->f_flags & O_NONBLOCK) ||
685                     (filp->f_flags & O_NDELAY)) {
686                         err = -EWOULDBLOCK;
687                         goto out;
688                 }
689                 DBG(2, "A blocking open() has been requested. Wait for the "
690                        "device to be released...");
691                 up_read(&zc0301_dev_lock);
692                 err = wait_event_interruptible_exclusive(cam->wait_open,
693                                                 (cam->state & DEV_DISCONNECTED)
694                                                          || !cam->users);
695                 down_read(&zc0301_dev_lock);
696                 if (err)
697                         goto out;
698                 if (cam->state & DEV_DISCONNECTED) {
699                         err = -ENODEV;
700                         goto out;
701                 }
702         }
703
704         if (cam->state & DEV_MISCONFIGURED) {
705                 err = zc0301_init(cam);
706                 if (err) {
707                         DBG(1, "Initialization failed again. "
708                                "I will retry on next open().");
709                         goto out;
710                 }
711                 cam->state &= ~DEV_MISCONFIGURED;
712         }
713
714         if ((err = zc0301_start_transfer(cam)))
715                 goto out;
716
717         filp->private_data = cam;
718         cam->users++;
719         cam->io = IO_NONE;
720         cam->stream = STREAM_OFF;
721         cam->nbuffers = 0;
722         cam->frame_count = 0;
723         zc0301_empty_framequeues(cam);
724
725         DBG(3, "Video device /dev/video%d is open", cam->v4ldev->num);
726
727 out:
728         mutex_unlock(&cam->open_mutex);
729         if (err)
730                 kref_put(&cam->kref, zc0301_release_resources);
731         up_read(&zc0301_dev_lock);
732         return err;
733 }
734
735
736 static int zc0301_release(struct file *filp)
737 {
738         struct zc0301_device* cam;
739
740         down_write(&zc0301_dev_lock);
741
742         cam = video_drvdata(filp);
743
744         zc0301_stop_transfer(cam);
745         zc0301_release_buffers(cam);
746         cam->users--;
747         wake_up_interruptible_nr(&cam->wait_open, 1);
748
749         DBG(3, "Video device /dev/video%d closed", cam->v4ldev->num);
750
751         kref_put(&cam->kref, zc0301_release_resources);
752
753         up_write(&zc0301_dev_lock);
754
755         return 0;
756 }
757
758
759 static ssize_t
760 zc0301_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
761 {
762         struct zc0301_device *cam = video_drvdata(filp);
763         struct zc0301_frame_t* f, * i;
764         unsigned long lock_flags;
765         long timeout;
766         int err = 0;
767
768         if (mutex_lock_interruptible(&cam->fileop_mutex))
769                 return -ERESTARTSYS;
770
771         if (cam->state & DEV_DISCONNECTED) {
772                 DBG(1, "Device not present");
773                 mutex_unlock(&cam->fileop_mutex);
774                 return -ENODEV;
775         }
776
777         if (cam->state & DEV_MISCONFIGURED) {
778                 DBG(1, "The camera is misconfigured. Close and open it "
779                        "again.");
780                 mutex_unlock(&cam->fileop_mutex);
781                 return -EIO;
782         }
783
784         if (cam->io == IO_MMAP) {
785                 DBG(3, "Close and open the device again to choose the read "
786                        "method");
787                 mutex_unlock(&cam->fileop_mutex);
788                 return -EBUSY;
789         }
790
791         if (cam->io == IO_NONE) {
792                 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
793                         DBG(1, "read() failed, not enough memory");
794                         mutex_unlock(&cam->fileop_mutex);
795                         return -ENOMEM;
796                 }
797                 cam->io = IO_READ;
798                 cam->stream = STREAM_ON;
799         }
800
801         if (list_empty(&cam->inqueue)) {
802                 if (!list_empty(&cam->outqueue))
803                         zc0301_empty_framequeues(cam);
804                 zc0301_queue_unusedframes(cam);
805         }
806
807         if (!count) {
808                 mutex_unlock(&cam->fileop_mutex);
809                 return 0;
810         }
811
812         if (list_empty(&cam->outqueue)) {
813                 if (filp->f_flags & O_NONBLOCK) {
814                         mutex_unlock(&cam->fileop_mutex);
815                         return -EAGAIN;
816                 }
817                 timeout = wait_event_interruptible_timeout
818                           ( cam->wait_frame,
819                             (!list_empty(&cam->outqueue)) ||
820                             (cam->state & DEV_DISCONNECTED) ||
821                             (cam->state & DEV_MISCONFIGURED),
822                             msecs_to_jiffies(
823                                 cam->module_param.frame_timeout * 1000
824                             )
825                           );
826                 if (timeout < 0) {
827                         mutex_unlock(&cam->fileop_mutex);
828                         return timeout;
829                 }
830                 if (cam->state & DEV_DISCONNECTED) {
831                         mutex_unlock(&cam->fileop_mutex);
832                         return -ENODEV;
833                 }
834                 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
835                         mutex_unlock(&cam->fileop_mutex);
836                         return -EIO;
837                 }
838         }
839
840         f = list_entry(cam->outqueue.prev, struct zc0301_frame_t, frame);
841
842         if (count > f->buf.bytesused)
843                 count = f->buf.bytesused;
844
845         if (copy_to_user(buf, f->bufmem, count)) {
846                 err = -EFAULT;
847                 goto exit;
848         }
849         *f_pos += count;
850
851 exit:
852         spin_lock_irqsave(&cam->queue_lock, lock_flags);
853         list_for_each_entry(i, &cam->outqueue, frame)
854                 i->state = F_UNUSED;
855         INIT_LIST_HEAD(&cam->outqueue);
856         spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
857
858         zc0301_queue_unusedframes(cam);
859
860         PDBGG("Frame #%lu, bytes read: %zu",
861               (unsigned long)f->buf.index, count);
862
863         mutex_unlock(&cam->fileop_mutex);
864
865         return err ? err : count;
866 }
867
868
869 static unsigned int zc0301_poll(struct file *filp, poll_table *wait)
870 {
871         struct zc0301_device *cam = video_drvdata(filp);
872         struct zc0301_frame_t* f;
873         unsigned long lock_flags;
874         unsigned int mask = 0;
875
876         if (mutex_lock_interruptible(&cam->fileop_mutex))
877                 return POLLERR;
878
879         if (cam->state & DEV_DISCONNECTED) {
880                 DBG(1, "Device not present");
881                 goto error;
882         }
883
884         if (cam->state & DEV_MISCONFIGURED) {
885                 DBG(1, "The camera is misconfigured. Close and open it "
886                        "again.");
887                 goto error;
888         }
889
890         if (cam->io == IO_NONE) {
891                 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
892                         DBG(1, "poll() failed, not enough memory");
893                         goto error;
894                 }
895                 cam->io = IO_READ;
896                 cam->stream = STREAM_ON;
897         }
898
899         if (cam->io == IO_READ) {
900                 spin_lock_irqsave(&cam->queue_lock, lock_flags);
901                 list_for_each_entry(f, &cam->outqueue, frame)
902                         f->state = F_UNUSED;
903                 INIT_LIST_HEAD(&cam->outqueue);
904                 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
905                 zc0301_queue_unusedframes(cam);
906         }
907
908         poll_wait(filp, &cam->wait_frame, wait);
909
910         if (!list_empty(&cam->outqueue))
911                 mask |= POLLIN | POLLRDNORM;
912
913         mutex_unlock(&cam->fileop_mutex);
914
915         return mask;
916
917 error:
918         mutex_unlock(&cam->fileop_mutex);
919         return POLLERR;
920 }
921
922
923 static void zc0301_vm_open(struct vm_area_struct* vma)
924 {
925         struct zc0301_frame_t* f = vma->vm_private_data;
926         f->vma_use_count++;
927 }
928
929
930 static void zc0301_vm_close(struct vm_area_struct* vma)
931 {
932         /* NOTE: buffers are not freed here */
933         struct zc0301_frame_t* f = vma->vm_private_data;
934         f->vma_use_count--;
935 }
936
937
938 static const struct vm_operations_struct zc0301_vm_ops = {
939         .open = zc0301_vm_open,
940         .close = zc0301_vm_close,
941 };
942
943
944 static int zc0301_mmap(struct file* filp, struct vm_area_struct *vma)
945 {
946         struct zc0301_device *cam = video_drvdata(filp);
947         unsigned long size = vma->vm_end - vma->vm_start,
948                       start = vma->vm_start;
949         void *pos;
950         u32 i;
951
952         if (mutex_lock_interruptible(&cam->fileop_mutex))
953                 return -ERESTARTSYS;
954
955         if (cam->state & DEV_DISCONNECTED) {
956                 DBG(1, "Device not present");
957                 mutex_unlock(&cam->fileop_mutex);
958                 return -ENODEV;
959         }
960
961         if (cam->state & DEV_MISCONFIGURED) {
962                 DBG(1, "The camera is misconfigured. Close and open it "
963                        "again.");
964                 mutex_unlock(&cam->fileop_mutex);
965                 return -EIO;
966         }
967
968         if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
969                 mutex_unlock(&cam->fileop_mutex);
970                 return -EACCES;
971         }
972
973         if (cam->io != IO_MMAP ||
974             size != PAGE_ALIGN(cam->frame[0].buf.length)) {
975                 mutex_unlock(&cam->fileop_mutex);
976                 return -EINVAL;
977         }
978
979         for (i = 0; i < cam->nbuffers; i++) {
980                 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
981                         break;
982         }
983         if (i == cam->nbuffers) {
984                 mutex_unlock(&cam->fileop_mutex);
985                 return -EINVAL;
986         }
987
988         vma->vm_flags |= VM_IO;
989         vma->vm_flags |= VM_RESERVED;
990
991         pos = cam->frame[i].bufmem;
992         while (size > 0) { /* size is page-aligned */
993                 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
994                         mutex_unlock(&cam->fileop_mutex);
995                         return -EAGAIN;
996                 }
997                 start += PAGE_SIZE;
998                 pos += PAGE_SIZE;
999                 size -= PAGE_SIZE;
1000         }
1001
1002         vma->vm_ops = &zc0301_vm_ops;
1003         vma->vm_private_data = &cam->frame[i];
1004         zc0301_vm_open(vma);
1005
1006         mutex_unlock(&cam->fileop_mutex);
1007
1008         return 0;
1009 }
1010
1011 /*****************************************************************************/
1012
1013 static int
1014 zc0301_vidioc_querycap(struct zc0301_device* cam, void __user * arg)
1015 {
1016         struct v4l2_capability cap = {
1017                 .driver = "zc0301",
1018                 .version = ZC0301_MODULE_VERSION_CODE,
1019                 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1020                                 V4L2_CAP_STREAMING,
1021         };
1022
1023         strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1024         if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1025                 strlcpy(cap.bus_info, dev_name(&cam->usbdev->dev),
1026                         sizeof(cap.bus_info));
1027
1028         if (copy_to_user(arg, &cap, sizeof(cap)))
1029                 return -EFAULT;
1030
1031         return 0;
1032 }
1033
1034
1035 static int
1036 zc0301_vidioc_enuminput(struct zc0301_device* cam, void __user * arg)
1037 {
1038         struct v4l2_input i;
1039
1040         if (copy_from_user(&i, arg, sizeof(i)))
1041                 return -EFAULT;
1042
1043         if (i.index)
1044                 return -EINVAL;
1045
1046         memset(&i, 0, sizeof(i));
1047         strcpy(i.name, "Camera");
1048         i.type = V4L2_INPUT_TYPE_CAMERA;
1049
1050         if (copy_to_user(arg, &i, sizeof(i)))
1051                 return -EFAULT;
1052
1053         return 0;
1054 }
1055
1056
1057 static int
1058 zc0301_vidioc_g_input(struct zc0301_device* cam, void __user * arg)
1059 {
1060         int index = 0;
1061
1062         if (copy_to_user(arg, &index, sizeof(index)))
1063                 return -EFAULT;
1064
1065         return 0;
1066 }
1067
1068
1069 static int
1070 zc0301_vidioc_s_input(struct zc0301_device* cam, void __user * arg)
1071 {
1072         int index;
1073
1074         if (copy_from_user(&index, arg, sizeof(index)))
1075                 return -EFAULT;
1076
1077         if (index != 0)
1078                 return -EINVAL;
1079
1080         return 0;
1081 }
1082
1083
1084 static int
1085 zc0301_vidioc_query_ctrl(struct zc0301_device* cam, void __user * arg)
1086 {
1087         struct zc0301_sensor* s = &cam->sensor;
1088         struct v4l2_queryctrl qc;
1089         u8 i;
1090
1091         if (copy_from_user(&qc, arg, sizeof(qc)))
1092                 return -EFAULT;
1093
1094         for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1095                 if (qc.id && qc.id == s->qctrl[i].id) {
1096                         memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1097                         if (copy_to_user(arg, &qc, sizeof(qc)))
1098                                 return -EFAULT;
1099                         return 0;
1100                 }
1101
1102         return -EINVAL;
1103 }
1104
1105
1106 static int
1107 zc0301_vidioc_g_ctrl(struct zc0301_device* cam, void __user * arg)
1108 {
1109         struct zc0301_sensor* s = &cam->sensor;
1110         struct v4l2_control ctrl;
1111         int err = 0;
1112         u8 i;
1113
1114         if (!s->get_ctrl && !s->set_ctrl)
1115                 return -EINVAL;
1116
1117         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1118                 return -EFAULT;
1119
1120         if (!s->get_ctrl) {
1121                 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1122                         if (ctrl.id == s->qctrl[i].id) {
1123                                 ctrl.value = s->_qctrl[i].default_value;
1124                                 goto exit;
1125                         }
1126                 return -EINVAL;
1127         } else
1128                 err = s->get_ctrl(cam, &ctrl);
1129
1130 exit:
1131         if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1132                 return -EFAULT;
1133
1134         return err;
1135 }
1136
1137
1138 static int
1139 zc0301_vidioc_s_ctrl(struct zc0301_device* cam, void __user * arg)
1140 {
1141         struct zc0301_sensor* s = &cam->sensor;
1142         struct v4l2_control ctrl;
1143         u8 i;
1144         int err = 0;
1145
1146         if (!s->set_ctrl)
1147                 return -EINVAL;
1148
1149         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1150                 return -EFAULT;
1151
1152         for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1153                 if (ctrl.id == s->qctrl[i].id) {
1154                         if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1155                                 return -EINVAL;
1156                         if (ctrl.value < s->qctrl[i].minimum ||
1157                             ctrl.value > s->qctrl[i].maximum)
1158                                 return -ERANGE;
1159                         ctrl.value -= ctrl.value % s->qctrl[i].step;
1160                         break;
1161                 }
1162
1163         if ((err = s->set_ctrl(cam, &ctrl)))
1164                 return err;
1165
1166         s->_qctrl[i].default_value = ctrl.value;
1167
1168         return 0;
1169 }
1170
1171
1172 static int
1173 zc0301_vidioc_cropcap(struct zc0301_device* cam, void __user * arg)
1174 {
1175         struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1176
1177         cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1178         cc->pixelaspect.numerator = 1;
1179         cc->pixelaspect.denominator = 1;
1180
1181         if (copy_to_user(arg, cc, sizeof(*cc)))
1182                 return -EFAULT;
1183
1184         return 0;
1185 }
1186
1187
1188 static int
1189 zc0301_vidioc_g_crop(struct zc0301_device* cam, void __user * arg)
1190 {
1191         struct zc0301_sensor* s = &cam->sensor;
1192         struct v4l2_crop crop = {
1193                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1194         };
1195
1196         memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1197
1198         if (copy_to_user(arg, &crop, sizeof(crop)))
1199                 return -EFAULT;
1200
1201         return 0;
1202 }
1203
1204
1205 static int
1206 zc0301_vidioc_s_crop(struct zc0301_device* cam, void __user * arg)
1207 {
1208         struct zc0301_sensor* s = &cam->sensor;
1209         struct v4l2_crop crop;
1210         struct v4l2_rect* rect;
1211         struct v4l2_rect* bounds = &(s->cropcap.bounds);
1212         const enum zc0301_stream_state stream = cam->stream;
1213         const u32 nbuffers = cam->nbuffers;
1214         u32 i;
1215         int err = 0;
1216
1217         if (copy_from_user(&crop, arg, sizeof(crop)))
1218                 return -EFAULT;
1219
1220         rect = &(crop.c);
1221
1222         if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1223                 return -EINVAL;
1224
1225         if (cam->module_param.force_munmap)
1226                 for (i = 0; i < cam->nbuffers; i++)
1227                         if (cam->frame[i].vma_use_count) {
1228                                 DBG(3, "VIDIOC_S_CROP failed. "
1229                                        "Unmap the buffers first.");
1230                                 return -EBUSY;
1231                         }
1232
1233         if (!s->set_crop) {
1234                 memcpy(rect, &(s->_rect), sizeof(*rect));
1235                 if (copy_to_user(arg, &crop, sizeof(crop)))
1236                         return -EFAULT;
1237                 return 0;
1238         }
1239
1240         rect->left &= ~7L;
1241         rect->top &= ~7L;
1242         if (rect->width < 8)
1243                 rect->width = 8;
1244         if (rect->height < 8)
1245                 rect->height = 8;
1246         if (rect->width > bounds->width)
1247                 rect->width = bounds->width;
1248         if (rect->height > bounds->height)
1249                 rect->height = bounds->height;
1250         if (rect->left < bounds->left)
1251                 rect->left = bounds->left;
1252         if (rect->top < bounds->top)
1253                 rect->top = bounds->top;
1254         if (rect->left + rect->width > bounds->left + bounds->width)
1255                 rect->left = bounds->left+bounds->width - rect->width;
1256         if (rect->top + rect->height > bounds->top + bounds->height)
1257                 rect->top = bounds->top+bounds->height - rect->height;
1258         rect->width &= ~7L;
1259         rect->height &= ~7L;
1260
1261         if (cam->stream == STREAM_ON)
1262                 if ((err = zc0301_stream_interrupt(cam)))
1263                         return err;
1264
1265         if (copy_to_user(arg, &crop, sizeof(crop))) {
1266                 cam->stream = stream;
1267                 return -EFAULT;
1268         }
1269
1270         if (cam->module_param.force_munmap || cam->io == IO_READ)
1271                 zc0301_release_buffers(cam);
1272
1273         if (s->set_crop)
1274                 err += s->set_crop(cam, rect);
1275
1276         if (err) { /* atomic, no rollback in ioctl() */
1277                 cam->state |= DEV_MISCONFIGURED;
1278                 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1279                        "use the camera, close and open /dev/video%d again.",
1280                     cam->v4ldev->num);
1281                 return -EIO;
1282         }
1283
1284         s->pix_format.width = rect->width;
1285         s->pix_format.height = rect->height;
1286         memcpy(&(s->_rect), rect, sizeof(*rect));
1287
1288         if ((cam->module_param.force_munmap  || cam->io == IO_READ) &&
1289             nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1290                 cam->state |= DEV_MISCONFIGURED;
1291                 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1292                        "use the camera, close and open /dev/video%d again.",
1293                     cam->v4ldev->num);
1294                 return -ENOMEM;
1295         }
1296
1297         if (cam->io == IO_READ)
1298                 zc0301_empty_framequeues(cam);
1299         else if (cam->module_param.force_munmap)
1300                 zc0301_requeue_outqueue(cam);
1301
1302         cam->stream = stream;
1303
1304         return 0;
1305 }
1306
1307
1308 static int
1309 zc0301_vidioc_enum_framesizes(struct zc0301_device* cam, void __user * arg)
1310 {
1311         struct v4l2_frmsizeenum frmsize;
1312
1313         if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
1314                 return -EFAULT;
1315
1316         if (frmsize.index != 0 && frmsize.index != 1)
1317                 return -EINVAL;
1318
1319         if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG)
1320                 return -EINVAL;
1321
1322         frmsize.type = V4L2_FRMSIZE_TYPE_DISCRETE;
1323
1324         if (frmsize.index == 1) {
1325                 frmsize.discrete.width = cam->sensor.cropcap.defrect.width;
1326                 frmsize.discrete.height = cam->sensor.cropcap.defrect.height;
1327         }
1328         memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
1329
1330         if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
1331                 return -EFAULT;
1332
1333         return 0;
1334 }
1335
1336
1337 static int
1338 zc0301_vidioc_enum_fmt(struct zc0301_device* cam, void __user * arg)
1339 {
1340         struct v4l2_fmtdesc fmtd;
1341
1342         if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1343                 return -EFAULT;
1344
1345         if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1346                 return -EINVAL;
1347
1348         if (fmtd.index == 0) {
1349                 strcpy(fmtd.description, "JPEG");
1350                 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
1351                 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1352         } else
1353                 return -EINVAL;
1354
1355         fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1356         memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1357
1358         if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1359                 return -EFAULT;
1360
1361         return 0;
1362 }
1363
1364
1365 static int
1366 zc0301_vidioc_g_fmt(struct zc0301_device* cam, void __user * arg)
1367 {
1368         struct v4l2_format format;
1369         struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1370
1371         if (copy_from_user(&format, arg, sizeof(format)))
1372                 return -EFAULT;
1373
1374         if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1375                 return -EINVAL;
1376
1377         pfmt->bytesperline = 0;
1378         pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1379         pfmt->field = V4L2_FIELD_NONE;
1380         memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1381
1382         if (copy_to_user(arg, &format, sizeof(format)))
1383                 return -EFAULT;
1384
1385         return 0;
1386 }
1387
1388
1389 static int
1390 zc0301_vidioc_try_s_fmt(struct zc0301_device* cam, unsigned int cmd,
1391                         void __user * arg)
1392 {
1393         struct zc0301_sensor* s = &cam->sensor;
1394         struct v4l2_format format;
1395         struct v4l2_pix_format* pix;
1396         struct v4l2_pix_format* pfmt = &(s->pix_format);
1397         struct v4l2_rect* bounds = &(s->cropcap.bounds);
1398         struct v4l2_rect rect;
1399         const enum zc0301_stream_state stream = cam->stream;
1400         const u32 nbuffers = cam->nbuffers;
1401         u32 i;
1402         int err = 0;
1403
1404         if (copy_from_user(&format, arg, sizeof(format)))
1405                 return -EFAULT;
1406
1407         pix = &(format.fmt.pix);
1408
1409         if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1410                 return -EINVAL;
1411
1412         memcpy(&rect, &(s->_rect), sizeof(rect));
1413
1414         if (!s->set_crop) {
1415                 pix->width = rect.width;
1416                 pix->height = rect.height;
1417         } else {
1418                 rect.width = pix->width;
1419                 rect.height = pix->height;
1420         }
1421
1422         if (rect.width < 8)
1423                 rect.width = 8;
1424         if (rect.height < 8)
1425                 rect.height = 8;
1426         if (rect.width > bounds->left + bounds->width - rect.left)
1427                 rect.width = bounds->left + bounds->width - rect.left;
1428         if (rect.height > bounds->top + bounds->height - rect.top)
1429                 rect.height = bounds->top + bounds->height - rect.top;
1430         rect.width &= ~7L;
1431         rect.height &= ~7L;
1432
1433         pix->width = rect.width;
1434         pix->height = rect.height;
1435         pix->pixelformat = pfmt->pixelformat;
1436         pix->priv = pfmt->priv;
1437         pix->colorspace = pfmt->colorspace;
1438         pix->bytesperline = 0;
1439         pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
1440         pix->field = V4L2_FIELD_NONE;
1441
1442         if (cmd == VIDIOC_TRY_FMT) {
1443                 if (copy_to_user(arg, &format, sizeof(format)))
1444                         return -EFAULT;
1445                 return 0;
1446         }
1447
1448         if (cam->module_param.force_munmap)
1449                 for (i = 0; i < cam->nbuffers; i++)
1450                         if (cam->frame[i].vma_use_count) {
1451                                 DBG(3, "VIDIOC_S_FMT failed. "
1452                                        "Unmap the buffers first.");
1453                                 return -EBUSY;
1454                         }
1455
1456         if (cam->stream == STREAM_ON)
1457                 if ((err = zc0301_stream_interrupt(cam)))
1458                         return err;
1459
1460         if (copy_to_user(arg, &format, sizeof(format))) {
1461                 cam->stream = stream;
1462                 return -EFAULT;
1463         }
1464
1465         if (cam->module_param.force_munmap || cam->io == IO_READ)
1466                 zc0301_release_buffers(cam);
1467
1468         if (s->set_crop)
1469                 err += s->set_crop(cam, &rect);
1470
1471         if (err) { /* atomic, no rollback in ioctl() */
1472                 cam->state |= DEV_MISCONFIGURED;
1473                 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
1474                        "use the camera, close and open /dev/video%d again.",
1475                     cam->v4ldev->num);
1476                 return -EIO;
1477         }
1478
1479         memcpy(pfmt, pix, sizeof(*pix));
1480         memcpy(&(s->_rect), &rect, sizeof(rect));
1481
1482         if ((cam->module_param.force_munmap  || cam->io == IO_READ) &&
1483             nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1484                 cam->state |= DEV_MISCONFIGURED;
1485                 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
1486                        "use the camera, close and open /dev/video%d again.",
1487                     cam->v4ldev->num);
1488                 return -ENOMEM;
1489         }
1490
1491         if (cam->io == IO_READ)
1492                 zc0301_empty_framequeues(cam);
1493         else if (cam->module_param.force_munmap)
1494                 zc0301_requeue_outqueue(cam);
1495
1496         cam->stream = stream;
1497
1498         return 0;
1499 }
1500
1501
1502 static int
1503 zc0301_vidioc_g_jpegcomp(struct zc0301_device* cam, void __user * arg)
1504 {
1505         if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
1506                 return -EFAULT;
1507
1508         return 0;
1509 }
1510
1511
1512 static int
1513 zc0301_vidioc_s_jpegcomp(struct zc0301_device* cam, void __user * arg)
1514 {
1515         struct v4l2_jpegcompression jc;
1516         const enum zc0301_stream_state stream = cam->stream;
1517         int err = 0;
1518
1519         if (copy_from_user(&jc, arg, sizeof(jc)))
1520                 return -EFAULT;
1521
1522         if (jc.quality != 0)
1523                 return -EINVAL;
1524
1525         if (cam->stream == STREAM_ON)
1526                 if ((err = zc0301_stream_interrupt(cam)))
1527                         return err;
1528
1529         err += zc0301_set_compression(cam, &jc);
1530         if (err) { /* atomic, no rollback in ioctl() */
1531                 cam->state |= DEV_MISCONFIGURED;
1532                 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
1533                        "problems. To use the camera, close and open "
1534                        "/dev/video%d again.", cam->v4ldev->num);
1535                 return -EIO;
1536         }
1537
1538         cam->compression.quality = jc.quality;
1539
1540         cam->stream = stream;
1541
1542         return 0;
1543 }
1544
1545
1546 static int
1547 zc0301_vidioc_reqbufs(struct zc0301_device* cam, void __user * arg)
1548 {
1549         struct v4l2_requestbuffers rb;
1550         u32 i;
1551         int err;
1552
1553         if (copy_from_user(&rb, arg, sizeof(rb)))
1554                 return -EFAULT;
1555
1556         if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1557             rb.memory != V4L2_MEMORY_MMAP)
1558                 return -EINVAL;
1559
1560         if (cam->io == IO_READ) {
1561                 DBG(3, "Close and open the device again to choose the mmap "
1562                        "I/O method");
1563                 return -EBUSY;
1564         }
1565
1566         for (i = 0; i < cam->nbuffers; i++)
1567                 if (cam->frame[i].vma_use_count) {
1568                         DBG(3, "VIDIOC_REQBUFS failed. "
1569                                "Previous buffers are still mapped.");
1570                         return -EBUSY;
1571                 }
1572
1573         if (cam->stream == STREAM_ON)
1574                 if ((err = zc0301_stream_interrupt(cam)))
1575                         return err;
1576
1577         zc0301_empty_framequeues(cam);
1578
1579         zc0301_release_buffers(cam);
1580         if (rb.count)
1581                 rb.count = zc0301_request_buffers(cam, rb.count, IO_MMAP);
1582
1583         if (copy_to_user(arg, &rb, sizeof(rb))) {
1584                 zc0301_release_buffers(cam);
1585                 cam->io = IO_NONE;
1586                 return -EFAULT;
1587         }
1588
1589         cam->io = rb.count ? IO_MMAP : IO_NONE;
1590
1591         return 0;
1592 }
1593
1594
1595 static int
1596 zc0301_vidioc_querybuf(struct zc0301_device* cam, void __user * arg)
1597 {
1598         struct v4l2_buffer b;
1599
1600         if (copy_from_user(&b, arg, sizeof(b)))
1601                 return -EFAULT;
1602
1603         if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1604             b.index >= cam->nbuffers || cam->io != IO_MMAP)
1605                 return -EINVAL;
1606
1607         memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
1608
1609         if (cam->frame[b.index].vma_use_count)
1610                 b.flags |= V4L2_BUF_FLAG_MAPPED;
1611
1612         if (cam->frame[b.index].state == F_DONE)
1613                 b.flags |= V4L2_BUF_FLAG_DONE;
1614         else if (cam->frame[b.index].state != F_UNUSED)
1615                 b.flags |= V4L2_BUF_FLAG_QUEUED;
1616
1617         if (copy_to_user(arg, &b, sizeof(b)))
1618                 return -EFAULT;
1619
1620         return 0;
1621 }
1622
1623
1624 static int
1625 zc0301_vidioc_qbuf(struct zc0301_device* cam, void __user * arg)
1626 {
1627         struct v4l2_buffer b;
1628         unsigned long lock_flags;
1629
1630         if (copy_from_user(&b, arg, sizeof(b)))
1631                 return -EFAULT;
1632
1633         if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1634             b.index >= cam->nbuffers || cam->io != IO_MMAP)
1635                 return -EINVAL;
1636
1637         if (cam->frame[b.index].state != F_UNUSED)
1638                 return -EINVAL;
1639
1640         cam->frame[b.index].state = F_QUEUED;
1641
1642         spin_lock_irqsave(&cam->queue_lock, lock_flags);
1643         list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
1644         spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1645
1646         PDBGG("Frame #%lu queued", (unsigned long)b.index);
1647
1648         return 0;
1649 }
1650
1651
1652 static int
1653 zc0301_vidioc_dqbuf(struct zc0301_device* cam, struct file* filp,
1654                     void __user * arg)
1655 {
1656         struct v4l2_buffer b;
1657         struct zc0301_frame_t *f;
1658         unsigned long lock_flags;
1659         long timeout;
1660
1661         if (copy_from_user(&b, arg, sizeof(b)))
1662                 return -EFAULT;
1663
1664         if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
1665                 return -EINVAL;
1666
1667         if (list_empty(&cam->outqueue)) {
1668                 if (cam->stream == STREAM_OFF)
1669                         return -EINVAL;
1670                 if (filp->f_flags & O_NONBLOCK)
1671                         return -EAGAIN;
1672                 timeout = wait_event_interruptible_timeout
1673                           ( cam->wait_frame,
1674                             (!list_empty(&cam->outqueue)) ||
1675                             (cam->state & DEV_DISCONNECTED) ||
1676                             (cam->state & DEV_MISCONFIGURED),
1677                             cam->module_param.frame_timeout *
1678                             1000 * msecs_to_jiffies(1) );
1679                 if (timeout < 0)
1680                         return timeout;
1681                 if (cam->state & DEV_DISCONNECTED)
1682                         return -ENODEV;
1683                 if (!timeout || (cam->state & DEV_MISCONFIGURED))
1684                         return -EIO;
1685         }
1686
1687         spin_lock_irqsave(&cam->queue_lock, lock_flags);
1688         f = list_entry(cam->outqueue.next, struct zc0301_frame_t, frame);
1689         list_del(cam->outqueue.next);
1690         spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1691
1692         f->state = F_UNUSED;
1693
1694         memcpy(&b, &f->buf, sizeof(b));
1695         if (f->vma_use_count)
1696                 b.flags |= V4L2_BUF_FLAG_MAPPED;
1697
1698         if (copy_to_user(arg, &b, sizeof(b)))
1699                 return -EFAULT;
1700
1701         PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
1702
1703         return 0;
1704 }
1705
1706
1707 static int
1708 zc0301_vidioc_streamon(struct zc0301_device* cam, void __user * arg)
1709 {
1710         int type;
1711
1712         if (copy_from_user(&type, arg, sizeof(type)))
1713                 return -EFAULT;
1714
1715         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1716                 return -EINVAL;
1717
1718         cam->stream = STREAM_ON;
1719
1720         DBG(3, "Stream on");
1721
1722         return 0;
1723 }
1724
1725
1726 static int
1727 zc0301_vidioc_streamoff(struct zc0301_device* cam, void __user * arg)
1728 {
1729         int type, err;
1730
1731         if (copy_from_user(&type, arg, sizeof(type)))
1732                 return -EFAULT;
1733
1734         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1735                 return -EINVAL;
1736
1737         if (cam->stream == STREAM_ON)
1738                 if ((err = zc0301_stream_interrupt(cam)))
1739                         return err;
1740
1741         zc0301_empty_framequeues(cam);
1742
1743         DBG(3, "Stream off");
1744
1745         return 0;
1746 }
1747
1748
1749 static int
1750 zc0301_vidioc_g_parm(struct zc0301_device* cam, void __user * arg)
1751 {
1752         struct v4l2_streamparm sp;
1753
1754         if (copy_from_user(&sp, arg, sizeof(sp)))
1755                 return -EFAULT;
1756
1757         if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1758                 return -EINVAL;
1759
1760         sp.parm.capture.extendedmode = 0;
1761         sp.parm.capture.readbuffers = cam->nreadbuffers;
1762
1763         if (copy_to_user(arg, &sp, sizeof(sp)))
1764                 return -EFAULT;
1765
1766         return 0;
1767 }
1768
1769
1770 static int
1771 zc0301_vidioc_s_parm(struct zc0301_device* cam, void __user * arg)
1772 {
1773         struct v4l2_streamparm sp;
1774
1775         if (copy_from_user(&sp, arg, sizeof(sp)))
1776                 return -EFAULT;
1777
1778         if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1779                 return -EINVAL;
1780
1781         sp.parm.capture.extendedmode = 0;
1782
1783         if (sp.parm.capture.readbuffers == 0)
1784                 sp.parm.capture.readbuffers = cam->nreadbuffers;
1785
1786         if (sp.parm.capture.readbuffers > ZC0301_MAX_FRAMES)
1787                 sp.parm.capture.readbuffers = ZC0301_MAX_FRAMES;
1788
1789         if (copy_to_user(arg, &sp, sizeof(sp)))
1790                 return -EFAULT;
1791
1792         cam->nreadbuffers = sp.parm.capture.readbuffers;
1793
1794         return 0;
1795 }
1796
1797
1798 static long zc0301_ioctl_v4l2(struct file *filp,
1799                              unsigned int cmd, void __user *arg)
1800 {
1801         struct zc0301_device *cam = video_drvdata(filp);
1802
1803         switch (cmd) {
1804
1805         case VIDIOC_QUERYCAP:
1806                 return zc0301_vidioc_querycap(cam, arg);
1807
1808         case VIDIOC_ENUMINPUT:
1809                 return zc0301_vidioc_enuminput(cam, arg);
1810
1811         case VIDIOC_G_INPUT:
1812                 return zc0301_vidioc_g_input(cam, arg);
1813
1814         case VIDIOC_S_INPUT:
1815                 return zc0301_vidioc_s_input(cam, arg);
1816
1817         case VIDIOC_QUERYCTRL:
1818                 return zc0301_vidioc_query_ctrl(cam, arg);
1819
1820         case VIDIOC_G_CTRL:
1821                 return zc0301_vidioc_g_ctrl(cam, arg);
1822
1823         case VIDIOC_S_CTRL:
1824                 return zc0301_vidioc_s_ctrl(cam, arg);
1825
1826         case VIDIOC_CROPCAP:
1827                 return zc0301_vidioc_cropcap(cam, arg);
1828
1829         case VIDIOC_G_CROP:
1830                 return zc0301_vidioc_g_crop(cam, arg);
1831
1832         case VIDIOC_S_CROP:
1833                 return zc0301_vidioc_s_crop(cam, arg);
1834
1835         case VIDIOC_ENUM_FMT:
1836                 return zc0301_vidioc_enum_fmt(cam, arg);
1837
1838         case VIDIOC_G_FMT:
1839                 return zc0301_vidioc_g_fmt(cam, arg);
1840
1841         case VIDIOC_TRY_FMT:
1842         case VIDIOC_S_FMT:
1843                 return zc0301_vidioc_try_s_fmt(cam, cmd, arg);
1844
1845         case VIDIOC_ENUM_FRAMESIZES:
1846                 return zc0301_vidioc_enum_framesizes(cam, arg);
1847
1848         case VIDIOC_G_JPEGCOMP:
1849                 return zc0301_vidioc_g_jpegcomp(cam, arg);
1850
1851         case VIDIOC_S_JPEGCOMP:
1852                 return zc0301_vidioc_s_jpegcomp(cam, arg);
1853
1854         case VIDIOC_REQBUFS:
1855                 return zc0301_vidioc_reqbufs(cam, arg);
1856
1857         case VIDIOC_QUERYBUF:
1858                 return zc0301_vidioc_querybuf(cam, arg);
1859
1860         case VIDIOC_QBUF:
1861                 return zc0301_vidioc_qbuf(cam, arg);
1862
1863         case VIDIOC_DQBUF:
1864                 return zc0301_vidioc_dqbuf(cam, filp, arg);
1865
1866         case VIDIOC_STREAMON:
1867                 return zc0301_vidioc_streamon(cam, arg);
1868
1869         case VIDIOC_STREAMOFF:
1870                 return zc0301_vidioc_streamoff(cam, arg);
1871
1872         case VIDIOC_G_PARM:
1873                 return zc0301_vidioc_g_parm(cam, arg);
1874
1875         case VIDIOC_S_PARM:
1876                 return zc0301_vidioc_s_parm(cam, arg);
1877
1878         case VIDIOC_G_STD:
1879         case VIDIOC_S_STD:
1880         case VIDIOC_QUERYSTD:
1881         case VIDIOC_ENUMSTD:
1882         case VIDIOC_QUERYMENU:
1883         case VIDIOC_ENUM_FRAMEINTERVALS:
1884                 return -EINVAL;
1885
1886         default:
1887                 return -EINVAL;
1888
1889         }
1890 }
1891
1892
1893 static long zc0301_ioctl(struct file *filp,
1894                         unsigned int cmd, unsigned long arg)
1895 {
1896         struct zc0301_device *cam = video_drvdata(filp);
1897         int err = 0;
1898
1899         if (mutex_lock_interruptible(&cam->fileop_mutex))
1900                 return -ERESTARTSYS;
1901
1902         if (cam->state & DEV_DISCONNECTED) {
1903                 DBG(1, "Device not present");
1904                 mutex_unlock(&cam->fileop_mutex);
1905                 return -ENODEV;
1906         }
1907
1908         if (cam->state & DEV_MISCONFIGURED) {
1909                 DBG(1, "The camera is misconfigured. Close and open it "
1910                        "again.");
1911                 mutex_unlock(&cam->fileop_mutex);
1912                 return -EIO;
1913         }
1914
1915         V4LDBG(3, "zc0301", cmd);
1916
1917         err = zc0301_ioctl_v4l2(filp, cmd, (void __user *)arg);
1918
1919         mutex_unlock(&cam->fileop_mutex);
1920
1921         return err;
1922 }
1923
1924
1925 static const struct v4l2_file_operations zc0301_fops = {
1926         .owner =   THIS_MODULE,
1927         .open =    zc0301_open,
1928         .release = zc0301_release,
1929         .ioctl =   zc0301_ioctl,
1930         .read =    zc0301_read,
1931         .poll =    zc0301_poll,
1932         .mmap =    zc0301_mmap,
1933 };
1934
1935 /*****************************************************************************/
1936
1937 static int
1938 zc0301_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
1939 {
1940         struct usb_device *udev = interface_to_usbdev(intf);
1941         struct zc0301_device* cam;
1942         static unsigned int dev_nr;
1943         unsigned int i;
1944         int err = 0;
1945
1946         if (!(cam = kzalloc(sizeof(struct zc0301_device), GFP_KERNEL)))
1947                 return -ENOMEM;
1948
1949         cam->usbdev = udev;
1950
1951         if (!(cam->control_buffer = kzalloc(4, GFP_KERNEL))) {
1952                 DBG(1, "kmalloc() failed");
1953                 err = -ENOMEM;
1954                 goto fail;
1955         }
1956
1957         if (!(cam->v4ldev = video_device_alloc())) {
1958                 DBG(1, "video_device_alloc() failed");
1959                 err = -ENOMEM;
1960                 goto fail;
1961         }
1962
1963         DBG(2, "ZC0301[P] Image Processor and Control Chip detected "
1964                "(vid/pid 0x%04X:0x%04X)",id->idVendor, id->idProduct);
1965
1966         for  (i = 0; zc0301_sensor_table[i]; i++) {
1967                 err = zc0301_sensor_table[i](cam);
1968                 if (!err)
1969                         break;
1970         }
1971
1972         if (!err)
1973                 DBG(2, "%s image sensor detected", cam->sensor.name);
1974         else {
1975                 DBG(1, "No supported image sensor detected");
1976                 err = -ENODEV;
1977                 goto fail;
1978         }
1979
1980         if (zc0301_init(cam)) {
1981                 DBG(1, "Initialization failed. I will retry on open().");
1982                 cam->state |= DEV_MISCONFIGURED;
1983         }
1984
1985         strcpy(cam->v4ldev->name, "ZC0301[P] PC Camera");
1986         cam->v4ldev->fops = &zc0301_fops;
1987         cam->v4ldev->minor = video_nr[dev_nr];
1988         cam->v4ldev->release = video_device_release;
1989         cam->v4ldev->parent = &udev->dev;
1990         video_set_drvdata(cam->v4ldev, cam);
1991
1992         init_completion(&cam->probe);
1993
1994         err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
1995                                     video_nr[dev_nr]);
1996         if (err) {
1997                 DBG(1, "V4L2 device registration failed");
1998                 if (err == -ENFILE && video_nr[dev_nr] == -1)
1999                         DBG(1, "Free /dev/videoX node not found");
2000                 video_nr[dev_nr] = -1;
2001                 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
2002                 complete_all(&cam->probe);
2003                 goto fail;
2004         }
2005
2006         DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->num);
2007
2008         cam->module_param.force_munmap = force_munmap[dev_nr];
2009         cam->module_param.frame_timeout = frame_timeout[dev_nr];
2010
2011         dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
2012
2013         usb_set_intfdata(intf, cam);
2014         kref_init(&cam->kref);
2015         usb_get_dev(cam->usbdev);
2016
2017         complete_all(&cam->probe);
2018
2019         return 0;
2020
2021 fail:
2022         if (cam) {
2023                 kfree(cam->control_buffer);
2024                 if (cam->v4ldev)
2025                         video_device_release(cam->v4ldev);
2026                 kfree(cam);
2027         }
2028         return err;
2029 }
2030
2031
2032 static void zc0301_usb_disconnect(struct usb_interface* intf)
2033 {
2034         struct zc0301_device* cam;
2035
2036         down_write(&zc0301_dev_lock);
2037
2038         cam = usb_get_intfdata(intf);
2039
2040         DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2041
2042         if (cam->users) {
2043                 DBG(2, "Device /dev/video%d is open! Deregistration and "
2044                        "memory deallocation are deferred.",
2045                     cam->v4ldev->num);
2046                 cam->state |= DEV_MISCONFIGURED;
2047                 zc0301_stop_transfer(cam);
2048                 cam->state |= DEV_DISCONNECTED;
2049                 wake_up_interruptible(&cam->wait_frame);
2050                 wake_up(&cam->wait_stream);
2051         } else
2052                 cam->state |= DEV_DISCONNECTED;
2053
2054         wake_up_interruptible_all(&cam->wait_open);
2055
2056         kref_put(&cam->kref, zc0301_release_resources);
2057
2058         up_write(&zc0301_dev_lock);
2059 }
2060
2061
2062 static struct usb_driver zc0301_usb_driver = {
2063         .name =       "zc0301",
2064         .id_table =   zc0301_id_table,
2065         .probe =      zc0301_usb_probe,
2066         .disconnect = zc0301_usb_disconnect,
2067 };
2068
2069 /*****************************************************************************/
2070
2071 static int __init zc0301_module_init(void)
2072 {
2073         int err = 0;
2074
2075         KDBG(2, ZC0301_MODULE_NAME " v" ZC0301_MODULE_VERSION);
2076         KDBG(3, ZC0301_MODULE_AUTHOR);
2077
2078         if ((err = usb_register(&zc0301_usb_driver)))
2079                 KDBG(1, "usb_register() failed");
2080
2081         return err;
2082 }
2083
2084
2085 static void __exit zc0301_module_exit(void)
2086 {
2087         usb_deregister(&zc0301_usb_driver);
2088 }
2089
2090
2091 module_init(zc0301_module_init);
2092 module_exit(zc0301_module_exit);