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