kbuild: document howto build external modules using several directories
[linux-2.6/verdex.git] / drivers / usb / image / mdc800.c
blob049871145d63735bd1fff17ee55dcc23a4ffd826
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>
100 #include <linux/usb.h>
101 #include <linux/fs.h>
104 * Version Information
106 #define DRIVER_VERSION "v0.7.5 (30/10/2000)"
107 #define DRIVER_AUTHOR "Henning Zabel <henning@uni-paderborn.de>"
108 #define DRIVER_DESC "USB Driver for Mustek MDC800 Digital Camera"
110 /* Vendor and Product Information */
111 #define MDC800_VENDOR_ID 0x055f
112 #define MDC800_PRODUCT_ID 0xa800
114 /* Timeouts (msec) */
115 #define TO_DOWNLOAD_GET_READY 1500
116 #define TO_DOWNLOAD_GET_BUSY 1500
117 #define TO_WRITE_GET_READY 1000
118 #define TO_DEFAULT_COMMAND 5000
119 #define TO_READ_FROM_IRQ TO_DEFAULT_COMMAND
120 #define TO_GET_READY TO_DEFAULT_COMMAND
122 /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
123 #define MDC800_DEVICE_MINOR_BASE 32
126 /**************************************************************************
127 Data and structs
128 ***************************************************************************/
131 typedef enum {
132 NOT_CONNECTED, READY, WORKING, DOWNLOAD
133 } mdc800_state;
136 /* Data for the driver */
137 struct mdc800_data
139 struct usb_device * dev; // Device Data
140 mdc800_state state;
142 unsigned int endpoint [4];
144 struct urb * irq_urb;
145 wait_queue_head_t irq_wait;
146 int irq_woken;
147 char* irq_urb_buffer;
149 int camera_busy; // is camera busy ?
150 int camera_request_ready; // Status to synchronize with irq
151 char camera_response [8]; // last Bytes send after busy
153 struct urb * write_urb;
154 char* write_urb_buffer;
155 wait_queue_head_t write_wait;
156 int written;
159 struct urb * download_urb;
160 char* download_urb_buffer;
161 wait_queue_head_t download_wait;
162 int downloaded;
163 int download_left; // Bytes left to download ?
166 /* Device Data */
167 char out [64]; // Answer Buffer
168 int out_ptr; // Index to the first not readen byte
169 int out_count; // Bytes in the buffer
171 int open; // Camera device open ?
172 struct semaphore io_lock; // IO -lock
174 char in [8]; // Command Input Buffer
175 int in_count;
177 int pic_index; // Cache for the Imagesize (-1 for nothing cached )
178 int pic_len;
179 int minor;
183 /* Specification of the Endpoints */
184 static struct usb_endpoint_descriptor mdc800_ed [4] =
187 .bLength = 0,
188 .bDescriptorType = 0,
189 .bEndpointAddress = 0x01,
190 .bmAttributes = 0x02,
191 .wMaxPacketSize = __constant_cpu_to_le16(8),
192 .bInterval = 0,
193 .bRefresh = 0,
194 .bSynchAddress = 0,
197 .bLength = 0,
198 .bDescriptorType = 0,
199 .bEndpointAddress = 0x82,
200 .bmAttributes = 0x03,
201 .wMaxPacketSize = __constant_cpu_to_le16(8),
202 .bInterval = 0,
203 .bRefresh = 0,
204 .bSynchAddress = 0,
207 .bLength = 0,
208 .bDescriptorType = 0,
209 .bEndpointAddress = 0x03,
210 .bmAttributes = 0x02,
211 .wMaxPacketSize = __constant_cpu_to_le16(64),
212 .bInterval = 0,
213 .bRefresh = 0,
214 .bSynchAddress = 0,
217 .bLength = 0,
218 .bDescriptorType = 0,
219 .bEndpointAddress = 0x84,
220 .bmAttributes = 0x02,
221 .wMaxPacketSize = __constant_cpu_to_le16(64),
222 .bInterval = 0,
223 .bRefresh = 0,
224 .bSynchAddress = 0,
228 /* The Variable used by the driver */
229 static struct mdc800_data* mdc800;
232 /***************************************************************************
233 The USB Part of the driver
234 ****************************************************************************/
236 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
238 return (
239 ( a->bEndpointAddress == b->bEndpointAddress )
240 && ( a->bmAttributes == b->bmAttributes )
241 && ( a->wMaxPacketSize == b->wMaxPacketSize )
247 * Checks whether the camera responds busy
249 static int mdc800_isBusy (char* ch)
251 int i=0;
252 while (i<8)
254 if (ch [i] != (char)0x99)
255 return 0;
256 i++;
258 return 1;
263 * Checks whether the Camera is ready
265 static int mdc800_isReady (char *ch)
267 int i=0;
268 while (i<8)
270 if (ch [i] != (char)0xbb)
271 return 0;
272 i++;
274 return 1;
280 * USB IRQ Handler for InputLine
282 static void mdc800_usb_irq (struct urb *urb, struct pt_regs *res)
284 int data_received=0, wake_up;
285 unsigned char* b=urb->transfer_buffer;
286 struct mdc800_data* mdc800=urb->context;
288 if (urb->status >= 0)
291 //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]);
293 if (mdc800_isBusy (b))
295 if (!mdc800->camera_busy)
297 mdc800->camera_busy=1;
298 dbg ("gets busy");
301 else
303 if (mdc800->camera_busy && mdc800_isReady (b))
305 mdc800->camera_busy=0;
306 dbg ("gets ready");
309 if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
311 /* Store Data in camera_answer field */
312 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]);
314 memcpy (mdc800->camera_response,b,8);
315 data_received=1;
318 wake_up= ( mdc800->camera_request_ready > 0 )
321 ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
323 ((mdc800->camera_request_ready == 2) && data_received)
325 ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
327 (urb->status < 0)
330 if (wake_up)
332 mdc800->camera_request_ready=0;
333 mdc800->irq_woken=1;
334 wake_up (&mdc800->irq_wait);
340 * Waits a while until the irq responds that camera is ready
342 * mode : 0: Wait for camera gets ready
343 * 1: Wait for receiving data
344 * 2: Wait for camera gets busy
346 * msec: Time to wait
348 static int mdc800_usb_waitForIRQ (int mode, int msec)
350 mdc800->camera_request_ready=1+mode;
352 wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000);
353 mdc800->irq_woken = 0;
355 if (mdc800->camera_request_ready>0)
357 mdc800->camera_request_ready=0;
358 err ("timeout waiting for camera.");
359 return -1;
362 if (mdc800->state == NOT_CONNECTED)
364 warn ("Camera gets disconnected during waiting for irq.");
365 mdc800->camera_request_ready=0;
366 return -2;
369 return 0;
374 * The write_urb callback function
376 static void mdc800_usb_write_notify (struct urb *urb, struct pt_regs *res)
378 struct mdc800_data* mdc800=urb->context;
380 if (urb->status != 0)
382 err ("writing command fails (status=%i)", urb->status);
384 else
386 mdc800->state=READY;
388 mdc800->written = 1;
389 wake_up (&mdc800->write_wait);
394 * The download_urb callback function
396 static void mdc800_usb_download_notify (struct urb *urb, struct pt_regs *res)
398 struct mdc800_data* mdc800=urb->context;
400 if (urb->status == 0)
402 /* Fill output buffer with these data */
403 memcpy (mdc800->out, urb->transfer_buffer, 64);
404 mdc800->out_count=64;
405 mdc800->out_ptr=0;
406 mdc800->download_left-=64;
407 if (mdc800->download_left == 0)
409 mdc800->state=READY;
412 else
414 err ("request bytes fails (status:%i)", urb->status);
416 mdc800->downloaded = 1;
417 wake_up (&mdc800->download_wait);
421 /***************************************************************************
422 Probing for the Camera
423 ***************************************************************************/
425 static struct usb_driver mdc800_usb_driver;
426 static struct file_operations mdc800_device_ops;
427 static struct usb_class_driver mdc800_class = {
428 .name = "mdc800%d",
429 .fops = &mdc800_device_ops,
430 .minor_base = MDC800_DEVICE_MINOR_BASE,
435 * Callback to search the Mustek MDC800 on the USB Bus
437 static int mdc800_usb_probe (struct usb_interface *intf,
438 const struct usb_device_id *id)
440 int i,j;
441 struct usb_host_interface *intf_desc;
442 struct usb_device *dev = interface_to_usbdev (intf);
443 int irq_interval=0;
444 int retval;
446 dbg ("(mdc800_usb_probe) called.");
449 if (mdc800->dev != NULL)
451 warn ("only one Mustek MDC800 is supported.");
452 return -ENODEV;
455 if (dev->descriptor.bNumConfigurations != 1)
457 err ("probe fails -> wrong Number of Configuration");
458 return -ENODEV;
460 intf_desc = intf->cur_altsetting;
462 if (
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 err ("probe fails -> wrong Interface");
470 return -ENODEV;
473 /* Check the Endpoints */
474 for (i=0; i<4; i++)
476 mdc800->endpoint[i]=-1;
477 for (j=0; j<4; j++)
479 if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
481 mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
482 if (i==1)
484 irq_interval=intf_desc->endpoint [j].desc.bInterval;
487 continue;
490 if (mdc800->endpoint[i] == -1)
492 err ("probe fails -> Wrong Endpoints.");
493 return -ENODEV;
498 info ("Found Mustek MDC800 on USB.");
500 down (&mdc800->io_lock);
502 retval = usb_register_dev(intf, &mdc800_class);
503 if (retval) {
504 err ("Not able to get a minor for this device.");
505 return -ENODEV;
508 mdc800->dev=dev;
509 mdc800->open=0;
511 /* Setup URB Structs */
512 usb_fill_int_urb (
513 mdc800->irq_urb,
514 mdc800->dev,
515 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
516 mdc800->irq_urb_buffer,
518 mdc800_usb_irq,
519 mdc800,
520 irq_interval
523 usb_fill_bulk_urb (
524 mdc800->write_urb,
525 mdc800->dev,
526 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
527 mdc800->write_urb_buffer,
529 mdc800_usb_write_notify,
530 mdc800
533 usb_fill_bulk_urb (
534 mdc800->download_urb,
535 mdc800->dev,
536 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
537 mdc800->download_urb_buffer,
539 mdc800_usb_download_notify,
540 mdc800
543 mdc800->state=READY;
545 up (&mdc800->io_lock);
547 usb_set_intfdata(intf, mdc800);
548 return 0;
553 * Disconnect USB device (maybe the MDC800)
555 static void mdc800_usb_disconnect (struct usb_interface *intf)
557 struct mdc800_data* mdc800 = usb_get_intfdata(intf);
559 dbg ("(mdc800_usb_disconnect) called");
561 if (mdc800) {
562 if (mdc800->state == NOT_CONNECTED)
563 return;
565 usb_deregister_dev(intf, &mdc800_class);
567 mdc800->state=NOT_CONNECTED;
569 usb_kill_urb(mdc800->irq_urb);
570 usb_kill_urb(mdc800->write_urb);
571 usb_kill_urb(mdc800->download_urb);
573 mdc800->dev = NULL;
574 usb_set_intfdata(intf, NULL);
576 info ("Mustek MDC800 disconnected from USB.");
580 /***************************************************************************
581 The Misc device Part (file_operations)
582 ****************************************************************************/
585 * This Function calc the Answersize for a command.
587 static int mdc800_getAnswerSize (char command)
589 switch ((unsigned char) command)
591 case 0x2a:
592 case 0x49:
593 case 0x51:
594 case 0x0d:
595 case 0x20:
596 case 0x07:
597 case 0x01:
598 case 0x25:
599 case 0x00:
600 return 8;
602 case 0x05:
603 case 0x3e:
604 return mdc800->pic_len;
606 case 0x09:
607 return 4096;
609 default:
610 return 0;
616 * Init the device: (1) alloc mem (2) Increase MOD Count ..
618 static int mdc800_device_open (struct inode* inode, struct file *file)
620 int retval=0;
621 int errn=0;
623 down (&mdc800->io_lock);
625 if (mdc800->state == NOT_CONNECTED)
627 errn=-EBUSY;
628 goto error_out;
630 if (mdc800->open)
632 errn=-EBUSY;
633 goto error_out;
636 mdc800->in_count=0;
637 mdc800->out_count=0;
638 mdc800->out_ptr=0;
639 mdc800->pic_index=0;
640 mdc800->pic_len=-1;
641 mdc800->download_left=0;
643 mdc800->camera_busy=0;
644 mdc800->camera_request_ready=0;
646 retval=0;
647 mdc800->irq_urb->dev = mdc800->dev;
648 if (usb_submit_urb (mdc800->irq_urb, GFP_KERNEL))
650 err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval, mdc800->irq_urb->status);
651 errn = -EIO;
652 goto error_out;
655 mdc800->open=1;
656 dbg ("Mustek MDC800 device opened.");
658 error_out:
659 up (&mdc800->io_lock);
660 return errn;
665 * Close the Camera and release Memory
667 static int mdc800_device_release (struct inode* inode, struct file *file)
669 int retval=0;
670 dbg ("Mustek MDC800 device closed.");
672 down (&mdc800->io_lock);
673 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
675 usb_kill_urb(mdc800->irq_urb);
676 usb_kill_urb(mdc800->write_urb);
677 usb_kill_urb(mdc800->download_urb);
678 mdc800->open=0;
680 else
682 retval=-EIO;
685 up(&mdc800->io_lock);
686 return retval;
691 * The Device read callback Function
693 static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
695 size_t left=len, sts=len; /* single transfer size */
696 char __user *ptr = buf;
698 down (&mdc800->io_lock);
699 if (mdc800->state == NOT_CONNECTED)
701 up (&mdc800->io_lock);
702 return -EBUSY;
704 if (mdc800->state == WORKING)
706 warn ("Illegal State \"working\" reached during read ?!");
707 up (&mdc800->io_lock);
708 return -EBUSY;
710 if (!mdc800->open)
712 up (&mdc800->io_lock);
713 return -EBUSY;
716 while (left)
718 if (signal_pending (current))
720 up (&mdc800->io_lock);
721 return -EINTR;
724 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
726 if (sts <= 0)
728 /* Too less Data in buffer */
729 if (mdc800->state == DOWNLOAD)
731 mdc800->out_count=0;
732 mdc800->out_ptr=0;
734 /* Download -> Request new bytes */
735 mdc800->download_urb->dev = mdc800->dev;
736 if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
738 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
739 up (&mdc800->io_lock);
740 return len-left;
742 wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
743 TO_DOWNLOAD_GET_READY*HZ/1000);
744 mdc800->downloaded = 0;
745 if (mdc800->download_urb->status != 0)
747 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
748 up (&mdc800->io_lock);
749 return len-left;
752 else
754 /* No more bytes -> that's an error*/
755 up (&mdc800->io_lock);
756 return -EIO;
759 else
761 /* Copy Bytes */
762 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
763 sts)) {
764 up(&mdc800->io_lock);
765 return -EFAULT;
767 ptr+=sts;
768 left-=sts;
769 mdc800->out_ptr+=sts;
773 up (&mdc800->io_lock);
774 return len-left;
779 * The Device write callback Function
780 * If a 8Byte Command is received, it will be send to the camera.
781 * After this the driver initiates the request for the answer or
782 * just waits until the camera becomes ready.
784 static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
786 size_t i=0;
788 down (&mdc800->io_lock);
789 if (mdc800->state != READY)
791 up (&mdc800->io_lock);
792 return -EBUSY;
794 if (!mdc800->open )
796 up (&mdc800->io_lock);
797 return -EBUSY;
800 while (i<len)
802 unsigned char c;
803 if (signal_pending (current))
805 up (&mdc800->io_lock);
806 return -EINTR;
809 if(get_user(c, buf+i))
811 up(&mdc800->io_lock);
812 return -EFAULT;
815 /* check for command start */
816 if (c == 0x55)
818 mdc800->in_count=0;
819 mdc800->out_count=0;
820 mdc800->out_ptr=0;
821 mdc800->download_left=0;
824 /* save command byte */
825 if (mdc800->in_count < 8)
827 mdc800->in[mdc800->in_count] = c;
828 mdc800->in_count++;
830 else
832 up (&mdc800->io_lock);
833 return -EIO;
836 /* Command Buffer full ? -> send it to camera */
837 if (mdc800->in_count == 8)
839 int answersize;
841 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
843 err ("Camera didn't get ready.\n");
844 up (&mdc800->io_lock);
845 return -EIO;
848 answersize=mdc800_getAnswerSize (mdc800->in[1]);
850 mdc800->state=WORKING;
851 memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
852 mdc800->write_urb->dev = mdc800->dev;
853 if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
855 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
856 up (&mdc800->io_lock);
857 return -EIO;
859 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
860 mdc800->written = 0;
861 if (mdc800->state == WORKING)
863 usb_kill_urb(mdc800->write_urb);
864 up (&mdc800->io_lock);
865 return -EIO;
868 switch ((unsigned char) mdc800->in[1])
870 case 0x05: /* Download Image */
871 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
872 if (mdc800->pic_len < 0)
874 err ("call 0x07 before 0x05,0x3e");
875 mdc800->state=READY;
876 up (&mdc800->io_lock);
877 return -EIO;
879 mdc800->pic_len=-1;
881 case 0x09: /* Download Thumbnail */
882 mdc800->download_left=answersize+64;
883 mdc800->state=DOWNLOAD;
884 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
885 break;
888 default:
889 if (answersize)
892 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
894 err ("requesting answer from irq fails");
895 up (&mdc800->io_lock);
896 return -EIO;
899 /* Write dummy data, (this is ugly but part of the USB Protocol */
900 /* if you use endpoint 1 as bulk and not as irq) */
901 memcpy (mdc800->out, mdc800->camera_response,8);
903 /* This is the interpreted answer */
904 memcpy (&mdc800->out[8], mdc800->camera_response,8);
906 mdc800->out_ptr=0;
907 mdc800->out_count=16;
909 /* Cache the Imagesize, if command was getImageSize */
910 if (mdc800->in [1] == (char) 0x07)
912 mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
914 dbg ("cached imagesize = %i",mdc800->pic_len);
918 else
920 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
922 err ("Command Timeout.");
923 up (&mdc800->io_lock);
924 return -EIO;
927 mdc800->state=READY;
928 break;
931 i++;
933 up (&mdc800->io_lock);
934 return i;
938 /***************************************************************************
939 Init and Cleanup this driver (Structs and types)
940 ****************************************************************************/
942 /* File Operations of this drivers */
943 static struct file_operations mdc800_device_ops =
945 .owner = THIS_MODULE,
946 .read = mdc800_device_read,
947 .write = mdc800_device_write,
948 .open = mdc800_device_open,
949 .release = mdc800_device_release,
954 static struct usb_device_id mdc800_table [] = {
955 { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
956 { } /* Terminating entry */
959 MODULE_DEVICE_TABLE (usb, mdc800_table);
961 * USB Driver Struct for this device
963 static struct usb_driver mdc800_usb_driver =
965 .name = "mdc800",
966 .probe = mdc800_usb_probe,
967 .disconnect = mdc800_usb_disconnect,
968 .id_table = mdc800_table
973 /************************************************************************
974 Init and Cleanup this driver (Main Functions)
975 *************************************************************************/
977 static int __init usb_mdc800_init (void)
979 int retval = -ENODEV;
980 /* Allocate Memory */
981 mdc800=kmalloc (sizeof (struct mdc800_data), GFP_KERNEL);
982 if (!mdc800)
983 goto cleanup_on_fail;
985 memset(mdc800, 0, sizeof(struct mdc800_data));
986 mdc800->dev = NULL;
987 mdc800->open=0;
988 mdc800->state=NOT_CONNECTED;
989 init_MUTEX (&mdc800->io_lock);
991 init_waitqueue_head (&mdc800->irq_wait);
992 init_waitqueue_head (&mdc800->write_wait);
993 init_waitqueue_head (&mdc800->download_wait);
995 mdc800->irq_woken = 0;
996 mdc800->downloaded = 0;
997 mdc800->written = 0;
999 mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
1000 if (!mdc800->irq_urb_buffer)
1001 goto cleanup_on_fail;
1002 mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
1003 if (!mdc800->write_urb_buffer)
1004 goto cleanup_on_fail;
1005 mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
1006 if (!mdc800->download_urb_buffer)
1007 goto cleanup_on_fail;
1009 mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
1010 if (!mdc800->irq_urb)
1011 goto cleanup_on_fail;
1012 mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
1013 if (!mdc800->download_urb)
1014 goto cleanup_on_fail;
1015 mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
1016 if (!mdc800->write_urb)
1017 goto cleanup_on_fail;
1019 /* Register the driver */
1020 retval = usb_register(&mdc800_usb_driver);
1021 if (retval)
1022 goto cleanup_on_fail;
1024 info (DRIVER_VERSION ":" DRIVER_DESC);
1026 return 0;
1028 /* Clean driver up, when something fails */
1030 cleanup_on_fail:
1032 if (mdc800 != NULL)
1034 err ("can't alloc memory!");
1036 kfree(mdc800->download_urb_buffer);
1037 kfree(mdc800->write_urb_buffer);
1038 kfree(mdc800->irq_urb_buffer);
1040 usb_free_urb(mdc800->write_urb);
1041 usb_free_urb(mdc800->download_urb);
1042 usb_free_urb(mdc800->irq_urb);
1044 kfree (mdc800);
1046 mdc800 = NULL;
1047 return retval;
1051 static void __exit usb_mdc800_cleanup (void)
1053 usb_deregister (&mdc800_usb_driver);
1055 usb_free_urb (mdc800->irq_urb);
1056 usb_free_urb (mdc800->download_urb);
1057 usb_free_urb (mdc800->write_urb);
1059 kfree (mdc800->irq_urb_buffer);
1060 kfree (mdc800->write_urb_buffer);
1061 kfree (mdc800->download_urb_buffer);
1063 kfree (mdc800);
1064 mdc800 = NULL;
1067 module_init (usb_mdc800_init);
1068 module_exit (usb_mdc800_cleanup);
1070 MODULE_AUTHOR( DRIVER_AUTHOR );
1071 MODULE_DESCRIPTION( DRIVER_DESC );
1072 MODULE_LICENSE("GPL");