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
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 Protocoll of the camera
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.
34 * Fixed potential SMP races with Spinlocks.
35 * Thanks to Oliver Neukum <oliver.neukum@lrz.uni-muenchen.de> who
36 * noticed the race conditions.
39 * Fixed: Setting urb->dev before submitting urb.
40 * by Greg KH <greg@kroah.com>
44 * bugfix : The mdc800->state field gets set to READY after the
45 * the diconnect function sets it to NOT_CONNECTED. This makes the
46 * driver running like the camera is connected and causes some
50 * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
51 * problems when compiled as Module.
54 * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
55 * was updated to use these values.
58 * The Init und Exit Module Function are updated.
62 * Rewrite of the driver : The driver now uses URB's. The old stuff
66 * Rewrite of this driver: The Emulation of the rs232 protocoll
67 * has been removed from the driver. A special executeCommand function
68 * for this driver is included to gphoto.
69 * The driver supports two kind of communication to bulk endpoints.
70 * Either with the dev->bus->ops->bulk... or with callback function.
74 * first Version that gets a version number. Most of the needed
79 #include <linux/version.h>
80 #include <linux/sched.h>
81 #include <linux/signal.h>
82 #include <linux/spinlock.h>
83 #include <linux/errno.h>
84 #include <linux/random.h>
85 #include <linux/poll.h>
86 #include <linux/init.h>
87 #include <linux/malloc.h>
88 #include <linux/module.h>
89 #include <linux/smp_lock.h>
91 #include <linux/usb.h>
93 #define VERSION "0.7.5"
94 #define RELEASE_DATE "(30/10/2000)"
96 /* Vendor and Product Information */
97 #define MDC800_VENDOR_ID 0x055f
98 #define MDC800_PRODUCT_ID 0xa800
100 /* Timeouts (msec) */
101 #define TO_DOWNLOAD_GET_READY 1500
102 #define TO_DOWNLOAD_GET_BUSY 1500
103 #define TO_WRITE_GET_READY 1000
104 #define TO_DEFAULT_COMMAND 5000
105 #define TO_READ_FROM_IRQ TO_DEFAULT_COMMAND
106 #define TO_GET_READY TO_DEFAULT_COMMAND
108 /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
109 #define MDC800_DEVICE_MINOR_BASE 32
112 /**************************************************************************
114 ***************************************************************************/
118 NOT_CONNECTED
, READY
, WORKING
, DOWNLOAD
122 /* Data for the driver */
125 struct usb_device
* dev
; // Device Data
128 unsigned int endpoint
[4];
131 wait_queue_head_t irq_wait
;
132 char* irq_urb_buffer
;
134 int camera_busy
; // is camera busy ?
135 int camera_request_ready
; // Status to synchronize with irq
136 char camera_response
[8]; // last Bytes send after busy
139 char* write_urb_buffer
;
140 wait_queue_head_t write_wait
;
144 char* download_urb_buffer
;
145 wait_queue_head_t download_wait
;
146 int download_left
; // Bytes left to download ?
150 char out
[64]; // Answer Buffer
151 int out_ptr
; // Index to the first not readen byte
152 int out_count
; // Bytes in the buffer
154 int open
; // Camera device open ?
155 spinlock_t io_lock
; // IO -lock
157 char in
[8]; // Command Input Buffer
160 int pic_index
; // Cache for the Imagesize (-1 for nothing cached )
165 /* Specification of the Endpoints */
166 static struct usb_endpoint_descriptor mdc800_ed
[4] =
168 { 0,0, 0x01, 0x02, 8, 0,0,0 },
169 { 0,0, 0x82, 0x03, 8, 0,0,0 },
170 { 0,0, 0x03, 0x02, 64, 0,0,0 },
171 { 0,0, 0x84, 0x02, 64, 0,0,0 }
175 /* The Variable used by the driver */
176 static struct mdc800_data
* mdc800
=0;
179 /***************************************************************************
180 The USB Part of the driver
181 ****************************************************************************/
183 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor
*a
,struct usb_endpoint_descriptor
*b
)
186 ( a
->bEndpointAddress
== b
->bEndpointAddress
)
187 && ( a
->bmAttributes
== b
->bmAttributes
)
188 && ( a
->wMaxPacketSize
== b
->wMaxPacketSize
)
194 * Checks wether the camera responds busy
196 static int mdc800_isBusy (char* ch
)
201 if (ch
[i
] != (char)0x99)
210 * Checks wether the Camera is ready
212 static int mdc800_isReady (char *ch
)
217 if (ch
[i
] != (char)0xbb)
227 * USB IRQ Handler for InputLine
229 static void mdc800_usb_irq (struct urb
*urb
)
231 int data_received
=0, wake_up
;
232 unsigned char* b
=urb
->transfer_buffer
;
233 struct mdc800_data
* mdc800
=urb
->context
;
235 if (urb
->status
>= 0)
238 //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]);
240 if (mdc800_isBusy (b
))
242 if (!mdc800
->camera_busy
)
244 mdc800
->camera_busy
=1;
250 if (mdc800
->camera_busy
&& mdc800_isReady (b
))
252 mdc800
->camera_busy
=0;
256 if (!(mdc800_isBusy (b
) || mdc800_isReady (b
)))
258 /* Store Data in camera_answer field */
259 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]);
261 memcpy (mdc800
->camera_response
,b
,8);
265 wake_up
= ( mdc800
->camera_request_ready
> 0 )
268 ((mdc800
->camera_request_ready
== 1) && (!mdc800
->camera_busy
))
270 ((mdc800
->camera_request_ready
== 2) && data_received
)
272 ((mdc800
->camera_request_ready
== 3) && (mdc800
->camera_busy
))
279 mdc800
->camera_request_ready
=0;
280 wake_up_interruptible (&mdc800
->irq_wait
);
286 * Waits a while until the irq responds that camera is ready
288 * mode : 0: Wait for camera gets ready
289 * 1: Wait for receiving data
290 * 2: Wait for camera gets busy
294 static int mdc800_usb_waitForIRQ (int mode
, int msec
)
296 mdc800
->camera_request_ready
=1+mode
;
298 interruptible_sleep_on_timeout (&mdc800
->irq_wait
, msec
*HZ
/1000);
300 if (mdc800
->camera_request_ready
>0)
302 mdc800
->camera_request_ready
=0;
303 err ("timeout waiting for camera.");
307 if (mdc800
->state
== NOT_CONNECTED
)
309 warn ("Camera gets disconnected during waiting for irq.");
310 mdc800
->camera_request_ready
=0;
319 * The write_urb callback function
321 static void mdc800_usb_write_notify (struct urb
*urb
)
323 struct mdc800_data
* mdc800
=urb
->context
;
325 if (urb
->status
!= 0)
327 err ("writing command fails (status=%i)", urb
->status
);
333 wake_up_interruptible (&mdc800
->write_wait
);
338 * The download_urb callback function
340 static void mdc800_usb_download_notify (struct urb
*urb
)
342 struct mdc800_data
* mdc800
=urb
->context
;
344 if (urb
->status
== 0)
346 /* Fill output buffer with these data */
347 memcpy (mdc800
->out
, urb
->transfer_buffer
, 64);
348 mdc800
->out_count
=64;
350 mdc800
->download_left
-=64;
351 if (mdc800
->download_left
== 0)
358 err ("request bytes fails (status:%i)", urb
->status
);
360 wake_up_interruptible (&mdc800
->download_wait
);
364 /***************************************************************************
365 Probing for the Camera
366 ***************************************************************************/
368 static struct usb_driver mdc800_usb_driver
;
371 * Callback to search the Mustek MDC800 on the USB Bus
373 static void* mdc800_usb_probe (struct usb_device
*dev
,unsigned int ifnum
,
374 const struct usb_device_id
*id
)
377 struct usb_interface_descriptor
*intf_desc
;
380 dbg ("(mdc800_usb_probe) called.");
383 if (mdc800
->dev
!= 0)
385 warn ("only one Mustek MDC800 is supported.");
389 if (dev
->descriptor
.bNumConfigurations
!= 1)
391 err ("probe fails -> wrong Number of Configuration");
394 intf_desc
=&dev
->actconfig
->interface
[ifnum
].altsetting
[0];
397 ( intf_desc
->bInterfaceClass
!= 0xff )
398 || ( intf_desc
->bInterfaceSubClass
!= 0 )
399 || ( intf_desc
->bInterfaceProtocol
!= 0 )
400 || ( intf_desc
->bNumEndpoints
!= 4)
403 err ("probe fails -> wrong Interface");
407 /* Check the Endpoints */
410 mdc800
->endpoint
[i
]=-1;
413 if (mdc800_endpoint_equals (&intf_desc
->endpoint
[j
],&mdc800_ed
[i
]))
415 mdc800
->endpoint
[i
]=intf_desc
->endpoint
[j
].bEndpointAddress
;
418 irq_interval
=intf_desc
->endpoint
[j
].bInterval
;
424 if (mdc800
->endpoint
[i
] == -1)
426 err ("probe fails -> Wrong Endpoints.");
432 usb_driver_claim_interface (&mdc800_usb_driver
, &dev
->actconfig
->interface
[ifnum
], mdc800
);
433 if (usb_set_interface (dev
, ifnum
, 0) < 0)
435 err ("MDC800 Configuration fails.");
439 info ("Found Mustek MDC800 on USB.");
441 spin_lock (&mdc800
->io_lock
);
446 /* Setup URB Structs */
450 usb_rcvintpipe (mdc800
->dev
,mdc800
->endpoint
[1]),
451 mdc800
->irq_urb_buffer
,
461 usb_sndbulkpipe (mdc800
->dev
, mdc800
->endpoint
[0]),
462 mdc800
->write_urb_buffer
,
464 mdc800_usb_write_notify
,
469 mdc800
->download_urb
,
471 usb_rcvbulkpipe (mdc800
->dev
, mdc800
->endpoint
[3]),
472 mdc800
->download_urb_buffer
,
474 mdc800_usb_download_notify
,
480 spin_unlock (&mdc800
->io_lock
);
487 * Disconnect USB device (maybe the MDC800)
489 static void mdc800_usb_disconnect (struct usb_device
*dev
,void* ptr
)
491 struct mdc800_data
* mdc800
=(struct mdc800_data
*) ptr
;
493 dbg ("(mdc800_usb_disconnect) called");
495 if (mdc800
->state
== NOT_CONNECTED
)
498 mdc800
->state
=NOT_CONNECTED
;
500 usb_unlink_urb (mdc800
->irq_urb
);
501 usb_unlink_urb (mdc800
->write_urb
);
502 usb_unlink_urb (mdc800
->download_urb
);
504 usb_driver_release_interface (&mdc800_usb_driver
, &dev
->actconfig
->interface
[1]);
507 info ("Mustek MDC800 disconnected from USB.");
511 /***************************************************************************
512 The Misc device Part (file_operations)
513 ****************************************************************************/
516 * This Function calc the Answersize for a command.
518 static int mdc800_getAnswerSize (char command
)
520 switch ((unsigned char) command
)
535 return mdc800
->pic_len
;
547 * Init the device: (1) alloc mem (2) Increase MOD Count ..
549 static int mdc800_device_open (struct inode
* inode
, struct file
*file
)
554 spin_lock (&mdc800
->io_lock
);
556 if (mdc800
->state
== NOT_CONNECTED
)
572 mdc800
->download_left
=0;
574 mdc800
->camera_busy
=0;
575 mdc800
->camera_request_ready
=0;
578 mdc800
->irq_urb
->dev
= mdc800
->dev
;
579 if (usb_submit_urb (mdc800
->irq_urb
))
581 err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval
, mdc800
->irq_urb
->status
);
587 dbg ("Mustek MDC800 device opened.");
590 spin_unlock (&mdc800
->io_lock
);
596 * Close the Camera and release Memory
598 static int mdc800_device_release (struct inode
* inode
, struct file
*file
)
601 dbg ("Mustek MDC800 device closed.");
603 spin_lock (&mdc800
->io_lock
);
604 if (mdc800
->open
&& (mdc800
->state
!= NOT_CONNECTED
))
607 usb_unlink_urb (mdc800
->irq_urb
);
608 usb_unlink_urb (mdc800
->write_urb
);
609 usb_unlink_urb (mdc800
->download_urb
);
615 spin_unlock (&mdc800
->io_lock
);
622 * The Device read callback Function
624 static ssize_t
mdc800_device_read (struct file
*file
, char *buf
, size_t len
, loff_t
*pos
)
626 int left
=len
, sts
=len
; /* single transfer size */
629 spin_lock (&mdc800
->io_lock
);
630 if (mdc800
->state
== NOT_CONNECTED
)
632 spin_unlock (&mdc800
->io_lock
);
635 if (mdc800
->state
== WORKING
)
637 warn ("Illegal State \"working\" reached during read ?!");
638 spin_unlock (&mdc800
->io_lock
);
643 spin_unlock (&mdc800
->io_lock
);
649 if (signal_pending (current
))
651 spin_unlock (&mdc800
->io_lock
);
655 sts
=left
> (mdc800
->out_count
-mdc800
->out_ptr
)?mdc800
->out_count
-mdc800
->out_ptr
:left
;
659 /* Too less Data in buffer */
660 if (mdc800
->state
== DOWNLOAD
)
665 /* Download -> Request new bytes */
666 mdc800
->download_urb
->dev
= mdc800
->dev
;
667 if (usb_submit_urb (mdc800
->download_urb
))
669 err ("Can't submit download urb (status=%i)",mdc800
->download_urb
->status
);
670 spin_unlock (&mdc800
->io_lock
);
673 interruptible_sleep_on_timeout (&mdc800
->download_wait
, TO_DOWNLOAD_GET_READY
*HZ
/1000);
674 if (mdc800
->download_urb
->status
!= 0)
676 err ("request download-bytes fails (status=%i)",mdc800
->download_urb
->status
);
677 spin_unlock (&mdc800
->io_lock
);
683 /* No more bytes -> that's an error*/
684 spin_unlock (&mdc800
->io_lock
);
691 memcpy (ptr
, &mdc800
->out
[mdc800
->out_ptr
], sts
);
694 mdc800
->out_ptr
+=sts
;
698 spin_unlock (&mdc800
->io_lock
);
704 * The Device write callback Function
705 * If a 8Byte Command is received, it will be send to the camera.
706 * After this the driver initiates the request for the answer or
707 * just waits until the camera becomes ready.
709 static ssize_t
mdc800_device_write (struct file
*file
, const char *buf
, size_t len
, loff_t
*pos
)
713 spin_lock (&mdc800
->io_lock
);
714 if (mdc800
->state
!= READY
)
716 spin_unlock (&mdc800
->io_lock
);
721 spin_unlock (&mdc800
->io_lock
);
727 if (signal_pending (current
))
729 spin_unlock (&mdc800
->io_lock
);
733 /* check for command start */
734 if (buf
[i
] == (char) 0x55)
739 mdc800
->download_left
=0;
742 /* save command byte */
743 if (mdc800
->in_count
< 8)
745 mdc800
->in
[mdc800
->in_count
]=buf
[i
];
750 err ("Command is to long !\n");
751 spin_unlock (&mdc800
->io_lock
);
755 /* Command Buffer full ? -> send it to camera */
756 if (mdc800
->in_count
== 8)
760 if (mdc800_usb_waitForIRQ (0,TO_GET_READY
))
762 err ("Camera didn't get ready.\n");
763 spin_unlock (&mdc800
->io_lock
);
767 answersize
=mdc800_getAnswerSize (mdc800
->in
[1]);
769 mdc800
->state
=WORKING
;
770 memcpy (mdc800
->write_urb
->transfer_buffer
, mdc800
->in
,8);
771 mdc800
->write_urb
->dev
= mdc800
->dev
;
772 if (usb_submit_urb (mdc800
->write_urb
))
774 err ("submitting write urb fails (status=%i)", mdc800
->write_urb
->status
);
775 spin_unlock (&mdc800
->io_lock
);
778 interruptible_sleep_on_timeout (&mdc800
->write_wait
, TO_WRITE_GET_READY
*HZ
/1000);
779 if (mdc800
->state
== WORKING
)
781 usb_unlink_urb (mdc800
->write_urb
);
782 spin_unlock (&mdc800
->io_lock
);
786 switch ((unsigned char) mdc800
->in
[1])
788 case 0x05: /* Download Image */
789 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
790 if (mdc800
->pic_len
< 0)
792 err ("call 0x07 before 0x05,0x3e");
794 spin_unlock (&mdc800
->io_lock
);
799 case 0x09: /* Download Thumbnail */
800 mdc800
->download_left
=answersize
+64;
801 mdc800
->state
=DOWNLOAD
;
802 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY
);
810 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ
))
812 err ("requesting answer from irq fails");
813 spin_unlock (&mdc800
->io_lock
);
817 /* Write dummy data, (this is ugly but part of the USB Protokoll */
818 /* if you use endpoint 1 as bulk and not as irq */
819 memcpy (mdc800
->out
, mdc800
->camera_response
,8);
821 /* This is the interpreted answer */
822 memcpy (&mdc800
->out
[8], mdc800
->camera_response
,8);
825 mdc800
->out_count
=16;
827 /* Cache the Imagesize, if command was getImageSize */
828 if (mdc800
->in
[1] == (char) 0x07)
830 mdc800
->pic_len
=(int) 65536*(unsigned char) mdc800
->camera_response
[0]+256*(unsigned char) mdc800
->camera_response
[1]+(unsigned char) mdc800
->camera_response
[2];
832 dbg ("cached imagesize = %i",mdc800
->pic_len
);
838 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND
))
840 err ("Command Timeout.");
841 spin_unlock (&mdc800
->io_lock
);
851 spin_unlock (&mdc800
->io_lock
);
856 /***************************************************************************
857 Init and Cleanup this driver (Structs and types)
858 ****************************************************************************/
860 /* File Operations of this drivers */
861 static struct file_operations mdc800_device_ops
=
864 read
: mdc800_device_read
,
865 write
: mdc800_device_write
,
866 open
: mdc800_device_open
,
867 release
: mdc800_device_release
,
872 static struct usb_device_id mdc800_table
[] = {
873 { idVendor
: MDC800_VENDOR_ID
, idProduct
: MDC800_PRODUCT_ID
},
874 { } /* Terminating entry */
877 MODULE_DEVICE_TABLE (usb
, mdc800_table
);
879 * USB Driver Struct for this device
881 static struct usb_driver mdc800_usb_driver
=
884 probe
: mdc800_usb_probe
,
885 disconnect
: mdc800_usb_disconnect
,
886 fops
: &mdc800_device_ops
,
887 minor
: MDC800_DEVICE_MINOR_BASE
,
888 id_table
: mdc800_table
893 /************************************************************************
894 Init and Cleanup this driver (Main Functions)
895 *************************************************************************/
897 #define try(A) if ((A) == 0) goto cleanup_on_fail;
898 #define try_free_mem(A) if (A != 0) { kfree (A); A=0; }
899 #define try_free_urb(A) if (A != 0) { usb_free_urb (A); A=0; }
901 int __init
usb_mdc800_init (void)
903 /* Allocate Memory */
904 try (mdc800
=kmalloc (sizeof (struct mdc800_data
), GFP_KERNEL
));
906 memset(mdc800
, 0, sizeof(struct mdc800_data
));
909 mdc800
->state
=NOT_CONNECTED
;
910 spin_lock_init (&mdc800
->io_lock
);
912 init_waitqueue_head (&mdc800
->irq_wait
);
913 init_waitqueue_head (&mdc800
->write_wait
);
914 init_waitqueue_head (&mdc800
->download_wait
);
916 try (mdc800
->irq_urb_buffer
=kmalloc (8, GFP_KERNEL
));
917 try (mdc800
->write_urb_buffer
=kmalloc (8, GFP_KERNEL
));
918 try (mdc800
->download_urb_buffer
=kmalloc (64, GFP_KERNEL
));
920 try (mdc800
->irq_urb
=usb_alloc_urb (0));
921 try (mdc800
->download_urb
=usb_alloc_urb (0));
922 try (mdc800
->write_urb
=usb_alloc_urb (0));
924 /* Register the driver */
925 if (usb_register (&mdc800_usb_driver
) < 0)
926 goto cleanup_on_fail
;
928 info ("Mustek Digital Camera Driver " VERSION
" (MDC800)");
929 info (RELEASE_DATE
" Henning Zabel <henning@uni-paderborn.de>");
933 /* Clean driver up, when something fails */
939 err ("can't alloc memory!");
941 try_free_mem (mdc800
->download_urb_buffer
);
942 try_free_mem (mdc800
->write_urb_buffer
);
943 try_free_mem (mdc800
->irq_urb_buffer
);
945 try_free_urb (mdc800
->write_urb
);
946 try_free_urb (mdc800
->download_urb
);
947 try_free_urb (mdc800
->irq_urb
);
956 void __exit
usb_mdc800_cleanup (void)
958 usb_deregister (&mdc800_usb_driver
);
960 usb_free_urb (mdc800
->irq_urb
);
961 usb_free_urb (mdc800
->download_urb
);
962 usb_free_urb (mdc800
->write_urb
);
964 kfree (mdc800
->irq_urb_buffer
);
965 kfree (mdc800
->write_urb_buffer
);
966 kfree (mdc800
->download_urb_buffer
);
973 MODULE_AUTHOR ("Henning Zabel <henning@uni-paderborn.de>");
974 MODULE_DESCRIPTION ("USB Driver for Mustek MDC800 Digital Camera");
976 module_init (usb_mdc800_init
);
977 module_exit (usb_mdc800_cleanup
);