Merge remote-tracking branch 'i7300_edac/linux_next'
[linux-2.6/next.git] / drivers / usb / image / mdc800.c
blob575b56c79e9724b120bf00638eda3333da583f4f
1 /*
2 * copyright (C) 1999/2000 by Henning Zabel <henning@uni-paderborn.de>
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.
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.
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.
21 * USB-Kernel Driver for the Mustek MDC800 Digital Camera
22 * (c) 1999/2000 Henning Zabel <henning@uni-paderborn.de>
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
31 * The driver supports only one camera.
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)
39 * Identify version on module load.
40 * (08/04/2001) gb
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)
48 * Fixed: Setting urb->dev before submitting urb.
49 * by Greg KH <greg@kroah.com>
50 * (13/10/2000)
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.
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)
63 * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
64 * was updated to use these values.
65 * (26/03/2000)
67 * The Init und Exit Module Function are updated.
68 * (01/03/2000)
70 * version 0.7.0
71 * Rewrite of the driver : The driver now uses URB's. The old stuff
72 * has been removed.
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)
82 * version 0.5.0:
83 * first Version that gets a version number. Most of the needed
84 * functions work.
85 * (20/10/1999)
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>
100 #include <linux/usb.h>
101 #include <linux/fs.h>
104 * Version Information
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"
110 /* Vendor and Product Information */
111 #define MDC800_VENDOR_ID 0x055f
112 #define MDC800_PRODUCT_ID 0xa800
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
122 /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
123 #define MDC800_DEVICE_MINOR_BASE 32
126 /**************************************************************************
127 Data and structs
128 ***************************************************************************/
131 typedef enum {
132 NOT_CONNECTED, READY, WORKING, DOWNLOAD
133 } mdc800_state;
136 /* Data for the driver */
137 struct mdc800_data
139 struct usb_device * dev; // Device Data
140 mdc800_state state;
142 unsigned int endpoint [4];
144 struct urb * irq_urb;
145 wait_queue_head_t irq_wait;
146 int irq_woken;
147 char* irq_urb_buffer;
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
153 struct urb * write_urb;
154 char* write_urb_buffer;
155 wait_queue_head_t write_wait;
156 int written;
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 ?
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
171 int open; // Camera device open ?
172 struct mutex io_lock; // IO -lock
174 char in [8]; // Command Input Buffer
175 int in_count;
177 int pic_index; // Cache for the Imagesize (-1 for nothing cached )
178 int pic_len;
179 int minor;
183 /* Specification of the Endpoints */
184 static struct usb_endpoint_descriptor mdc800_ed [4] =
187 .bLength = 0,
188 .bDescriptorType = 0,
189 .bEndpointAddress = 0x01,
190 .bmAttributes = 0x02,
191 .wMaxPacketSize = cpu_to_le16(8),
192 .bInterval = 0,
193 .bRefresh = 0,
194 .bSynchAddress = 0,
197 .bLength = 0,
198 .bDescriptorType = 0,
199 .bEndpointAddress = 0x82,
200 .bmAttributes = 0x03,
201 .wMaxPacketSize = cpu_to_le16(8),
202 .bInterval = 0,
203 .bRefresh = 0,
204 .bSynchAddress = 0,
207 .bLength = 0,
208 .bDescriptorType = 0,
209 .bEndpointAddress = 0x03,
210 .bmAttributes = 0x02,
211 .wMaxPacketSize = cpu_to_le16(64),
212 .bInterval = 0,
213 .bRefresh = 0,
214 .bSynchAddress = 0,
217 .bLength = 0,
218 .bDescriptorType = 0,
219 .bEndpointAddress = 0x84,
220 .bmAttributes = 0x02,
221 .wMaxPacketSize = cpu_to_le16(64),
222 .bInterval = 0,
223 .bRefresh = 0,
224 .bSynchAddress = 0,
228 /* The Variable used by the driver */
229 static struct mdc800_data* mdc800;
232 /***************************************************************************
233 The USB Part of the driver
234 ****************************************************************************/
236 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
238 return (
239 ( a->bEndpointAddress == b->bEndpointAddress )
240 && ( a->bmAttributes == b->bmAttributes )
241 && ( a->wMaxPacketSize == b->wMaxPacketSize )
247 * Checks whether the camera responds busy
249 static int mdc800_isBusy (char* ch)
251 int i=0;
252 while (i<8)
254 if (ch [i] != (char)0x99)
255 return 0;
256 i++;
258 return 1;
263 * Checks whether the Camera is ready
265 static int mdc800_isReady (char *ch)
267 int i=0;
268 while (i<8)
270 if (ch [i] != (char)0xbb)
271 return 0;
272 i++;
274 return 1;
280 * USB IRQ Handler for InputLine
282 static void mdc800_usb_irq (struct urb *urb)
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;
289 if (status >= 0) {
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]);
293 if (mdc800_isBusy (b))
295 if (!mdc800->camera_busy)
297 mdc800->camera_busy=1;
298 dbg ("gets busy");
301 else
303 if (mdc800->camera_busy && mdc800_isReady (b))
305 mdc800->camera_busy=0;
306 dbg ("gets ready");
309 if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
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]);
314 memcpy (mdc800->camera_response,b,8);
315 data_received=1;
318 wake_up= ( mdc800->camera_request_ready > 0 )
321 ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
323 ((mdc800->camera_request_ready == 2) && data_received)
325 ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
327 (status < 0)
330 if (wake_up)
332 mdc800->camera_request_ready=0;
333 mdc800->irq_woken=1;
334 wake_up (&mdc800->irq_wait);
340 * Waits a while until the irq responds that camera is ready
342 * mode : 0: Wait for camera gets ready
343 * 1: Wait for receiving data
344 * 2: Wait for camera gets busy
346 * msec: Time to wait
348 static int mdc800_usb_waitForIRQ (int mode, int msec)
350 mdc800->camera_request_ready=1+mode;
352 wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000);
353 mdc800->irq_woken = 0;
355 if (mdc800->camera_request_ready>0)
357 mdc800->camera_request_ready=0;
358 dev_err(&mdc800->dev->dev, "timeout waiting for camera.\n");
359 return -1;
362 if (mdc800->state == NOT_CONNECTED)
364 printk(KERN_WARNING "mdc800: Camera gets disconnected "
365 "during waiting for irq.\n");
366 mdc800->camera_request_ready=0;
367 return -2;
370 return 0;
375 * The write_urb callback function
377 static void mdc800_usb_write_notify (struct urb *urb)
379 struct mdc800_data* mdc800=urb->context;
380 int status = urb->status;
382 if (status != 0)
383 dev_err(&mdc800->dev->dev,
384 "writing command fails (status=%i)\n", status);
385 else
386 mdc800->state=READY;
387 mdc800->written = 1;
388 wake_up (&mdc800->write_wait);
393 * The download_urb callback function
395 static void mdc800_usb_download_notify (struct urb *urb)
397 struct mdc800_data* mdc800=urb->context;
398 int status = urb->status;
400 if (status == 0) {
401 /* Fill output buffer with these data */
402 memcpy (mdc800->out, urb->transfer_buffer, 64);
403 mdc800->out_count=64;
404 mdc800->out_ptr=0;
405 mdc800->download_left-=64;
406 if (mdc800->download_left == 0)
408 mdc800->state=READY;
410 } else {
411 dev_err(&mdc800->dev->dev,
412 "request bytes fails (status:%i)\n", status);
414 mdc800->downloaded = 1;
415 wake_up (&mdc800->download_wait);
419 /***************************************************************************
420 Probing for the Camera
421 ***************************************************************************/
423 static struct usb_driver mdc800_usb_driver;
424 static const struct file_operations mdc800_device_ops;
425 static struct usb_class_driver mdc800_class = {
426 .name = "mdc800%d",
427 .fops = &mdc800_device_ops,
428 .minor_base = MDC800_DEVICE_MINOR_BASE,
433 * Callback to search the Mustek MDC800 on the USB Bus
435 static int mdc800_usb_probe (struct usb_interface *intf,
436 const struct usb_device_id *id)
438 int i,j;
439 struct usb_host_interface *intf_desc;
440 struct usb_device *dev = interface_to_usbdev (intf);
441 int irq_interval=0;
442 int retval;
444 dbg ("(mdc800_usb_probe) called.");
447 if (mdc800->dev != NULL)
449 dev_warn(&intf->dev, "only one Mustek MDC800 is supported.\n");
450 return -ENODEV;
453 if (dev->descriptor.bNumConfigurations != 1)
455 dev_err(&intf->dev,
456 "probe fails -> wrong Number of Configuration\n");
457 return -ENODEV;
459 intf_desc = intf->cur_altsetting;
461 if (
462 ( intf_desc->desc.bInterfaceClass != 0xff )
463 || ( intf_desc->desc.bInterfaceSubClass != 0 )
464 || ( intf_desc->desc.bInterfaceProtocol != 0 )
465 || ( intf_desc->desc.bNumEndpoints != 4)
468 dev_err(&intf->dev, "probe fails -> wrong Interface\n");
469 return -ENODEV;
472 /* Check the Endpoints */
473 for (i=0; i<4; i++)
475 mdc800->endpoint[i]=-1;
476 for (j=0; j<4; j++)
478 if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
480 mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
481 if (i==1)
483 irq_interval=intf_desc->endpoint [j].desc.bInterval;
487 if (mdc800->endpoint[i] == -1)
489 dev_err(&intf->dev, "probe fails -> Wrong Endpoints.\n");
490 return -ENODEV;
495 dev_info(&intf->dev, "Found Mustek MDC800 on USB.\n");
497 mutex_lock(&mdc800->io_lock);
499 retval = usb_register_dev(intf, &mdc800_class);
500 if (retval) {
501 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
502 mutex_unlock(&mdc800->io_lock);
503 return -ENODEV;
506 mdc800->dev=dev;
507 mdc800->open=0;
509 /* Setup URB Structs */
510 usb_fill_int_urb (
511 mdc800->irq_urb,
512 mdc800->dev,
513 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
514 mdc800->irq_urb_buffer,
516 mdc800_usb_irq,
517 mdc800,
518 irq_interval
521 usb_fill_bulk_urb (
522 mdc800->write_urb,
523 mdc800->dev,
524 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
525 mdc800->write_urb_buffer,
527 mdc800_usb_write_notify,
528 mdc800
531 usb_fill_bulk_urb (
532 mdc800->download_urb,
533 mdc800->dev,
534 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
535 mdc800->download_urb_buffer,
537 mdc800_usb_download_notify,
538 mdc800
541 mdc800->state=READY;
543 mutex_unlock(&mdc800->io_lock);
545 usb_set_intfdata(intf, mdc800);
546 return 0;
551 * Disconnect USB device (maybe the MDC800)
553 static void mdc800_usb_disconnect (struct usb_interface *intf)
555 struct mdc800_data* mdc800 = usb_get_intfdata(intf);
557 dbg ("(mdc800_usb_disconnect) called");
559 if (mdc800) {
560 if (mdc800->state == NOT_CONNECTED)
561 return;
563 usb_deregister_dev(intf, &mdc800_class);
565 /* must be under lock to make sure no URB
566 is submitted after usb_kill_urb() */
567 mutex_lock(&mdc800->io_lock);
568 mdc800->state=NOT_CONNECTED;
570 usb_kill_urb(mdc800->irq_urb);
571 usb_kill_urb(mdc800->write_urb);
572 usb_kill_urb(mdc800->download_urb);
573 mutex_unlock(&mdc800->io_lock);
575 mdc800->dev = NULL;
576 usb_set_intfdata(intf, NULL);
578 dev_info(&intf->dev, "Mustek MDC800 disconnected from USB.\n");
582 /***************************************************************************
583 The Misc device Part (file_operations)
584 ****************************************************************************/
587 * This Function calc the Answersize for a command.
589 static int mdc800_getAnswerSize (char command)
591 switch ((unsigned char) command)
593 case 0x2a:
594 case 0x49:
595 case 0x51:
596 case 0x0d:
597 case 0x20:
598 case 0x07:
599 case 0x01:
600 case 0x25:
601 case 0x00:
602 return 8;
604 case 0x05:
605 case 0x3e:
606 return mdc800->pic_len;
608 case 0x09:
609 return 4096;
611 default:
612 return 0;
618 * Init the device: (1) alloc mem (2) Increase MOD Count ..
620 static int mdc800_device_open (struct inode* inode, struct file *file)
622 int retval=0;
623 int errn=0;
625 mutex_lock(&mdc800->io_lock);
627 if (mdc800->state == NOT_CONNECTED)
629 errn=-EBUSY;
630 goto error_out;
632 if (mdc800->open)
634 errn=-EBUSY;
635 goto error_out;
638 mdc800->in_count=0;
639 mdc800->out_count=0;
640 mdc800->out_ptr=0;
641 mdc800->pic_index=0;
642 mdc800->pic_len=-1;
643 mdc800->download_left=0;
645 mdc800->camera_busy=0;
646 mdc800->camera_request_ready=0;
648 retval=0;
649 mdc800->irq_urb->dev = mdc800->dev;
650 retval = usb_submit_urb (mdc800->irq_urb, GFP_KERNEL);
651 if (retval) {
652 dev_err(&mdc800->dev->dev,
653 "request USB irq fails (submit_retval=%i).\n", retval);
654 errn = -EIO;
655 goto error_out;
658 mdc800->open=1;
659 dbg ("Mustek MDC800 device opened.");
661 error_out:
662 mutex_unlock(&mdc800->io_lock);
663 return errn;
668 * Close the Camera and release Memory
670 static int mdc800_device_release (struct inode* inode, struct file *file)
672 int retval=0;
673 dbg ("Mustek MDC800 device closed.");
675 mutex_lock(&mdc800->io_lock);
676 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
678 usb_kill_urb(mdc800->irq_urb);
679 usb_kill_urb(mdc800->write_urb);
680 usb_kill_urb(mdc800->download_urb);
681 mdc800->open=0;
683 else
685 retval=-EIO;
688 mutex_unlock(&mdc800->io_lock);
689 return retval;
694 * The Device read callback Function
696 static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
698 size_t left=len, sts=len; /* single transfer size */
699 char __user *ptr = buf;
700 int retval;
702 mutex_lock(&mdc800->io_lock);
703 if (mdc800->state == NOT_CONNECTED)
705 mutex_unlock(&mdc800->io_lock);
706 return -EBUSY;
708 if (mdc800->state == WORKING)
710 printk(KERN_WARNING "mdc800: Illegal State \"working\""
711 "reached during read ?!\n");
712 mutex_unlock(&mdc800->io_lock);
713 return -EBUSY;
715 if (!mdc800->open)
717 mutex_unlock(&mdc800->io_lock);
718 return -EBUSY;
721 while (left)
723 if (signal_pending (current))
725 mutex_unlock(&mdc800->io_lock);
726 return -EINTR;
729 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
731 if (sts <= 0)
733 /* Too less Data in buffer */
734 if (mdc800->state == DOWNLOAD)
736 mdc800->out_count=0;
737 mdc800->out_ptr=0;
739 /* Download -> Request new bytes */
740 mdc800->download_urb->dev = mdc800->dev;
741 retval = usb_submit_urb (mdc800->download_urb, GFP_KERNEL);
742 if (retval) {
743 dev_err(&mdc800->dev->dev,
744 "Can't submit download urb "
745 "(retval=%i)\n", retval);
746 mutex_unlock(&mdc800->io_lock);
747 return len-left;
749 wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
750 TO_DOWNLOAD_GET_READY*HZ/1000);
751 mdc800->downloaded = 0;
752 if (mdc800->download_urb->status != 0)
754 dev_err(&mdc800->dev->dev,
755 "request download-bytes fails "
756 "(status=%i)\n",
757 mdc800->download_urb->status);
758 mutex_unlock(&mdc800->io_lock);
759 return len-left;
762 else
764 /* No more bytes -> that's an error*/
765 mutex_unlock(&mdc800->io_lock);
766 return -EIO;
769 else
771 /* Copy Bytes */
772 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
773 sts)) {
774 mutex_unlock(&mdc800->io_lock);
775 return -EFAULT;
777 ptr+=sts;
778 left-=sts;
779 mdc800->out_ptr+=sts;
783 mutex_unlock(&mdc800->io_lock);
784 return len-left;
789 * The Device write callback Function
790 * If a 8Byte Command is received, it will be send to the camera.
791 * After this the driver initiates the request for the answer or
792 * just waits until the camera becomes ready.
794 static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
796 size_t i=0;
797 int retval;
799 mutex_lock(&mdc800->io_lock);
800 if (mdc800->state != READY)
802 mutex_unlock(&mdc800->io_lock);
803 return -EBUSY;
805 if (!mdc800->open )
807 mutex_unlock(&mdc800->io_lock);
808 return -EBUSY;
811 while (i<len)
813 unsigned char c;
814 if (signal_pending (current))
816 mutex_unlock(&mdc800->io_lock);
817 return -EINTR;
820 if(get_user(c, buf+i))
822 mutex_unlock(&mdc800->io_lock);
823 return -EFAULT;
826 /* check for command start */
827 if (c == 0x55)
829 mdc800->in_count=0;
830 mdc800->out_count=0;
831 mdc800->out_ptr=0;
832 mdc800->download_left=0;
835 /* save command byte */
836 if (mdc800->in_count < 8)
838 mdc800->in[mdc800->in_count] = c;
839 mdc800->in_count++;
841 else
843 mutex_unlock(&mdc800->io_lock);
844 return -EIO;
847 /* Command Buffer full ? -> send it to camera */
848 if (mdc800->in_count == 8)
850 int answersize;
852 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
854 dev_err(&mdc800->dev->dev,
855 "Camera didn't get ready.\n");
856 mutex_unlock(&mdc800->io_lock);
857 return -EIO;
860 answersize=mdc800_getAnswerSize (mdc800->in[1]);
862 mdc800->state=WORKING;
863 memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
864 mdc800->write_urb->dev = mdc800->dev;
865 retval = usb_submit_urb (mdc800->write_urb, GFP_KERNEL);
866 if (retval) {
867 dev_err(&mdc800->dev->dev,
868 "submitting write urb fails "
869 "(retval=%i)\n", retval);
870 mutex_unlock(&mdc800->io_lock);
871 return -EIO;
873 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
874 mdc800->written = 0;
875 if (mdc800->state == WORKING)
877 usb_kill_urb(mdc800->write_urb);
878 mutex_unlock(&mdc800->io_lock);
879 return -EIO;
882 switch ((unsigned char) mdc800->in[1])
884 case 0x05: /* Download Image */
885 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
886 if (mdc800->pic_len < 0)
888 dev_err(&mdc800->dev->dev,
889 "call 0x07 before "
890 "0x05,0x3e\n");
891 mdc800->state=READY;
892 mutex_unlock(&mdc800->io_lock);
893 return -EIO;
895 mdc800->pic_len=-1;
897 case 0x09: /* Download Thumbnail */
898 mdc800->download_left=answersize+64;
899 mdc800->state=DOWNLOAD;
900 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
901 break;
904 default:
905 if (answersize)
908 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
910 dev_err(&mdc800->dev->dev, "requesting answer from irq fails\n");
911 mutex_unlock(&mdc800->io_lock);
912 return -EIO;
915 /* Write dummy data, (this is ugly but part of the USB Protocol */
916 /* if you use endpoint 1 as bulk and not as irq) */
917 memcpy (mdc800->out, mdc800->camera_response,8);
919 /* This is the interpreted answer */
920 memcpy (&mdc800->out[8], mdc800->camera_response,8);
922 mdc800->out_ptr=0;
923 mdc800->out_count=16;
925 /* Cache the Imagesize, if command was getImageSize */
926 if (mdc800->in [1] == (char) 0x07)
928 mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
930 dbg ("cached imagesize = %i",mdc800->pic_len);
934 else
936 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
938 dev_err(&mdc800->dev->dev, "Command Timeout.\n");
939 mutex_unlock(&mdc800->io_lock);
940 return -EIO;
943 mdc800->state=READY;
944 break;
947 i++;
949 mutex_unlock(&mdc800->io_lock);
950 return i;
954 /***************************************************************************
955 Init and Cleanup this driver (Structs and types)
956 ****************************************************************************/
958 /* File Operations of this drivers */
959 static const struct file_operations mdc800_device_ops =
961 .owner = THIS_MODULE,
962 .read = mdc800_device_read,
963 .write = mdc800_device_write,
964 .open = mdc800_device_open,
965 .release = mdc800_device_release,
966 .llseek = noop_llseek,
971 static const struct usb_device_id mdc800_table[] = {
972 { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
973 { } /* Terminating entry */
976 MODULE_DEVICE_TABLE (usb, mdc800_table);
978 * USB Driver Struct for this device
980 static struct usb_driver mdc800_usb_driver =
982 .name = "mdc800",
983 .probe = mdc800_usb_probe,
984 .disconnect = mdc800_usb_disconnect,
985 .id_table = mdc800_table
990 /************************************************************************
991 Init and Cleanup this driver (Main Functions)
992 *************************************************************************/
994 static int __init usb_mdc800_init (void)
996 int retval = -ENODEV;
997 /* Allocate Memory */
998 mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
999 if (!mdc800)
1000 goto cleanup_on_fail;
1002 mdc800->dev = NULL;
1003 mdc800->state=NOT_CONNECTED;
1004 mutex_init (&mdc800->io_lock);
1006 init_waitqueue_head (&mdc800->irq_wait);
1007 init_waitqueue_head (&mdc800->write_wait);
1008 init_waitqueue_head (&mdc800->download_wait);
1010 mdc800->irq_woken = 0;
1011 mdc800->downloaded = 0;
1012 mdc800->written = 0;
1014 mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
1015 if (!mdc800->irq_urb_buffer)
1016 goto cleanup_on_fail;
1017 mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
1018 if (!mdc800->write_urb_buffer)
1019 goto cleanup_on_fail;
1020 mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
1021 if (!mdc800->download_urb_buffer)
1022 goto cleanup_on_fail;
1024 mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
1025 if (!mdc800->irq_urb)
1026 goto cleanup_on_fail;
1027 mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
1028 if (!mdc800->download_urb)
1029 goto cleanup_on_fail;
1030 mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
1031 if (!mdc800->write_urb)
1032 goto cleanup_on_fail;
1034 /* Register the driver */
1035 retval = usb_register(&mdc800_usb_driver);
1036 if (retval)
1037 goto cleanup_on_fail;
1039 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1040 DRIVER_DESC "\n");
1042 return 0;
1044 /* Clean driver up, when something fails */
1046 cleanup_on_fail:
1048 if (mdc800 != NULL)
1050 printk(KERN_ERR "mdc800: can't alloc memory!\n");
1052 kfree(mdc800->download_urb_buffer);
1053 kfree(mdc800->write_urb_buffer);
1054 kfree(mdc800->irq_urb_buffer);
1056 usb_free_urb(mdc800->write_urb);
1057 usb_free_urb(mdc800->download_urb);
1058 usb_free_urb(mdc800->irq_urb);
1060 kfree (mdc800);
1062 mdc800 = NULL;
1063 return retval;
1067 static void __exit usb_mdc800_cleanup (void)
1069 usb_deregister (&mdc800_usb_driver);
1071 usb_free_urb (mdc800->irq_urb);
1072 usb_free_urb (mdc800->download_urb);
1073 usb_free_urb (mdc800->write_urb);
1075 kfree (mdc800->irq_urb_buffer);
1076 kfree (mdc800->write_urb_buffer);
1077 kfree (mdc800->download_urb_buffer);
1079 kfree (mdc800);
1080 mdc800 = NULL;
1083 module_init (usb_mdc800_init);
1084 module_exit (usb_mdc800_cleanup);
1086 MODULE_AUTHOR( DRIVER_AUTHOR );
1087 MODULE_DESCRIPTION( DRIVER_DESC );
1088 MODULE_LICENSE("GPL");