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/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] =
185 { 0,0, 0x01, 0x02, 8, 0,0,0 },
186 { 0,0, 0x82, 0x03, 8, 0,0,0 },
187 { 0,0, 0x03, 0x02, 64, 0,0,0 },
188 { 0,0, 0x84, 0x02, 64, 0,0,0 }
192 /* The Variable used by the driver */
193 static struct mdc800_data* mdc800=0;
196 /***************************************************************************
197 The USB Part of the driver
198 ****************************************************************************/
200 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
203 ( a->bEndpointAddress == b->bEndpointAddress )
204 && ( a->bmAttributes == b->bmAttributes )
205 && ( a->wMaxPacketSize == b->wMaxPacketSize )
211 * Checks whether the camera responds busy
213 static int mdc800_isBusy (char* ch)
218 if (ch [i] != (char)0x99)
227 * Checks whether the Camera is ready
229 static int mdc800_isReady (char *ch)
234 if (ch [i] != (char)0xbb)
244 * USB IRQ Handler for InputLine
246 static void mdc800_usb_irq (struct urb *urb, struct pt_regs *res)
248 int data_received=0, wake_up;
249 unsigned char* b=urb->transfer_buffer;
250 struct mdc800_data* mdc800=urb->context;
252 if (urb->status >= 0)
255 //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]);
257 if (mdc800_isBusy (b))
259 if (!mdc800->camera_busy)
261 mdc800->camera_busy=1;
267 if (mdc800->camera_busy && mdc800_isReady (b))
269 mdc800->camera_busy=0;
273 if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
275 /* Store Data in camera_answer field */
276 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]);
278 memcpy (mdc800->camera_response,b,8);
282 wake_up= ( mdc800->camera_request_ready > 0 )
285 ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
287 ((mdc800->camera_request_ready == 2) && data_received)
289 ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
296 mdc800->camera_request_ready=0;
298 wake_up_interruptible (&mdc800->irq_wait);
304 * Waits a while until the irq responds that camera is ready
306 * mode : 0: Wait for camera gets ready
307 * 1: Wait for receiving data
308 * 2: Wait for camera gets busy
312 static int mdc800_usb_waitForIRQ (int mode, int msec)
314 DECLARE_WAITQUEUE(wait, current);
316 mdc800->camera_request_ready=1+mode;
318 add_wait_queue(&mdc800->irq_wait, &wait);
319 set_current_state(TASK_INTERRUPTIBLE);
320 if (!mdc800->irq_woken)
322 schedule_timeout (msec*HZ/1000);
324 remove_wait_queue(&mdc800->irq_wait, &wait);
325 set_current_state(TASK_RUNNING);
326 mdc800->irq_woken = 0;
328 if (mdc800->camera_request_ready>0)
330 mdc800->camera_request_ready=0;
331 err ("timeout waiting for camera.");
335 if (mdc800->state == NOT_CONNECTED)
337 warn ("Camera gets disconnected during waiting for irq.");
338 mdc800->camera_request_ready=0;
347 * The write_urb callback function
349 static void mdc800_usb_write_notify (struct urb *urb, struct pt_regs *res)
351 struct mdc800_data* mdc800=urb->context;
353 if (urb->status != 0)
355 err ("writing command fails (status=%i)", urb->status);
362 wake_up_interruptible (&mdc800->write_wait);
367 * The download_urb callback function
369 static void mdc800_usb_download_notify (struct urb *urb, struct pt_regs *res)
371 struct mdc800_data* mdc800=urb->context;
373 if (urb->status == 0)
375 /* Fill output buffer with these data */
376 memcpy (mdc800->out, urb->transfer_buffer, 64);
377 mdc800->out_count=64;
379 mdc800->download_left-=64;
380 if (mdc800->download_left == 0)
387 err ("request bytes fails (status:%i)", urb->status);
389 mdc800->downloaded = 1;
390 wake_up_interruptible (&mdc800->download_wait);
394 /***************************************************************************
395 Probing for the Camera
396 ***************************************************************************/
398 static struct usb_driver mdc800_usb_driver;
399 static struct file_operations mdc800_device_ops;
400 static struct usb_class_driver mdc800_class = {
401 .name = "usb/mdc800%d",
402 .fops = &mdc800_device_ops,
403 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
404 .minor_base = MDC800_DEVICE_MINOR_BASE,
409 * Callback to search the Mustek MDC800 on the USB Bus
411 static int mdc800_usb_probe (struct usb_interface *intf,
412 const struct usb_device_id *id)
415 struct usb_host_interface *intf_desc;
416 struct usb_device *dev = interface_to_usbdev (intf);
420 dbg ("(mdc800_usb_probe) called.");
423 if (mdc800->dev != 0)
425 warn ("only one Mustek MDC800 is supported.");
429 if (dev->descriptor.bNumConfigurations != 1)
431 err ("probe fails -> wrong Number of Configuration");
434 intf_desc = intf->cur_altsetting;
437 ( intf_desc->desc.bInterfaceClass != 0xff )
438 || ( intf_desc->desc.bInterfaceSubClass != 0 )
439 || ( intf_desc->desc.bInterfaceProtocol != 0 )
440 || ( intf_desc->desc.bNumEndpoints != 4)
443 err ("probe fails -> wrong Interface");
447 /* Check the Endpoints */
450 mdc800->endpoint[i]=-1;
453 if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
455 mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
458 irq_interval=intf_desc->endpoint [j].desc.bInterval;
464 if (mdc800->endpoint[i] == -1)
466 err ("probe fails -> Wrong Endpoints.");
472 info ("Found Mustek MDC800 on USB.");
474 down (&mdc800->io_lock);
476 retval = usb_register_dev(intf, &mdc800_class);
478 err ("Not able to get a minor for this device.");
485 /* Setup URB Structs */
489 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
490 mdc800->irq_urb_buffer,
500 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
501 mdc800->write_urb_buffer,
503 mdc800_usb_write_notify,
508 mdc800->download_urb,
510 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
511 mdc800->download_urb_buffer,
513 mdc800_usb_download_notify,
519 up (&mdc800->io_lock);
521 usb_set_intfdata(intf, mdc800);
527 * Disconnect USB device (maybe the MDC800)
529 static void mdc800_usb_disconnect (struct usb_interface *intf)
531 struct mdc800_data* mdc800 = usb_get_intfdata(intf);
533 dbg ("(mdc800_usb_disconnect) called");
536 if (mdc800->state == NOT_CONNECTED)
539 usb_deregister_dev(intf, &mdc800_class);
541 mdc800->state=NOT_CONNECTED;
543 usb_unlink_urb (mdc800->irq_urb);
544 usb_unlink_urb (mdc800->write_urb);
545 usb_unlink_urb (mdc800->download_urb);
548 usb_set_intfdata(intf, NULL);
550 info ("Mustek MDC800 disconnected from USB.");
554 /***************************************************************************
555 The Misc device Part (file_operations)
556 ****************************************************************************/
559 * This Function calc the Answersize for a command.
561 static int mdc800_getAnswerSize (char command)
563 switch ((unsigned char) command)
578 return mdc800->pic_len;
590 * Init the device: (1) alloc mem (2) Increase MOD Count ..
592 static int mdc800_device_open (struct inode* inode, struct file *file)
597 down (&mdc800->io_lock);
599 if (mdc800->state == NOT_CONNECTED)
615 mdc800->download_left=0;
617 mdc800->camera_busy=0;
618 mdc800->camera_request_ready=0;
621 mdc800->irq_urb->dev = mdc800->dev;
622 if (usb_submit_urb (mdc800->irq_urb, GFP_KERNEL))
624 err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval, mdc800->irq_urb->status);
630 dbg ("Mustek MDC800 device opened.");
633 up (&mdc800->io_lock);
639 * Close the Camera and release Memory
641 static int mdc800_device_release (struct inode* inode, struct file *file)
644 dbg ("Mustek MDC800 device closed.");
646 down (&mdc800->io_lock);
647 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
649 usb_unlink_urb (mdc800->irq_urb);
650 usb_unlink_urb (mdc800->write_urb);
651 usb_unlink_urb (mdc800->download_urb);
659 up(&mdc800->io_lock);
665 * The Device read callback Function
667 static ssize_t mdc800_device_read (struct file *file, char *buf, size_t len, loff_t *pos)
669 size_t left=len, sts=len; /* single transfer size */
671 DECLARE_WAITQUEUE(wait, current);
673 down (&mdc800->io_lock);
674 if (mdc800->state == NOT_CONNECTED)
676 up (&mdc800->io_lock);
679 if (mdc800->state == WORKING)
681 warn ("Illegal State \"working\" reached during read ?!");
682 up (&mdc800->io_lock);
687 up (&mdc800->io_lock);
693 if (signal_pending (current))
695 up (&mdc800->io_lock);
699 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
703 /* Too less Data in buffer */
704 if (mdc800->state == DOWNLOAD)
709 /* Download -> Request new bytes */
710 mdc800->download_urb->dev = mdc800->dev;
711 if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
713 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
714 up (&mdc800->io_lock);
717 add_wait_queue(&mdc800->download_wait, &wait);
718 set_current_state(TASK_INTERRUPTIBLE);
719 if (!mdc800->downloaded)
721 schedule_timeout (TO_DOWNLOAD_GET_READY*HZ/1000);
723 set_current_state(TASK_RUNNING);
724 remove_wait_queue(&mdc800->download_wait, &wait);
725 mdc800->downloaded = 0;
726 if (mdc800->download_urb->status != 0)
728 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
729 up (&mdc800->io_lock);
735 /* No more bytes -> that's an error*/
736 up (&mdc800->io_lock);
743 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
745 up(&mdc800->io_lock);
750 mdc800->out_ptr+=sts;
754 up (&mdc800->io_lock);
760 * The Device write callback Function
761 * If a 8Byte Command is received, it will be send to the camera.
762 * After this the driver initiates the request for the answer or
763 * just waits until the camera becomes ready.
765 static ssize_t mdc800_device_write (struct file *file, const char *buf, size_t len, loff_t *pos)
768 DECLARE_WAITQUEUE(wait, current);
770 down (&mdc800->io_lock);
771 if (mdc800->state != READY)
773 up (&mdc800->io_lock);
778 up (&mdc800->io_lock);
785 if (signal_pending (current))
787 up (&mdc800->io_lock);
791 if(get_user(c, buf+i))
793 up(&mdc800->io_lock);
797 /* check for command start */
803 mdc800->download_left=0;
806 /* save command byte */
807 if (mdc800->in_count < 8)
809 mdc800->in[mdc800->in_count] = c;
814 up (&mdc800->io_lock);
818 /* Command Buffer full ? -> send it to camera */
819 if (mdc800->in_count == 8)
823 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
825 err ("Camera didn't get ready.\n");
826 up (&mdc800->io_lock);
830 answersize=mdc800_getAnswerSize (mdc800->in[1]);
832 mdc800->state=WORKING;
833 memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
834 mdc800->write_urb->dev = mdc800->dev;
835 if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
837 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
838 up (&mdc800->io_lock);
841 add_wait_queue(&mdc800->write_wait, &wait);
842 set_current_state(TASK_INTERRUPTIBLE);
843 if (!mdc800->written)
845 schedule_timeout (TO_WRITE_GET_READY*HZ/1000);
847 set_current_state(TASK_RUNNING);
848 remove_wait_queue(&mdc800->write_wait, &wait);
850 if (mdc800->state == WORKING)
852 usb_unlink_urb (mdc800->write_urb);
853 up (&mdc800->io_lock);
857 switch ((unsigned char) mdc800->in[1])
859 case 0x05: /* Download Image */
860 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
861 if (mdc800->pic_len < 0)
863 err ("call 0x07 before 0x05,0x3e");
865 up (&mdc800->io_lock);
870 case 0x09: /* Download Thumbnail */
871 mdc800->download_left=answersize+64;
872 mdc800->state=DOWNLOAD;
873 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
881 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
883 err ("requesting answer from irq fails");
884 up (&mdc800->io_lock);
888 /* Write dummy data, (this is ugly but part of the USB Protocol */
889 /* if you use endpoint 1 as bulk and not as irq) */
890 memcpy (mdc800->out, mdc800->camera_response,8);
892 /* This is the interpreted answer */
893 memcpy (&mdc800->out[8], mdc800->camera_response,8);
896 mdc800->out_count=16;
898 /* Cache the Imagesize, if command was getImageSize */
899 if (mdc800->in [1] == (char) 0x07)
901 mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
903 dbg ("cached imagesize = %i",mdc800->pic_len);
909 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
911 err ("Command Timeout.");
912 up (&mdc800->io_lock);
922 up (&mdc800->io_lock);
927 /***************************************************************************
928 Init and Cleanup this driver (Structs and types)
929 ****************************************************************************/
931 /* File Operations of this drivers */
932 static struct file_operations mdc800_device_ops =
934 .owner = THIS_MODULE,
935 .read = mdc800_device_read,
936 .write = mdc800_device_write,
937 .open = mdc800_device_open,
938 .release = mdc800_device_release,
943 static struct usb_device_id mdc800_table [] = {
944 { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
945 { } /* Terminating entry */
948 MODULE_DEVICE_TABLE (usb, mdc800_table);
950 * USB Driver Struct for this device
952 static struct usb_driver mdc800_usb_driver =
954 .owner = THIS_MODULE,
956 .probe = mdc800_usb_probe,
957 .disconnect = mdc800_usb_disconnect,
958 .id_table = mdc800_table
963 /************************************************************************
964 Init and Cleanup this driver (Main Functions)
965 *************************************************************************/
967 #define try(A) if ((A) == 0) goto cleanup_on_fail;
968 #define try_free_mem(A) if (A != 0) { kfree (A); A=0; }
969 #define try_free_urb(A) if (A != 0) { usb_free_urb (A); A=0; }
971 static int __init usb_mdc800_init (void)
973 int retval = -ENODEV;
974 /* Allocate Memory */
975 try (mdc800=kmalloc (sizeof (struct mdc800_data), GFP_KERNEL));
977 memset(mdc800, 0, sizeof(struct mdc800_data));
980 mdc800->state=NOT_CONNECTED;
981 init_MUTEX (&mdc800->io_lock);
983 init_waitqueue_head (&mdc800->irq_wait);
984 init_waitqueue_head (&mdc800->write_wait);
985 init_waitqueue_head (&mdc800->download_wait);
987 mdc800->irq_woken = 0;
988 mdc800->downloaded = 0;
991 try (mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL));
992 try (mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL));
993 try (mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL));
995 try (mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL));
996 try (mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL));
997 try (mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL));
999 /* Register the driver */
1000 retval = usb_register(&mdc800_usb_driver);
1002 goto cleanup_on_fail;
1004 info (DRIVER_VERSION ":" DRIVER_DESC);
1008 /* Clean driver up, when something fails */
1014 err ("can't alloc memory!");
1016 try_free_mem (mdc800->download_urb_buffer);
1017 try_free_mem (mdc800->write_urb_buffer);
1018 try_free_mem (mdc800->irq_urb_buffer);
1020 try_free_urb (mdc800->write_urb);
1021 try_free_urb (mdc800->download_urb);
1022 try_free_urb (mdc800->irq_urb);
1031 static void __exit usb_mdc800_cleanup (void)
1033 usb_deregister (&mdc800_usb_driver);
1035 usb_free_urb (mdc800->irq_urb);
1036 usb_free_urb (mdc800->download_urb);
1037 usb_free_urb (mdc800->write_urb);
1039 kfree (mdc800->irq_urb_buffer);
1040 kfree (mdc800->write_urb_buffer);
1041 kfree (mdc800->download_urb_buffer);
1047 module_init (usb_mdc800_init);
1048 module_exit (usb_mdc800_cleanup);
1050 MODULE_AUTHOR( DRIVER_AUTHOR );
1051 MODULE_DESCRIPTION( DRIVER_DESC );
1052 MODULE_LICENSE("GPL");