fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / usb / class / usblp.c
index 8836be0..6377db1 100644 (file)
@@ -3,10 +3,11 @@
  *
  * Copyright (c) 1999 Michael Gee      <michael@linuxspecific.com>
  * Copyright (c) 1999 Pavel Machek     <pavel@suse.cz>
- * Copyright (c) 2000 Randy Dunlap     <rddunlap@osdl.org>
+ * Copyright (c) 2000 Randy Dunlap     <rdunlap@xenotime.net>
  * Copyright (c) 2000 Vojtech Pavlik   <vojtech@suse.cz>
  # Copyright (c) 2001 Pete Zaitcev     <zaitcev@redhat.com>
  # Copyright (c) 2001 David Paschal    <paschal@rcsis.com>
+ * Copyright (c) 2006 Oliver Neukum    <oliver@neukum.name>
  *
  * USB Printer Device Class driver for USB printers and printer cables
  *
@@ -54,6 +55,7 @@
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/lp.h>
+#include <linux/mutex.h>
 #undef DEBUG
 #include <linux/usb.h>
 
@@ -75,6 +77,7 @@
 #define IOCNR_HP_SET_CHANNEL           4
 #define IOCNR_GET_BUS_ADDRESS          5
 #define IOCNR_GET_VID_PID              6
+#define IOCNR_SOFT_RESET               7
 /* Get device_id string: */
 #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
 /* The following ioctls were added for http://hpoj.sourceforge.net: */
@@ -90,6 +93,8 @@
 #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
 /* Get two-int array: [0]=vendor ID, [1]=product ID: */
 #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
+/* Perform class specific soft reset */
+#define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
 
 /*
  * A DEVICE_ID string may include the printer's serial number.
@@ -111,7 +116,7 @@ MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:H
 #define USBLP_MINORS           16
 #define USBLP_MINOR_BASE       0
 
-#define USBLP_WRITE_TIMEOUT    (5*HZ)                  /* 5 seconds */
+#define USBLP_WRITE_TIMEOUT    (5000)                  /* 5 seconds */
 
 #define USBLP_FIRST_PROTOCOL   1
 #define USBLP_LAST_PROTOCOL    3
@@ -125,7 +130,7 @@ MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:H
 
 struct usblp {
        struct usb_device       *dev;                   /* USB device */
-       struct semaphore        sem;                    /* locks this struct, especially "dev" */
+       struct mutex            mut;                    /* locks this struct, especially "dev" */
        char                    *writebuf;              /* write transfer_buffer */
        char                    *readbuf;               /* read transfer_buffer */
        char                    *statusbuf;             /* status transfer_buffer */
@@ -149,6 +154,7 @@ struct usblp {
        unsigned char           used;                   /* True if open */
        unsigned char           present;                /* True if not disconnected */
        unsigned char           bidir;                  /* interface is bidirectional */
+       unsigned char           sleeping;               /* interface is suspended */
        unsigned char           *device_id_string;      /* IEEE 1284 DEVICE ID string (ptr) */
                                                        /* first 2 bytes are (big-endian) length */
 };
@@ -178,6 +184,7 @@ static void usblp_dump(struct usblp *usblp) {
        dbg("quirks=%d", usblp->quirks);
        dbg("used=%d", usblp->used);
        dbg("bidir=%d", usblp->bidir);
+       dbg("sleeping=%d", usblp->sleeping);
        dbg("device_id_string=\"%s\"",
                usblp->device_id_string ?
                        usblp->device_id_string + 2 :
@@ -196,7 +203,7 @@ struct quirk_printer_struct {
 #define USBLP_QUIRK_BIDIR      0x1     /* reports bidir but requires unidirectional mode (no INs/reads) */
 #define USBLP_QUIRK_USB_INIT   0x2     /* needs vendor USB init string */
 
-static struct quirk_printer_struct quirk_printers[] = {
+static const struct quirk_printer_struct quirk_printers[] = {
        { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
        { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
        { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
@@ -210,6 +217,7 @@ static struct quirk_printer_struct quirk_printers[] = {
        { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
        { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
        { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
+       { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
        { 0, 0 }
 };
 
@@ -218,7 +226,8 @@ static int usblp_set_protocol(struct usblp *usblp, int protocol);
 static int usblp_cache_device_id_string(struct usblp *usblp);
 
 /* forward reference to make our lives easier */
-extern struct usb_driver usblp_driver;
+static struct usb_driver usblp_driver;
+static DEFINE_MUTEX(usblp_mutex);      /* locks the existence of usblp's */
 
 /*
  * Functions for usblp control messages.
@@ -265,31 +274,36 @@ static int proto_bias = -1;
  * URB callback.
  */
 
-static void usblp_bulk_read(struct urb *urb, struct pt_regs *regs)
+static void usblp_bulk_read(struct urb *urb)
 {
        struct usblp *usblp = urb->context;
 
-       if (!usblp || !usblp->dev || !usblp->used || !usblp->present)
+       if (unlikely(!usblp || !usblp->dev || !usblp->used))
                return;
 
+       if (unlikely(!usblp->present))
+               goto unplug;
        if (unlikely(urb->status))
                warn("usblp%d: nonzero read/write bulk status received: %d",
                        usblp->minor, urb->status);
        usblp->rcomplete = 1;
+unplug:
        wake_up_interruptible(&usblp->wait);
 }
 
-static void usblp_bulk_write(struct urb *urb, struct pt_regs *regs)
+static void usblp_bulk_write(struct urb *urb)
 {
        struct usblp *usblp = urb->context;
 
-       if (!usblp || !usblp->dev || !usblp->used || !usblp->present)
+       if (unlikely(!usblp || !usblp->dev || !usblp->used))
                return;
-
+       if (unlikely(!usblp->present))
+               goto unplug;
        if (unlikely(urb->status))
                warn("usblp%d: nonzero read/write bulk status received: %d",
                        usblp->minor, urb->status);
        usblp->wcomplete = 1;
+unplug:
        wake_up_interruptible(&usblp->wait);
 }
 
@@ -297,7 +311,7 @@ static void usblp_bulk_write(struct urb *urb, struct pt_regs *regs)
  * Get and print printer errors.
  */
 
-static char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
+static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
 
 static int usblp_check_status(struct usblp *usblp, int err)
 {
@@ -306,8 +320,9 @@ static int usblp_check_status(struct usblp *usblp, int err)
 
        error = usblp_read_status (usblp, usblp->statusbuf);
        if (error < 0) {
-               err("usblp%d: error %d reading printer status",
-                       usblp->minor, error);
+               if (printk_ratelimit())
+                       err("usblp%d: error %d reading printer status",
+                               usblp->minor, error);
                return 0;
        }
 
@@ -326,6 +341,20 @@ static int usblp_check_status(struct usblp *usblp, int err)
        return newerr;
 }
 
+static int handle_bidir (struct usblp *usblp)
+{
+       if (usblp->bidir && usblp->used && !usblp->sleeping) {
+               usblp->readcount = 0;
+               usblp->readurb->dev = usblp->dev;
+               if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
+                       usblp->used = 0;
+                       return -EIO;
+               }
+       }
+
+       return 0;
+}
+
 /*
  * File op functions.
  */
@@ -340,7 +369,7 @@ static int usblp_open(struct inode *inode, struct file *file)
        if (minor < 0)
                return -ENODEV;
 
-       lock_kernel();
+       mutex_lock (&usblp_mutex);
 
        retval = -ENODEV;
        intf = usb_find_interface(&usblp_driver, minor);
@@ -375,18 +404,15 @@ static int usblp_open(struct inode *inode, struct file *file)
        usblp->writeurb->transfer_buffer_length = 0;
        usblp->wcomplete = 1; /* we begin writeable */
        usblp->rcomplete = 0;
+       usblp->writeurb->status = 0;
+       usblp->readurb->status = 0;
 
-       if (usblp->bidir) {
-               usblp->readcount = 0;
-               usblp->readurb->dev = usblp->dev;
-               if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
-                       retval = -EIO;
-                       usblp->used = 0;
-                       file->private_data = NULL;
-               }
+       if (handle_bidir(usblp) < 0) {
+               file->private_data = NULL;
+               retval = -EIO;
        }
 out:
-       unlock_kernel();
+       mutex_unlock (&usblp_mutex);
        return retval;
 }
 
@@ -394,10 +420,6 @@ static void usblp_cleanup (struct usblp *usblp)
 {
        info("usblp%d: removed", usblp->minor);
 
-       usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
-                       usblp->writebuf, usblp->writeurb->transfer_dma);
-       usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
-                       usblp->readbuf, usblp->readurb->transfer_dma);
        kfree (usblp->device_id_string);
        kfree (usblp->statusbuf);
        usb_free_urb(usblp->writeurb);
@@ -407,22 +429,22 @@ static void usblp_cleanup (struct usblp *usblp)
 
 static void usblp_unlink_urbs(struct usblp *usblp)
 {
-       usb_unlink_urb(usblp->writeurb);
+       usb_kill_urb(usblp->writeurb);
        if (usblp->bidir)
-               usb_unlink_urb(usblp->readurb);
+               usb_kill_urb(usblp->readurb);
 }
 
 static int usblp_release(struct inode *inode, struct file *file)
 {
        struct usblp *usblp = file->private_data;
 
-       down (&usblp->sem);
+       mutex_lock (&usblp_mutex);
        usblp->used = 0;
        if (usblp->present) {
                usblp_unlink_urbs(usblp);
-               up(&usblp->sem);
        } else          /* finish cleanup from disconnect */
                usblp_cleanup (usblp);
+       mutex_unlock (&usblp_mutex);
        return 0;
 }
 
@@ -435,7 +457,7 @@ static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait
                               | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
 }
 
-static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        struct usblp *usblp = file->private_data;
        int length, err, i;
@@ -444,12 +466,17 @@ static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
        int twoints[2];
        int retval = 0;
 
-       down (&usblp->sem);
+       mutex_lock (&usblp->mut);
        if (!usblp->present) {
                retval = -ENODEV;
                goto done;
        }
 
+       if (usblp->sleeping) {
+               retval = -ENODEV;
+               goto done;
+       }
+
        dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
                _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
 
@@ -527,7 +554,7 @@ static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 
                        case IOCNR_HP_SET_CHANNEL:
                                if (_IOC_DIR(cmd) != _IOC_WRITE ||
-                                   usblp->dev->descriptor.idVendor != 0x03F0 ||
+                                   le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
                                    usblp->quirks & USBLP_QUIRK_BIDIR) {
                                        retval = -EINVAL;
                                        goto done;
@@ -574,8 +601,8 @@ static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                                        goto done;
                                }
 
-                               twoints[0] = usblp->dev->descriptor.idVendor;
-                               twoints[1] = usblp->dev->descriptor.idProduct;
+                               twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
+                               twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
                                if (copy_to_user((void __user *)arg,
                                                (unsigned char *)twoints,
                                                sizeof(twoints))) {
@@ -587,6 +614,13 @@ static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                                        usblp->minor, twoints[0], twoints[1]);
                                break;
 
+                       case IOCNR_SOFT_RESET:
+                               if (_IOC_DIR(cmd) != _IOC_NONE) {
+                                       retval = -EINVAL;
+                                       goto done;
+                               }
+                               retval = usblp_reset(usblp);
+                               break;
                        default:
                                retval = -ENOTTY;
                }
@@ -595,7 +629,9 @@ static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 
                        case LPGETSTATUS:
                                if (usblp_read_status(usblp, usblp->statusbuf)) {
-                                       err("usblp%d: failed reading printer status", usblp->minor);
+                                       if (printk_ratelimit())
+                                               err("usblp%d: failed reading printer status",
+                                                       usblp->minor);
                                        retval = -EIO;
                                        goto done;
                                }
@@ -609,15 +645,14 @@ static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                }
 
 done:
-       up (&usblp->sem);
+       mutex_unlock (&usblp->mut);
        return retval;
 }
 
 static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
 {
-       DECLARE_WAITQUEUE(wait, current);
        struct usblp *usblp = file->private_data;
-       int timeout, err = 0, transfer_length = 0;
+       int timeout, intr, rv, err = 0, transfer_length = 0;
        size_t writecount = 0;
 
        while (writecount < count) {
@@ -629,30 +664,24 @@ static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t
                        }
 
                        timeout = USBLP_WRITE_TIMEOUT;
-                       add_wait_queue(&usblp->wait, &wait);
-                       while ( 1==1 ) {
 
-                               if (signal_pending(current)) {
-                                       remove_wait_queue(&usblp->wait, &wait);
-                                       return writecount ? writecount : -EINTR;
-                               }
-                               set_current_state(TASK_INTERRUPTIBLE);
-                               if (timeout && !usblp->wcomplete) {
-                                       timeout = schedule_timeout(timeout);
-                               } else {
-                                       set_current_state(TASK_RUNNING);
-                                       break;
-                               }
-                       }
-                       remove_wait_queue(&usblp->wait, &wait);
+                       rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
+                       if (rv < 0)
+                               return writecount ? writecount : -EINTR;
                }
-
-               down (&usblp->sem);
+               intr = mutex_lock_interruptible (&usblp->mut);
+               if (intr)
+                       return writecount ? writecount : -EINTR;
                if (!usblp->present) {
-                       up (&usblp->sem);
+                       mutex_unlock (&usblp->mut);
                        return -ENODEV;
                }
 
+               if (usblp->sleeping) {
+                       mutex_unlock (&usblp->mut);
+                       return writecount ? writecount : -ENODEV;
+               }
+
                if (usblp->writeurb->status != 0) {
                        if (usblp->quirks & USBLP_QUIRK_BIDIR) {
                                if (!usblp->wcomplete)
@@ -661,10 +690,10 @@ static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t
                                err = usblp->writeurb->status;
                        } else
                                err = usblp_check_status(usblp, err);
-                       up (&usblp->sem);
+                       mutex_unlock (&usblp->mut);
 
                        /* if the fault was due to disconnect, let khubd's
-                        * call to usblp_disconnect() grab usblp->sem ...
+                        * call to usblp_disconnect() grab usblp->mut ...
                         */
                        schedule ();
                        continue;
@@ -676,7 +705,7 @@ static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t
                 */
                writecount += transfer_length;
                if (writecount == count) {
-                       up(&usblp->sem);
+                       mutex_unlock(&usblp->mut);
                        break;
                }
 
@@ -688,7 +717,7 @@ static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t
 
                if (copy_from_user(usblp->writeurb->transfer_buffer, 
                                   buffer + writecount, transfer_length)) {
-                       up(&usblp->sem);
+                       mutex_unlock(&usblp->mut);
                        return writecount ? writecount : -EFAULT;
                }
 
@@ -696,14 +725,15 @@ static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t
                usblp->wcomplete = 0;
                err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
                if (err) {
+                       usblp->wcomplete = 1;
                        if (err != -ENOMEM)
                                count = -EIO;
                        else
                                count = writecount ? writecount : -ENOMEM;
-                       up (&usblp->sem);
+                       mutex_unlock (&usblp->mut);
                        break;
                }
-               up (&usblp->sem);
+               mutex_unlock (&usblp->mut);
        }
 
        return count;
@@ -712,12 +742,14 @@ static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t
 static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
 {
        struct usblp *usblp = file->private_data;
-       DECLARE_WAITQUEUE(wait, current);
+       int rv, intr;
 
        if (!usblp->bidir)
                return -EINVAL;
 
-       down (&usblp->sem);
+       intr = mutex_lock_interruptible (&usblp->mut);
+       if (intr)
+               return -EINTR;
        if (!usblp->present) {
                count = -ENODEV;
                goto done;
@@ -730,25 +762,13 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count,
                        count = -EAGAIN;
                        goto done;
                }
-
-               add_wait_queue(&usblp->wait, &wait);
-               while (1==1) {
-                       if (signal_pending(current)) {
-                               count = -EINTR;
-                               remove_wait_queue(&usblp->wait, &wait);
-                               goto done;
-                       }
-                       up (&usblp->sem);
-                       set_current_state(TASK_INTERRUPTIBLE);
-                       if (!usblp->rcomplete) {
-                               schedule();
-                       } else {
-                               set_current_state(TASK_RUNNING);
-                               break;
-                       }
-                       down (&usblp->sem);
+               mutex_unlock(&usblp->mut);
+               rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
+               mutex_lock(&usblp->mut);
+               if (rv < 0) {
+                       count = -EINTR;
+                       goto done;
                }
-               remove_wait_queue(&usblp->wait, &wait);
        }
 
        if (!usblp->present) {
@@ -756,11 +776,17 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count,
                goto done;
        }
 
+       if (usblp->sleeping) {
+               count = -ENODEV;
+               goto done;
+       }
+
        if (usblp->readurb->status) {
                err("usblp%d: error %d reading from printer",
                        usblp->minor, usblp->readurb->status);
                usblp->readurb->dev = usblp->dev;
                usblp->readcount = 0;
+               usblp->rcomplete = 0;
                if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
                        dbg("error submitting urb");
                count = -EIO;
@@ -786,7 +812,7 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count,
        }
 
 done:
-       up (&usblp->sem);
+       mutex_unlock (&usblp->mut);
        return count;
 }
 
@@ -819,40 +845,53 @@ static unsigned int usblp_quirks (__u16 vendor, __u16 product)
        return 0;
 }
 
-static struct file_operations usblp_fops = {
+static const struct file_operations usblp_fops = {
        .owner =        THIS_MODULE,
        .read =         usblp_read,
        .write =        usblp_write,
        .poll =         usblp_poll,
-       .ioctl =        usblp_ioctl,
+       .unlocked_ioctl =       usblp_ioctl,
+       .compat_ioctl =         usblp_ioctl,
        .open =         usblp_open,
        .release =      usblp_release,
 };
 
 static struct usb_class_driver usblp_class = {
-       .name =         "usb/lp%d",
+       .name =         "lp%d",
        .fops =         &usblp_fops,
-       .mode =         S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
        .minor_base =   USBLP_MINOR_BASE,
 };
 
+static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct usb_interface *intf = to_usb_interface(dev);
+       struct usblp *usblp = usb_get_intfdata (intf);
+
+       if (usblp->device_id_string[0] == 0 &&
+           usblp->device_id_string[1] == 0)
+               return 0;
+
+       return sprintf(buf, "%s", usblp->device_id_string+2);
+}
+
+static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
+
 static int usblp_probe(struct usb_interface *intf,
                       const struct usb_device_id *id)
 {
        struct usb_device *dev = interface_to_usbdev (intf);
-       struct usblp *usblp = 0;
+       struct usblp *usblp = NULL;
        int protocol;
        int retval;
 
        /* Malloc and start initializing usblp structure so we can use it
         * directly. */
-       if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) {
+       if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
                err("out of memory for usblp");
                goto abort;
        }
-       memset(usblp, 0, sizeof(struct usblp));
        usblp->dev = dev;
-       init_MUTEX (&usblp->sem);
+       mutex_init (&usblp->mut);
        init_waitqueue_head(&usblp->wait);
        usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
        usblp->intf = intf;
@@ -902,15 +941,15 @@ static int usblp_probe(struct usb_interface *intf,
 
        /* Lookup quirks for this printer. */
        usblp->quirks = usblp_quirks(
-               dev->descriptor.idVendor,
-               dev->descriptor.idProduct);
+               le16_to_cpu(dev->descriptor.idVendor),
+               le16_to_cpu(dev->descriptor.idProduct));
 
        /* Analyze and pick initial alternate settings and endpoints. */
        protocol = usblp_select_alts(usblp);
        if (protocol < 0) {
                dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
-                       dev->descriptor.idVendor,
-                       dev->descriptor.idProduct);
+                       le16_to_cpu(dev->descriptor.idVendor),
+                       le16_to_cpu(dev->descriptor.idProduct));
                goto abort;
        }
 
@@ -920,19 +959,14 @@ static int usblp_probe(struct usb_interface *intf,
 
        /* Retrieve and store the device ID string. */
        usblp_cache_device_id_string(usblp);
+       retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
+       if (retval)
+               goto abort_intfdata;
 
 #ifdef DEBUG
        usblp_check_status(usblp, 0);
 #endif
 
-       info("usblp%d: USB %sdirectional printer dev %d "
-               "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
-               usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
-               usblp->ifnum,
-               usblp->protocol[usblp->current_protocol].alt_setting,
-               usblp->current_protocol, usblp->dev->descriptor.idVendor,
-               usblp->dev->descriptor.idProduct);
-
        usb_set_intfdata (intf, usblp);
 
        usblp->present = 1;
@@ -943,11 +977,20 @@ static int usblp_probe(struct usb_interface *intf,
                goto abort_intfdata;
        }
        usblp->minor = intf->minor;
+       info("usblp%d: USB %sdirectional printer dev %d "
+               "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
+               usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
+               usblp->ifnum,
+               usblp->protocol[usblp->current_protocol].alt_setting,
+               usblp->current_protocol,
+               le16_to_cpu(usblp->dev->descriptor.idVendor),
+               le16_to_cpu(usblp->dev->descriptor.idProduct));
 
        return 0;
 
 abort_intfdata:
        usb_set_intfdata (intf, NULL);
+       device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
 abort:
        if (usblp) {
                if (usblp->writebuf)
@@ -1008,22 +1051,17 @@ static int usblp_select_alts(struct usblp *usblp)
                        continue;
 
                /* Look for bulk OUT and IN endpoints. */
-               epwrite = epread = 0;
+               epwrite = epread = NULL;
                for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
                        epd = &ifd->endpoint[e].desc;
 
-                       if ((epd->bmAttributes&USB_ENDPOINT_XFERTYPE_MASK)!=
-                           USB_ENDPOINT_XFER_BULK)
-                               continue;
-
-                       if (!(epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK)) {
+                       if (usb_endpoint_is_bulk_out(epd))
                                if (!epwrite)
                                        epwrite = epd;
 
-                       } else {
+                       if (usb_endpoint_is_bulk_in(epd))
                                if (!epread)
                                        epread = epd;
-                       }
                }
 
                /* Ignore buggy hardware without the right endpoints. */
@@ -1087,7 +1125,7 @@ static int usblp_set_protocol(struct usblp *usblp, int protocol)
                usblp->writebuf, 0,
                usblp_bulk_write, usblp);
 
-       usblp->bidir = (usblp->protocol[protocol].epread != 0);
+       usblp->bidir = (usblp->protocol[protocol].epread != NULL);
        if (usblp->bidir)
                usb_fill_bulk_urb(usblp->readurb, usblp->dev,
                        usb_rcvbulkpipe(usblp->dev,
@@ -1118,7 +1156,7 @@ static int usblp_cache_device_id_string(struct usblp *usblp)
        /* First two bytes are length in big-endian.
         * They count themselves, and we copy them into
         * the user's buffer. */
-       length = be16_to_cpu(*((u16 *)usblp->device_id_string));
+       length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
        if (length < 2)
                length = 2;
        else if (length >= USBLP_DEVICE_ID_SIZE)
@@ -1142,18 +1180,56 @@ static void usblp_disconnect(struct usb_interface *intf)
                BUG ();
        }
 
-       down (&usblp->sem);
-       lock_kernel();
+       device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
+
+       mutex_lock (&usblp_mutex);
+       mutex_lock (&usblp->mut);
        usblp->present = 0;
        usb_set_intfdata (intf, NULL);
 
        usblp_unlink_urbs(usblp);
+       usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
+                       usblp->writebuf, usblp->writeurb->transfer_dma);
+       usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
+                       usblp->readbuf, usblp->readurb->transfer_dma);
+       mutex_unlock (&usblp->mut);
 
        if (!usblp->used)
                usblp_cleanup (usblp);
-       else    /* cleanup later, on release */
-               up (&usblp->sem);
-       unlock_kernel();
+       mutex_unlock (&usblp_mutex);
+}
+
+static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
+{
+       struct usblp *usblp = usb_get_intfdata (intf);
+
+       /* this races against normal access and open */
+       mutex_lock (&usblp_mutex);
+       mutex_lock (&usblp->mut);
+       /* we take no more IO */
+       usblp->sleeping = 1;
+       usblp_unlink_urbs(usblp);
+       mutex_unlock (&usblp->mut);
+       mutex_unlock (&usblp_mutex);
+
+       return 0;
+}
+
+static int usblp_resume (struct usb_interface *intf)
+{
+       struct usblp *usblp = usb_get_intfdata (intf);
+       int r;
+
+       mutex_lock (&usblp_mutex);
+       mutex_lock (&usblp->mut);
+
+       usblp->sleeping = 0;
+       r = handle_bidir (usblp);
+
+       mutex_unlock (&usblp->mut);
+       mutex_unlock (&usblp_mutex);
+
+       return r;
 }
 
 static struct usb_device_id usblp_ids [] = {
@@ -1169,10 +1245,11 @@ static struct usb_device_id usblp_ids [] = {
 MODULE_DEVICE_TABLE (usb, usblp_ids);
 
 static struct usb_driver usblp_driver = {
-       .owner =        THIS_MODULE,
        .name =         "usblp",
        .probe =        usblp_probe,
        .disconnect =   usblp_disconnect,
+       .suspend =      usblp_suspend,
+       .resume =       usblp_resume,
        .id_table =     usblp_ids,
 };
 
@@ -1180,10 +1257,9 @@ static int __init usblp_init(void)
 {
        int retval;
        retval = usb_register(&usblp_driver);
-       if (retval)
-               goto out;
-       info(DRIVER_VERSION ": " DRIVER_DESC);
-out:
+       if (!retval)
+               info(DRIVER_VERSION ": " DRIVER_DESC);
+
        return retval;
 }
 
@@ -1197,6 +1273,6 @@ module_exit(usblp_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
-MODULE_PARM(proto_bias, "i");
+module_param(proto_bias, int, S_IRUGO | S_IWUSR);
 MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
 MODULE_LICENSE("GPL");