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 Protocol 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 <oliver@neukum.name>
39 * Identify version on module load.
43 * Fixed potential SMP races with Spinlocks.
44 * Thanks to Oliver Neukum <oliver@neukum.name> 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/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 #include <linux/smp_lock.h>
101 #include <linux/usb.h>
102 #include <linux/fs.h>
105 * Version Information
107 #define DRIVER_VERSION "v0.7.5 (30/10/2000)"
108 #define DRIVER_AUTHOR "Henning Zabel <henning@uni-paderborn.de>"
109 #define DRIVER_DESC "USB Driver for Mustek MDC800 Digital Camera"
111 /* Vendor and Product Information */
112 #define MDC800_VENDOR_ID 0x055f
113 #define MDC800_PRODUCT_ID 0xa800
115 /* Timeouts (msec) */
116 #define TO_DOWNLOAD_GET_READY 1500
117 #define TO_DOWNLOAD_GET_BUSY 1500
118 #define TO_WRITE_GET_READY 1000
119 #define TO_DEFAULT_COMMAND 5000
120 #define TO_READ_FROM_IRQ TO_DEFAULT_COMMAND
121 #define TO_GET_READY TO_DEFAULT_COMMAND
123 /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
124 #define MDC800_DEVICE_MINOR_BASE 32
127 /**************************************************************************
129 ***************************************************************************/
133 NOT_CONNECTED, READY, WORKING, DOWNLOAD
137 /* Data for the driver */
140 struct usb_device * dev; // Device Data
143 unsigned int endpoint [4];
145 struct urb * irq_urb;
146 wait_queue_head_t irq_wait;
148 char* irq_urb_buffer;
150 int camera_busy; // is camera busy ?
151 int camera_request_ready; // Status to synchronize with irq
152 char camera_response [8]; // last Bytes send after busy
154 struct urb * write_urb;
155 char* write_urb_buffer;
156 wait_queue_head_t write_wait;
160 struct urb * download_urb;
161 char* download_urb_buffer;
162 wait_queue_head_t download_wait;
164 int download_left; // Bytes left to download ?
168 char out [64]; // Answer Buffer
169 int out_ptr; // Index to the first not readen byte
170 int out_count; // Bytes in the buffer
172 int open; // Camera device open ?
173 struct mutex io_lock; // IO -lock
175 char in [8]; // Command Input Buffer
178 int pic_index; // Cache for the Imagesize (-1 for nothing cached )
184 /* Specification of the Endpoints */
185 static struct usb_endpoint_descriptor mdc800_ed [4] =
189 .bDescriptorType = 0,
190 .bEndpointAddress = 0x01,
191 .bmAttributes = 0x02,
192 .wMaxPacketSize = cpu_to_le16(8),
199 .bDescriptorType = 0,
200 .bEndpointAddress = 0x82,
201 .bmAttributes = 0x03,
202 .wMaxPacketSize = cpu_to_le16(8),
209 .bDescriptorType = 0,
210 .bEndpointAddress = 0x03,
211 .bmAttributes = 0x02,
212 .wMaxPacketSize = cpu_to_le16(64),
219 .bDescriptorType = 0,
220 .bEndpointAddress = 0x84,
221 .bmAttributes = 0x02,
222 .wMaxPacketSize = cpu_to_le16(64),
229 /* The Variable used by the driver */
230 static struct mdc800_data* mdc800;
233 /***************************************************************************
234 The USB Part of the driver
235 ****************************************************************************/
237 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
240 ( a->bEndpointAddress == b->bEndpointAddress )
241 && ( a->bmAttributes == b->bmAttributes )
242 && ( a->wMaxPacketSize == b->wMaxPacketSize )
248 * Checks whether the camera responds busy
250 static int mdc800_isBusy (char* ch)
255 if (ch [i] != (char)0x99)
264 * Checks whether the Camera is ready
266 static int mdc800_isReady (char *ch)
271 if (ch [i] != (char)0xbb)
281 * USB IRQ Handler for InputLine
283 static void mdc800_usb_irq (struct urb *urb)
285 int data_received=0, wake_up;
286 unsigned char* b=urb->transfer_buffer;
287 struct mdc800_data* mdc800=urb->context;
288 int status = urb->status;
292 //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]);
294 if (mdc800_isBusy (b))
296 if (!mdc800->camera_busy)
298 mdc800->camera_busy=1;
304 if (mdc800->camera_busy && mdc800_isReady (b))
306 mdc800->camera_busy=0;
310 if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
312 /* Store Data in camera_answer field */
313 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]);
315 memcpy (mdc800->camera_response,b,8);
319 wake_up= ( mdc800->camera_request_ready > 0 )
322 ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
324 ((mdc800->camera_request_ready == 2) && data_received)
326 ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
333 mdc800->camera_request_ready=0;
335 wake_up (&mdc800->irq_wait);
341 * Waits a while until the irq responds that camera is ready
343 * mode : 0: Wait for camera gets ready
344 * 1: Wait for receiving data
345 * 2: Wait for camera gets busy
349 static int mdc800_usb_waitForIRQ (int mode, int msec)
351 mdc800->camera_request_ready=1+mode;
353 wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000);
354 mdc800->irq_woken = 0;
356 if (mdc800->camera_request_ready>0)
358 mdc800->camera_request_ready=0;
359 dev_err(&mdc800->dev->dev, "timeout waiting for camera.\n");
363 if (mdc800->state == NOT_CONNECTED)
365 printk(KERN_WARNING "mdc800: Camera gets disconnected "
366 "during waiting for irq.\n");
367 mdc800->camera_request_ready=0;
376 * The write_urb callback function
378 static void mdc800_usb_write_notify (struct urb *urb)
380 struct mdc800_data* mdc800=urb->context;
381 int status = urb->status;
384 dev_err(&mdc800->dev->dev,
385 "writing command fails (status=%i)\n", status);
389 wake_up (&mdc800->write_wait);
394 * The download_urb callback function
396 static void mdc800_usb_download_notify (struct urb *urb)
398 struct mdc800_data* mdc800=urb->context;
399 int status = urb->status;
402 /* Fill output buffer with these data */
403 memcpy (mdc800->out, urb->transfer_buffer, 64);
404 mdc800->out_count=64;
406 mdc800->download_left-=64;
407 if (mdc800->download_left == 0)
412 dev_err(&mdc800->dev->dev,
413 "request bytes fails (status:%i)\n", status);
415 mdc800->downloaded = 1;
416 wake_up (&mdc800->download_wait);
420 /***************************************************************************
421 Probing for the Camera
422 ***************************************************************************/
424 static struct usb_driver mdc800_usb_driver;
425 static const struct file_operations mdc800_device_ops;
426 static struct usb_class_driver mdc800_class = {
428 .fops = &mdc800_device_ops,
429 .minor_base = MDC800_DEVICE_MINOR_BASE,
434 * Callback to search the Mustek MDC800 on the USB Bus
436 static int mdc800_usb_probe (struct usb_interface *intf,
437 const struct usb_device_id *id)
440 struct usb_host_interface *intf_desc;
441 struct usb_device *dev = interface_to_usbdev (intf);
445 dbg ("(mdc800_usb_probe) called.");
448 if (mdc800->dev != NULL)
450 dev_warn(&intf->dev, "only one Mustek MDC800 is supported.\n");
454 if (dev->descriptor.bNumConfigurations != 1)
457 "probe fails -> wrong Number of Configuration\n");
460 intf_desc = intf->cur_altsetting;
463 ( intf_desc->desc.bInterfaceClass != 0xff )
464 || ( intf_desc->desc.bInterfaceSubClass != 0 )
465 || ( intf_desc->desc.bInterfaceProtocol != 0 )
466 || ( intf_desc->desc.bNumEndpoints != 4)
469 dev_err(&intf->dev, "probe fails -> wrong Interface\n");
473 /* Check the Endpoints */
476 mdc800->endpoint[i]=-1;
479 if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
481 mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
484 irq_interval=intf_desc->endpoint [j].desc.bInterval;
488 if (mdc800->endpoint[i] == -1)
490 dev_err(&intf->dev, "probe fails -> Wrong Endpoints.\n");
496 dev_info(&intf->dev, "Found Mustek MDC800 on USB.\n");
498 mutex_lock(&mdc800->io_lock);
500 retval = usb_register_dev(intf, &mdc800_class);
502 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
503 mutex_unlock(&mdc800->io_lock);
510 /* Setup URB Structs */
514 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
515 mdc800->irq_urb_buffer,
525 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
526 mdc800->write_urb_buffer,
528 mdc800_usb_write_notify,
533 mdc800->download_urb,
535 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
536 mdc800->download_urb_buffer,
538 mdc800_usb_download_notify,
544 mutex_unlock(&mdc800->io_lock);
546 usb_set_intfdata(intf, mdc800);
552 * Disconnect USB device (maybe the MDC800)
554 static void mdc800_usb_disconnect (struct usb_interface *intf)
556 struct mdc800_data* mdc800 = usb_get_intfdata(intf);
558 dbg ("(mdc800_usb_disconnect) called");
561 if (mdc800->state == NOT_CONNECTED)
564 usb_deregister_dev(intf, &mdc800_class);
566 /* must be under lock to make sure no URB
567 is submitted after usb_kill_urb() */
568 mutex_lock(&mdc800->io_lock);
569 mdc800->state=NOT_CONNECTED;
571 usb_kill_urb(mdc800->irq_urb);
572 usb_kill_urb(mdc800->write_urb);
573 usb_kill_urb(mdc800->download_urb);
574 mutex_unlock(&mdc800->io_lock);
577 usb_set_intfdata(intf, NULL);
579 dev_info(&intf->dev, "Mustek MDC800 disconnected from USB.\n");
583 /***************************************************************************
584 The Misc device Part (file_operations)
585 ****************************************************************************/
588 * This Function calc the Answersize for a command.
590 static int mdc800_getAnswerSize (char command)
592 switch ((unsigned char) command)
607 return mdc800->pic_len;
619 * Init the device: (1) alloc mem (2) Increase MOD Count ..
621 static int mdc800_device_open (struct inode* inode, struct file *file)
627 mutex_lock(&mdc800->io_lock);
629 if (mdc800->state == NOT_CONNECTED)
645 mdc800->download_left=0;
647 mdc800->camera_busy=0;
648 mdc800->camera_request_ready=0;
651 mdc800->irq_urb->dev = mdc800->dev;
652 retval = usb_submit_urb (mdc800->irq_urb, GFP_KERNEL);
654 dev_err(&mdc800->dev->dev,
655 "request USB irq fails (submit_retval=%i).\n", retval);
661 dbg ("Mustek MDC800 device opened.");
664 mutex_unlock(&mdc800->io_lock);
671 * Close the Camera and release Memory
673 static int mdc800_device_release (struct inode* inode, struct file *file)
676 dbg ("Mustek MDC800 device closed.");
678 mutex_lock(&mdc800->io_lock);
679 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
681 usb_kill_urb(mdc800->irq_urb);
682 usb_kill_urb(mdc800->write_urb);
683 usb_kill_urb(mdc800->download_urb);
691 mutex_unlock(&mdc800->io_lock);
697 * The Device read callback Function
699 static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
701 size_t left=len, sts=len; /* single transfer size */
702 char __user *ptr = buf;
705 mutex_lock(&mdc800->io_lock);
706 if (mdc800->state == NOT_CONNECTED)
708 mutex_unlock(&mdc800->io_lock);
711 if (mdc800->state == WORKING)
713 printk(KERN_WARNING "mdc800: Illegal State \"working\""
714 "reached during read ?!\n");
715 mutex_unlock(&mdc800->io_lock);
720 mutex_unlock(&mdc800->io_lock);
726 if (signal_pending (current))
728 mutex_unlock(&mdc800->io_lock);
732 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
736 /* Too less Data in buffer */
737 if (mdc800->state == DOWNLOAD)
742 /* Download -> Request new bytes */
743 mdc800->download_urb->dev = mdc800->dev;
744 retval = usb_submit_urb (mdc800->download_urb, GFP_KERNEL);
746 dev_err(&mdc800->dev->dev,
747 "Can't submit download urb "
748 "(retval=%i)\n", retval);
749 mutex_unlock(&mdc800->io_lock);
752 wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
753 TO_DOWNLOAD_GET_READY*HZ/1000);
754 mdc800->downloaded = 0;
755 if (mdc800->download_urb->status != 0)
757 dev_err(&mdc800->dev->dev,
758 "request download-bytes fails "
760 mdc800->download_urb->status);
761 mutex_unlock(&mdc800->io_lock);
767 /* No more bytes -> that's an error*/
768 mutex_unlock(&mdc800->io_lock);
775 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
777 mutex_unlock(&mdc800->io_lock);
782 mdc800->out_ptr+=sts;
786 mutex_unlock(&mdc800->io_lock);
792 * The Device write callback Function
793 * If a 8Byte Command is received, it will be send to the camera.
794 * After this the driver initiates the request for the answer or
795 * just waits until the camera becomes ready.
797 static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
802 mutex_lock(&mdc800->io_lock);
803 if (mdc800->state != READY)
805 mutex_unlock(&mdc800->io_lock);
810 mutex_unlock(&mdc800->io_lock);
817 if (signal_pending (current))
819 mutex_unlock(&mdc800->io_lock);
823 if(get_user(c, buf+i))
825 mutex_unlock(&mdc800->io_lock);
829 /* check for command start */
835 mdc800->download_left=0;
838 /* save command byte */
839 if (mdc800->in_count < 8)
841 mdc800->in[mdc800->in_count] = c;
846 mutex_unlock(&mdc800->io_lock);
850 /* Command Buffer full ? -> send it to camera */
851 if (mdc800->in_count == 8)
855 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
857 dev_err(&mdc800->dev->dev,
858 "Camera didn't get ready.\n");
859 mutex_unlock(&mdc800->io_lock);
863 answersize=mdc800_getAnswerSize (mdc800->in[1]);
865 mdc800->state=WORKING;
866 memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
867 mdc800->write_urb->dev = mdc800->dev;
868 retval = usb_submit_urb (mdc800->write_urb, GFP_KERNEL);
870 dev_err(&mdc800->dev->dev,
871 "submitting write urb fails "
872 "(retval=%i)\n", retval);
873 mutex_unlock(&mdc800->io_lock);
876 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
878 if (mdc800->state == WORKING)
880 usb_kill_urb(mdc800->write_urb);
881 mutex_unlock(&mdc800->io_lock);
885 switch ((unsigned char) mdc800->in[1])
887 case 0x05: /* Download Image */
888 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
889 if (mdc800->pic_len < 0)
891 dev_err(&mdc800->dev->dev,
895 mutex_unlock(&mdc800->io_lock);
900 case 0x09: /* Download Thumbnail */
901 mdc800->download_left=answersize+64;
902 mdc800->state=DOWNLOAD;
903 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
911 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
913 dev_err(&mdc800->dev->dev, "requesting answer from irq fails\n");
914 mutex_unlock(&mdc800->io_lock);
918 /* Write dummy data, (this is ugly but part of the USB Protocol */
919 /* if you use endpoint 1 as bulk and not as irq) */
920 memcpy (mdc800->out, mdc800->camera_response,8);
922 /* This is the interpreted answer */
923 memcpy (&mdc800->out[8], mdc800->camera_response,8);
926 mdc800->out_count=16;
928 /* Cache the Imagesize, if command was getImageSize */
929 if (mdc800->in [1] == (char) 0x07)
931 mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
933 dbg ("cached imagesize = %i",mdc800->pic_len);
939 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
941 dev_err(&mdc800->dev->dev, "Command Timeout.\n");
942 mutex_unlock(&mdc800->io_lock);
952 mutex_unlock(&mdc800->io_lock);
957 /***************************************************************************
958 Init and Cleanup this driver (Structs and types)
959 ****************************************************************************/
961 /* File Operations of this drivers */
962 static const struct file_operations mdc800_device_ops =
964 .owner = THIS_MODULE,
965 .read = mdc800_device_read,
966 .write = mdc800_device_write,
967 .open = mdc800_device_open,
968 .release = mdc800_device_release,
973 static const struct usb_device_id mdc800_table[] = {
974 { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
975 { } /* Terminating entry */
978 MODULE_DEVICE_TABLE (usb, mdc800_table);
980 * USB Driver Struct for this device
982 static struct usb_driver mdc800_usb_driver =
985 .probe = mdc800_usb_probe,
986 .disconnect = mdc800_usb_disconnect,
987 .id_table = mdc800_table
992 /************************************************************************
993 Init and Cleanup this driver (Main Functions)
994 *************************************************************************/
996 static int __init usb_mdc800_init (void)
998 int retval = -ENODEV;
999 /* Allocate Memory */
1000 mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
1002 goto cleanup_on_fail;
1005 mdc800->state=NOT_CONNECTED;
1006 mutex_init (&mdc800->io_lock);
1008 init_waitqueue_head (&mdc800->irq_wait);
1009 init_waitqueue_head (&mdc800->write_wait);
1010 init_waitqueue_head (&mdc800->download_wait);
1012 mdc800->irq_woken = 0;
1013 mdc800->downloaded = 0;
1014 mdc800->written = 0;
1016 mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
1017 if (!mdc800->irq_urb_buffer)
1018 goto cleanup_on_fail;
1019 mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
1020 if (!mdc800->write_urb_buffer)
1021 goto cleanup_on_fail;
1022 mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
1023 if (!mdc800->download_urb_buffer)
1024 goto cleanup_on_fail;
1026 mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
1027 if (!mdc800->irq_urb)
1028 goto cleanup_on_fail;
1029 mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
1030 if (!mdc800->download_urb)
1031 goto cleanup_on_fail;
1032 mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
1033 if (!mdc800->write_urb)
1034 goto cleanup_on_fail;
1036 /* Register the driver */
1037 retval = usb_register(&mdc800_usb_driver);
1039 goto cleanup_on_fail;
1041 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1046 /* Clean driver up, when something fails */
1052 printk(KERN_ERR "mdc800: can't alloc memory!\n");
1054 kfree(mdc800->download_urb_buffer);
1055 kfree(mdc800->write_urb_buffer);
1056 kfree(mdc800->irq_urb_buffer);
1058 usb_free_urb(mdc800->write_urb);
1059 usb_free_urb(mdc800->download_urb);
1060 usb_free_urb(mdc800->irq_urb);
1069 static void __exit usb_mdc800_cleanup (void)
1071 usb_deregister (&mdc800_usb_driver);
1073 usb_free_urb (mdc800->irq_urb);
1074 usb_free_urb (mdc800->download_urb);
1075 usb_free_urb (mdc800->write_urb);
1077 kfree (mdc800->irq_urb_buffer);
1078 kfree (mdc800->write_urb_buffer);
1079 kfree (mdc800->download_urb_buffer);
1085 module_init (usb_mdc800_init);
1086 module_exit (usb_mdc800_cleanup);
1088 MODULE_AUTHOR( DRIVER_AUTHOR );
1089 MODULE_DESCRIPTION( DRIVER_DESC );
1090 MODULE_LICENSE("GPL");