usb gadget: issue notifications from ACM function
[linux-3.10.git] / drivers / usb / image / mdc800.c
1 /*
2  * copyright (C) 1999/2000 by Henning Zabel <henning@uni-paderborn.de>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19
20 /*
21  *      USB-Kernel Driver for the Mustek MDC800 Digital Camera
22  *      (c) 1999/2000 Henning Zabel <henning@uni-paderborn.de>
23  *
24  *
25  * The driver brings the USB functions of the MDC800 to Linux.
26  * To use the Camera you must support the USB Protocol of the camera
27  * to the Kernel Node.
28  * The Driver uses a misc device Node. Create it with :
29  * mknod /dev/mustek c 180 32
30  *
31  * The driver supports only one camera.
32  * 
33  * Fix: mdc800 used sleep_on and slept with io_lock held.
34  * Converted sleep_on to waitqueues with schedule_timeout and made io_lock
35  * a semaphore from a spinlock.
36  * by Oliver Neukum <oliver@neukum.name>
37  * (02/12/2001)
38  * 
39  * Identify version on module load.
40  * (08/04/2001) gb
41  *
42  * version 0.7.5
43  * Fixed potential SMP races with Spinlocks.
44  * Thanks to Oliver Neukum <oliver@neukum.name> who 
45  * noticed the race conditions.
46  * (30/10/2000)
47  *
48  * Fixed: Setting urb->dev before submitting urb.
49  * by Greg KH <greg@kroah.com>
50  * (13/10/2000)
51  *
52  * version 0.7.3
53  * bugfix : The mdc800->state field gets set to READY after the
54  * the diconnect function sets it to NOT_CONNECTED. This makes the
55  * driver running like the camera is connected and causes some
56  * hang ups.
57  *
58  * version 0.7.1
59  * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
60  * problems when compiled as Module.
61  * (04/04/2000)
62  *
63  * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
64  * was updated to use these values.
65  * (26/03/2000)
66  *
67  * The Init und Exit Module Function are updated.
68  * (01/03/2000)
69  *
70  * version 0.7.0
71  * Rewrite of the driver : The driver now uses URB's. The old stuff
72  * has been removed.
73  *
74  * version 0.6.0
75  * Rewrite of this driver: The Emulation of the rs232 protocoll
76  * has been removed from the driver. A special executeCommand function
77  * for this driver is included to gphoto.
78  * The driver supports two kind of communication to bulk endpoints.
79  * Either with the dev->bus->ops->bulk... or with callback function.
80  * (09/11/1999)
81  *
82  * version 0.5.0:
83  * first Version that gets a version number. Most of the needed
84  * functions work.
85  * (20/10/1999)
86  */
87
88 #include <linux/sched.h>
89 #include <linux/signal.h>
90 #include <linux/spinlock.h>
91 #include <linux/errno.h>
92 #include <linux/random.h>
93 #include <linux/poll.h>
94 #include <linux/init.h>
95 #include <linux/slab.h>
96 #include <linux/module.h>
97 #include <linux/wait.h>
98 #include <linux/mutex.h>
99
100 #include <linux/usb.h>
101 #include <linux/fs.h>
102
103 /*
104  * Version Information
105  */
106 #define DRIVER_VERSION "v0.7.5 (30/10/2000)"
107 #define DRIVER_AUTHOR "Henning Zabel <henning@uni-paderborn.de>"
108 #define DRIVER_DESC "USB Driver for Mustek MDC800 Digital Camera"
109
110 /* Vendor and Product Information */
111 #define MDC800_VENDOR_ID        0x055f
112 #define MDC800_PRODUCT_ID       0xa800
113
114 /* Timeouts (msec) */
115 #define TO_DOWNLOAD_GET_READY           1500
116 #define TO_DOWNLOAD_GET_BUSY            1500
117 #define TO_WRITE_GET_READY              1000
118 #define TO_DEFAULT_COMMAND              5000
119 #define TO_READ_FROM_IRQ                TO_DEFAULT_COMMAND
120 #define TO_GET_READY                    TO_DEFAULT_COMMAND
121
122 /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
123 #define MDC800_DEVICE_MINOR_BASE 32
124
125
126 /**************************************************************************
127         Data and structs
128 ***************************************************************************/
129
130
131 typedef enum {
132         NOT_CONNECTED, READY, WORKING, DOWNLOAD
133 } mdc800_state;
134
135
136 /* Data for the driver */
137 struct mdc800_data
138 {
139         struct usb_device *     dev;                    // Device Data
140         mdc800_state            state;
141
142         unsigned int            endpoint [4];
143
144         struct urb *            irq_urb;
145         wait_queue_head_t       irq_wait;
146         int                     irq_woken;
147         char*                   irq_urb_buffer;
148
149         int                     camera_busy;          // is camera busy ?
150         int                     camera_request_ready; // Status to synchronize with irq
151         char                    camera_response [8];  // last Bytes send after busy
152
153         struct urb *            write_urb;
154         char*                   write_urb_buffer;
155         wait_queue_head_t       write_wait;
156         int                     written;
157
158
159         struct urb *            download_urb;
160         char*                   download_urb_buffer;
161         wait_queue_head_t       download_wait;
162         int                     downloaded;
163         int                     download_left;          // Bytes left to download ?
164
165
166         /* Device Data */
167         char                    out [64];       // Answer Buffer
168         int                     out_ptr;        // Index to the first not readen byte
169         int                     out_count;      // Bytes in the buffer
170
171         int                     open;           // Camera device open ?
172         struct mutex            io_lock;        // IO -lock
173
174         char                    in [8];         // Command Input Buffer
175         int                     in_count;
176
177         int                     pic_index;      // Cache for the Imagesize (-1 for nothing cached )
178         int                     pic_len;
179         int                     minor;
180 };
181
182
183 /* Specification of the Endpoints */
184 static struct usb_endpoint_descriptor mdc800_ed [4] =
185 {
186         { 
187                 .bLength =              0,
188                 .bDescriptorType =      0,
189                 .bEndpointAddress =     0x01,
190                 .bmAttributes =         0x02,
191                 .wMaxPacketSize =       __constant_cpu_to_le16(8),
192                 .bInterval =            0,
193                 .bRefresh =             0,
194                 .bSynchAddress =        0,
195         },
196         {
197                 .bLength =              0,
198                 .bDescriptorType =      0,
199                 .bEndpointAddress =     0x82,
200                 .bmAttributes =         0x03,
201                 .wMaxPacketSize =       __constant_cpu_to_le16(8),
202                 .bInterval =            0,
203                 .bRefresh =             0,
204                 .bSynchAddress =        0,
205         },
206         {
207                 .bLength =              0,
208                 .bDescriptorType =      0,
209                 .bEndpointAddress =     0x03,
210                 .bmAttributes =         0x02,
211                 .wMaxPacketSize =       __constant_cpu_to_le16(64),
212                 .bInterval =            0,
213                 .bRefresh =             0,
214                 .bSynchAddress =        0,
215         },
216         {
217                 .bLength =              0,
218                 .bDescriptorType =      0,
219                 .bEndpointAddress =     0x84,
220                 .bmAttributes =         0x02,
221                 .wMaxPacketSize =       __constant_cpu_to_le16(64),
222                 .bInterval =            0,
223                 .bRefresh =             0,
224                 .bSynchAddress =        0,
225         },
226 };
227
228 /* The Variable used by the driver */
229 static struct mdc800_data* mdc800;
230
231
232 /***************************************************************************
233         The USB Part of the driver
234 ****************************************************************************/
235
236 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
237 {
238         return (
239                    ( a->bEndpointAddress == b->bEndpointAddress )
240                 && ( a->bmAttributes     == b->bmAttributes     )
241                 && ( a->wMaxPacketSize   == b->wMaxPacketSize   )
242         );
243 }
244
245
246 /*
247  * Checks whether the camera responds busy
248  */
249 static int mdc800_isBusy (char* ch)
250 {
251         int i=0;
252         while (i<8)
253         {
254                 if (ch [i] != (char)0x99)
255                         return 0;
256                 i++;
257         }
258         return 1;
259 }
260
261
262 /*
263  * Checks whether the Camera is ready
264  */
265 static int mdc800_isReady (char *ch)
266 {
267         int i=0;
268         while (i<8)
269         {
270                 if (ch [i] != (char)0xbb)
271                         return 0;
272                 i++;
273         }
274         return 1;
275 }
276
277
278
279 /*
280  * USB IRQ Handler for InputLine
281  */
282 static void mdc800_usb_irq (struct urb *urb)
283 {
284         int data_received=0, wake_up;
285         unsigned char* b=urb->transfer_buffer;
286         struct mdc800_data* mdc800=urb->context;
287         int status = urb->status;
288
289         if (status >= 0) {
290
291                 //dbg ("%i %i %i %i %i %i %i %i \n",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
292
293                 if (mdc800_isBusy (b))
294                 {
295                         if (!mdc800->camera_busy)
296                         {
297                                 mdc800->camera_busy=1;
298                                 dbg ("gets busy");
299                         }
300                 }
301                 else
302                 {
303                         if (mdc800->camera_busy && mdc800_isReady (b))
304                         {
305                                 mdc800->camera_busy=0;
306                                 dbg ("gets ready");
307                         }
308                 }
309                 if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
310                 {
311                         /* Store Data in camera_answer field */
312                         dbg ("%i %i %i %i %i %i %i %i ",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
313
314                         memcpy (mdc800->camera_response,b,8);
315                         data_received=1;
316                 }
317         }
318         wake_up= ( mdc800->camera_request_ready > 0 )
319                 &&
320                 (
321                         ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
322                 ||
323                         ((mdc800->camera_request_ready == 2) && data_received)
324                 ||
325                         ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
326                 ||
327                         (status < 0)
328                 );
329
330         if (wake_up)
331         {
332                 mdc800->camera_request_ready=0;
333                 mdc800->irq_woken=1;
334                 wake_up (&mdc800->irq_wait);
335         }
336 }
337
338
339 /*
340  * Waits a while until the irq responds that camera is ready
341  *
342  *  mode : 0: Wait for camera gets ready
343  *         1: Wait for receiving data
344  *         2: Wait for camera gets busy
345  *
346  * msec: Time to wait
347  */
348 static int mdc800_usb_waitForIRQ (int mode, int msec)
349 {
350         mdc800->camera_request_ready=1+mode;
351
352         wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000);
353         mdc800->irq_woken = 0;
354
355         if (mdc800->camera_request_ready>0)
356         {
357                 mdc800->camera_request_ready=0;
358                 err ("timeout waiting for camera.");
359                 return -1;
360         }
361         
362         if (mdc800->state == NOT_CONNECTED)
363         {
364                 warn ("Camera gets disconnected during waiting for irq.");
365                 mdc800->camera_request_ready=0;
366                 return -2;
367         }
368         
369         return 0;
370 }
371
372
373 /*
374  * The write_urb callback function
375  */
376 static void mdc800_usb_write_notify (struct urb *urb)
377 {
378         struct mdc800_data* mdc800=urb->context;
379         int status = urb->status;
380
381         if (status != 0)
382                 err ("writing command fails (status=%i)", status);
383         else
384                 mdc800->state=READY;
385         mdc800->written = 1;
386         wake_up (&mdc800->write_wait);
387 }
388
389
390 /*
391  * The download_urb callback function
392  */
393 static void mdc800_usb_download_notify (struct urb *urb)
394 {
395         struct mdc800_data* mdc800=urb->context;
396         int status = urb->status;
397
398         if (status == 0) {
399                 /* Fill output buffer with these data */
400                 memcpy (mdc800->out,  urb->transfer_buffer, 64);
401                 mdc800->out_count=64;
402                 mdc800->out_ptr=0;
403                 mdc800->download_left-=64;
404                 if (mdc800->download_left == 0)
405                 {
406                         mdc800->state=READY;
407                 }
408         } else {
409                 err ("request bytes fails (status:%i)", status);
410         }
411         mdc800->downloaded = 1;
412         wake_up (&mdc800->download_wait);
413 }
414
415
416 /***************************************************************************
417         Probing for the Camera
418  ***************************************************************************/
419
420 static struct usb_driver mdc800_usb_driver;
421 static const struct file_operations mdc800_device_ops;
422 static struct usb_class_driver mdc800_class = {
423         .name =         "mdc800%d",
424         .fops =         &mdc800_device_ops,
425         .minor_base =   MDC800_DEVICE_MINOR_BASE,
426 };
427
428
429 /*
430  * Callback to search the Mustek MDC800 on the USB Bus
431  */
432 static int mdc800_usb_probe (struct usb_interface *intf,
433                                const struct usb_device_id *id)
434 {
435         int i,j;
436         struct usb_host_interface *intf_desc;
437         struct usb_device *dev = interface_to_usbdev (intf);
438         int irq_interval=0;
439         int retval;
440
441         dbg ("(mdc800_usb_probe) called.");
442
443
444         if (mdc800->dev != NULL)
445         {
446                 warn ("only one Mustek MDC800 is supported.");
447                 return -ENODEV;
448         }
449
450         if (dev->descriptor.bNumConfigurations != 1)
451         {
452                 err ("probe fails -> wrong Number of Configuration");
453                 return -ENODEV;
454         }
455         intf_desc = intf->cur_altsetting;
456
457         if (
458                         ( intf_desc->desc.bInterfaceClass != 0xff )
459                 ||      ( intf_desc->desc.bInterfaceSubClass != 0 )
460                 || ( intf_desc->desc.bInterfaceProtocol != 0 )
461                 || ( intf_desc->desc.bNumEndpoints != 4)
462         )
463         {
464                 err ("probe fails -> wrong Interface");
465                 return -ENODEV;
466         }
467
468         /* Check the Endpoints */
469         for (i=0; i<4; i++)
470         {
471                 mdc800->endpoint[i]=-1;
472                 for (j=0; j<4; j++)
473                 {
474                         if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
475                         {
476                                 mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
477                                 if (i==1)
478                                 {
479                                         irq_interval=intf_desc->endpoint [j].desc.bInterval;
480                                 }
481                         }
482                 }
483                 if (mdc800->endpoint[i] == -1)
484                 {
485                         err ("probe fails -> Wrong Endpoints.");
486                         return -ENODEV;
487                 }
488         }
489
490
491         info ("Found Mustek MDC800 on USB.");
492
493         mutex_lock(&mdc800->io_lock);
494
495         retval = usb_register_dev(intf, &mdc800_class);
496         if (retval) {
497                 err ("Not able to get a minor for this device.");
498                 return -ENODEV;
499         }
500
501         mdc800->dev=dev;
502         mdc800->open=0;
503
504         /* Setup URB Structs */
505         usb_fill_int_urb (
506                 mdc800->irq_urb,
507                 mdc800->dev,
508                 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
509                 mdc800->irq_urb_buffer,
510                 8,
511                 mdc800_usb_irq,
512                 mdc800,
513                 irq_interval
514         );
515
516         usb_fill_bulk_urb (
517                 mdc800->write_urb,
518                 mdc800->dev,
519                 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
520                 mdc800->write_urb_buffer,
521                 8,
522                 mdc800_usb_write_notify,
523                 mdc800
524         );
525
526         usb_fill_bulk_urb (
527                 mdc800->download_urb,
528                 mdc800->dev,
529                 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
530                 mdc800->download_urb_buffer,
531                 64,
532                 mdc800_usb_download_notify,
533                 mdc800
534         );
535
536         mdc800->state=READY;
537
538         mutex_unlock(&mdc800->io_lock);
539         
540         usb_set_intfdata(intf, mdc800);
541         return 0;
542 }
543
544
545 /*
546  * Disconnect USB device (maybe the MDC800)
547  */
548 static void mdc800_usb_disconnect (struct usb_interface *intf)
549 {
550         struct mdc800_data* mdc800 = usb_get_intfdata(intf);
551
552         dbg ("(mdc800_usb_disconnect) called");
553
554         if (mdc800) {
555                 if (mdc800->state == NOT_CONNECTED)
556                         return;
557
558                 usb_deregister_dev(intf, &mdc800_class);
559
560                 /* must be under lock to make sure no URB
561                    is submitted after usb_kill_urb() */
562                 mutex_lock(&mdc800->io_lock);
563                 mdc800->state=NOT_CONNECTED;
564
565                 usb_kill_urb(mdc800->irq_urb);
566                 usb_kill_urb(mdc800->write_urb);
567                 usb_kill_urb(mdc800->download_urb);
568                 mutex_unlock(&mdc800->io_lock);
569
570                 mdc800->dev = NULL;
571                 usb_set_intfdata(intf, NULL);
572         }
573         info ("Mustek MDC800 disconnected from USB.");
574 }
575
576
577 /***************************************************************************
578         The Misc device Part (file_operations)
579 ****************************************************************************/
580
581 /*
582  * This Function calc the Answersize for a command.
583  */
584 static int mdc800_getAnswerSize (char command)
585 {
586         switch ((unsigned char) command)
587         {
588                 case 0x2a:
589                 case 0x49:
590                 case 0x51:
591                 case 0x0d:
592                 case 0x20:
593                 case 0x07:
594                 case 0x01:
595                 case 0x25:
596                 case 0x00:
597                         return 8;
598
599                 case 0x05:
600                 case 0x3e:
601                         return mdc800->pic_len;
602
603                 case 0x09:
604                         return 4096;
605
606                 default:
607                         return 0;
608         }
609 }
610
611
612 /*
613  * Init the device: (1) alloc mem (2) Increase MOD Count ..
614  */
615 static int mdc800_device_open (struct inode* inode, struct file *file)
616 {
617         int retval=0;
618         int errn=0;
619
620         mutex_lock(&mdc800->io_lock);
621         
622         if (mdc800->state == NOT_CONNECTED)
623         {
624                 errn=-EBUSY;
625                 goto error_out;
626         }
627         if (mdc800->open)
628         {
629                 errn=-EBUSY;
630                 goto error_out;
631         }
632
633         mdc800->in_count=0;
634         mdc800->out_count=0;
635         mdc800->out_ptr=0;
636         mdc800->pic_index=0;
637         mdc800->pic_len=-1;
638         mdc800->download_left=0;
639
640         mdc800->camera_busy=0;
641         mdc800->camera_request_ready=0;
642
643         retval=0;
644         mdc800->irq_urb->dev = mdc800->dev;
645         retval = usb_submit_urb (mdc800->irq_urb, GFP_KERNEL);
646         if (retval) {
647                 err ("request USB irq fails (submit_retval=%i).", retval);
648                 errn = -EIO;
649                 goto error_out;
650         }
651
652         mdc800->open=1;
653         dbg ("Mustek MDC800 device opened.");
654
655 error_out:
656         mutex_unlock(&mdc800->io_lock);
657         return errn;
658 }
659
660
661 /*
662  * Close the Camera and release Memory
663  */
664 static int mdc800_device_release (struct inode* inode, struct file *file)
665 {
666         int retval=0;
667         dbg ("Mustek MDC800 device closed.");
668
669         mutex_lock(&mdc800->io_lock);
670         if (mdc800->open && (mdc800->state != NOT_CONNECTED))
671         {
672                 usb_kill_urb(mdc800->irq_urb);
673                 usb_kill_urb(mdc800->write_urb);
674                 usb_kill_urb(mdc800->download_urb);
675                 mdc800->open=0;
676         }
677         else
678         {
679                 retval=-EIO;
680         }
681
682         mutex_unlock(&mdc800->io_lock);
683         return retval;
684 }
685
686
687 /*
688  * The Device read callback Function
689  */
690 static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
691 {
692         size_t left=len, sts=len; /* single transfer size */
693         char __user *ptr = buf;
694         int retval;
695
696         mutex_lock(&mdc800->io_lock);
697         if (mdc800->state == NOT_CONNECTED)
698         {
699                 mutex_unlock(&mdc800->io_lock);
700                 return -EBUSY;
701         }
702         if (mdc800->state == WORKING)
703         {
704                 warn ("Illegal State \"working\" reached during read ?!");
705                 mutex_unlock(&mdc800->io_lock);
706                 return -EBUSY;
707         }
708         if (!mdc800->open)
709         {
710                 mutex_unlock(&mdc800->io_lock);
711                 return -EBUSY;
712         }
713
714         while (left)
715         {
716                 if (signal_pending (current)) 
717                 {
718                         mutex_unlock(&mdc800->io_lock);
719                         return -EINTR;
720                 }
721
722                 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
723
724                 if (sts <= 0)
725                 {
726                         /* Too less Data in buffer */
727                         if (mdc800->state == DOWNLOAD)
728                         {
729                                 mdc800->out_count=0;
730                                 mdc800->out_ptr=0;
731
732                                 /* Download -> Request new bytes */
733                                 mdc800->download_urb->dev = mdc800->dev;
734                                 retval = usb_submit_urb (mdc800->download_urb, GFP_KERNEL);
735                                 if (retval) {
736                                         err ("Can't submit download urb (retval=%i)",retval);
737                                         mutex_unlock(&mdc800->io_lock);
738                                         return len-left;
739                                 }
740                                 wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
741                                                                                 TO_DOWNLOAD_GET_READY*HZ/1000);
742                                 mdc800->downloaded = 0;
743                                 if (mdc800->download_urb->status != 0)
744                                 {
745                                         err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
746                                         mutex_unlock(&mdc800->io_lock);
747                                         return len-left;
748                                 }
749                         }
750                         else
751                         {
752                                 /* No more bytes -> that's an error*/
753                                 mutex_unlock(&mdc800->io_lock);
754                                 return -EIO;
755                         }
756                 }
757                 else
758                 {
759                         /* Copy Bytes */
760                         if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
761                                                 sts)) {
762                                 mutex_unlock(&mdc800->io_lock);
763                                 return -EFAULT;
764                         }
765                         ptr+=sts;
766                         left-=sts;
767                         mdc800->out_ptr+=sts;
768                 }
769         }
770
771         mutex_unlock(&mdc800->io_lock);
772         return len-left;
773 }
774
775
776 /*
777  * The Device write callback Function
778  * If a 8Byte Command is received, it will be send to the camera.
779  * After this the driver initiates the request for the answer or
780  * just waits until the camera becomes ready.
781  */
782 static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
783 {
784         size_t i=0;
785         int retval;
786
787         mutex_lock(&mdc800->io_lock);
788         if (mdc800->state != READY)
789         {
790                 mutex_unlock(&mdc800->io_lock);
791                 return -EBUSY;
792         }
793         if (!mdc800->open )
794         {
795                 mutex_unlock(&mdc800->io_lock);
796                 return -EBUSY;
797         }
798
799         while (i<len)
800         {
801                 unsigned char c;
802                 if (signal_pending (current)) 
803                 {
804                         mutex_unlock(&mdc800->io_lock);
805                         return -EINTR;
806                 }
807                 
808                 if(get_user(c, buf+i))
809                 {
810                         mutex_unlock(&mdc800->io_lock);
811                         return -EFAULT;
812                 }
813
814                 /* check for command start */
815                 if (c == 0x55)
816                 {
817                         mdc800->in_count=0;
818                         mdc800->out_count=0;
819                         mdc800->out_ptr=0;
820                         mdc800->download_left=0;
821                 }
822
823                 /* save command byte */
824                 if (mdc800->in_count < 8)
825                 {
826                         mdc800->in[mdc800->in_count] = c;
827                         mdc800->in_count++;
828                 }
829                 else
830                 {
831                         mutex_unlock(&mdc800->io_lock);
832                         return -EIO;
833                 }
834
835                 /* Command Buffer full ? -> send it to camera */
836                 if (mdc800->in_count == 8)
837                 {
838                         int answersize;
839
840                         if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
841                         {
842                                 err ("Camera didn't get ready.\n");
843                                 mutex_unlock(&mdc800->io_lock);
844                                 return -EIO;
845                         }
846
847                         answersize=mdc800_getAnswerSize (mdc800->in[1]);
848
849                         mdc800->state=WORKING;
850                         memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
851                         mdc800->write_urb->dev = mdc800->dev;
852                         retval = usb_submit_urb (mdc800->write_urb, GFP_KERNEL);
853                         if (retval) {
854                                 err ("submitting write urb fails (retval=%i)", retval);
855                                 mutex_unlock(&mdc800->io_lock);
856                                 return -EIO;
857                         }
858                         wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
859                         mdc800->written = 0;
860                         if (mdc800->state == WORKING)
861                         {
862                                 usb_kill_urb(mdc800->write_urb);
863                                 mutex_unlock(&mdc800->io_lock);
864                                 return -EIO;
865                         }
866
867                         switch ((unsigned char) mdc800->in[1])
868                         {
869                                 case 0x05: /* Download Image */
870                                 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
871                                         if (mdc800->pic_len < 0)
872                                         {
873                                                 err ("call 0x07 before 0x05,0x3e");
874                                                 mdc800->state=READY;
875                                                 mutex_unlock(&mdc800->io_lock);
876                                                 return -EIO;
877                                         }
878                                         mdc800->pic_len=-1;
879
880                                 case 0x09: /* Download Thumbnail */
881                                         mdc800->download_left=answersize+64;
882                                         mdc800->state=DOWNLOAD;
883                                         mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
884                                         break;
885
886
887                                 default:
888                                         if (answersize)
889                                         {
890
891                                                 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
892                                                 {
893                                                         err ("requesting answer from irq fails");
894                                                         mutex_unlock(&mdc800->io_lock);
895                                                         return -EIO;
896                                                 }
897
898                                                 /* Write dummy data, (this is ugly but part of the USB Protocol */
899                                                 /* if you use endpoint 1 as bulk and not as irq) */
900                                                 memcpy (mdc800->out, mdc800->camera_response,8);
901
902                                                 /* This is the interpreted answer */
903                                                 memcpy (&mdc800->out[8], mdc800->camera_response,8);
904
905                                                 mdc800->out_ptr=0;
906                                                 mdc800->out_count=16;
907
908                                                 /* Cache the Imagesize, if command was getImageSize */
909                                                 if (mdc800->in [1] == (char) 0x07)
910                                                 {
911                                                         mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
912
913                                                         dbg ("cached imagesize = %i",mdc800->pic_len);
914                                                 }
915
916                                         }
917                                         else
918                                         {
919                                                 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
920                                                 {
921                                                         err ("Command Timeout.");
922                                                         mutex_unlock(&mdc800->io_lock);
923                                                         return -EIO;
924                                                 }
925                                         }
926                                         mdc800->state=READY;
927                                         break;
928                         }
929                 }
930                 i++;
931         }
932         mutex_unlock(&mdc800->io_lock);
933         return i;
934 }
935
936
937 /***************************************************************************
938         Init and Cleanup this driver (Structs and types)
939 ****************************************************************************/
940
941 /* File Operations of this drivers */
942 static const struct file_operations mdc800_device_ops =
943 {
944         .owner =        THIS_MODULE,
945         .read =         mdc800_device_read,
946         .write =        mdc800_device_write,
947         .open =         mdc800_device_open,
948         .release =      mdc800_device_release,
949 };
950
951
952
953 static struct usb_device_id mdc800_table [] = {
954         { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
955         { }                                             /* Terminating entry */
956 };
957
958 MODULE_DEVICE_TABLE (usb, mdc800_table);
959 /*
960  * USB Driver Struct for this device
961  */
962 static struct usb_driver mdc800_usb_driver =
963 {
964         .name =         "mdc800",
965         .probe =        mdc800_usb_probe,
966         .disconnect =   mdc800_usb_disconnect,
967         .id_table =     mdc800_table
968 };
969
970
971
972 /************************************************************************
973         Init and Cleanup this driver (Main Functions)
974 *************************************************************************/
975
976 static int __init usb_mdc800_init (void)
977 {
978         int retval = -ENODEV;
979         /* Allocate Memory */
980         mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
981         if (!mdc800)
982                 goto cleanup_on_fail;
983
984         mdc800->dev = NULL;
985         mdc800->state=NOT_CONNECTED;
986         mutex_init (&mdc800->io_lock);
987
988         init_waitqueue_head (&mdc800->irq_wait);
989         init_waitqueue_head (&mdc800->write_wait);
990         init_waitqueue_head (&mdc800->download_wait);
991
992         mdc800->irq_woken = 0;
993         mdc800->downloaded = 0;
994         mdc800->written = 0;
995
996         mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
997         if (!mdc800->irq_urb_buffer)
998                 goto cleanup_on_fail;
999         mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
1000         if (!mdc800->write_urb_buffer)
1001                 goto cleanup_on_fail;
1002         mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
1003         if (!mdc800->download_urb_buffer)
1004                 goto cleanup_on_fail;
1005
1006         mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
1007         if (!mdc800->irq_urb)
1008                 goto cleanup_on_fail;
1009         mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
1010         if (!mdc800->download_urb)
1011                 goto cleanup_on_fail;
1012         mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
1013         if (!mdc800->write_urb)
1014                 goto cleanup_on_fail;
1015
1016         /* Register the driver */
1017         retval = usb_register(&mdc800_usb_driver);
1018         if (retval)
1019                 goto cleanup_on_fail;
1020
1021         info (DRIVER_VERSION ":" DRIVER_DESC);
1022
1023         return 0;
1024
1025         /* Clean driver up, when something fails */
1026
1027 cleanup_on_fail:
1028
1029         if (mdc800 != NULL)
1030         {
1031                 err ("can't alloc memory!");
1032
1033                 kfree(mdc800->download_urb_buffer);
1034                 kfree(mdc800->write_urb_buffer);
1035                 kfree(mdc800->irq_urb_buffer);
1036
1037                 usb_free_urb(mdc800->write_urb);
1038                 usb_free_urb(mdc800->download_urb);
1039                 usb_free_urb(mdc800->irq_urb);
1040
1041                 kfree (mdc800);
1042         }
1043         mdc800 = NULL;
1044         return retval;
1045 }
1046
1047
1048 static void __exit usb_mdc800_cleanup (void)
1049 {
1050         usb_deregister (&mdc800_usb_driver);
1051
1052         usb_free_urb (mdc800->irq_urb);
1053         usb_free_urb (mdc800->download_urb);
1054         usb_free_urb (mdc800->write_urb);
1055
1056         kfree (mdc800->irq_urb_buffer);
1057         kfree (mdc800->write_urb_buffer);
1058         kfree (mdc800->download_urb_buffer);
1059
1060         kfree (mdc800);
1061         mdc800 = NULL;
1062 }
1063
1064 module_init (usb_mdc800_init);
1065 module_exit (usb_mdc800_cleanup);
1066
1067 MODULE_AUTHOR( DRIVER_AUTHOR );
1068 MODULE_DESCRIPTION( DRIVER_DESC );
1069 MODULE_LICENSE("GPL");
1070