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 <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/smp_lock.h>
99 #include <linux/usb.h>
100 #include <linux/fs.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 )
182 /* Specification of the Endpoints */
183 static struct usb_endpoint_descriptor mdc800_ed [4] =
187 .bDescriptorType = 0,
188 .bEndpointAddress = 0x01,
189 .bmAttributes = 0x02,
190 .wMaxPacketSize = __constant_cpu_to_le16(8),
197 .bDescriptorType = 0,
198 .bEndpointAddress = 0x82,
199 .bmAttributes = 0x03,
200 .wMaxPacketSize = __constant_cpu_to_le16(8),
207 .bDescriptorType = 0,
208 .bEndpointAddress = 0x03,
209 .bmAttributes = 0x02,
210 .wMaxPacketSize = __constant_cpu_to_le16(64),
217 .bDescriptorType = 0,
218 .bEndpointAddress = 0x84,
219 .bmAttributes = 0x02,
220 .wMaxPacketSize = __constant_cpu_to_le16(64),
227 /* The Variable used by the driver */
228 static struct mdc800_data* mdc800;
231 /***************************************************************************
232 The USB Part of the driver
233 ****************************************************************************/
235 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
238 ( a->bEndpointAddress == b->bEndpointAddress )
239 && ( a->bmAttributes == b->bmAttributes )
240 && ( a->wMaxPacketSize == b->wMaxPacketSize )
246 * Checks whether the camera responds busy
248 static int mdc800_isBusy (char* ch)
253 if (ch [i] != (char)0x99)
262 * Checks whether the Camera is ready
264 static int mdc800_isReady (char *ch)
269 if (ch [i] != (char)0xbb)
279 * USB IRQ Handler for InputLine
281 static void mdc800_usb_irq (struct urb *urb, struct pt_regs *res)
283 int data_received=0, wake_up;
284 unsigned char* b=urb->transfer_buffer;
285 struct mdc800_data* mdc800=urb->context;
287 if (urb->status >= 0)
290 //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 if (mdc800_isBusy (b))
294 if (!mdc800->camera_busy)
296 mdc800->camera_busy=1;
302 if (mdc800->camera_busy && mdc800_isReady (b))
304 mdc800->camera_busy=0;
308 if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
310 /* Store Data in camera_answer field */
311 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 memcpy (mdc800->camera_response,b,8);
317 wake_up= ( mdc800->camera_request_ready > 0 )
320 ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
322 ((mdc800->camera_request_ready == 2) && data_received)
324 ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
331 mdc800->camera_request_ready=0;
333 wake_up_interruptible (&mdc800->irq_wait);
339 * Waits a while until the irq responds that camera is ready
341 * mode : 0: Wait for camera gets ready
342 * 1: Wait for receiving data
343 * 2: Wait for camera gets busy
347 static int mdc800_usb_waitForIRQ (int mode, int msec)
349 DECLARE_WAITQUEUE(wait, current);
352 mdc800->camera_request_ready=1+mode;
354 add_wait_queue(&mdc800->irq_wait, &wait);
355 timeout = msec*HZ/1000;
356 while (!mdc800->irq_woken && timeout)
358 set_current_state(TASK_UNINTERRUPTIBLE);
359 timeout = schedule_timeout (timeout);
361 remove_wait_queue(&mdc800->irq_wait, &wait);
362 mdc800->irq_woken = 0;
364 if (mdc800->camera_request_ready>0)
366 mdc800->camera_request_ready=0;
367 err ("timeout waiting for camera.");
371 if (mdc800->state == NOT_CONNECTED)
373 warn ("Camera gets disconnected during waiting for irq.");
374 mdc800->camera_request_ready=0;
383 * The write_urb callback function
385 static void mdc800_usb_write_notify (struct urb *urb, struct pt_regs *res)
387 struct mdc800_data* mdc800=urb->context;
389 if (urb->status != 0)
391 err ("writing command fails (status=%i)", urb->status);
398 wake_up_interruptible (&mdc800->write_wait);
403 * The download_urb callback function
405 static void mdc800_usb_download_notify (struct urb *urb, struct pt_regs *res)
407 struct mdc800_data* mdc800=urb->context;
409 if (urb->status == 0)
411 /* Fill output buffer with these data */
412 memcpy (mdc800->out, urb->transfer_buffer, 64);
413 mdc800->out_count=64;
415 mdc800->download_left-=64;
416 if (mdc800->download_left == 0)
423 err ("request bytes fails (status:%i)", urb->status);
425 mdc800->downloaded = 1;
426 wake_up_interruptible (&mdc800->download_wait);
430 /***************************************************************************
431 Probing for the Camera
432 ***************************************************************************/
434 static struct usb_driver mdc800_usb_driver;
435 static struct file_operations mdc800_device_ops;
436 static struct usb_class_driver mdc800_class = {
437 .name = "usb/mdc800%d",
438 .fops = &mdc800_device_ops,
439 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
440 .minor_base = MDC800_DEVICE_MINOR_BASE,
445 * Callback to search the Mustek MDC800 on the USB Bus
447 static int mdc800_usb_probe (struct usb_interface *intf,
448 const struct usb_device_id *id)
451 struct usb_host_interface *intf_desc;
452 struct usb_device *dev = interface_to_usbdev (intf);
456 dbg ("(mdc800_usb_probe) called.");
459 if (mdc800->dev != NULL)
461 warn ("only one Mustek MDC800 is supported.");
465 if (dev->descriptor.bNumConfigurations != 1)
467 err ("probe fails -> wrong Number of Configuration");
470 intf_desc = intf->cur_altsetting;
473 ( intf_desc->desc.bInterfaceClass != 0xff )
474 || ( intf_desc->desc.bInterfaceSubClass != 0 )
475 || ( intf_desc->desc.bInterfaceProtocol != 0 )
476 || ( intf_desc->desc.bNumEndpoints != 4)
479 err ("probe fails -> wrong Interface");
483 /* Check the Endpoints */
486 mdc800->endpoint[i]=-1;
489 if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
491 mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
494 irq_interval=intf_desc->endpoint [j].desc.bInterval;
500 if (mdc800->endpoint[i] == -1)
502 err ("probe fails -> Wrong Endpoints.");
508 info ("Found Mustek MDC800 on USB.");
510 down (&mdc800->io_lock);
512 retval = usb_register_dev(intf, &mdc800_class);
514 err ("Not able to get a minor for this device.");
521 /* Setup URB Structs */
525 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
526 mdc800->irq_urb_buffer,
536 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
537 mdc800->write_urb_buffer,
539 mdc800_usb_write_notify,
544 mdc800->download_urb,
546 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
547 mdc800->download_urb_buffer,
549 mdc800_usb_download_notify,
555 up (&mdc800->io_lock);
557 usb_set_intfdata(intf, mdc800);
563 * Disconnect USB device (maybe the MDC800)
565 static void mdc800_usb_disconnect (struct usb_interface *intf)
567 struct mdc800_data* mdc800 = usb_get_intfdata(intf);
569 dbg ("(mdc800_usb_disconnect) called");
572 if (mdc800->state == NOT_CONNECTED)
575 usb_deregister_dev(intf, &mdc800_class);
577 mdc800->state=NOT_CONNECTED;
579 usb_kill_urb(mdc800->irq_urb);
580 usb_kill_urb(mdc800->write_urb);
581 usb_kill_urb(mdc800->download_urb);
584 usb_set_intfdata(intf, NULL);
586 info ("Mustek MDC800 disconnected from USB.");
590 /***************************************************************************
591 The Misc device Part (file_operations)
592 ****************************************************************************/
595 * This Function calc the Answersize for a command.
597 static int mdc800_getAnswerSize (char command)
599 switch ((unsigned char) command)
614 return mdc800->pic_len;
626 * Init the device: (1) alloc mem (2) Increase MOD Count ..
628 static int mdc800_device_open (struct inode* inode, struct file *file)
633 down (&mdc800->io_lock);
635 if (mdc800->state == NOT_CONNECTED)
651 mdc800->download_left=0;
653 mdc800->camera_busy=0;
654 mdc800->camera_request_ready=0;
657 mdc800->irq_urb->dev = mdc800->dev;
658 if (usb_submit_urb (mdc800->irq_urb, GFP_KERNEL))
660 err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval, mdc800->irq_urb->status);
666 dbg ("Mustek MDC800 device opened.");
669 up (&mdc800->io_lock);
675 * Close the Camera and release Memory
677 static int mdc800_device_release (struct inode* inode, struct file *file)
680 dbg ("Mustek MDC800 device closed.");
682 down (&mdc800->io_lock);
683 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
685 usb_kill_urb(mdc800->irq_urb);
686 usb_kill_urb(mdc800->write_urb);
687 usb_kill_urb(mdc800->download_urb);
695 up(&mdc800->io_lock);
701 * The Device read callback Function
703 static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
705 size_t left=len, sts=len; /* single transfer size */
706 char __user *ptr = buf;
708 DECLARE_WAITQUEUE(wait, current);
710 down (&mdc800->io_lock);
711 if (mdc800->state == NOT_CONNECTED)
713 up (&mdc800->io_lock);
716 if (mdc800->state == WORKING)
718 warn ("Illegal State \"working\" reached during read ?!");
719 up (&mdc800->io_lock);
724 up (&mdc800->io_lock);
730 if (signal_pending (current))
732 up (&mdc800->io_lock);
736 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
740 /* Too less Data in buffer */
741 if (mdc800->state == DOWNLOAD)
746 /* Download -> Request new bytes */
747 mdc800->download_urb->dev = mdc800->dev;
748 if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
750 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
751 up (&mdc800->io_lock);
754 add_wait_queue(&mdc800->download_wait, &wait);
755 timeout = TO_DOWNLOAD_GET_READY*HZ/1000;
756 while (!mdc800->downloaded && timeout)
758 set_current_state(TASK_UNINTERRUPTIBLE);
759 timeout = schedule_timeout (timeout);
761 remove_wait_queue(&mdc800->download_wait, &wait);
762 mdc800->downloaded = 0;
763 if (mdc800->download_urb->status != 0)
765 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
766 up (&mdc800->io_lock);
772 /* No more bytes -> that's an error*/
773 up (&mdc800->io_lock);
780 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
782 up(&mdc800->io_lock);
787 mdc800->out_ptr+=sts;
791 up (&mdc800->io_lock);
797 * The Device write callback Function
798 * If a 8Byte Command is received, it will be send to the camera.
799 * After this the driver initiates the request for the answer or
800 * just waits until the camera becomes ready.
802 static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
805 DECLARE_WAITQUEUE(wait, current);
807 down (&mdc800->io_lock);
808 if (mdc800->state != READY)
810 up (&mdc800->io_lock);
815 up (&mdc800->io_lock);
822 if (signal_pending (current))
824 up (&mdc800->io_lock);
828 if(get_user(c, buf+i))
830 up(&mdc800->io_lock);
834 /* check for command start */
840 mdc800->download_left=0;
843 /* save command byte */
844 if (mdc800->in_count < 8)
846 mdc800->in[mdc800->in_count] = c;
851 up (&mdc800->io_lock);
855 /* Command Buffer full ? -> send it to camera */
856 if (mdc800->in_count == 8)
861 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
863 err ("Camera didn't get ready.\n");
864 up (&mdc800->io_lock);
868 answersize=mdc800_getAnswerSize (mdc800->in[1]);
870 mdc800->state=WORKING;
871 memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
872 mdc800->write_urb->dev = mdc800->dev;
873 if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
875 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
876 up (&mdc800->io_lock);
879 add_wait_queue(&mdc800->write_wait, &wait);
880 timeout = TO_WRITE_GET_READY*HZ/1000;
881 while (!mdc800->written && timeout)
883 set_current_state(TASK_UNINTERRUPTIBLE);
884 timeout = schedule_timeout (timeout);
886 remove_wait_queue(&mdc800->write_wait, &wait);
888 if (mdc800->state == WORKING)
890 usb_kill_urb(mdc800->write_urb);
891 up (&mdc800->io_lock);
895 switch ((unsigned char) mdc800->in[1])
897 case 0x05: /* Download Image */
898 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
899 if (mdc800->pic_len < 0)
901 err ("call 0x07 before 0x05,0x3e");
903 up (&mdc800->io_lock);
908 case 0x09: /* Download Thumbnail */
909 mdc800->download_left=answersize+64;
910 mdc800->state=DOWNLOAD;
911 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
919 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
921 err ("requesting answer from irq fails");
922 up (&mdc800->io_lock);
926 /* Write dummy data, (this is ugly but part of the USB Protocol */
927 /* if you use endpoint 1 as bulk and not as irq) */
928 memcpy (mdc800->out, mdc800->camera_response,8);
930 /* This is the interpreted answer */
931 memcpy (&mdc800->out[8], mdc800->camera_response,8);
934 mdc800->out_count=16;
936 /* Cache the Imagesize, if command was getImageSize */
937 if (mdc800->in [1] == (char) 0x07)
939 mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
941 dbg ("cached imagesize = %i",mdc800->pic_len);
947 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
949 err ("Command Timeout.");
950 up (&mdc800->io_lock);
960 up (&mdc800->io_lock);
965 /***************************************************************************
966 Init and Cleanup this driver (Structs and types)
967 ****************************************************************************/
969 /* File Operations of this drivers */
970 static struct file_operations mdc800_device_ops =
972 .owner = THIS_MODULE,
973 .read = mdc800_device_read,
974 .write = mdc800_device_write,
975 .open = mdc800_device_open,
976 .release = mdc800_device_release,
981 static struct usb_device_id mdc800_table [] = {
982 { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
983 { } /* Terminating entry */
986 MODULE_DEVICE_TABLE (usb, mdc800_table);
988 * USB Driver Struct for this device
990 static struct usb_driver mdc800_usb_driver =
992 .owner = THIS_MODULE,
994 .probe = mdc800_usb_probe,
995 .disconnect = mdc800_usb_disconnect,
996 .id_table = mdc800_table
1001 /************************************************************************
1002 Init and Cleanup this driver (Main Functions)
1003 *************************************************************************/
1005 #define try(A) if (!(A)) goto cleanup_on_fail;
1007 static int __init usb_mdc800_init (void)
1009 int retval = -ENODEV;
1010 /* Allocate Memory */
1011 try (mdc800=kmalloc (sizeof (struct mdc800_data), GFP_KERNEL));
1013 memset(mdc800, 0, sizeof(struct mdc800_data));
1016 mdc800->state=NOT_CONNECTED;
1017 init_MUTEX (&mdc800->io_lock);
1019 init_waitqueue_head (&mdc800->irq_wait);
1020 init_waitqueue_head (&mdc800->write_wait);
1021 init_waitqueue_head (&mdc800->download_wait);
1023 mdc800->irq_woken = 0;
1024 mdc800->downloaded = 0;
1025 mdc800->written = 0;
1027 try (mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL));
1028 try (mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL));
1029 try (mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL));
1031 try (mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL));
1032 try (mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL));
1033 try (mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL));
1035 /* Register the driver */
1036 retval = usb_register(&mdc800_usb_driver);
1038 goto cleanup_on_fail;
1040 info (DRIVER_VERSION ":" DRIVER_DESC);
1044 /* Clean driver up, when something fails */
1050 err ("can't alloc memory!");
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);
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);
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");