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