xref: /linux/drivers/usb/image/mdc800.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * copyright (C) 1999/2000 by Henning Zabel <henning@uni-paderborn.de>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 
21 /*
22  *	USB-Kernel Driver for the Mustek MDC800 Digital Camera
23  *	(c) 1999/2000 Henning Zabel <henning@uni-paderborn.de>
24  *
25  *
26  * The driver brings the USB functions of the MDC800 to Linux.
27  * To use the Camera you must support the USB Protocol of the camera
28  * to the Kernel Node.
29  * The Driver uses a misc device Node. Create it with :
30  * mknod /dev/mustek c 180 32
31  *
32  * The driver supports only one camera.
33  *
34  * Fix: mdc800 used sleep_on and slept with io_lock held.
35  * Converted sleep_on to waitqueues with schedule_timeout and made io_lock
36  * a semaphore from a spinlock.
37  * by Oliver Neukum <oliver@neukum.name>
38  * (02/12/2001)
39  *
40  * Identify version on module load.
41  * (08/04/2001) gb
42  *
43  * version 0.7.5
44  * Fixed potential SMP races with Spinlocks.
45  * Thanks to Oliver Neukum <oliver@neukum.name> who
46  * noticed the race conditions.
47  * (30/10/2000)
48  *
49  * Fixed: Setting urb->dev before submitting urb.
50  * by Greg KH <greg@kroah.com>
51  * (13/10/2000)
52  *
53  * version 0.7.3
54  * bugfix : The mdc800->state field gets set to READY after the
55  * the disconnect function sets it to NOT_CONNECTED. This makes the
56  * driver running like the camera is connected and causes some
57  * hang ups.
58  *
59  * version 0.7.1
60  * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
61  * problems when compiled as Module.
62  * (04/04/2000)
63  *
64  * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
65  * was updated to use these values.
66  * (26/03/2000)
67  *
68  * The Init und Exit Module Function are updated.
69  * (01/03/2000)
70  *
71  * version 0.7.0
72  * Rewrite of the driver : The driver now uses URB's. The old stuff
73  * has been removed.
74  *
75  * version 0.6.0
76  * Rewrite of this driver: The Emulation of the rs232 protocoll
77  * has been removed from the driver. A special executeCommand function
78  * for this driver is included to gphoto.
79  * The driver supports two kind of communication to bulk endpoints.
80  * Either with the dev->bus->ops->bulk... or with callback function.
81  * (09/11/1999)
82  *
83  * version 0.5.0:
84  * first Version that gets a version number. Most of the needed
85  * functions work.
86  * (20/10/1999)
87  */
88 
89 #include <linux/sched/signal.h>
90 #include <linux/signal.h>
91 #include <linux/spinlock.h>
92 #include <linux/errno.h>
93 #include <linux/random.h>
94 #include <linux/poll.h>
95 #include <linux/init.h>
96 #include <linux/slab.h>
97 #include <linux/module.h>
98 #include <linux/wait.h>
99 #include <linux/mutex.h>
100 
101 #include <linux/usb.h>
102 #include <linux/fs.h>
103 
104 /*
105  * Version Information
106  */
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"
110 
111 /* Vendor and Product Information */
112 #define MDC800_VENDOR_ID 	0x055f
113 #define MDC800_PRODUCT_ID	0xa800
114 
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
122 
123 /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
124 #define MDC800_DEVICE_MINOR_BASE 32
125 
126 
127 /**************************************************************************
128 	Data and structs
129 ***************************************************************************/
130 
131 
132 typedef enum {
133 	NOT_CONNECTED, READY, WORKING, DOWNLOAD
134 } mdc800_state;
135 
136 
137 /* Data for the driver */
138 struct mdc800_data
139 {
140 	struct usb_device *	dev;			// Device Data
141 	mdc800_state 		state;
142 
143 	unsigned int		endpoint [4];
144 
145 	struct urb *		irq_urb;
146 	wait_queue_head_t	irq_wait;
147 	int			irq_woken;
148 	char*			irq_urb_buffer;
149 
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
153 
154 	struct urb *   		write_urb;
155 	char*			write_urb_buffer;
156 	wait_queue_head_t	write_wait;
157 	int			written;
158 
159 
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 ?
165 
166 
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
171 
172 	int			open;		// Camera device open ?
173 	struct mutex		io_lock;	// IO -lock
174 
175 	char 			in [8];		// Command Input Buffer
176 	int  			in_count;
177 
178 	int			pic_index;	// Cache for the Imagesize (-1 for nothing cached )
179 	int			pic_len;
180 	int			minor;
181 };
182 
183 
184 /* Specification of the Endpoints */
185 static struct usb_endpoint_descriptor mdc800_ed [4] =
186 {
187 	{
188 		.bLength = 		0,
189 		.bDescriptorType =	0,
190 		.bEndpointAddress =	0x01,
191 		.bmAttributes = 	0x02,
192 		.wMaxPacketSize =	cpu_to_le16(8),
193 		.bInterval = 		0,
194 		.bRefresh = 		0,
195 		.bSynchAddress = 	0,
196 	},
197 	{
198 		.bLength = 		0,
199 		.bDescriptorType = 	0,
200 		.bEndpointAddress = 	0x82,
201 		.bmAttributes = 	0x03,
202 		.wMaxPacketSize = 	cpu_to_le16(8),
203 		.bInterval = 		0,
204 		.bRefresh = 		0,
205 		.bSynchAddress = 	0,
206 	},
207 	{
208 		.bLength = 		0,
209 		.bDescriptorType = 	0,
210 		.bEndpointAddress = 	0x03,
211 		.bmAttributes = 	0x02,
212 		.wMaxPacketSize = 	cpu_to_le16(64),
213 		.bInterval = 		0,
214 		.bRefresh = 		0,
215 		.bSynchAddress = 	0,
216 	},
217 	{
218 		.bLength = 		0,
219 		.bDescriptorType = 	0,
220 		.bEndpointAddress = 	0x84,
221 		.bmAttributes = 	0x02,
222 		.wMaxPacketSize = 	cpu_to_le16(64),
223 		.bInterval = 		0,
224 		.bRefresh = 		0,
225 		.bSynchAddress = 	0,
226 	},
227 };
228 
229 /* The Variable used by the driver */
230 static struct mdc800_data* mdc800;
231 
232 
233 /***************************************************************************
234 	The USB Part of the driver
235 ****************************************************************************/
236 
237 static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
238 {
239 	return (
240 		   ( a->bEndpointAddress == b->bEndpointAddress )
241 		&& ( a->bmAttributes     == b->bmAttributes     )
242 		&& ( a->wMaxPacketSize   == b->wMaxPacketSize   )
243 	);
244 }
245 
246 
247 /*
248  * Checks whether the camera responds busy
249  */
250 static int mdc800_isBusy (char* ch)
251 {
252 	int i=0;
253 	while (i<8)
254 	{
255 		if (ch [i] != (char)0x99)
256 			return 0;
257 		i++;
258 	}
259 	return 1;
260 }
261 
262 
263 /*
264  * Checks whether the Camera is ready
265  */
266 static int mdc800_isReady (char *ch)
267 {
268 	int i=0;
269 	while (i<8)
270 	{
271 		if (ch [i] != (char)0xbb)
272 			return 0;
273 		i++;
274 	}
275 	return 1;
276 }
277 
278 
279 
280 /*
281  * USB IRQ Handler for InputLine
282  */
283 static void mdc800_usb_irq (struct urb *urb)
284 {
285 	int data_received=0, wake_up;
286 	unsigned char* b=urb->transfer_buffer;
287 	struct mdc800_data* mdc800=urb->context;
288 	struct device *dev = &mdc800->dev->dev;
289 	int status = urb->status;
290 
291 	if (status >= 0) {
292 		if (mdc800_isBusy (b))
293 		{
294 			if (!mdc800->camera_busy)
295 			{
296 				mdc800->camera_busy=1;
297 				dev_dbg(dev, "gets busy\n");
298 			}
299 		}
300 		else
301 		{
302 			if (mdc800->camera_busy && mdc800_isReady (b))
303 			{
304 				mdc800->camera_busy=0;
305 				dev_dbg(dev, "gets ready\n");
306 			}
307 		}
308 		if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
309 		{
310 			/* Store Data in camera_answer field */
311 			dev_dbg(dev, "%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]);
312 
313 			memcpy (mdc800->camera_response,b,8);
314 			data_received=1;
315 		}
316 	}
317 	wake_up= ( mdc800->camera_request_ready > 0 )
318 		&&
319 		(
320 			((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
321 		||
322 			((mdc800->camera_request_ready == 2) && data_received)
323 		||
324 			((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
325 		||
326 			(status < 0)
327 		);
328 
329 	if (wake_up)
330 	{
331 		mdc800->camera_request_ready=0;
332 		mdc800->irq_woken=1;
333 		wake_up (&mdc800->irq_wait);
334 	}
335 }
336 
337 
338 /*
339  * Waits a while until the irq responds that camera is ready
340  *
341  *  mode : 0: Wait for camera gets ready
342  *         1: Wait for receiving data
343  *         2: Wait for camera gets busy
344  *
345  * msec: Time to wait
346  */
347 static int mdc800_usb_waitForIRQ (int mode, int msec)
348 {
349 	mdc800->camera_request_ready=1+mode;
350 
351 	wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken,
352 			   msecs_to_jiffies(msec));
353 	mdc800->irq_woken = 0;
354 
355 	if (mdc800->camera_request_ready>0)
356 	{
357 		mdc800->camera_request_ready=0;
358 		dev_err(&mdc800->dev->dev, "timeout waiting for camera.\n");
359 		return -1;
360 	}
361 
362 	if (mdc800->state == NOT_CONNECTED)
363 	{
364 		printk(KERN_WARNING "mdc800: Camera gets disconnected "
365 		       "during waiting for irq.\n");
366 		mdc800->camera_request_ready=0;
367 		return -2;
368 	}
369 
370 	return 0;
371 }
372 
373 
374 /*
375  * The write_urb callback function
376  */
377 static void mdc800_usb_write_notify (struct urb *urb)
378 {
379 	struct mdc800_data* mdc800=urb->context;
380 	int status = urb->status;
381 
382 	if (status != 0)
383 		dev_err(&mdc800->dev->dev,
384 			"writing command fails (status=%i)\n", status);
385 	else
386 		mdc800->state=READY;
387 	mdc800->written = 1;
388 	wake_up (&mdc800->write_wait);
389 }
390 
391 
392 /*
393  * The download_urb callback function
394  */
395 static void mdc800_usb_download_notify (struct urb *urb)
396 {
397 	struct mdc800_data* mdc800=urb->context;
398 	int status = urb->status;
399 
400 	if (status == 0) {
401 		/* Fill output buffer with these data */
402 		memcpy (mdc800->out,  urb->transfer_buffer, 64);
403 		mdc800->out_count=64;
404 		mdc800->out_ptr=0;
405 		mdc800->download_left-=64;
406 		if (mdc800->download_left == 0)
407 		{
408 			mdc800->state=READY;
409 		}
410 	} else {
411 		dev_err(&mdc800->dev->dev,
412 			"request bytes fails (status:%i)\n", status);
413 	}
414 	mdc800->downloaded = 1;
415 	wake_up (&mdc800->download_wait);
416 }
417 
418 
419 /***************************************************************************
420 	Probing for the Camera
421  ***************************************************************************/
422 
423 static struct usb_driver mdc800_usb_driver;
424 static const struct file_operations mdc800_device_ops;
425 static struct usb_class_driver mdc800_class = {
426 	.name =		"mdc800%d",
427 	.fops =		&mdc800_device_ops,
428 	.minor_base =	MDC800_DEVICE_MINOR_BASE,
429 };
430 
431 
432 /*
433  * Callback to search the Mustek MDC800 on the USB Bus
434  */
435 static int mdc800_usb_probe (struct usb_interface *intf,
436 			       const struct usb_device_id *id)
437 {
438 	int i,j;
439 	struct usb_host_interface *intf_desc;
440 	struct usb_device *dev = interface_to_usbdev (intf);
441 	int irq_interval=0;
442 	int retval;
443 
444 	dev_dbg(&intf->dev, "(%s) called.\n", __func__);
445 
446 
447 	if (mdc800->dev != NULL)
448 	{
449 		dev_warn(&intf->dev, "only one Mustek MDC800 is supported.\n");
450 		return -ENODEV;
451 	}
452 
453 	if (dev->descriptor.bNumConfigurations != 1)
454 	{
455 		dev_err(&intf->dev,
456 			"probe fails -> wrong Number of Configuration\n");
457 		return -ENODEV;
458 	}
459 	intf_desc = intf->cur_altsetting;
460 
461 	if (
462 			( intf_desc->desc.bInterfaceClass != 0xff )
463 		||	( intf_desc->desc.bInterfaceSubClass != 0 )
464 		|| ( intf_desc->desc.bInterfaceProtocol != 0 )
465 		|| ( intf_desc->desc.bNumEndpoints != 4)
466 	)
467 	{
468 		dev_err(&intf->dev, "probe fails -> wrong Interface\n");
469 		return -ENODEV;
470 	}
471 
472 	/* Check the Endpoints */
473 	for (i=0; i<4; i++)
474 	{
475 		mdc800->endpoint[i]=-1;
476 		for (j=0; j<4; j++)
477 		{
478 			if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
479 			{
480 				mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
481 				if (i==1)
482 				{
483 					irq_interval=intf_desc->endpoint [j].desc.bInterval;
484 				}
485 			}
486 		}
487 		if (mdc800->endpoint[i] == -1)
488 		{
489 			dev_err(&intf->dev, "probe fails -> Wrong Endpoints.\n");
490 			return -ENODEV;
491 		}
492 	}
493 
494 
495 	dev_info(&intf->dev, "Found Mustek MDC800 on USB.\n");
496 
497 	mutex_lock(&mdc800->io_lock);
498 
499 	retval = usb_register_dev(intf, &mdc800_class);
500 	if (retval) {
501 		dev_err(&intf->dev, "Not able to get a minor for this device.\n");
502 		mutex_unlock(&mdc800->io_lock);
503 		return -ENODEV;
504 	}
505 
506 	mdc800->dev=dev;
507 	mdc800->open=0;
508 
509 	/* Setup URB Structs */
510 	usb_fill_int_urb (
511 		mdc800->irq_urb,
512 		mdc800->dev,
513 		usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
514 		mdc800->irq_urb_buffer,
515 		8,
516 		mdc800_usb_irq,
517 		mdc800,
518 		irq_interval
519 	);
520 
521 	usb_fill_bulk_urb (
522 		mdc800->write_urb,
523 		mdc800->dev,
524 		usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
525 		mdc800->write_urb_buffer,
526 		8,
527 		mdc800_usb_write_notify,
528 		mdc800
529 	);
530 
531 	usb_fill_bulk_urb (
532 		mdc800->download_urb,
533 		mdc800->dev,
534 		usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
535 		mdc800->download_urb_buffer,
536 		64,
537 		mdc800_usb_download_notify,
538 		mdc800
539 	);
540 
541 	mdc800->state=READY;
542 
543 	mutex_unlock(&mdc800->io_lock);
544 
545 	usb_set_intfdata(intf, mdc800);
546 	return 0;
547 }
548 
549 
550 /*
551  * Disconnect USB device (maybe the MDC800)
552  */
553 static void mdc800_usb_disconnect (struct usb_interface *intf)
554 {
555 	struct mdc800_data* mdc800 = usb_get_intfdata(intf);
556 
557 	dev_dbg(&intf->dev, "(%s) called\n", __func__);
558 
559 	if (mdc800) {
560 		if (mdc800->state == NOT_CONNECTED)
561 			return;
562 
563 		usb_deregister_dev(intf, &mdc800_class);
564 
565 		/* must be under lock to make sure no URB
566 		   is submitted after usb_kill_urb() */
567 		mutex_lock(&mdc800->io_lock);
568 		mdc800->state=NOT_CONNECTED;
569 
570 		usb_kill_urb(mdc800->irq_urb);
571 		usb_kill_urb(mdc800->write_urb);
572 		usb_kill_urb(mdc800->download_urb);
573 		mutex_unlock(&mdc800->io_lock);
574 
575 		mdc800->dev = NULL;
576 		usb_set_intfdata(intf, NULL);
577 	}
578 	dev_info(&intf->dev, "Mustek MDC800 disconnected from USB.\n");
579 }
580 
581 
582 /***************************************************************************
583 	The Misc device Part (file_operations)
584 ****************************************************************************/
585 
586 /*
587  * This Function calc the Answersize for a command.
588  */
589 static int mdc800_getAnswerSize (char command)
590 {
591 	switch ((unsigned char) command)
592 	{
593 		case 0x2a:
594 		case 0x49:
595 		case 0x51:
596 		case 0x0d:
597 		case 0x20:
598 		case 0x07:
599 		case 0x01:
600 		case 0x25:
601 		case 0x00:
602 			return 8;
603 
604 		case 0x05:
605 		case 0x3e:
606 			return mdc800->pic_len;
607 
608 		case 0x09:
609 			return 4096;
610 
611 		default:
612 			return 0;
613 	}
614 }
615 
616 
617 /*
618  * Init the device: (1) alloc mem (2) Increase MOD Count ..
619  */
620 static int mdc800_device_open (struct inode* inode, struct file *file)
621 {
622 	int retval=0;
623 	int errn=0;
624 
625 	mutex_lock(&mdc800->io_lock);
626 
627 	if (mdc800->state == NOT_CONNECTED)
628 	{
629 		errn=-EBUSY;
630 		goto error_out;
631 	}
632 	if (mdc800->open)
633 	{
634 		errn=-EBUSY;
635 		goto error_out;
636 	}
637 
638 	mdc800->in_count=0;
639 	mdc800->out_count=0;
640 	mdc800->out_ptr=0;
641 	mdc800->pic_index=0;
642 	mdc800->pic_len=-1;
643 	mdc800->download_left=0;
644 
645 	mdc800->camera_busy=0;
646 	mdc800->camera_request_ready=0;
647 
648 	retval=0;
649 	mdc800->irq_urb->dev = mdc800->dev;
650 	retval = usb_submit_urb (mdc800->irq_urb, GFP_KERNEL);
651 	if (retval) {
652 		dev_err(&mdc800->dev->dev,
653 			"request USB irq fails (submit_retval=%i).\n", retval);
654 		errn = -EIO;
655 		goto error_out;
656 	}
657 
658 	mdc800->open=1;
659 	dev_dbg(&mdc800->dev->dev, "Mustek MDC800 device opened.\n");
660 
661 error_out:
662 	mutex_unlock(&mdc800->io_lock);
663 	return errn;
664 }
665 
666 
667 /*
668  * Close the Camera and release Memory
669  */
670 static int mdc800_device_release (struct inode* inode, struct file *file)
671 {
672 	int retval=0;
673 
674 	mutex_lock(&mdc800->io_lock);
675 	if (mdc800->open && (mdc800->state != NOT_CONNECTED))
676 	{
677 		usb_kill_urb(mdc800->irq_urb);
678 		usb_kill_urb(mdc800->write_urb);
679 		usb_kill_urb(mdc800->download_urb);
680 		mdc800->open=0;
681 	}
682 	else
683 	{
684 		retval=-EIO;
685 	}
686 
687 	mutex_unlock(&mdc800->io_lock);
688 	return retval;
689 }
690 
691 
692 /*
693  * The Device read callback Function
694  */
695 static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
696 {
697 	size_t left=len, sts=len; /* single transfer size */
698 	char __user *ptr = buf;
699 	int retval;
700 
701 	mutex_lock(&mdc800->io_lock);
702 	if (mdc800->state == NOT_CONNECTED)
703 	{
704 		mutex_unlock(&mdc800->io_lock);
705 		return -EBUSY;
706 	}
707 	if (mdc800->state == WORKING)
708 	{
709 		printk(KERN_WARNING "mdc800: Illegal State \"working\""
710 		       "reached during read ?!\n");
711 		mutex_unlock(&mdc800->io_lock);
712 		return -EBUSY;
713 	}
714 	if (!mdc800->open)
715 	{
716 		mutex_unlock(&mdc800->io_lock);
717 		return -EBUSY;
718 	}
719 
720 	while (left)
721 	{
722 		if (signal_pending (current))
723 		{
724 			mutex_unlock(&mdc800->io_lock);
725 			return -EINTR;
726 		}
727 
728 		sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
729 
730 		if (sts <= 0)
731 		{
732 			/* Too less Data in buffer */
733 			if (mdc800->state == DOWNLOAD)
734 			{
735 				mdc800->out_count=0;
736 				mdc800->out_ptr=0;
737 
738 				/* Download -> Request new bytes */
739 				mdc800->download_urb->dev = mdc800->dev;
740 				retval = usb_submit_urb (mdc800->download_urb, GFP_KERNEL);
741 				if (retval) {
742 					dev_err(&mdc800->dev->dev,
743 						"Can't submit download urb "
744 						"(retval=%i)\n", retval);
745 					mutex_unlock(&mdc800->io_lock);
746 					return len-left;
747 				}
748 				wait_event_timeout(mdc800->download_wait,
749 				     mdc800->downloaded,
750 				     msecs_to_jiffies(TO_DOWNLOAD_GET_READY));
751 				mdc800->downloaded = 0;
752 				if (mdc800->download_urb->status != 0)
753 				{
754 					dev_err(&mdc800->dev->dev,
755 						"request download-bytes fails "
756 						"(status=%i)\n",
757 						mdc800->download_urb->status);
758 					mutex_unlock(&mdc800->io_lock);
759 					return len-left;
760 				}
761 			}
762 			else
763 			{
764 				/* No more bytes -> that's an error*/
765 				mutex_unlock(&mdc800->io_lock);
766 				return -EIO;
767 			}
768 		}
769 		else
770 		{
771 			/* Copy Bytes */
772 			if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
773 						sts)) {
774 				mutex_unlock(&mdc800->io_lock);
775 				return -EFAULT;
776 			}
777 			ptr+=sts;
778 			left-=sts;
779 			mdc800->out_ptr+=sts;
780 		}
781 	}
782 
783 	mutex_unlock(&mdc800->io_lock);
784 	return len-left;
785 }
786 
787 
788 /*
789  * The Device write callback Function
790  * If a 8Byte Command is received, it will be send to the camera.
791  * After this the driver initiates the request for the answer or
792  * just waits until the camera becomes ready.
793  */
794 static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
795 {
796 	size_t i=0;
797 	int retval;
798 
799 	mutex_lock(&mdc800->io_lock);
800 	if (mdc800->state != READY)
801 	{
802 		mutex_unlock(&mdc800->io_lock);
803 		return -EBUSY;
804 	}
805 	if (!mdc800->open )
806 	{
807 		mutex_unlock(&mdc800->io_lock);
808 		return -EBUSY;
809 	}
810 
811 	while (i<len)
812 	{
813 		unsigned char c;
814 		if (signal_pending (current))
815 		{
816 			mutex_unlock(&mdc800->io_lock);
817 			return -EINTR;
818 		}
819 
820 		if(get_user(c, buf+i))
821 		{
822 			mutex_unlock(&mdc800->io_lock);
823 			return -EFAULT;
824 		}
825 
826 		/* check for command start */
827 		if (c == 0x55)
828 		{
829 			mdc800->in_count=0;
830 			mdc800->out_count=0;
831 			mdc800->out_ptr=0;
832 			mdc800->download_left=0;
833 		}
834 
835 		/* save command byte */
836 		if (mdc800->in_count < 8)
837 		{
838 			mdc800->in[mdc800->in_count] = c;
839 			mdc800->in_count++;
840 		}
841 		else
842 		{
843 			mutex_unlock(&mdc800->io_lock);
844 			return -EIO;
845 		}
846 
847 		/* Command Buffer full ? -> send it to camera */
848 		if (mdc800->in_count == 8)
849 		{
850 			int answersize;
851 
852 			if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
853 			{
854 				dev_err(&mdc800->dev->dev,
855 					"Camera didn't get ready.\n");
856 				mutex_unlock(&mdc800->io_lock);
857 				return -EIO;
858 			}
859 
860 			answersize=mdc800_getAnswerSize (mdc800->in[1]);
861 
862 			mdc800->state=WORKING;
863 			memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
864 			mdc800->write_urb->dev = mdc800->dev;
865 			retval = usb_submit_urb (mdc800->write_urb, GFP_KERNEL);
866 			if (retval) {
867 				dev_err(&mdc800->dev->dev,
868 					"submitting write urb fails "
869 					"(retval=%i)\n", retval);
870 				mutex_unlock(&mdc800->io_lock);
871 				return -EIO;
872 			}
873 			wait_event_timeout(mdc800->write_wait, mdc800->written,
874 					msecs_to_jiffies(TO_WRITE_GET_READY));
875 			mdc800->written = 0;
876 			if (mdc800->state == WORKING)
877 			{
878 				usb_kill_urb(mdc800->write_urb);
879 				mutex_unlock(&mdc800->io_lock);
880 				return -EIO;
881 			}
882 
883 			switch ((unsigned char) mdc800->in[1])
884 			{
885 				case 0x05: /* Download Image */
886 				case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
887 					if (mdc800->pic_len < 0)
888 					{
889 						dev_err(&mdc800->dev->dev,
890 							"call 0x07 before "
891 							"0x05,0x3e\n");
892 						mdc800->state=READY;
893 						mutex_unlock(&mdc800->io_lock);
894 						return -EIO;
895 					}
896 					mdc800->pic_len=-1;
897 					/* fall through */
898 
899 				case 0x09: /* Download Thumbnail */
900 					mdc800->download_left=answersize+64;
901 					mdc800->state=DOWNLOAD;
902 					mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
903 					break;
904 
905 
906 				default:
907 					if (answersize)
908 					{
909 
910 						if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
911 						{
912 							dev_err(&mdc800->dev->dev, "requesting answer from irq fails\n");
913 							mutex_unlock(&mdc800->io_lock);
914 							return -EIO;
915 						}
916 
917 						/* Write dummy data, (this is ugly but part of the USB Protocol */
918 						/* if you use endpoint 1 as bulk and not as irq) */
919 						memcpy (mdc800->out, mdc800->camera_response,8);
920 
921 						/* This is the interpreted answer */
922 						memcpy (&mdc800->out[8], mdc800->camera_response,8);
923 
924 						mdc800->out_ptr=0;
925 						mdc800->out_count=16;
926 
927 						/* Cache the Imagesize, if command was getImageSize */
928 						if (mdc800->in [1] == (char) 0x07)
929 						{
930 							mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
931 
932 							dev_dbg(&mdc800->dev->dev, "cached imagesize = %i\n", mdc800->pic_len);
933 						}
934 
935 					}
936 					else
937 					{
938 						if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
939 						{
940 							dev_err(&mdc800->dev->dev, "Command Timeout.\n");
941 							mutex_unlock(&mdc800->io_lock);
942 							return -EIO;
943 						}
944 					}
945 					mdc800->state=READY;
946 					break;
947 			}
948 		}
949 		i++;
950 	}
951 	mutex_unlock(&mdc800->io_lock);
952 	return i;
953 }
954 
955 
956 /***************************************************************************
957 	Init and Cleanup this driver (Structs and types)
958 ****************************************************************************/
959 
960 /* File Operations of this drivers */
961 static const struct file_operations mdc800_device_ops =
962 {
963 	.owner =	THIS_MODULE,
964 	.read =		mdc800_device_read,
965 	.write =	mdc800_device_write,
966 	.open =		mdc800_device_open,
967 	.release =	mdc800_device_release,
968 	.llseek =	noop_llseek,
969 };
970 
971 
972 
973 static const struct usb_device_id mdc800_table[] = {
974 	{ USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
975 	{ }						/* Terminating entry */
976 };
977 
978 MODULE_DEVICE_TABLE (usb, mdc800_table);
979 /*
980  * USB Driver Struct for this device
981  */
982 static struct usb_driver mdc800_usb_driver =
983 {
984 	.name =		"mdc800",
985 	.probe =	mdc800_usb_probe,
986 	.disconnect =	mdc800_usb_disconnect,
987 	.id_table =	mdc800_table
988 };
989 
990 
991 
992 /************************************************************************
993 	Init and Cleanup this driver (Main Functions)
994 *************************************************************************/
995 
996 static int __init usb_mdc800_init (void)
997 {
998 	int retval = -ENODEV;
999 	/* Allocate Memory */
1000 	mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
1001 	if (!mdc800)
1002 		goto cleanup_on_fail;
1003 
1004 	mdc800->dev = NULL;
1005 	mdc800->state=NOT_CONNECTED;
1006 	mutex_init (&mdc800->io_lock);
1007 
1008 	init_waitqueue_head (&mdc800->irq_wait);
1009 	init_waitqueue_head (&mdc800->write_wait);
1010 	init_waitqueue_head (&mdc800->download_wait);
1011 
1012 	mdc800->irq_woken = 0;
1013 	mdc800->downloaded = 0;
1014 	mdc800->written = 0;
1015 
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;
1025 
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;
1035 
1036 	/* Register the driver */
1037 	retval = usb_register(&mdc800_usb_driver);
1038 	if (retval)
1039 		goto cleanup_on_fail;
1040 
1041 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1042 	       DRIVER_DESC "\n");
1043 
1044 	return 0;
1045 
1046 	/* Clean driver up, when something fails */
1047 
1048 cleanup_on_fail:
1049 
1050 	if (mdc800 != NULL)
1051 	{
1052 		printk(KERN_ERR "mdc800: can't alloc memory!\n");
1053 
1054 		kfree(mdc800->download_urb_buffer);
1055 		kfree(mdc800->write_urb_buffer);
1056 		kfree(mdc800->irq_urb_buffer);
1057 
1058 		usb_free_urb(mdc800->write_urb);
1059 		usb_free_urb(mdc800->download_urb);
1060 		usb_free_urb(mdc800->irq_urb);
1061 
1062 		kfree (mdc800);
1063 	}
1064 	mdc800 = NULL;
1065 	return retval;
1066 }
1067 
1068 
1069 static void __exit usb_mdc800_cleanup (void)
1070 {
1071 	usb_deregister (&mdc800_usb_driver);
1072 
1073 	usb_free_urb (mdc800->irq_urb);
1074 	usb_free_urb (mdc800->download_urb);
1075 	usb_free_urb (mdc800->write_urb);
1076 
1077 	kfree (mdc800->irq_urb_buffer);
1078 	kfree (mdc800->write_urb_buffer);
1079 	kfree (mdc800->download_urb_buffer);
1080 
1081 	kfree (mdc800);
1082 	mdc800 = NULL;
1083 }
1084 
1085 module_init (usb_mdc800_init);
1086 module_exit (usb_mdc800_cleanup);
1087 
1088 MODULE_AUTHOR( DRIVER_AUTHOR );
1089 MODULE_DESCRIPTION( DRIVER_DESC );
1090 MODULE_LICENSE("GPL");
1091 
1092