aboutsummaryrefslogtreecommitdiff
path: root/drivers/usb/image
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 15:20:36 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 15:20:36 -0700
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/usb/image
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/usb/image')
-rw-r--r--drivers/usb/image/Kconfig30
-rw-r--r--drivers/usb/image/Makefile6
-rw-r--r--drivers/usb/image/mdc800.c1063
-rw-r--r--drivers/usb/image/microtek.c862
-rw-r--r--drivers/usb/image/microtek.h55
5 files changed, 2016 insertions, 0 deletions
diff --git a/drivers/usb/image/Kconfig b/drivers/usb/image/Kconfig
new file mode 100644
index 00000000000..95ce703110d
--- /dev/null
+++ b/drivers/usb/image/Kconfig
@@ -0,0 +1,30 @@
+#
+# USB Imageing devices configuration
+#
+comment "USB Imaging devices"
+ depends on USB
+
+config USB_MDC800
+ tristate "USB Mustek MDC800 Digital Camera support (EXPERIMENTAL)"
+ depends on USB && EXPERIMENTAL
+ ---help---
+ Say Y here if you want to connect this type of still camera to
+ your computer's USB port. This driver can be used with gphoto 0.4.3
+ and higher (look at <http://www.gphoto.org/>).
+ To use it create a device node with "mknod /dev/mustek c 180 32" and
+ configure it in your software.
+
+ To compile this driver as a module, choose M here: the
+ module will be called mdc800.
+
+config USB_MICROTEK
+ tristate "Microtek X6USB scanner support"
+ depends on USB && SCSI
+ help
+ Say Y here if you want support for the Microtek X6USB and
+ possibly the Phantom 336CX, Phantom C6 and ScanMaker V6U(S)L.
+ Support for anything but the X6 is experimental.
+ Please report failures and successes.
+ The scanner will appear as a scsi generic device to the rest
+ of the system. Scsi support is required.
+ This driver can be compiled as a module, called microtek.
diff --git a/drivers/usb/image/Makefile b/drivers/usb/image/Makefile
new file mode 100644
index 00000000000..4148ae30635
--- /dev/null
+++ b/drivers/usb/image/Makefile
@@ -0,0 +1,6 @@
+#
+# Makefile for USB Image drivers
+#
+
+obj-$(CONFIG_USB_MDC800) += mdc800.o
+obj-$(CONFIG_USB_MICROTEK) += microtek.o
diff --git a/drivers/usb/image/mdc800.c b/drivers/usb/image/mdc800.c
new file mode 100644
index 00000000000..5791723e608
--- /dev/null
+++ b/drivers/usb/image/mdc800.c
@@ -0,0 +1,1063 @@
+/*
+ * copyright (C) 1999/2000 by Henning Zabel <henning@uni-paderborn.de>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+
+/*
+ * USB-Kernel Driver for the Mustek MDC800 Digital Camera
+ * (c) 1999/2000 Henning Zabel <henning@uni-paderborn.de>
+ *
+ *
+ * The driver brings the USB functions of the MDC800 to Linux.
+ * To use the Camera you must support the USB Protocoll of the camera
+ * to the Kernel Node.
+ * The Driver uses a misc device Node. Create it with :
+ * mknod /dev/mustek c 180 32
+ *
+ * The driver supports only one camera.
+ *
+ * Fix: mdc800 used sleep_on and slept with io_lock held.
+ * Converted sleep_on to waitqueues with schedule_timeout and made io_lock
+ * a semaphore from a spinlock.
+ * by Oliver Neukum <oliver@neukum.name>
+ * (02/12/2001)
+ *
+ * Identify version on module load.
+ * (08/04/2001) gb
+ *
+ * version 0.7.5
+ * Fixed potential SMP races with Spinlocks.
+ * Thanks to Oliver Neukum <oliver@neukum.name> who
+ * noticed the race conditions.
+ * (30/10/2000)
+ *
+ * Fixed: Setting urb->dev before submitting urb.
+ * by Greg KH <greg@kroah.com>
+ * (13/10/2000)
+ *
+ * version 0.7.3
+ * bugfix : The mdc800->state field gets set to READY after the
+ * the diconnect function sets it to NOT_CONNECTED. This makes the
+ * driver running like the camera is connected and causes some
+ * hang ups.
+ *
+ * version 0.7.1
+ * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
+ * problems when compiled as Module.
+ * (04/04/2000)
+ *
+ * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
+ * was updated to use these values.
+ * (26/03/2000)
+ *
+ * The Init und Exit Module Function are updated.
+ * (01/03/2000)
+ *
+ * version 0.7.0
+ * Rewrite of the driver : The driver now uses URB's. The old stuff
+ * has been removed.
+ *
+ * version 0.6.0
+ * Rewrite of this driver: The Emulation of the rs232 protocoll
+ * has been removed from the driver. A special executeCommand function
+ * for this driver is included to gphoto.
+ * The driver supports two kind of communication to bulk endpoints.
+ * Either with the dev->bus->ops->bulk... or with callback function.
+ * (09/11/1999)
+ *
+ * version 0.5.0:
+ * first Version that gets a version number. Most of the needed
+ * functions work.
+ * (20/10/1999)
+ */
+
+#include <linux/sched.h>
+#include <linux/signal.h>
+#include <linux/spinlock.h>
+#include <linux/errno.h>
+#include <linux/random.h>
+#include <linux/poll.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/smp_lock.h>
+#include <linux/wait.h>
+
+#include <linux/usb.h>
+#include <linux/fs.h>
+
+/*
+ * Version Information
+ */
+#define DRIVER_VERSION "v0.7.5 (30/10/2000)"
+#define DRIVER_AUTHOR "Henning Zabel <henning@uni-paderborn.de>"
+#define DRIVER_DESC "USB Driver for Mustek MDC800 Digital Camera"
+
+/* Vendor and Product Information */
+#define MDC800_VENDOR_ID 0x055f
+#define MDC800_PRODUCT_ID 0xa800
+
+/* Timeouts (msec) */
+#define TO_DOWNLOAD_GET_READY 1500
+#define TO_DOWNLOAD_GET_BUSY 1500
+#define TO_WRITE_GET_READY 1000
+#define TO_DEFAULT_COMMAND 5000
+#define TO_READ_FROM_IRQ TO_DEFAULT_COMMAND
+#define TO_GET_READY TO_DEFAULT_COMMAND
+
+/* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
+#define MDC800_DEVICE_MINOR_BASE 32
+
+
+/**************************************************************************
+ Data and structs
+***************************************************************************/
+
+
+typedef enum {
+ NOT_CONNECTED, READY, WORKING, DOWNLOAD
+} mdc800_state;
+
+
+/* Data for the driver */
+struct mdc800_data
+{
+ struct usb_device * dev; // Device Data
+ mdc800_state state;
+
+ unsigned int endpoint [4];
+
+ struct urb * irq_urb;
+ wait_queue_head_t irq_wait;
+ int irq_woken;
+ char* irq_urb_buffer;
+
+ int camera_busy; // is camera busy ?
+ int camera_request_ready; // Status to synchronize with irq
+ char camera_response [8]; // last Bytes send after busy
+
+ struct urb * write_urb;
+ char* write_urb_buffer;
+ wait_queue_head_t write_wait;
+ int written;
+
+
+ struct urb * download_urb;
+ char* download_urb_buffer;
+ wait_queue_head_t download_wait;
+ int downloaded;
+ int download_left; // Bytes left to download ?
+
+
+ /* Device Data */
+ char out [64]; // Answer Buffer
+ int out_ptr; // Index to the first not readen byte
+ int out_count; // Bytes in the buffer
+
+ int open; // Camera device open ?
+ struct semaphore io_lock; // IO -lock
+
+ char in [8]; // Command Input Buffer
+ int in_count;
+
+ int pic_index; // Cache for the Imagesize (-1 for nothing cached )
+ int pic_len;
+ int minor;
+};
+
+
+/* Specification of the Endpoints */
+static struct usb_endpoint_descriptor mdc800_ed [4] =
+{
+ {
+ .bLength = 0,
+ .bDescriptorType = 0,
+ .bEndpointAddress = 0x01,
+ .bmAttributes = 0x02,
+ .wMaxPacketSize = __constant_cpu_to_le16(8),
+ .bInterval = 0,
+ .bRefresh = 0,
+ .bSynchAddress = 0,
+ },
+ {
+ .bLength = 0,
+ .bDescriptorType = 0,
+ .bEndpointAddress = 0x82,
+ .bmAttributes = 0x03,
+ .wMaxPacketSize = __constant_cpu_to_le16(8),
+ .bInterval = 0,
+ .bRefresh = 0,
+ .bSynchAddress = 0,
+ },
+ {
+ .bLength = 0,
+ .bDescriptorType = 0,
+ .bEndpointAddress = 0x03,
+ .bmAttributes = 0x02,
+ .wMaxPacketSize = __constant_cpu_to_le16(64),
+ .bInterval = 0,
+ .bRefresh = 0,
+ .bSynchAddress = 0,
+ },
+ {
+ .bLength = 0,
+ .bDescriptorType = 0,
+ .bEndpointAddress = 0x84,
+ .bmAttributes = 0x02,
+ .wMaxPacketSize = __constant_cpu_to_le16(64),
+ .bInterval = 0,
+ .bRefresh = 0,
+ .bSynchAddress = 0,
+ },
+};
+
+/* The Variable used by the driver */
+static struct mdc800_data* mdc800;
+
+
+/***************************************************************************
+ The USB Part of the driver
+****************************************************************************/
+
+static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
+{
+ return (
+ ( a->bEndpointAddress == b->bEndpointAddress )
+ && ( a->bmAttributes == b->bmAttributes )
+ && ( a->wMaxPacketSize == b->wMaxPacketSize )
+ );
+}
+
+
+/*
+ * Checks whether the camera responds busy
+ */
+static int mdc800_isBusy (char* ch)
+{
+ int i=0;
+ while (i<8)
+ {
+ if (ch [i] != (char)0x99)
+ return 0;
+ i++;
+ }
+ return 1;
+}
+
+
+/*
+ * Checks whether the Camera is ready
+ */
+static int mdc800_isReady (char *ch)
+{
+ int i=0;
+ while (i<8)
+ {
+ if (ch [i] != (char)0xbb)
+ return 0;
+ i++;
+ }
+ return 1;
+}
+
+
+
+/*
+ * USB IRQ Handler for InputLine
+ */
+static void mdc800_usb_irq (struct urb *urb, struct pt_regs *res)
+{
+ int data_received=0, wake_up;
+ unsigned char* b=urb->transfer_buffer;
+ struct mdc800_data* mdc800=urb->context;
+
+ if (urb->status >= 0)
+ {
+
+ //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]);
+
+ if (mdc800_isBusy (b))
+ {
+ if (!mdc800->camera_busy)
+ {
+ mdc800->camera_busy=1;
+ dbg ("gets busy");
+ }
+ }
+ else
+ {
+ if (mdc800->camera_busy && mdc800_isReady (b))
+ {
+ mdc800->camera_busy=0;
+ dbg ("gets ready");
+ }
+ }
+ if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
+ {
+ /* Store Data in camera_answer field */
+ 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]);
+
+ memcpy (mdc800->camera_response,b,8);
+ data_received=1;
+ }
+ }
+ wake_up= ( mdc800->camera_request_ready > 0 )
+ &&
+ (
+ ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
+ ||
+ ((mdc800->camera_request_ready == 2) && data_received)
+ ||
+ ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
+ ||
+ (urb->status < 0)
+ );
+
+ if (wake_up)
+ {
+ mdc800->camera_request_ready=0;
+ mdc800->irq_woken=1;
+ wake_up (&mdc800->irq_wait);
+ }
+}
+
+
+/*
+ * Waits a while until the irq responds that camera is ready
+ *
+ * mode : 0: Wait for camera gets ready
+ * 1: Wait for receiving data
+ * 2: Wait for camera gets busy
+ *
+ * msec: Time to wait
+ */
+static int mdc800_usb_waitForIRQ (int mode, int msec)
+{
+ mdc800->camera_request_ready=1+mode;
+
+ wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000);
+ mdc800->irq_woken = 0;
+
+ if (mdc800->camera_request_ready>0)
+ {
+ mdc800->camera_request_ready=0;
+ err ("timeout waiting for camera.");
+ return -1;
+ }
+
+ if (mdc800->state == NOT_CONNECTED)
+ {
+ warn ("Camera gets disconnected during waiting for irq.");
+ mdc800->camera_request_ready=0;
+ return -2;
+ }
+
+ return 0;
+}
+
+
+/*
+ * The write_urb callback function
+ */
+static void mdc800_usb_write_notify (struct urb *urb, struct pt_regs *res)
+{
+ struct mdc800_data* mdc800=urb->context;
+
+ if (urb->status != 0)
+ {
+ err ("writing command fails (status=%i)", urb->status);
+ }
+ else
+ {
+ mdc800->state=READY;
+ }
+ mdc800->written = 1;
+ wake_up (&mdc800->write_wait);
+}
+
+
+/*
+ * The download_urb callback function
+ */
+static void mdc800_usb_download_notify (struct urb *urb, struct pt_regs *res)
+{
+ struct mdc800_data* mdc800=urb->context;
+
+ if (urb->status == 0)
+ {
+ /* Fill output buffer with these data */
+ memcpy (mdc800->out, urb->transfer_buffer, 64);
+ mdc800->out_count=64;
+ mdc800->out_ptr=0;
+ mdc800->download_left-=64;
+ if (mdc800->download_left == 0)
+ {
+ mdc800->state=READY;
+ }
+ }
+ else
+ {
+ err ("request bytes fails (status:%i)", urb->status);
+ }
+ mdc800->downloaded = 1;
+ wake_up (&mdc800->download_wait);
+}
+
+
+/***************************************************************************
+ Probing for the Camera
+ ***************************************************************************/
+
+static struct usb_driver mdc800_usb_driver;
+static struct file_operations mdc800_device_ops;
+static struct usb_class_driver mdc800_class = {
+ .name = "usb/mdc800%d",
+ .fops = &mdc800_device_ops,
+ .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
+ .minor_base = MDC800_DEVICE_MINOR_BASE,
+};
+
+
+/*
+ * Callback to search the Mustek MDC800 on the USB Bus
+ */
+static int mdc800_usb_probe (struct usb_interface *intf,
+ const struct usb_device_id *id)
+{
+ int i,j;
+ struct usb_host_interface *intf_desc;
+ struct usb_device *dev = interface_to_usbdev (intf);
+ int irq_interval=0;
+ int retval;
+
+ dbg ("(mdc800_usb_probe) called.");
+
+
+ if (mdc800->dev != NULL)
+ {
+ warn ("only one Mustek MDC800 is supported.");
+ return -ENODEV;
+ }
+
+ if (dev->descriptor.bNumConfigurations != 1)
+ {
+ err ("probe fails -> wrong Number of Configuration");
+ return -ENODEV;
+ }
+ intf_desc = intf->cur_altsetting;
+
+ if (
+ ( intf_desc->desc.bInterfaceClass != 0xff )
+ || ( intf_desc->desc.bInterfaceSubClass != 0 )
+ || ( intf_desc->desc.bInterfaceProtocol != 0 )
+ || ( intf_desc->desc.bNumEndpoints != 4)
+ )
+ {
+ err ("probe fails -> wrong Interface");
+ return -ENODEV;
+ }
+
+ /* Check the Endpoints */
+ for (i=0; i<4; i++)
+ {
+ mdc800->endpoint[i]=-1;
+ for (j=0; j<4; j++)
+ {
+ if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
+ {
+ mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
+ if (i==1)
+ {
+ irq_interval=intf_desc->endpoint [j].desc.bInterval;
+ }
+
+ continue;
+ }
+ }
+ if (mdc800->endpoint[i] == -1)
+ {
+ err ("probe fails -> Wrong Endpoints.");
+ return -ENODEV;
+ }
+ }
+
+
+ info ("Found Mustek MDC800 on USB.");
+
+ down (&mdc800->io_lock);
+
+ retval = usb_register_dev(intf, &mdc800_class);
+ if (retval) {
+ err ("Not able to get a minor for this device.");
+ return -ENODEV;
+ }
+
+ mdc800->dev=dev;
+ mdc800->open=0;
+
+ /* Setup URB Structs */
+ usb_fill_int_urb (
+ mdc800->irq_urb,
+ mdc800->dev,
+ usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
+ mdc800->irq_urb_buffer,
+ 8,
+ mdc800_usb_irq,
+ mdc800,
+ irq_interval
+ );
+
+ usb_fill_bulk_urb (
+ mdc800->write_urb,
+ mdc800->dev,
+ usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
+ mdc800->write_urb_buffer,
+ 8,
+ mdc800_usb_write_notify,
+ mdc800
+ );
+
+ usb_fill_bulk_urb (
+ mdc800->download_urb,
+ mdc800->dev,
+ usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
+ mdc800->download_urb_buffer,
+ 64,
+ mdc800_usb_download_notify,
+ mdc800
+ );
+
+ mdc800->state=READY;
+
+ up (&mdc800->io_lock);
+
+ usb_set_intfdata(intf, mdc800);
+ return 0;
+}
+
+
+/*
+ * Disconnect USB device (maybe the MDC800)
+ */
+static void mdc800_usb_disconnect (struct usb_interface *intf)
+{
+ struct mdc800_data* mdc800 = usb_get_intfdata(intf);
+
+ dbg ("(mdc800_usb_disconnect) called");
+
+ if (mdc800) {
+ if (mdc800->state == NOT_CONNECTED)
+ return;
+
+ usb_deregister_dev(intf, &mdc800_class);
+
+ mdc800->state=NOT_CONNECTED;
+
+ usb_kill_urb(mdc800->irq_urb);
+ usb_kill_urb(mdc800->write_urb);
+ usb_kill_urb(mdc800->download_urb);
+
+ mdc800->dev = NULL;
+ usb_set_intfdata(intf, NULL);
+ }
+ info ("Mustek MDC800 disconnected from USB.");
+}
+
+
+/***************************************************************************
+ The Misc device Part (file_operations)
+****************************************************************************/
+
+/*
+ * This Function calc the Answersize for a command.
+ */
+static int mdc800_getAnswerSize (char command)
+{
+ switch ((unsigned char) command)
+ {
+ case 0x2a:
+ case 0x49:
+ case 0x51:
+ case 0x0d:
+ case 0x20:
+ case 0x07:
+ case 0x01:
+ case 0x25:
+ case 0x00:
+ return 8;
+
+ case 0x05:
+ case 0x3e:
+ return mdc800->pic_len;
+
+ case 0x09:
+ return 4096;
+
+ default:
+ return 0;
+ }
+}
+
+
+/*
+ * Init the device: (1) alloc mem (2) Increase MOD Count ..
+ */
+static int mdc800_device_open (struct inode* inode, struct file *file)
+{
+ int retval=0;
+ int errn=0;
+
+ down (&mdc800->io_lock);
+
+ if (mdc800->state == NOT_CONNECTED)
+ {
+ errn=-EBUSY;
+ goto error_out;
+ }
+ if (mdc800->open)
+ {
+ errn=-EBUSY;
+ goto error_out;
+ }
+
+ mdc800->in_count=0;
+ mdc800->out_count=0;
+ mdc800->out_ptr=0;
+ mdc800->pic_index=0;
+ mdc800->pic_len=-1;
+ mdc800->download_left=0;
+
+ mdc800->camera_busy=0;
+ mdc800->camera_request_ready=0;
+
+ retval=0;
+ mdc800->irq_urb->dev = mdc800->dev;
+ if (usb_submit_urb (mdc800->irq_urb, GFP_KERNEL))
+ {
+ err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval, mdc800->irq_urb->status);
+ errn = -EIO;
+ goto error_out;
+ }
+
+ mdc800->open=1;
+ dbg ("Mustek MDC800 device opened.");
+
+error_out:
+ up (&mdc800->io_lock);
+ return errn;
+}
+
+
+/*
+ * Close the Camera and release Memory
+ */
+static int mdc800_device_release (struct inode* inode, struct file *file)
+{
+ int retval=0;
+ dbg ("Mustek MDC800 device closed.");
+
+ down (&mdc800->io_lock);
+ if (mdc800->open && (mdc800->state != NOT_CONNECTED))
+ {
+ usb_kill_urb(mdc800->irq_urb);
+ usb_kill_urb(mdc800->write_urb);
+ usb_kill_urb(mdc800->download_urb);
+ mdc800->open=0;
+ }
+ else
+ {
+ retval=-EIO;
+ }
+
+ up(&mdc800->io_lock);
+ return retval;
+}
+
+
+/*
+ * The Device read callback Function
+ */
+static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
+{
+ size_t left=len, sts=len; /* single transfer size */
+ char __user *ptr = buf;
+
+ down (&mdc800->io_lock);
+ if (mdc800->state == NOT_CONNECTED)
+ {
+ up (&mdc800->io_lock);
+ return -EBUSY;
+ }
+ if (mdc800->state == WORKING)
+ {
+ warn ("Illegal State \"working\" reached during read ?!");
+ up (&mdc800->io_lock);
+ return -EBUSY;
+ }
+ if (!mdc800->open)
+ {
+ up (&mdc800->io_lock);
+ return -EBUSY;
+ }
+
+ while (left)
+ {
+ if (signal_pending (current))
+ {
+ up (&mdc800->io_lock);
+ return -EINTR;
+ }
+
+ sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
+
+ if (sts <= 0)
+ {
+ /* Too less Data in buffer */
+ if (mdc800->state == DOWNLOAD)
+ {
+ mdc800->out_count=0;
+ mdc800->out_ptr=0;
+
+ /* Download -> Request new bytes */
+ mdc800->download_urb->dev = mdc800->dev;
+ if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
+ {
+ err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
+ up (&mdc800->io_lock);
+ return len-left;
+ }
+ wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
+ TO_DOWNLOAD_GET_READY*HZ/1000);
+ mdc800->downloaded = 0;
+ if (mdc800->download_urb->status != 0)
+ {
+ err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
+ up (&mdc800->io_lock);
+ return len-left;
+ }
+ }
+ else
+ {
+ /* No more bytes -> that's an error*/
+ up (&mdc800->io_lock);
+ return -EIO;
+ }
+ }
+ else
+ {
+ /* Copy Bytes */
+ if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
+ sts)) {
+ up(&mdc800->io_lock);
+ return -EFAULT;
+ }
+ ptr+=sts;
+ left-=sts;
+ mdc800->out_ptr+=sts;
+ }
+ }
+
+ up (&mdc800->io_lock);
+ return len-left;
+}
+
+
+/*
+ * The Device write callback Function
+ * If a 8Byte Command is received, it will be send to the camera.
+ * After this the driver initiates the request for the answer or
+ * just waits until the camera becomes ready.
+ */
+static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
+{
+ size_t i=0;
+
+ down (&mdc800->io_lock);
+ if (mdc800->state != READY)
+ {
+ up (&mdc800->io_lock);
+ return -EBUSY;
+ }
+ if (!mdc800->open )
+ {
+ up (&mdc800->io_lock);
+ return -EBUSY;
+ }
+
+ while (i<len)
+ {
+ unsigned char c;
+ if (signal_pending (current))
+ {
+ up (&mdc800->io_lock);
+ return -EINTR;
+ }
+
+ if(get_user(c, buf+i))
+ {
+ up(&mdc800->io_lock);
+ return -EFAULT;
+ }
+
+ /* check for command start */
+ if (c == 0x55)
+ {
+ mdc800->in_count=0;
+ mdc800->out_count=0;
+ mdc800->out_ptr=0;
+ mdc800->download_left=0;
+ }
+
+ /* save command byte */
+ if (mdc800->in_count < 8)
+ {
+ mdc800->in[mdc800->in_count] = c;
+ mdc800->in_count++;
+ }
+ else
+ {
+ up (&mdc800->io_lock);
+ return -EIO;
+ }
+
+ /* Command Buffer full ? -> send it to camera */
+ if (mdc800->in_count == 8)
+ {
+ int answersize;
+
+ if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
+ {
+ err ("Camera didn't get ready.\n");
+ up (&mdc800->io_lock);
+ return -EIO;
+ }
+
+ answersize=mdc800_getAnswerSize (mdc800->in[1]);
+
+ mdc800->state=WORKING;
+ memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
+ mdc800->write_urb->dev = mdc800->dev;
+ if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
+ {
+ err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
+ up (&mdc800->io_lock);
+ return -EIO;
+ }
+ wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
+ mdc800->written = 0;
+ if (mdc800->state == WORKING)
+ {
+ usb_kill_urb(mdc800->write_urb);
+ up (&mdc800->io_lock);
+ return -EIO;
+ }
+
+ switch ((unsigned char) mdc800->in[1])
+ {
+ case 0x05: /* Download Image */
+ case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
+ if (mdc800->pic_len < 0)
+ {
+ err ("call 0x07 before 0x05,0x3e");
+ mdc800->state=READY;
+ up (&mdc800->io_lock);
+ return -EIO;
+ }
+ mdc800->pic_len=-1;
+
+ case 0x09: /* Download Thumbnail */
+ mdc800->download_left=answersize+64;
+ mdc800->state=DOWNLOAD;
+ mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
+ break;
+
+
+ default:
+ if (answersize)
+ {
+
+ if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
+ {
+ err ("requesting answer from irq fails");
+ up (&mdc800->io_lock);
+ return -EIO;
+ }
+
+ /* Write dummy data, (this is ugly but part of the USB Protocol */
+ /* if you use endpoint 1 as bulk and not as irq) */
+ memcpy (mdc800->out, mdc800->camera_response,8);
+
+ /* This is the interpreted answer */
+ memcpy (&mdc800->out[8], mdc800->camera_response,8);
+
+ mdc800->out_ptr=0;
+ mdc800->out_count=16;
+
+ /* Cache the Imagesize, if command was getImageSize */
+ if (mdc800->in [1] == (char) 0x07)
+ {
+ mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
+
+ dbg ("cached imagesize = %i",mdc800->pic_len);
+ }
+
+ }
+ else
+ {
+ if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
+ {
+ err ("Command Timeout.");
+ up (&mdc800->io_lock);
+ return -EIO;
+ }
+ }
+ mdc800->state=READY;
+ break;
+ }
+ }
+ i++;
+ }
+ up (&mdc800->io_lock);
+ return i;
+}
+
+
+/***************************************************************************
+ Init and Cleanup this driver (Structs and types)
+****************************************************************************/
+
+/* File Operations of this drivers */
+static struct file_operations mdc800_device_ops =
+{
+ .owner = THIS_MODULE,
+ .read = mdc800_device_read,
+ .write = mdc800_device_write,
+ .open = mdc800_device_open,
+ .release = mdc800_device_release,
+};
+
+
+
+static struct usb_device_id mdc800_table [] = {
+ { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
+ { } /* Terminating entry */
+};
+
+MODULE_DEVICE_TABLE (usb, mdc800_table);
+/*
+ * USB Driver Struct for this device
+ */
+static struct usb_driver mdc800_usb_driver =
+{
+ .owner = THIS_MODULE,
+ .name = "mdc800",
+ .probe = mdc800_usb_probe,
+ .disconnect = mdc800_usb_disconnect,
+ .id_table = mdc800_table
+};
+
+
+
+/************************************************************************
+ Init and Cleanup this driver (Main Functions)
+*************************************************************************/
+
+#define try(A) if (!(A)) goto cleanup_on_fail;
+
+static int __init usb_mdc800_init (void)
+{
+ int retval = -ENODEV;
+ /* Allocate Memory */
+ try (mdc800=kmalloc (sizeof (struct mdc800_data), GFP_KERNEL));
+
+ memset(mdc800, 0, sizeof(struct mdc800_data));
+ mdc800->dev = NULL;
+ mdc800->open=0;
+ mdc800->state=NOT_CONNECTED;
+ init_MUTEX (&mdc800->io_lock);
+
+ init_waitqueue_head (&mdc800->irq_wait);
+ init_waitqueue_head (&mdc800->write_wait);
+ init_waitqueue_head (&mdc800->download_wait);
+
+ mdc800->irq_woken = 0;
+ mdc800->downloaded = 0;
+ mdc800->written = 0;
+
+ try (mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL));
+ try (mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL));
+ try (mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL));
+
+ try (mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL));
+ try (mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL));
+ try (mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL));
+
+ /* Register the driver */
+ retval = usb_register(&mdc800_usb_driver);
+ if (retval)
+ goto cleanup_on_fail;
+
+ info (DRIVER_VERSION ":" DRIVER_DESC);
+
+ return 0;
+
+ /* Clean driver up, when something fails */
+
+cleanup_on_fail:
+
+ if (mdc800 != NULL)
+ {
+ err ("can't alloc memory!");
+
+ kfree(mdc800->download_urb_buffer);
+ kfree(mdc800->write_urb_buffer);
+ kfree(mdc800->irq_urb_buffer);
+
+ usb_free_urb(mdc800->write_urb);
+ usb_free_urb(mdc800->download_urb);
+ usb_free_urb(mdc800->irq_urb);
+
+ kfree (mdc800);
+ }
+ mdc800 = NULL;
+ return retval;
+}
+
+
+static void __exit usb_mdc800_cleanup (void)
+{
+ usb_deregister (&mdc800_usb_driver);
+
+ usb_free_urb (mdc800->irq_urb);
+ usb_free_urb (mdc800->download_urb);
+ usb_free_urb (mdc800->write_urb);
+
+ kfree (mdc800->irq_urb_buffer);
+ kfree (mdc800->write_urb_buffer);
+ kfree (mdc800->download_urb_buffer);
+
+ kfree (mdc800);
+ mdc800 = NULL;
+}
+
+module_init (usb_mdc800_init);
+module_exit (usb_mdc800_cleanup);
+
+MODULE_AUTHOR( DRIVER_AUTHOR );
+MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
+
diff --git a/drivers/usb/image/microtek.c b/drivers/usb/image/microtek.c
new file mode 100644
index 00000000000..de0d32f6618
--- /dev/null
+++ b/drivers/usb/image/microtek.c
@@ -0,0 +1,862 @@
+/* Driver for Microtek Scanmaker X6 USB scanner, and possibly others.
+ *
+ * (C) Copyright 2000 John Fremlin <vii@penguinpowered.com>
+ * (C) Copyright 2000 Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de>
+ *
+ * Parts shamelessly stolen from usb-storage and copyright by their
+ * authors. Thanks to Matt Dharm for giving us permission!
+ *
+ * This driver implements a SCSI host controller driver and a USB
+ * device driver. To avoid confusion, all the USB related stuff is
+ * prefixed by mts_usb_ and all the SCSI stuff by mts_scsi_.
+ *
+ * Microtek (www.microtek.com) did not release the specifications for
+ * their USB protocol to us, so we had to reverse engineer them. We
+ * don't know for which models they are valid.
+ *
+ * The X6 USB has three bulk endpoints, one output (0x1) down which
+ * commands and outgoing data are sent, and two input: 0x82 from which
+ * normal data is read from the scanner (in packets of maximum 32
+ * bytes) and from which the status byte is read, and 0x83 from which
+ * the results of a scan (or preview) are read in up to 64 * 1024 byte
+ * chunks by the Windows driver. We don't know how much it is possible
+ * to read at a time from 0x83.
+ *
+ * It seems possible to read (with URB transfers) everything from 0x82
+ * in one go, without bothering to read in 32 byte chunks.
+ *
+ * There seems to be an optimisation of a further READ implicit if
+ * you simply read from 0x83.
+ *
+ * Guessed protocol:
+ *
+ * Send raw SCSI command to EP 0x1
+ *
+ * If there is data to receive:
+ * If the command was READ datatype=image:
+ * Read a lot of data from EP 0x83
+ * Else:
+ * Read data from EP 0x82
+ * Else:
+ * If there is data to transmit:
+ * Write it to EP 0x1
+ *
+ * Read status byte from EP 0x82
+ *
+ * References:
+ *
+ * The SCSI command set for the scanner is available from
+ * ftp://ftp.microtek.com/microtek/devpack/
+ *
+ * Microtek NV sent us a more up to date version of the document. If
+ * you want it, just send mail.
+ *
+ * Status:
+ *
+ * Untested with multiple scanners.
+ * Untested on SMP.
+ * Untested on a bigendian machine.
+ *
+ * History:
+ *
+ * 20000417 starting history
+ * 20000417 fixed load oops
+ * 20000417 fixed unload oops
+ * 20000419 fixed READ IMAGE detection
+ * 20000424 started conversion to use URBs
+ * 20000502 handled short transfers as errors
+ * 20000513 rename and organisation of functions (john)
+ * 20000513 added IDs for all products supported by Windows driver (john)
+ * 20000514 Rewrote mts_scsi_queuecommand to use URBs (john)
+ * 20000514 Version 0.0.8j
+ * 20000514 Fix reporting of non-existant devices to SCSI layer (john)
+ * 20000514 Added MTS_DEBUG_INT (john)
+ * 20000514 Changed "usb-microtek" to "microtek" for consistency (john)
+ * 20000514 Stupid bug fixes (john)
+ * 20000514 Version 0.0.9j
+ * 20000515 Put transfer context and URB in mts_desc (john)
+ * 20000515 Added prelim turn off debugging support (john)
+ * 20000515 Version 0.0.10j
+ * 20000515 Fixed up URB allocation (clear URB on alloc) (john)
+ * 20000515 Version 0.0.11j
+ * 20000516 Removed unnecessary spinlock in mts_transfer_context (john)
+ * 20000516 Removed unnecessary up on instance lock in mts_remove_nolock (john)
+ * 20000516 Implemented (badly) scsi_abort (john)
+ * 20000516 Version 0.0.12j
+ * 20000517 Hopefully removed mts_remove_nolock quasideadlock (john)
+ * 20000517 Added mts_debug_dump to print ll USB info (john)
+ * 20000518 Tweaks and documentation updates (john)
+ * 20000518 Version 0.0.13j
+ * 20000518 Cleaned up abort handling (john)
+ * 20000523 Removed scsi_command and various scsi_..._resets (john)
+ * 20000523 Added unlink URB on scsi_abort, now OHCI supports it (john)
+ * 20000523 Fixed last tiresome compile warning (john)
+ * 20000523 Version 0.0.14j (though version 0.1 has come out?)
+ * 20000602 Added primitive reset
+ * 20000602 Version 0.2.0
+ * 20000603 various cosmetic changes
+ * 20000603 Version 0.2.1
+ * 20000620 minor cosmetic changes
+ * 20000620 Version 0.2.2
+ * 20000822 Hopefully fixed deadlock in mts_remove_nolock()
+ * 20000822 Fixed minor race in mts_transfer_cleanup()
+ * 20000822 Fixed deadlock on submission error in queuecommand
+ * 20000822 Version 0.2.3
+ * 20000913 Reduced module size if debugging is off
+ * 20000913 Version 0.2.4
+ * 20010210 New abort logic
+ * 20010210 Version 0.3.0
+ * 20010217 Merged scatter/gather
+ * 20010218 Version 0.4.0
+ * 20010218 Cosmetic fixes
+ * 20010218 Version 0.4.1
+ * 20010306 Abort while using scatter/gather
+ * 20010306 Version 0.4.2
+ * 20010311 Remove all timeouts and tidy up generally (john)
+ * 20010320 check return value of scsi_register()
+ * 20010320 Version 0.4.3
+ * 20010408 Identify version on module load.
+ * 20011003 Fix multiple requests
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/sched.h>
+#include <linux/signal.h>
+#include <linux/errno.h>
+#include <linux/random.h>
+#include <linux/poll.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/smp_lock.h>
+#include <linux/usb.h>
+#include <linux/proc_fs.h>
+
+#include <asm/atomic.h>
+#include <linux/blkdev.h>
+#include "../../scsi/scsi.h"
+#include <scsi/scsi_host.h>
+
+#include "microtek.h"
+
+/*
+ * Version Information
+ */
+#define DRIVER_VERSION "v0.4.3"
+#define DRIVER_AUTHOR "John Fremlin <vii@penguinpowered.com>, Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de>"
+#define DRIVER_DESC "Microtek Scanmaker X6 USB scanner driver"
+
+/* Should we do debugging? */
+
+//#define MTS_DO_DEBUG
+
+/* USB layer driver interface */
+
+static int mts_usb_probe(struct usb_interface *intf,
+ const struct usb_device_id *id);
+static void mts_usb_disconnect(struct usb_interface *intf);
+
+static struct usb_device_id mts_usb_ids [];
+
+static struct usb_driver mts_usb_driver = {
+ .owner = THIS_MODULE,
+ .name = "microtekX6",
+ .probe = mts_usb_probe,
+ .disconnect = mts_usb_disconnect,
+ .id_table = mts_usb_ids,
+};
+
+
+/* Internal driver stuff */
+
+#define MTS_VERSION "0.4.3"
+#define MTS_NAME "microtek usb (rev " MTS_VERSION "): "
+
+#define MTS_WARNING(x...) \
+ printk( KERN_WARNING MTS_NAME x )
+#define MTS_ERROR(x...) \
+ printk( KERN_ERR MTS_NAME x )
+#define MTS_INT_ERROR(x...) \
+ MTS_ERROR(x)
+#define MTS_MESSAGE(x...) \
+ printk( KERN_INFO MTS_NAME x )
+
+#if defined MTS_DO_DEBUG
+
+#define MTS_DEBUG(x...) \
+ printk( KERN_DEBUG MTS_NAME x )
+
+#define MTS_DEBUG_GOT_HERE() \
+ MTS_DEBUG("got to %s:%d (%s)\n", __FILE__, (int)__LINE__, __PRETTY_FUNCTION__ )
+#define MTS_DEBUG_INT() \
+ do { MTS_DEBUG_GOT_HERE(); \
+ MTS_DEBUG("transfer = 0x%x context = 0x%x\n",(int)transfer,(int)context ); \
+ MTS_DEBUG("status = 0x%x data-length = 0x%x sent = 0x%x\n",(int)transfer->status,(int)context->data_length, (int)transfer->actual_length ); \
+ mts_debug_dump(context->instance);\
+ } while(0)
+#else
+
+#define MTS_NUL_STATEMENT do { } while(0)
+
+#define MTS_DEBUG(x...) MTS_NUL_STATEMENT
+#define MTS_DEBUG_GOT_HERE() MTS_NUL_STATEMENT
+#define MTS_DEBUG_INT() MTS_NUL_STATEMENT
+
+#endif
+
+
+
+#define MTS_INT_INIT()\
+ struct mts_transfer_context* context = (struct mts_transfer_context*)transfer->context; \
+ MTS_DEBUG_INT();\
+
+#ifdef MTS_DO_DEBUG
+
+static inline void mts_debug_dump(struct mts_desc* desc) {
+ MTS_DEBUG("desc at 0x%x: toggle = %02x%02x\n",
+ (int)desc,
+ (int)desc->usb_dev->toggle[1],(int)desc->usb_dev->toggle[0]
+ );
+ MTS_DEBUG("ep_out=%x ep_response=%x ep_image=%x\n",
+ usb_sndbulkpipe(desc->usb_dev,desc->ep_out),
+ usb_rcvbulkpipe(desc->usb_dev,desc->ep_response),
+ usb_rcvbulkpipe(desc->usb_dev,desc->ep_image)
+ );
+}
+
+
+static inline void mts_show_command(Scsi_Cmnd *srb)
+{
+ char *what = NULL;
+
+ switch (srb->cmnd[0]) {
+ case TEST_UNIT_READY: what = "TEST_UNIT_READY"; break;
+ case REZERO_UNIT: what = "REZERO_UNIT"; break;
+ case REQUEST_SENSE: what = "REQUEST_SENSE"; break;
+ case FORMAT_UNIT: what = "FORMAT_UNIT"; break;
+ case READ_BLOCK_LIMITS: what = "READ_BLOCK_LIMITS"; break;
+ case REASSIGN_BLOCKS: what = "REASSIGN_BLOCKS"; break;
+ case READ_6: what = "READ_6"; break;
+ case WRITE_6: what = "WRITE_6"; break;
+ case SEEK_6: what = "SEEK_6"; break;
+ case READ_REVERSE: what = "READ_REVERSE"; break;
+ case WRITE_FILEMARKS: what = "WRITE_FILEMARKS"; break;
+ case SPACE: what = "SPACE"; break;
+ case INQUIRY: what = "INQUIRY"; break;
+ case RECOVER_BUFFERED_DATA: what = "RECOVER_BUFFERED_DATA"; break;
+ case MODE_SELECT: what = "MODE_SELECT"; break;
+ case RESERVE: what = "RESERVE"; break;
+ case RELEASE: what = "RELEASE"; break;
+ case COPY: what = "COPY"; break;
+ case ERASE: what = "ERASE"; break;
+ case MODE_SENSE: what = "MODE_SENSE"; break;
+ case START_STOP: what = "START_STOP"; break;
+ case RECEIVE_DIAGNOSTIC: what = "RECEIVE_DIAGNOSTIC"; break;
+ case SEND_DIAGNOSTIC: what = "SEND_DIAGNOSTIC"; break;
+ case ALLOW_MEDIUM_REMOVAL: what = "ALLOW_MEDIUM_REMOVAL"; break;
+ case SET_WINDOW: what = "SET_WINDOW"; break;
+ case READ_CAPACITY: what = "READ_CAPACITY"; break;
+ case READ_10: what = "READ_10"; break;
+ case WRITE_10: what = "WRITE_10"; break;
+ case SEEK_10: what = "SEEK_10"; break;
+ case WRITE_VERIFY: what = "WRITE_VERIFY"; break;
+ case VERIFY: what = "VERIFY"; break;
+ case SEARCH_HIGH: what = "SEARCH_HIGH"; break;
+ case SEARCH_EQUAL: what = "SEARCH_EQUAL"; break;
+ case SEARCH_LOW: what = "SEARCH_LOW"; break;
+ case SET_LIMITS: what = "SET_LIMITS"; break;
+ case READ_POSITION: what = "READ_POSITION"; break;
+ case SYNCHRONIZE_CACHE: what = "SYNCHRONIZE_CACHE"; break;
+ case LOCK_UNLOCK_CACHE: what = "LOCK_UNLOCK_CACHE"; break;
+ case READ_DEFECT_DATA: what = "READ_DEFECT_DATA"; break;
+ case MEDIUM_SCAN: what = "MEDIUM_SCAN"; break;
+ case COMPARE: what = "COMPARE"; break;
+ case COPY_VERIFY: what = "COPY_VERIFY"; break;
+ case WRITE_BUFFER: what = "WRITE_BUFFER"; break;
+ case READ_BUFFER: what = "READ_BUFFER"; break;
+ case UPDATE_BLOCK: what = "UPDATE_BLOCK"; break;
+ case READ_LONG: what = "READ_LONG"; break;
+ case WRITE_LONG: what = "WRITE_LONG"; break;
+ case CHANGE_DEFINITION: what = "CHANGE_DEFINITION"; break;
+ case WRITE_SAME: what = "WRITE_SAME"; break;
+ case READ_TOC: what = "READ_TOC"; break;
+ case LOG_SELECT: what = "LOG_SELECT"; break;
+ case LOG_SENSE: what = "LOG_SENSE"; break;
+ case MODE_SELECT_10: what = "MODE_SELECT_10"; break;
+ case MODE_SENSE_10: what = "MODE_SENSE_10"; break;
+ case MOVE_MEDIUM: what = "MOVE_MEDIUM"; break;
+ case READ_12: what = "READ_12"; break;
+ case WRITE_12: what = "WRITE_12"; break;
+ case WRITE_VERIFY_12: what = "WRITE_VERIFY_12"; break;
+ case SEARCH_HIGH_12: what = "SEARCH_HIGH_12"; break;
+ case SEARCH_EQUAL_12: what = "SEARCH_EQUAL_12"; break;
+ case SEARCH_LOW_12: what = "SEARCH_LOW_12"; break;
+ case READ_ELEMENT_STATUS: what = "READ_ELEMENT_STATUS"; break;
+ case SEND_VOLUME_TAG: what = "SEND_VOLUME_TAG"; break;
+ case WRITE_LONG_2: what = "WRITE_LONG_2"; break;
+ default:
+ MTS_DEBUG("can't decode command\n");
+ goto out;
+ break;
+ }
+ MTS_DEBUG( "Command %s (%d bytes)\n", what, srb->cmd_len);
+
+ out:
+ MTS_DEBUG( " %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
+ srb->cmnd[0], srb->cmnd[1], srb->cmnd[2], srb->cmnd[3], srb->cmnd[4], srb->cmnd[5],
+ srb->cmnd[6], srb->cmnd[7], srb->cmnd[8], srb->cmnd[9]);
+}
+
+#else
+
+static inline void mts_show_command(Scsi_Cmnd * dummy)
+{
+}
+
+static inline void mts_debug_dump(struct mts_desc* dummy)
+{
+}
+
+#endif
+
+static inline void mts_urb_abort(struct mts_desc* desc) {
+ MTS_DEBUG_GOT_HERE();
+ mts_debug_dump(desc);
+
+ usb_kill_urb( desc->urb );
+}
+
+static int mts_scsi_abort (Scsi_Cmnd *srb)
+{
+ struct mts_desc* desc = (struct mts_desc*)(srb->device->host->hostdata[0]);
+
+ MTS_DEBUG_GOT_HERE();
+
+ mts_urb_abort(desc);
+
+ return SCSI_ABORT_PENDING;
+}
+
+static int mts_scsi_host_reset (Scsi_Cmnd *srb)
+{
+ struct mts_desc* desc = (struct mts_desc*)(srb->device->host->hostdata[0]);
+ int result, rc;
+
+ MTS_DEBUG_GOT_HERE();
+ mts_debug_dump(desc);
+
+ rc = usb_lock_device_for_reset(desc->usb_dev, desc->usb_intf);
+ if (rc < 0)
+ return FAILED;
+ result = usb_reset_device(desc->usb_dev);;
+ if (rc)
+ usb_unlock_device(desc->usb_dev);
+ return result ? FAILED : SUCCESS;
+}
+
+static
+int mts_scsi_queuecommand (Scsi_Cmnd *srb, mts_scsi_cmnd_callback callback );
+
+static void mts_transfer_cleanup( struct urb *transfer );
+static void mts_do_sg(struct urb * transfer, struct pt_regs *regs);
+
+
+inline static
+void mts_int_submit_urb (struct urb* transfer,
+ int pipe,
+ void* data,
+ unsigned length,
+ usb_complete_t callback )
+/* Interrupt context! */
+
+/* Holding transfer->context->lock! */
+{
+ int res;
+
+ MTS_INT_INIT();
+
+ usb_fill_bulk_urb(transfer,
+ context->instance->usb_dev,
+ pipe,
+ data,
+ length,
+ callback,
+ context
+ );
+
+ transfer->status = 0;
+
+ res = usb_submit_urb( transfer, GFP_ATOMIC );
+ if ( unlikely(res) ) {
+ MTS_INT_ERROR( "could not submit URB! Error was %d\n",(int)res );
+ context->srb->result = DID_ERROR << 16;
+ mts_transfer_cleanup(transfer);
+ }
+ return;
+}
+
+
+static void mts_transfer_cleanup( struct urb *transfer )
+/* Interrupt context! */
+{
+ MTS_INT_INIT();
+
+ if ( likely(context->final_callback != NULL) )
+ context->final_callback(context->srb);
+
+}
+
+static void mts_transfer_done( struct urb *transfer, struct pt_regs *regs )
+{
+ MTS_INT_INIT();
+
+ context->srb->result &= MTS_SCSI_ERR_MASK;
+ context->srb->result |= (unsigned)context->status<<1;
+
+ mts_transfer_cleanup(transfer);
+
+ return;
+}
+
+
+static void mts_get_status( struct urb *transfer )
+/* Interrupt context! */
+{
+ MTS_INT_INIT();
+
+ mts_int_submit_urb(transfer,
+ usb_rcvbulkpipe(context->instance->usb_dev,
+ context->instance->ep_response),
+ &context->status,
+ 1,
+ mts_transfer_done );
+}
+
+static void mts_data_done( struct urb* transfer, struct pt_regs *regs )
+/* Interrupt context! */
+{
+ MTS_INT_INIT();
+
+ if ( context->data_length != transfer->actual_length ) {
+ context->srb->resid = context->data_length - transfer->actual_length;
+ } else if ( unlikely(transfer->status) ) {
+ context->srb->result = (transfer->status == -ENOENT ? DID_ABORT : DID_ERROR)<<16;
+ }
+
+ mts_get_status(transfer);
+
+ return;
+}
+
+
+static void mts_command_done( struct urb *transfer, struct pt_regs *regs )
+/* Interrupt context! */
+{
+ MTS_INT_INIT();
+
+ if ( unlikely(transfer->status) ) {
+ if (transfer->status == -ENOENT) {
+ /* We are being killed */
+ MTS_DEBUG_GOT_HERE();
+ context->srb->result = DID_ABORT<<16;
+ } else {
+ /* A genuine error has occurred */
+ MTS_DEBUG_GOT_HERE();
+
+ context->srb->result = DID_ERROR<<16;
+ }
+ mts_transfer_cleanup(transfer);
+
+ return;
+ }
+
+ if (context->srb->cmnd[0] == REQUEST_SENSE) {
+ mts_int_submit_urb(transfer,
+ context->data_pipe,
+ context->srb->sense_buffer,
+ context->data_length,
+ mts_data_done);
+ } else { if ( context->data ) {
+ mts_int_submit_urb(transfer,
+ context->data_pipe,
+ context->data,
+ context->data_length,
+ context->srb->use_sg ? mts_do_sg : mts_data_done);
+ } else {
+ mts_get_status(transfer);
+ }
+ }
+
+ return;
+}
+
+static void mts_do_sg (struct urb* transfer, struct pt_regs *regs)
+{
+ struct scatterlist * sg;
+ MTS_INT_INIT();
+
+ MTS_DEBUG("Processing fragment %d of %d\n", context->fragment,context->srb->use_sg);
+
+ if (unlikely(transfer->status)) {
+ context->srb->result = (transfer->status == -ENOENT ? DID_ABORT : DID_ERROR)<<16;
+ mts_transfer_cleanup(transfer);
+ }
+
+ sg = context->srb->buffer;
+ context->fragment++;
+ mts_int_submit_urb(transfer,
+ context->data_pipe,
+ page_address(sg[context->fragment].page) +
+ sg[context->fragment].offset,
+ sg[context->fragment].length,
+ context->fragment + 1 == context->srb->use_sg ? mts_data_done : mts_do_sg);
+ return;
+}
+
+static const u8 mts_read_image_sig[] = { 0x28, 00, 00, 00 };
+static const u8 mts_read_image_sig_len = 4;
+static const unsigned char mts_direction[256/8] = {
+ 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
+ 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+
+#define MTS_DIRECTION_IS_IN(x) ((mts_direction[x>>3] >> (x & 7)) & 1)
+
+static void
+mts_build_transfer_context( Scsi_Cmnd *srb, struct mts_desc* desc )
+{
+ int pipe;
+ struct scatterlist * sg;
+
+ MTS_DEBUG_GOT_HERE();
+
+ desc->context.instance = desc;
+ desc->context.srb = srb;
+ desc->context.fragment = 0;
+
+ if (!srb->use_sg) {
+ if ( !srb->bufflen ){
+ desc->context.data = NULL;
+ desc->context.data_length = 0;
+ return;
+ } else {
+ desc->context.data = srb->buffer;
+ desc->context.data_length = srb->bufflen;
+ MTS_DEBUG("length = %d or %d\n",
+ srb->request_bufflen, srb->bufflen);
+ }
+ } else {
+ MTS_DEBUG("Using scatter/gather\n");
+ sg = srb->buffer;
+ desc->context.data = page_address(sg[0].page) + sg[0].offset;
+ desc->context.data_length = sg[0].length;
+ }
+
+
+ /* can't rely on srb->sc_data_direction */
+
+ /* Brutally ripped from usb-storage */
+
+ if ( !memcmp( srb->cmnd, mts_read_image_sig, mts_read_image_sig_len )
+) { pipe = usb_rcvbulkpipe(desc->usb_dev,desc->ep_image);
+ MTS_DEBUG( "transfering from desc->ep_image == %d\n",
+ (int)desc->ep_image );
+ } else if ( MTS_DIRECTION_IS_IN(srb->cmnd[0]) ) {
+ pipe = usb_rcvbulkpipe(desc->usb_dev,desc->ep_response);
+ MTS_DEBUG( "transfering from desc->ep_response == %d\n",
+ (int)desc->ep_response);
+ } else {
+ MTS_DEBUG("transfering to desc->ep_out == %d\n",
+ (int)desc->ep_out);
+ pipe = usb_sndbulkpipe(desc->usb_dev,desc->ep_out);
+ }
+ desc->context.data_pipe = pipe;
+}
+
+
+static
+int mts_scsi_queuecommand( Scsi_Cmnd *srb, mts_scsi_cmnd_callback callback )
+{
+ struct mts_desc* desc = (struct mts_desc*)(srb->device->host->hostdata[0]);
+ int err = 0;
+ int res;
+
+ MTS_DEBUG_GOT_HERE();
+ mts_show_command(srb);
+ mts_debug_dump(desc);
+
+ if ( srb->device->lun || srb->device->id || srb->device->channel ) {
+
+ MTS_DEBUG("Command to LUN=%d ID=%d CHANNEL=%d from SCSI layer\n",(int)srb->device->lun,(int)srb->device->id, (int)srb->device->channel );
+
+ MTS_DEBUG("this device doesn't exist\n");
+
+ srb->result = DID_BAD_TARGET << 16;
+
+ if(likely(callback != NULL))
+ callback(srb);
+
+ goto out;
+ }
+
+
+ usb_fill_bulk_urb(desc->urb,
+ desc->usb_dev,
+ usb_sndbulkpipe(desc->usb_dev,desc->ep_out),
+ srb->cmnd,
+ srb->cmd_len,
+ mts_command_done,
+ &desc->context
+ );
+
+
+ mts_build_transfer_context( srb, desc );
+ desc->context.final_callback = callback;
+
+ /* here we need ATOMIC as we are called with the iolock */
+ res=usb_submit_urb(desc->urb, GFP_ATOMIC);
+
+ if(unlikely(res)){
+ MTS_ERROR("error %d submitting URB\n",(int)res);
+ srb->result = DID_ERROR << 16;
+
+ if(likely(callback != NULL))
+ callback(srb);
+
+ }
+
+out:
+ return err;
+}
+
+static Scsi_Host_Template mts_scsi_host_template = {
+ .module = THIS_MODULE,
+ .name = "microtekX6",
+ .proc_name = "microtekX6",
+ .queuecommand = mts_scsi_queuecommand,
+ .eh_abort_handler = mts_scsi_abort,
+ .eh_host_reset_handler = mts_scsi_host_reset,
+ .sg_tablesize = SG_ALL,
+ .can_queue = 1,
+ .this_id = -1,
+ .cmd_per_lun = 1,
+ .use_clustering = 1,
+ .emulated = 1,
+};
+
+struct vendor_product
+{
+ char* name;
+ enum
+ {
+ mts_sup_unknown=0,
+ mts_sup_alpha,
+ mts_sup_full
+ }
+ support_status;
+} ;
+
+
+/* These are taken from the msmUSB.inf file on the Windows driver CD */
+const static struct vendor_product mts_supported_products[] =
+{
+ { "Phantom 336CX", mts_sup_unknown},
+ { "Phantom 336CX", mts_sup_unknown},
+ { "Scanmaker X6", mts_sup_alpha},
+ { "Phantom C6", mts_sup_unknown},
+ { "Phantom 336CX", mts_sup_unknown},
+ { "ScanMaker V6USL", mts_sup_unknown},
+ { "ScanMaker V6USL", mts_sup_unknown},
+ { "Scanmaker V6UL", mts_sup_unknown},
+ { "Scanmaker V6UPL", mts_sup_alpha},
+};
+
+/* The entries of microtek_table must correspond, line-by-line to
+ the entries of mts_supported_products[]. */
+
+static struct usb_device_id mts_usb_ids [] =
+{
+ { USB_DEVICE(0x4ce, 0x0300) },
+ { USB_DEVICE(0x5da, 0x0094) },
+ { USB_DEVICE(0x5da, 0x0099) },
+ { USB_DEVICE(0x5da, 0x009a) },
+ { USB_DEVICE(0x5da, 0x00a0) },
+ { USB_DEVICE(0x5da, 0x00a3) },
+ { USB_DEVICE(0x5da, 0x80a3) },
+ { USB_DEVICE(0x5da, 0x80ac) },
+ { USB_DEVICE(0x5da, 0x00b6) },
+ { } /* Terminating entry */
+};
+
+MODULE_DEVICE_TABLE (usb, mts_usb_ids);
+
+
+static int mts_usb_probe(struct usb_interface *intf,
+ const struct usb_device_id *id)
+{
+ int i;
+ int ep_out = -1;
+ int ep_in_set[3]; /* this will break if we have more than three endpoints
+ which is why we check */
+ int *ep_in_current = ep_in_set;
+ int err_retval = -ENOMEM;
+
+ struct mts_desc * new_desc;
+ struct vendor_product const* p;
+ struct usb_device *dev = interface_to_usbdev (intf);
+
+ /* the current altsetting on the interface we're probing */
+ struct usb_host_interface *altsetting;
+
+ MTS_DEBUG_GOT_HERE();
+ MTS_DEBUG( "usb-device descriptor at %x\n", (int)dev );
+
+ MTS_DEBUG( "product id = 0x%x, vendor id = 0x%x\n",
+ le16_to_cpu(dev->descriptor.idProduct),
+ le16_to_cpu(dev->descriptor.idVendor) );
+
+ MTS_DEBUG_GOT_HERE();
+
+ p = &mts_supported_products[id - mts_usb_ids];
+
+ MTS_DEBUG_GOT_HERE();
+
+ MTS_DEBUG( "found model %s\n", p->name );
+ if ( p->support_status != mts_sup_full )
+ MTS_MESSAGE( "model %s is not known to be fully supported, reports welcome!\n",
+ p->name );
+
+ /* the current altsetting on the interface we're probing */
+ altsetting = intf->cur_altsetting;
+
+
+ /* Check if the config is sane */
+
+ if ( altsetting->desc.bNumEndpoints != MTS_EP_TOTAL ) {
+ MTS_WARNING( "expecting %d got %d endpoints! Bailing out.\n",
+ (int)MTS_EP_TOTAL, (int)altsetting->desc.bNumEndpoints );
+ return -ENODEV;
+ }
+
+ for( i = 0; i < altsetting->desc.bNumEndpoints; i++ ) {
+ if ((altsetting->endpoint[i].desc.bmAttributes &
+ USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
+
+ MTS_WARNING( "can only deal with bulk endpoints; endpoint %d is not bulk.\n",
+ (int)altsetting->endpoint[i].desc.bEndpointAddress );
+ } else {
+ if (altsetting->endpoint[i].desc.bEndpointAddress &
+ USB_DIR_IN)
+ *ep_in_current++
+ = altsetting->endpoint[i].desc.bEndpointAddress &
+ USB_ENDPOINT_NUMBER_MASK;
+ else {
+ if ( ep_out != -1 ) {
+ MTS_WARNING( "can only deal with one output endpoints. Bailing out." );
+ return -ENODEV;
+ }
+
+ ep_out = altsetting->endpoint[i].desc.bEndpointAddress &
+ USB_ENDPOINT_NUMBER_MASK;
+ }
+ }
+
+ }
+
+
+ if ( ep_out == -1 ) {
+ MTS_WARNING( "couldn't find an output bulk endpoint. Bailing out.\n" );
+ return -ENODEV;
+ }
+
+
+ new_desc = kmalloc(sizeof(struct mts_desc), GFP_KERNEL);
+ if (!new_desc)
+ goto out;
+
+ memset(new_desc, 0, sizeof(*new_desc));
+ new_desc->urb = usb_alloc_urb(0, GFP_KERNEL);
+ if (!new_desc->urb)
+ goto out_kfree;
+
+ new_desc->usb_dev = dev;
+ new_desc->usb_intf = intf;
+ init_MUTEX(&new_desc->lock);
+
+ /* endpoints */
+ new_desc->ep_out = ep_out;
+ new_desc->ep_response = ep_in_set[0];
+ new_desc->ep_image = ep_in_set[1];
+
+ if ( new_desc->ep_out != MTS_EP_OUT )
+ MTS_WARNING( "will this work? Command EP is not usually %d\n",
+ (int)new_desc->ep_out );
+
+ if ( new_desc->ep_response != MTS_EP_RESPONSE )
+ MTS_WARNING( "will this work? Response EP is not usually %d\n",
+ (int)new_desc->ep_response );
+
+ if ( new_desc->ep_image != MTS_EP_IMAGE )
+ MTS_WARNING( "will this work? Image data EP is not usually %d\n",
+ (int)new_desc->ep_image );
+
+ new_desc->host = scsi_host_alloc(&mts_scsi_host_template,
+ sizeof(new_desc));
+ if (!new_desc->host)
+ goto out_free_urb;
+
+ new_desc->host->hostdata[0] = (unsigned long)new_desc;
+ if (scsi_add_host(new_desc->host, NULL)) {
+ err_retval = -EIO;
+ goto out_free_urb;
+ }
+ scsi_scan_host(new_desc->host);
+
+ usb_set_intfdata(intf, new_desc);
+ return 0;
+
+ out_free_urb:
+ usb_free_urb(new_desc->urb);
+ out_kfree:
+ kfree(new_desc);
+ out:
+ return err_retval;
+}
+
+static void mts_usb_disconnect (struct usb_interface *intf)
+{
+ struct mts_desc *desc = usb_get_intfdata(intf);
+
+ usb_set_intfdata(intf, NULL);
+
+ usb_kill_urb(desc->urb);
+ scsi_remove_host(desc->host);
+
+ scsi_host_put(desc->host);
+ usb_free_urb(desc->urb);
+ kfree(desc);
+}
+
+
+static int __init microtek_drv_init(void)
+{
+ return usb_register(&mts_usb_driver);
+}
+
+static void __exit microtek_drv_exit(void)
+{
+ usb_deregister(&mts_usb_driver);
+}
+
+module_init(microtek_drv_init);
+module_exit(microtek_drv_exit);
+
+MODULE_AUTHOR( DRIVER_AUTHOR );
+MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
+
+
diff --git a/drivers/usb/image/microtek.h b/drivers/usb/image/microtek.h
new file mode 100644
index 00000000000..3271deb8c00
--- /dev/null
+++ b/drivers/usb/image/microtek.h
@@ -0,0 +1,55 @@
+ /*
+ * Driver for Microtek Scanmaker X6 USB scanner and possibly others.
+ *
+ * (C) Copyright 2000 John Fremlin <vii@penguinpowered.com>
+ * (C) Copyright 2000 Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de>
+ *
+ * See microtek.c for history
+ *
+ */
+
+typedef void (*mts_scsi_cmnd_callback)(Scsi_Cmnd *);
+
+
+struct mts_transfer_context
+{
+ struct mts_desc* instance;
+ mts_scsi_cmnd_callback final_callback;
+ Scsi_Cmnd *srb;
+
+ void* data;
+ unsigned data_length;
+ int data_pipe;
+ int fragment;
+
+ u8 status; /* status returned from ep_response after command completion */
+};
+
+
+struct mts_desc {
+ struct mts_desc *next;
+ struct mts_desc *prev;
+
+ struct usb_device *usb_dev;
+ struct usb_interface *usb_intf;
+
+ /* Endpoint addresses */
+ u8 ep_out;
+ u8 ep_response;
+ u8 ep_image;
+
+ struct Scsi_Host * host;
+ struct semaphore lock;
+
+ struct urb *urb;
+ struct mts_transfer_context context;
+};
+
+
+#define MTS_EP_OUT 0x1
+#define MTS_EP_RESPONSE 0x2
+#define MTS_EP_IMAGE 0x3
+#define MTS_EP_TOTAL 0x3
+
+#define MTS_SCSI_ERR_MASK ~0x3fu
+