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.
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 <520047054719-0001@t-online.de>
39 * Identify version on module load.
43 * Fixed potential SMP races with Spinlocks.
44 * Thanks to Oliver Neukum <oliver.neukum@lrz.uni-muenchen.de> who
45 * noticed the race conditions.
48 * Fixed: Setting urb->dev before submitting urb.
49 * by Greg KH <greg@kroah.com>
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
59 * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
60 * problems when compiled as Module.
63 * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
64 * was updated to use these values.
67 * The Init und Exit Module Function are updated.
71 * Rewrite of the driver : The driver now uses URB's. The old stuff
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.
83 * first Version that gets a version number. Most of the needed
88 #include <linux/version.h>
89 #include <linux/sched.h>
90 #include <linux/signal.h>
91 #include <linux/spinlock.h>
92 #include <linux/errno.h>
93 #include <linux/random.h>
94 #include <linux/poll.h>
95 #include <linux/init.h>
96 #include <linux/slab.h>
97 #include <linux/module.h>
98 #include <linux/smp_lock.h>
100 #include <linux/usb.h>
103 * Version Information
105 #define DRIVER_VERSION "v0.7.5 (30/10/2000)"
106 #define DRIVER_AUTHOR "Henning Zabel <henning@uni-paderborn.de>"
107 #define DRIVER_DESC "USB Driver for Mustek MDC800 Digital Camera"
109 /* Vendor and Product Information */
110 #define MDC800_VENDOR_ID 0x055f
111 #define MDC800_PRODUCT_ID 0xa800
113 /* Timeouts (msec) */
114 #define TO_DOWNLOAD_GET_READY 1500
115 #define TO_DOWNLOAD_GET_BUSY 1500
116 #define TO_WRITE_GET_READY 1000
117 #define TO_DEFAULT_COMMAND 5000
118 #define TO_READ_FROM_IRQ TO_DEFAULT_COMMAND
119 #define TO_GET_READY TO_DEFAULT_COMMAND
121 /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
122 #define MDC800_DEVICE_MINOR_BASE 32
125 /**************************************************************************
127 ***************************************************************************/
131 NOT_CONNECTED, READY, WORKING, DOWNLOAD
135 /* Data for the driver */
138 struct usb_device * dev; // Device Data
141 unsigned int endpoint [4];
143 struct urb * irq_urb;
144 wait_queue_head_t irq_wait;
146 char* irq_urb_buffer;
148 int camera_busy; // is camera busy ?
149 int camera_request_ready; // Status to synchronize with irq
150 char camera_response [8]; // last Bytes send after busy
152 struct urb * write_urb;
153 char* write_urb_buffer;
154 wait_queue_head_t write_wait;
158 struct urb * download_urb;
159 char* download_urb_buffer;
160 wait_queue_head_t download_wait;
162 int download_left; // Bytes left to download ?
166 char out [64]; // Answer Buffer
167 int out_ptr; // Index to the first not readen byte
168 int out_count; // Bytes in the buffer
170 int open; // Camera device open ?
171 struct semaphore io_lock; // IO -lock
173 char in [8]; // Command Input Buffer
176 int pic_index; // Cache for the Imagesize (-1 for nothing cached )
181 /* Specification of the Endpoints */
182 static struct usb_endpoint_descriptor mdc800_ed [4] =
184 { 0,0, 0x01, 0x02, 8, 0,0,0 },
185 { 0,0, 0x82, 0x03, 8, 0,0,0 },
186 { 0,0, 0x03, 0x02, 64, 0,0,0 },
187 { 0,0, 0x84, 0x02, 64, 0,0,0 }
191 /* The Variable used by the driver */
192 static struct mdc800_data* mdc800=0;
195 /***************************************************************************
196 The USB Part of the driver
197 ****************************************************************************/
199 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
202 ( a->bEndpointAddress == b->bEndpointAddress )
203 && ( a->bmAttributes == b->bmAttributes )
204 && ( a->wMaxPacketSize == b->wMaxPacketSize )
210 * Checks wether the camera responds busy
212 static int mdc800_isBusy (char* ch)
217 if (ch [i] != (char)0x99)
226 * Checks wether the Camera is ready
228 static int mdc800_isReady (char *ch)
233 if (ch [i] != (char)0xbb)
243 * USB IRQ Handler for InputLine
245 static void mdc800_usb_irq (struct urb *urb)
247 int data_received=0, wake_up;
248 unsigned char* b=urb->transfer_buffer;
249 struct mdc800_data* mdc800=urb->context;
251 if (urb->status >= 0)
254 //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]);
256 if (mdc800_isBusy (b))
258 if (!mdc800->camera_busy)
260 mdc800->camera_busy=1;
266 if (mdc800->camera_busy && mdc800_isReady (b))
268 mdc800->camera_busy=0;
272 if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
274 /* Store Data in camera_answer field */
275 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]);
277 memcpy (mdc800->camera_response,b,8);
281 wake_up= ( mdc800->camera_request_ready > 0 )
284 ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
286 ((mdc800->camera_request_ready == 2) && data_received)
288 ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
295 mdc800->camera_request_ready=0;
297 wake_up_interruptible (&mdc800->irq_wait);
303 * Waits a while until the irq responds that camera is ready
305 * mode : 0: Wait for camera gets ready
306 * 1: Wait for receiving data
307 * 2: Wait for camera gets busy
311 static int mdc800_usb_waitForIRQ (int mode, int msec)
313 DECLARE_WAITQUEUE(wait, current);
315 mdc800->camera_request_ready=1+mode;
317 add_wait_queue(&mdc800->irq_wait, &wait);
318 set_current_state(TASK_INTERRUPTIBLE);
319 if (!mdc800->irq_woken)
321 schedule_timeout (msec*HZ/1000);
323 remove_wait_queue(&mdc800->irq_wait, &wait);
324 set_current_state(TASK_RUNNING);
325 mdc800->irq_woken = 0;
327 if (mdc800->camera_request_ready>0)
329 mdc800->camera_request_ready=0;
330 err ("timeout waiting for camera.");
334 if (mdc800->state == NOT_CONNECTED)
336 warn ("Camera gets disconnected during waiting for irq.");
337 mdc800->camera_request_ready=0;
346 * The write_urb callback function
348 static void mdc800_usb_write_notify (struct urb *urb)
350 struct mdc800_data* mdc800=urb->context;
352 if (urb->status != 0)
354 err ("writing command fails (status=%i)", urb->status);
361 wake_up_interruptible (&mdc800->write_wait);
366 * The download_urb callback function
368 static void mdc800_usb_download_notify (struct urb *urb)
370 struct mdc800_data* mdc800=urb->context;
372 if (urb->status == 0)
374 /* Fill output buffer with these data */
375 memcpy (mdc800->out, urb->transfer_buffer, 64);
376 mdc800->out_count=64;
378 mdc800->download_left-=64;
379 if (mdc800->download_left == 0)
386 err ("request bytes fails (status:%i)", urb->status);
388 mdc800->downloaded = 1;
389 wake_up_interruptible (&mdc800->download_wait);
393 /***************************************************************************
394 Probing for the Camera
395 ***************************************************************************/
397 static struct usb_driver mdc800_usb_driver;
400 * Callback to search the Mustek MDC800 on the USB Bus
402 static void* mdc800_usb_probe (struct usb_device *dev ,unsigned int ifnum,
403 const struct usb_device_id *id)
406 struct usb_interface_descriptor *intf_desc;
409 dbg ("(mdc800_usb_probe) called.");
412 if (mdc800->dev != 0)
414 warn ("only one Mustek MDC800 is supported.");
418 if (dev->descriptor.bNumConfigurations != 1)
420 err ("probe fails -> wrong Number of Configuration");
423 intf_desc=&dev->actconfig->interface[ifnum].altsetting[0];
426 ( intf_desc->bInterfaceClass != 0xff )
427 || ( intf_desc->bInterfaceSubClass != 0 )
428 || ( intf_desc->bInterfaceProtocol != 0 )
429 || ( intf_desc->bNumEndpoints != 4)
432 err ("probe fails -> wrong Interface");
436 /* Check the Endpoints */
439 mdc800->endpoint[i]=-1;
442 if (mdc800_endpoint_equals (&intf_desc->endpoint [j],&mdc800_ed [i]))
444 mdc800->endpoint[i]=intf_desc->endpoint [j].bEndpointAddress ;
447 irq_interval=intf_desc->endpoint [j].bInterval;
453 if (mdc800->endpoint[i] == -1)
455 err ("probe fails -> Wrong Endpoints.");
461 usb_driver_claim_interface (&mdc800_usb_driver, &dev->actconfig->interface[ifnum], mdc800);
462 if (usb_set_interface (dev, ifnum, 0) < 0)
464 err ("MDC800 Configuration fails.");
468 info ("Found Mustek MDC800 on USB.");
470 down (&mdc800->io_lock);
475 /* Setup URB Structs */
479 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
480 mdc800->irq_urb_buffer,
490 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
491 mdc800->write_urb_buffer,
493 mdc800_usb_write_notify,
498 mdc800->download_urb,
500 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
501 mdc800->download_urb_buffer,
503 mdc800_usb_download_notify,
509 up (&mdc800->io_lock);
516 * Disconnect USB device (maybe the MDC800)
518 static void mdc800_usb_disconnect (struct usb_device *dev,void* ptr)
520 struct mdc800_data* mdc800=(struct mdc800_data*) ptr;
522 dbg ("(mdc800_usb_disconnect) called");
524 if (mdc800->state == NOT_CONNECTED)
527 down (&mdc800->io_lock);
528 mdc800->state=NOT_CONNECTED;
530 usb_unlink_urb (mdc800->irq_urb);
531 usb_unlink_urb (mdc800->write_urb);
532 usb_unlink_urb (mdc800->download_urb);
534 usb_driver_release_interface (&mdc800_usb_driver, &dev->actconfig->interface[1]);
537 up (&mdc800->io_lock);
538 info ("Mustek MDC800 disconnected from USB.");
542 /***************************************************************************
543 The Misc device Part (file_operations)
544 ****************************************************************************/
547 * This Function calc the Answersize for a command.
549 static int mdc800_getAnswerSize (char command)
551 switch ((unsigned char) command)
566 return mdc800->pic_len;
578 * Init the device: (1) alloc mem (2) Increase MOD Count ..
580 static int mdc800_device_open (struct inode* inode, struct file *file)
585 down (&mdc800->io_lock);
587 if (mdc800->state == NOT_CONNECTED)
603 mdc800->download_left=0;
605 mdc800->camera_busy=0;
606 mdc800->camera_request_ready=0;
609 mdc800->irq_urb->dev = mdc800->dev;
610 if (usb_submit_urb (mdc800->irq_urb))
612 err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval, mdc800->irq_urb->status);
618 dbg ("Mustek MDC800 device opened.");
621 up (&mdc800->io_lock);
627 * Close the Camera and release Memory
629 static int mdc800_device_release (struct inode* inode, struct file *file)
632 dbg ("Mustek MDC800 device closed.");
634 down (&mdc800->io_lock);
635 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
637 usb_unlink_urb (mdc800->irq_urb);
638 usb_unlink_urb (mdc800->write_urb);
639 usb_unlink_urb (mdc800->download_urb);
647 up(&mdc800->io_lock);
653 * The Device read callback Function
655 static ssize_t mdc800_device_read (struct file *file, char *buf, size_t len, loff_t *pos)
657 size_t left=len, sts=len; /* single transfer size */
659 DECLARE_WAITQUEUE(wait, current);
661 down (&mdc800->io_lock);
662 if (mdc800->state == NOT_CONNECTED)
664 up (&mdc800->io_lock);
667 if (mdc800->state == WORKING)
669 warn ("Illegal State \"working\" reached during read ?!");
670 up (&mdc800->io_lock);
675 up (&mdc800->io_lock);
681 if (signal_pending (current))
683 up (&mdc800->io_lock);
687 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
691 /* Too less Data in buffer */
692 if (mdc800->state == DOWNLOAD)
697 /* Download -> Request new bytes */
698 mdc800->download_urb->dev = mdc800->dev;
699 if (usb_submit_urb (mdc800->download_urb))
701 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
702 up (&mdc800->io_lock);
705 add_wait_queue(&mdc800->download_wait, &wait);
706 set_current_state(TASK_INTERRUPTIBLE);
707 if (!mdc800->downloaded)
709 schedule_timeout (TO_DOWNLOAD_GET_READY*HZ/1000);
711 set_current_state(TASK_RUNNING);
712 remove_wait_queue(&mdc800->download_wait, &wait);
713 mdc800->downloaded = 0;
714 if (mdc800->download_urb->status != 0)
716 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
717 up (&mdc800->io_lock);
723 /* No more bytes -> that's an error*/
724 up (&mdc800->io_lock);
731 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr], sts)) {
732 up (&mdc800->io_lock);
737 mdc800->out_ptr+=sts;
741 up (&mdc800->io_lock);
747 * The Device write callback Function
748 * If a 8Byte Command is received, it will be send to the camera.
749 * After this the driver initiates the request for the answer or
750 * just waits until the camera becomes ready.
752 static ssize_t mdc800_device_write (struct file *file, const char *buf, size_t len, loff_t *pos)
755 DECLARE_WAITQUEUE(wait, current);
757 down (&mdc800->io_lock);
758 if (mdc800->state != READY)
760 up (&mdc800->io_lock);
765 up (&mdc800->io_lock);
772 if (signal_pending (current))
774 up (&mdc800->io_lock);
778 if(get_user(c, buf+i))
780 up(&mdc800->io_lock);
784 /* check for command start */
790 mdc800->download_left=0;
793 /* save command byte */
794 if (mdc800->in_count < 8)
796 mdc800->in[mdc800->in_count] = c;
801 up (&mdc800->io_lock);
805 /* Command Buffer full ? -> send it to camera */
806 if (mdc800->in_count == 8)
810 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
812 err ("Camera didn't get ready.\n");
813 up (&mdc800->io_lock);
817 answersize=mdc800_getAnswerSize (mdc800->in[1]);
819 mdc800->state=WORKING;
820 memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
821 mdc800->write_urb->dev = mdc800->dev;
822 if (usb_submit_urb (mdc800->write_urb))
824 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
825 up (&mdc800->io_lock);
828 add_wait_queue(&mdc800->write_wait, &wait);
829 set_current_state(TASK_INTERRUPTIBLE);
830 if (!mdc800->written)
832 schedule_timeout (TO_WRITE_GET_READY*HZ/1000);
834 set_current_state(TASK_RUNNING);
835 remove_wait_queue(&mdc800->write_wait, &wait);
837 if (mdc800->state == WORKING)
839 usb_unlink_urb (mdc800->write_urb);
840 up (&mdc800->io_lock);
844 switch ((unsigned char) mdc800->in[1])
846 case 0x05: /* Download Image */
847 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
848 if (mdc800->pic_len < 0)
850 err ("call 0x07 before 0x05,0x3e");
852 up (&mdc800->io_lock);
857 case 0x09: /* Download Thumbnail */
858 mdc800->download_left=answersize+64;
859 mdc800->state=DOWNLOAD;
860 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
868 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
870 err ("requesting answer from irq fails");
871 up (&mdc800->io_lock);
875 /* Write dummy data, (this is ugly but part of the USB Protocol */
876 /* if you use endpoint 1 as bulk and not as irq) */
877 memcpy (mdc800->out, mdc800->camera_response,8);
879 /* This is the interpreted answer */
880 memcpy (&mdc800->out[8], mdc800->camera_response,8);
883 mdc800->out_count=16;
885 /* Cache the Imagesize, if command was getImageSize */
886 if (mdc800->in [1] == (char) 0x07)
888 mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
890 dbg ("cached imagesize = %i",mdc800->pic_len);
896 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
898 err ("Command Timeout.");
899 up (&mdc800->io_lock);
909 up (&mdc800->io_lock);
914 /***************************************************************************
915 Init and Cleanup this driver (Structs and types)
916 ****************************************************************************/
918 /* File Operations of this drivers */
919 static struct file_operations mdc800_device_ops =
922 read: mdc800_device_read,
923 write: mdc800_device_write,
924 open: mdc800_device_open,
925 release: mdc800_device_release,
930 static struct usb_device_id mdc800_table [] = {
931 { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
932 { } /* Terminating entry */
935 MODULE_DEVICE_TABLE (usb, mdc800_table);
937 * USB Driver Struct for this device
939 static struct usb_driver mdc800_usb_driver =
942 probe: mdc800_usb_probe,
943 disconnect: mdc800_usb_disconnect,
944 fops: &mdc800_device_ops,
945 minor: MDC800_DEVICE_MINOR_BASE,
946 id_table: mdc800_table
951 /************************************************************************
952 Init and Cleanup this driver (Main Functions)
953 *************************************************************************/
955 #define try(A) if ((A) == 0) goto cleanup_on_fail;
956 #define try_free_mem(A) if (A != 0) { kfree (A); A=0; }
957 #define try_free_urb(A) if (A != 0) { usb_free_urb (A); A=0; }
959 int __init usb_mdc800_init (void)
961 /* Allocate Memory */
962 try (mdc800=kmalloc (sizeof (struct mdc800_data), GFP_KERNEL));
964 memset(mdc800, 0, sizeof(struct mdc800_data));
967 mdc800->state=NOT_CONNECTED;
968 init_MUTEX (&mdc800->io_lock);
970 init_waitqueue_head (&mdc800->irq_wait);
971 init_waitqueue_head (&mdc800->write_wait);
972 init_waitqueue_head (&mdc800->download_wait);
974 mdc800->irq_woken = 0;
975 mdc800->downloaded = 0;
978 try (mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL));
979 try (mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL));
980 try (mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL));
982 try (mdc800->irq_urb=usb_alloc_urb (0));
983 try (mdc800->download_urb=usb_alloc_urb (0));
984 try (mdc800->write_urb=usb_alloc_urb (0));
986 /* Register the driver */
987 if (usb_register (&mdc800_usb_driver) < 0)
988 goto cleanup_on_fail;
990 info (DRIVER_VERSION ":" DRIVER_DESC);
994 /* Clean driver up, when something fails */
1000 err ("can't alloc memory!");
1002 try_free_mem (mdc800->download_urb_buffer);
1003 try_free_mem (mdc800->write_urb_buffer);
1004 try_free_mem (mdc800->irq_urb_buffer);
1006 try_free_urb (mdc800->write_urb);
1007 try_free_urb (mdc800->download_urb);
1008 try_free_urb (mdc800->irq_urb);
1017 void __exit usb_mdc800_cleanup (void)
1019 usb_deregister (&mdc800_usb_driver);
1021 usb_free_urb (mdc800->irq_urb);
1022 usb_free_urb (mdc800->download_urb);
1023 usb_free_urb (mdc800->write_urb);
1025 kfree (mdc800->irq_urb_buffer);
1026 kfree (mdc800->write_urb_buffer);
1027 kfree (mdc800->download_urb_buffer);
1033 module_init (usb_mdc800_init);
1034 module_exit (usb_mdc800_cleanup);
1036 MODULE_AUTHOR( DRIVER_AUTHOR );
1037 MODULE_DESCRIPTION( DRIVER_DESC );
1038 MODULE_LICENSE("GPL");