Update libata drive blacklist to the latest from 2.6.21
[linux/fpc-iii.git] / drivers / usb / image / mdc800.c
blob63a84bbc310de32f4c3aac320e8e24663b0d0066
1 /*
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
12 * for more details.
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
27 * to the Kernel Node.
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>
37 * (02/12/2001)
39 * Identify version on module load.
40 * (08/04/2001) gb
42 * version 0.7.5
43 * Fixed potential SMP races with Spinlocks.
44 * Thanks to Oliver Neukum <oliver@neukum.name> who
45 * noticed the race conditions.
46 * (30/10/2000)
48 * Fixed: Setting urb->dev before submitting urb.
49 * by Greg KH <greg@kroah.com>
50 * (13/10/2000)
52 * version 0.7.3
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
56 * hang ups.
58 * version 0.7.1
59 * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
60 * problems when compiled as Module.
61 * (04/04/2000)
63 * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
64 * was updated to use these values.
65 * (26/03/2000)
67 * The Init und Exit Module Function are updated.
68 * (01/03/2000)
70 * version 0.7.0
71 * Rewrite of the driver : The driver now uses URB's. The old stuff
72 * has been removed.
74 * version 0.6.0
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.
80 * (09/11/1999)
82 * version 0.5.0:
83 * first Version that gets a version number. Most of the needed
84 * functions work.
85 * (20/10/1999)
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>
98 #include <linux/wait.h>
99 #include <linux/mutex.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 /**************************************************************************
128 Data and structs
129 ***************************************************************************/
132 typedef enum {
133 NOT_CONNECTED, READY, WORKING, DOWNLOAD
134 } mdc800_state;
137 /* Data for the driver */
138 struct mdc800_data
140 struct usb_device * dev; // Device Data
141 mdc800_state state;
143 unsigned int endpoint [4];
145 struct urb * irq_urb;
146 wait_queue_head_t irq_wait;
147 int irq_woken;
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;
157 int written;
160 struct urb * download_urb;
161 char* download_urb_buffer;
162 wait_queue_head_t download_wait;
163 int downloaded;
164 int download_left; // Bytes left to download ?
167 /* Device Data */
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
176 int in_count;
178 int pic_index; // Cache for the Imagesize (-1 for nothing cached )
179 int pic_len;
180 int minor;
184 /* Specification of the Endpoints */
185 static struct usb_endpoint_descriptor mdc800_ed [4] =
188 .bLength = 0,
189 .bDescriptorType = 0,
190 .bEndpointAddress = 0x01,
191 .bmAttributes = 0x02,
192 .wMaxPacketSize = __constant_cpu_to_le16(8),
193 .bInterval = 0,
194 .bRefresh = 0,
195 .bSynchAddress = 0,
198 .bLength = 0,
199 .bDescriptorType = 0,
200 .bEndpointAddress = 0x82,
201 .bmAttributes = 0x03,
202 .wMaxPacketSize = __constant_cpu_to_le16(8),
203 .bInterval = 0,
204 .bRefresh = 0,
205 .bSynchAddress = 0,
208 .bLength = 0,
209 .bDescriptorType = 0,
210 .bEndpointAddress = 0x03,
211 .bmAttributes = 0x02,
212 .wMaxPacketSize = __constant_cpu_to_le16(64),
213 .bInterval = 0,
214 .bRefresh = 0,
215 .bSynchAddress = 0,
218 .bLength = 0,
219 .bDescriptorType = 0,
220 .bEndpointAddress = 0x84,
221 .bmAttributes = 0x02,
222 .wMaxPacketSize = __constant_cpu_to_le16(64),
223 .bInterval = 0,
224 .bRefresh = 0,
225 .bSynchAddress = 0,
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)
239 return (
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)
252 int i=0;
253 while (i<8)
255 if (ch [i] != (char)0x99)
256 return 0;
257 i++;
259 return 1;
264 * Checks whether the Camera is ready
266 static int mdc800_isReady (char *ch)
268 int i=0;
269 while (i<8)
271 if (ch [i] != (char)0xbb)
272 return 0;
273 i++;
275 return 1;
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;
289 if (urb->status >= 0)
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;
299 dbg ("gets busy");
302 else
304 if (mdc800->camera_busy && mdc800_isReady (b))
306 mdc800->camera_busy=0;
307 dbg ("gets ready");
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);
316 data_received=1;
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))
328 (urb->status < 0)
331 if (wake_up)
333 mdc800->camera_request_ready=0;
334 mdc800->irq_woken=1;
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
347 * msec: Time to wait
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 err ("timeout waiting for camera.");
360 return -1;
363 if (mdc800->state == NOT_CONNECTED)
365 warn ("Camera gets disconnected during waiting for irq.");
366 mdc800->camera_request_ready=0;
367 return -2;
370 return 0;
375 * The write_urb callback function
377 static void mdc800_usb_write_notify (struct urb *urb)
379 struct mdc800_data* mdc800=urb->context;
381 if (urb->status != 0)
383 err ("writing command fails (status=%i)", urb->status);
385 else
387 mdc800->state=READY;
389 mdc800->written = 1;
390 wake_up (&mdc800->write_wait);
395 * The download_urb callback function
397 static void mdc800_usb_download_notify (struct urb *urb)
399 struct mdc800_data* mdc800=urb->context;
401 if (urb->status == 0)
403 /* Fill output buffer with these data */
404 memcpy (mdc800->out, urb->transfer_buffer, 64);
405 mdc800->out_count=64;
406 mdc800->out_ptr=0;
407 mdc800->download_left-=64;
408 if (mdc800->download_left == 0)
410 mdc800->state=READY;
413 else
415 err ("request bytes fails (status:%i)", urb->status);
417 mdc800->downloaded = 1;
418 wake_up (&mdc800->download_wait);
422 /***************************************************************************
423 Probing for the Camera
424 ***************************************************************************/
426 static struct usb_driver mdc800_usb_driver;
427 static const struct file_operations mdc800_device_ops;
428 static struct usb_class_driver mdc800_class = {
429 .name = "mdc800%d",
430 .fops = &mdc800_device_ops,
431 .minor_base = MDC800_DEVICE_MINOR_BASE,
436 * Callback to search the Mustek MDC800 on the USB Bus
438 static int mdc800_usb_probe (struct usb_interface *intf,
439 const struct usb_device_id *id)
441 int i,j;
442 struct usb_host_interface *intf_desc;
443 struct usb_device *dev = interface_to_usbdev (intf);
444 int irq_interval=0;
445 int retval;
447 dbg ("(mdc800_usb_probe) called.");
450 if (mdc800->dev != NULL)
452 warn ("only one Mustek MDC800 is supported.");
453 return -ENODEV;
456 if (dev->descriptor.bNumConfigurations != 1)
458 err ("probe fails -> wrong Number of Configuration");
459 return -ENODEV;
461 intf_desc = intf->cur_altsetting;
463 if (
464 ( intf_desc->desc.bInterfaceClass != 0xff )
465 || ( intf_desc->desc.bInterfaceSubClass != 0 )
466 || ( intf_desc->desc.bInterfaceProtocol != 0 )
467 || ( intf_desc->desc.bNumEndpoints != 4)
470 err ("probe fails -> wrong Interface");
471 return -ENODEV;
474 /* Check the Endpoints */
475 for (i=0; i<4; i++)
477 mdc800->endpoint[i]=-1;
478 for (j=0; j<4; j++)
480 if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
482 mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
483 if (i==1)
485 irq_interval=intf_desc->endpoint [j].desc.bInterval;
488 continue;
491 if (mdc800->endpoint[i] == -1)
493 err ("probe fails -> Wrong Endpoints.");
494 return -ENODEV;
499 info ("Found Mustek MDC800 on USB.");
501 mutex_lock(&mdc800->io_lock);
503 retval = usb_register_dev(intf, &mdc800_class);
504 if (retval) {
505 err ("Not able to get a minor for this device.");
506 return -ENODEV;
509 mdc800->dev=dev;
510 mdc800->open=0;
512 /* Setup URB Structs */
513 usb_fill_int_urb (
514 mdc800->irq_urb,
515 mdc800->dev,
516 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
517 mdc800->irq_urb_buffer,
519 mdc800_usb_irq,
520 mdc800,
521 irq_interval
524 usb_fill_bulk_urb (
525 mdc800->write_urb,
526 mdc800->dev,
527 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
528 mdc800->write_urb_buffer,
530 mdc800_usb_write_notify,
531 mdc800
534 usb_fill_bulk_urb (
535 mdc800->download_urb,
536 mdc800->dev,
537 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
538 mdc800->download_urb_buffer,
540 mdc800_usb_download_notify,
541 mdc800
544 mdc800->state=READY;
546 mutex_unlock(&mdc800->io_lock);
548 usb_set_intfdata(intf, mdc800);
549 return 0;
554 * Disconnect USB device (maybe the MDC800)
556 static void mdc800_usb_disconnect (struct usb_interface *intf)
558 struct mdc800_data* mdc800 = usb_get_intfdata(intf);
560 dbg ("(mdc800_usb_disconnect) called");
562 if (mdc800) {
563 if (mdc800->state == NOT_CONNECTED)
564 return;
566 usb_deregister_dev(intf, &mdc800_class);
568 mdc800->state=NOT_CONNECTED;
570 usb_kill_urb(mdc800->irq_urb);
571 usb_kill_urb(mdc800->write_urb);
572 usb_kill_urb(mdc800->download_urb);
574 mdc800->dev = NULL;
575 usb_set_intfdata(intf, NULL);
577 info ("Mustek MDC800 disconnected from USB.");
581 /***************************************************************************
582 The Misc device Part (file_operations)
583 ****************************************************************************/
586 * This Function calc the Answersize for a command.
588 static int mdc800_getAnswerSize (char command)
590 switch ((unsigned char) command)
592 case 0x2a:
593 case 0x49:
594 case 0x51:
595 case 0x0d:
596 case 0x20:
597 case 0x07:
598 case 0x01:
599 case 0x25:
600 case 0x00:
601 return 8;
603 case 0x05:
604 case 0x3e:
605 return mdc800->pic_len;
607 case 0x09:
608 return 4096;
610 default:
611 return 0;
617 * Init the device: (1) alloc mem (2) Increase MOD Count ..
619 static int mdc800_device_open (struct inode* inode, struct file *file)
621 int retval=0;
622 int errn=0;
624 mutex_lock(&mdc800->io_lock);
626 if (mdc800->state == NOT_CONNECTED)
628 errn=-EBUSY;
629 goto error_out;
631 if (mdc800->open)
633 errn=-EBUSY;
634 goto error_out;
637 mdc800->in_count=0;
638 mdc800->out_count=0;
639 mdc800->out_ptr=0;
640 mdc800->pic_index=0;
641 mdc800->pic_len=-1;
642 mdc800->download_left=0;
644 mdc800->camera_busy=0;
645 mdc800->camera_request_ready=0;
647 retval=0;
648 mdc800->irq_urb->dev = mdc800->dev;
649 if (usb_submit_urb (mdc800->irq_urb, GFP_KERNEL))
651 err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval, mdc800->irq_urb->status);
652 errn = -EIO;
653 goto error_out;
656 mdc800->open=1;
657 dbg ("Mustek MDC800 device opened.");
659 error_out:
660 mutex_unlock(&mdc800->io_lock);
661 return errn;
666 * Close the Camera and release Memory
668 static int mdc800_device_release (struct inode* inode, struct file *file)
670 int retval=0;
671 dbg ("Mustek MDC800 device closed.");
673 mutex_lock(&mdc800->io_lock);
674 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
676 usb_kill_urb(mdc800->irq_urb);
677 usb_kill_urb(mdc800->write_urb);
678 usb_kill_urb(mdc800->download_urb);
679 mdc800->open=0;
681 else
683 retval=-EIO;
686 mutex_unlock(&mdc800->io_lock);
687 return retval;
692 * The Device read callback Function
694 static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
696 size_t left=len, sts=len; /* single transfer size */
697 char __user *ptr = buf;
699 mutex_lock(&mdc800->io_lock);
700 if (mdc800->state == NOT_CONNECTED)
702 mutex_unlock(&mdc800->io_lock);
703 return -EBUSY;
705 if (mdc800->state == WORKING)
707 warn ("Illegal State \"working\" reached during read ?!");
708 mutex_unlock(&mdc800->io_lock);
709 return -EBUSY;
711 if (!mdc800->open)
713 mutex_unlock(&mdc800->io_lock);
714 return -EBUSY;
717 while (left)
719 if (signal_pending (current))
721 mutex_unlock(&mdc800->io_lock);
722 return -EINTR;
725 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
727 if (sts <= 0)
729 /* Too less Data in buffer */
730 if (mdc800->state == DOWNLOAD)
732 mdc800->out_count=0;
733 mdc800->out_ptr=0;
735 /* Download -> Request new bytes */
736 mdc800->download_urb->dev = mdc800->dev;
737 if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
739 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
740 mutex_unlock(&mdc800->io_lock);
741 return len-left;
743 wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
744 TO_DOWNLOAD_GET_READY*HZ/1000);
745 mdc800->downloaded = 0;
746 if (mdc800->download_urb->status != 0)
748 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
749 mutex_unlock(&mdc800->io_lock);
750 return len-left;
753 else
755 /* No more bytes -> that's an error*/
756 mutex_unlock(&mdc800->io_lock);
757 return -EIO;
760 else
762 /* Copy Bytes */
763 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
764 sts)) {
765 mutex_unlock(&mdc800->io_lock);
766 return -EFAULT;
768 ptr+=sts;
769 left-=sts;
770 mdc800->out_ptr+=sts;
774 mutex_unlock(&mdc800->io_lock);
775 return len-left;
780 * The Device write callback Function
781 * If a 8Byte Command is received, it will be send to the camera.
782 * After this the driver initiates the request for the answer or
783 * just waits until the camera becomes ready.
785 static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
787 size_t i=0;
789 mutex_lock(&mdc800->io_lock);
790 if (mdc800->state != READY)
792 mutex_unlock(&mdc800->io_lock);
793 return -EBUSY;
795 if (!mdc800->open )
797 mutex_unlock(&mdc800->io_lock);
798 return -EBUSY;
801 while (i<len)
803 unsigned char c;
804 if (signal_pending (current))
806 mutex_unlock(&mdc800->io_lock);
807 return -EINTR;
810 if(get_user(c, buf+i))
812 mutex_unlock(&mdc800->io_lock);
813 return -EFAULT;
816 /* check for command start */
817 if (c == 0x55)
819 mdc800->in_count=0;
820 mdc800->out_count=0;
821 mdc800->out_ptr=0;
822 mdc800->download_left=0;
825 /* save command byte */
826 if (mdc800->in_count < 8)
828 mdc800->in[mdc800->in_count] = c;
829 mdc800->in_count++;
831 else
833 mutex_unlock(&mdc800->io_lock);
834 return -EIO;
837 /* Command Buffer full ? -> send it to camera */
838 if (mdc800->in_count == 8)
840 int answersize;
842 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
844 err ("Camera didn't get ready.\n");
845 mutex_unlock(&mdc800->io_lock);
846 return -EIO;
849 answersize=mdc800_getAnswerSize (mdc800->in[1]);
851 mdc800->state=WORKING;
852 memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
853 mdc800->write_urb->dev = mdc800->dev;
854 if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
856 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
857 mutex_unlock(&mdc800->io_lock);
858 return -EIO;
860 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
861 mdc800->written = 0;
862 if (mdc800->state == WORKING)
864 usb_kill_urb(mdc800->write_urb);
865 mutex_unlock(&mdc800->io_lock);
866 return -EIO;
869 switch ((unsigned char) mdc800->in[1])
871 case 0x05: /* Download Image */
872 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
873 if (mdc800->pic_len < 0)
875 err ("call 0x07 before 0x05,0x3e");
876 mdc800->state=READY;
877 mutex_unlock(&mdc800->io_lock);
878 return -EIO;
880 mdc800->pic_len=-1;
882 case 0x09: /* Download Thumbnail */
883 mdc800->download_left=answersize+64;
884 mdc800->state=DOWNLOAD;
885 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
886 break;
889 default:
890 if (answersize)
893 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
895 err ("requesting answer from irq fails");
896 mutex_unlock(&mdc800->io_lock);
897 return -EIO;
900 /* Write dummy data, (this is ugly but part of the USB Protocol */
901 /* if you use endpoint 1 as bulk and not as irq) */
902 memcpy (mdc800->out, mdc800->camera_response,8);
904 /* This is the interpreted answer */
905 memcpy (&mdc800->out[8], mdc800->camera_response,8);
907 mdc800->out_ptr=0;
908 mdc800->out_count=16;
910 /* Cache the Imagesize, if command was getImageSize */
911 if (mdc800->in [1] == (char) 0x07)
913 mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
915 dbg ("cached imagesize = %i",mdc800->pic_len);
919 else
921 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
923 err ("Command Timeout.");
924 mutex_unlock(&mdc800->io_lock);
925 return -EIO;
928 mdc800->state=READY;
929 break;
932 i++;
934 mutex_unlock(&mdc800->io_lock);
935 return i;
939 /***************************************************************************
940 Init and Cleanup this driver (Structs and types)
941 ****************************************************************************/
943 /* File Operations of this drivers */
944 static const struct file_operations mdc800_device_ops =
946 .owner = THIS_MODULE,
947 .read = mdc800_device_read,
948 .write = mdc800_device_write,
949 .open = mdc800_device_open,
950 .release = mdc800_device_release,
955 static struct usb_device_id mdc800_table [] = {
956 { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
957 { } /* Terminating entry */
960 MODULE_DEVICE_TABLE (usb, mdc800_table);
962 * USB Driver Struct for this device
964 static struct usb_driver mdc800_usb_driver =
966 .name = "mdc800",
967 .probe = mdc800_usb_probe,
968 .disconnect = mdc800_usb_disconnect,
969 .id_table = mdc800_table
974 /************************************************************************
975 Init and Cleanup this driver (Main Functions)
976 *************************************************************************/
978 static int __init usb_mdc800_init (void)
980 int retval = -ENODEV;
981 /* Allocate Memory */
982 mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
983 if (!mdc800)
984 goto cleanup_on_fail;
986 mdc800->dev = NULL;
987 mdc800->state=NOT_CONNECTED;
988 mutex_init (&mdc800->io_lock);
990 init_waitqueue_head (&mdc800->irq_wait);
991 init_waitqueue_head (&mdc800->write_wait);
992 init_waitqueue_head (&mdc800->download_wait);
994 mdc800->irq_woken = 0;
995 mdc800->downloaded = 0;
996 mdc800->written = 0;
998 mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
999 if (!mdc800->irq_urb_buffer)
1000 goto cleanup_on_fail;
1001 mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
1002 if (!mdc800->write_urb_buffer)
1003 goto cleanup_on_fail;
1004 mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
1005 if (!mdc800->download_urb_buffer)
1006 goto cleanup_on_fail;
1008 mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
1009 if (!mdc800->irq_urb)
1010 goto cleanup_on_fail;
1011 mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
1012 if (!mdc800->download_urb)
1013 goto cleanup_on_fail;
1014 mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
1015 if (!mdc800->write_urb)
1016 goto cleanup_on_fail;
1018 /* Register the driver */
1019 retval = usb_register(&mdc800_usb_driver);
1020 if (retval)
1021 goto cleanup_on_fail;
1023 info (DRIVER_VERSION ":" DRIVER_DESC);
1025 return 0;
1027 /* Clean driver up, when something fails */
1029 cleanup_on_fail:
1031 if (mdc800 != NULL)
1033 err ("can't alloc memory!");
1035 kfree(mdc800->download_urb_buffer);
1036 kfree(mdc800->write_urb_buffer);
1037 kfree(mdc800->irq_urb_buffer);
1039 usb_free_urb(mdc800->write_urb);
1040 usb_free_urb(mdc800->download_urb);
1041 usb_free_urb(mdc800->irq_urb);
1043 kfree (mdc800);
1045 mdc800 = NULL;
1046 return retval;
1050 static void __exit usb_mdc800_cleanup (void)
1052 usb_deregister (&mdc800_usb_driver);
1054 usb_free_urb (mdc800->irq_urb);
1055 usb_free_urb (mdc800->download_urb);
1056 usb_free_urb (mdc800->write_urb);
1058 kfree (mdc800->irq_urb_buffer);
1059 kfree (mdc800->write_urb_buffer);
1060 kfree (mdc800->download_urb_buffer);
1062 kfree (mdc800);
1063 mdc800 = NULL;
1066 module_init (usb_mdc800_init);
1067 module_exit (usb_mdc800_cleanup);
1069 MODULE_AUTHOR( DRIVER_AUTHOR );
1070 MODULE_DESCRIPTION( DRIVER_DESC );
1071 MODULE_LICENSE("GPL");