virtio: add virtio disk geometry feature
[safe/jmp/linux-2.6] / drivers / block / ub.c
index f73446f..e322cce 100644 (file)
@@ -8,19 +8,16 @@
  * and is not licensed separately. See file COPYING for details.
  *
  * TODO (sorted by decreasing priority)
+ *  -- Return sense now that rq allows it (we always auto-sense anyway).
  *  -- set readonly flag for CDs, set removable flag for CF readers
  *  -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
- *  -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
  *  -- verify the 13 conditions and do bulk resets
- *  -- kill last_pipe and simply do two-state clearing on both pipes
  *  -- highmem
  *  -- move top_sense and work_bcs into separate allocations (if they survive)
  *     for cache purists and esoteric architectures.
  *  -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
  *  -- prune comments, they are too volumnous
- *  -- Exterminate P3 printks
  *  -- Resove XXX's
- *  -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
  *  -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
  */
 #include <linux/kernel.h>
 #include <linux/usb.h>
 #include <linux/usb_usual.h>
 #include <linux/blkdev.h>
-#include <linux/devfs_fs_kernel.h>
 #include <linux/timer.h>
+#include <linux/scatterlist.h>
 #include <scsi/scsi.h>
 
 #define DRV_NAME "ub"
-#define DEVFS_NAME DRV_NAME
 
 #define UB_MAJOR 180
 
@@ -180,7 +176,6 @@ struct ub_dev;
 #define UB_DIR_ILLEGAL2        2
 #define UB_DIR_WRITE   3
 
-/* P3 */
 #define UB_DIR_CHAR(c)  (((c)==UB_DIR_WRITE)? 'w': \
                         (((c)==UB_DIR_READ)? 'r': 'n'))
 
@@ -365,11 +360,12 @@ static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
 static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
     struct ub_scsi_cmd *cmd, struct ub_request *urq);
 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
-static void ub_end_rq(struct request *rq, int uptodate);
+static void ub_end_rq(struct request *rq, unsigned int status,
+    unsigned int cmd_len);
 static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
     struct ub_request *urq, struct ub_scsi_cmd *cmd);
 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
-static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
+static void ub_urb_complete(struct urb *urb);
 static void ub_scsi_action(unsigned long _dev);
 static void ub_scsi_dispatch(struct ub_dev *sc);
 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
@@ -383,7 +379,7 @@ static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
     int stalled_pipe);
 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
 static void ub_reset_enter(struct ub_dev *sc, int try);
-static void ub_reset_task(void *arg);
+static void ub_reset_task(struct work_struct *work);
 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
     struct ub_capacity *ret);
@@ -510,7 +506,7 @@ static void ub_cleanup(struct ub_dev *sc)
 {
        struct list_head *p;
        struct ub_lun *lun;
-       request_queue_t *q;
+       struct request_queue *q;
 
        while (!list_empty(&sc->luns)) {
                p = sc->luns.next;
@@ -536,6 +532,9 @@ static void ub_cleanup(struct ub_dev *sc)
                kfree(lun);
        }
 
+       usb_set_intfdata(sc->intf, NULL);
+       usb_put_intf(sc->intf);
+       usb_put_dev(sc->dev);
        kfree(sc);
 }
 
@@ -623,7 +622,7 @@ static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
  * The request function is our main entry point
  */
 
-static void ub_request_fn(request_queue_t *q)
+static void ub_request_fn(struct request_queue *q)
 {
        struct ub_lun *lun = q->queuedata;
        struct request *rq;
@@ -643,9 +642,15 @@ static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
        struct ub_request *urq;
        int n_elem;
 
-       if (atomic_read(&sc->poison) || lun->changed) {
+       if (atomic_read(&sc->poison)) {
                blkdev_dequeue_request(rq);
-               ub_end_rq(rq, 0);
+               ub_end_rq(rq, DID_NO_CONNECT << 16, blk_rq_bytes(rq));
+               return 0;
+       }
+
+       if (lun->changed && !blk_pc_request(rq)) {
+               blkdev_dequeue_request(rq);
+               ub_end_rq(rq, SAM_STAT_CHECK_CONDITION, blk_rq_bytes(rq));
                return 0;
        }
 
@@ -664,10 +669,12 @@ static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
        /*
         * get scatterlist from block layer
         */
+       sg_init_table(&urq->sgv[0], UB_MAX_REQ_SG);
        n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
        if (n_elem < 0) {
+               /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */
                printk(KERN_INFO "%s: failed request map (%d)\n",
-                   lun->name, n_elem); /* P3 */
+                   lun->name, n_elem);
                goto drop;
        }
        if (n_elem > UB_MAX_REQ_SG) {   /* Paranoia */
@@ -696,7 +703,7 @@ static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
 
 drop:
        ub_put_cmd(lun, cmd);
-       ub_end_rq(rq, 0);
+       ub_end_rq(rq, DID_ERROR << 16, blk_rq_bytes(rq));
        return 0;
 }
 
@@ -764,49 +771,73 @@ static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        struct ub_lun *lun = cmd->lun;
        struct ub_request *urq = cmd->back;
        struct request *rq;
-       int uptodate;
+       unsigned int scsi_status;
+       unsigned int cmd_len;
 
        rq = urq->rq;
 
        if (cmd->error == 0) {
-               uptodate = 1;
-
                if (blk_pc_request(rq)) {
                        if (cmd->act_len >= rq->data_len)
                                rq->data_len = 0;
                        else
                                rq->data_len -= cmd->act_len;
+                       scsi_status = 0;
+               } else {
+                       if (cmd->act_len != cmd->len) {
+                               if ((cmd->key == MEDIUM_ERROR ||
+                                    cmd->key == UNIT_ATTENTION) &&
+                                   ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
+                                       return;
+                               scsi_status = SAM_STAT_CHECK_CONDITION;
+                       } else {
+                               scsi_status = 0;
+                       }
                }
        } else {
-               uptodate = 0;
-
                if (blk_pc_request(rq)) {
                        /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
                        memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
                        rq->sense_len = UB_SENSE_SIZE;
                        if (sc->top_sense[0] != 0)
-                               rq->errors = SAM_STAT_CHECK_CONDITION;
+                               scsi_status = SAM_STAT_CHECK_CONDITION;
                        else
-                               rq->errors = DID_ERROR << 16;
+                               scsi_status = DID_ERROR << 16;
                } else {
                        if (cmd->error == -EIO) {
                                if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
                                        return;
                        }
+                       scsi_status = SAM_STAT_CHECK_CONDITION;
                }
        }
 
        urq->rq = NULL;
 
+       cmd_len = cmd->len;
        ub_put_cmd(lun, cmd);
-       ub_end_rq(rq, uptodate);
+       ub_end_rq(rq, scsi_status, cmd_len);
        blk_start_queue(lun->disk->queue);
 }
 
-static void ub_end_rq(struct request *rq, int uptodate)
+static void ub_end_rq(struct request *rq, unsigned int scsi_status,
+    unsigned int cmd_len)
 {
-       end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
-       end_that_request_last(rq, uptodate);
+       int error;
+       long rqlen;
+
+       if (scsi_status == 0) {
+               error = 0;
+       } else {
+               error = -EIO;
+               rq->errors = scsi_status;
+       }
+       rqlen = blk_rq_bytes(rq);    /* Oddly enough, this is the residue. */
+       if (__blk_end_request(rq, error, cmd_len)) {
+               printk(KERN_WARNING DRV_NAME
+                   ": __blk_end_request blew, %s-cmd total %u rqlen %ld\n",
+                   blk_pc_request(rq)? "pc": "fs", cmd_len, rqlen);
+       }
 }
 
 static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
@@ -821,7 +852,9 @@ static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
        if (urq->current_try >= 3)
                return -EIO;
        urq->current_try++;
-       /* P3 */ printk("%s: dir %c len/act %d/%d "
+
+       /* Remove this if anyone complains of flooding. */
+       printk(KERN_DEBUG "%s: dir %c len/act %d/%d "
            "[sense %x %02x %02x] retry %d\n",
            sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
            cmd->key, cmd->asc, cmd->ascq, urq->current_try);
@@ -909,11 +942,6 @@ static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
            bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
 
-       /* Fill what we shouldn't be filling, because usb-storage did so. */
-       sc->work_urb.actual_length = 0;
-       sc->work_urb.error_count = 0;
-       sc->work_urb.status = 0;
-
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
                /* XXX Clear stalls */
                ub_complete(&sc->work_done);
@@ -948,7 +976,7 @@ static void ub_urb_timeout(unsigned long arg)
  * the sc->lock taken) and from an interrupt (while we do NOT have
  * the sc->lock taken). Therefore, bounce this off to a tasklet.
  */
-static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
+static void ub_urb_complete(struct urb *urb)
 {
        struct ub_dev *sc = urb->context;
 
@@ -1238,8 +1266,6 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                         * to check. But it's not all right if the device
                         * counts disagree with our counts.
                         */
-                       /* P3 */ printk("%s: resid %d len %d act %d\n",
-                           sc->name, len, cmd->len, cmd->act_len);
                        goto Bad_End;
                }
 
@@ -1250,7 +1276,6 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                        ub_state_sense(sc, cmd);
                        return;
                case US_BULK_STAT_PHASE:
-                       /* P3 */ printk("%s: status PHASE\n", sc->name);
                        goto Bad_End;
                default:
                        printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
@@ -1301,12 +1326,8 @@ static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        else
                pipe = sc->send_bulk_pipe;
        sc->last_pipe = pipe;
-       usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
-           page_address(sg->page) + sg->offset, sg->length,
-           ub_urb_complete, sc);
-       sc->work_urb.actual_length = 0;
-       sc->work_urb.error_count = 0;
-       sc->work_urb.status = 0;
+       usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, sg_virt(sg),
+           sg->length, ub_urb_complete, sc);
 
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
                /* XXX Clear stalls */
@@ -1347,9 +1368,6 @@ static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        sc->last_pipe = sc->recv_bulk_pipe;
        usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
            &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
-       sc->work_urb.actual_length = 0;
-       sc->work_urb.error_count = 0;
-       sc->work_urb.status = 0;
 
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
                /* XXX Clear stalls */
@@ -1419,9 +1437,9 @@ static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        scmd->state = UB_CMDST_INIT;
        scmd->nsg = 1;
        sg = &scmd->sgv[0];
-       sg->page = virt_to_page(sc->top_sense);
-       sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1);
-       sg->length = UB_SENSE_SIZE;
+       sg_init_table(sg, UB_MAX_REQ_SG);
+       sg_set_page(sg, virt_to_page(sc->top_sense), UB_SENSE_SIZE,
+                       (unsigned long)sc->top_sense & (PAGE_SIZE-1));
        scmd->len = UB_SENSE_SIZE;
        scmd->lun = cmd->lun;
        scmd->done = ub_top_sense_done;
@@ -1464,9 +1482,6 @@ static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
 
        usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
            (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
-       sc->work_urb.actual_length = 0;
-       sc->work_urb.error_count = 0;
-       sc->work_urb.status = 0;
 
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
                ub_complete(&sc->work_done);
@@ -1539,10 +1554,8 @@ static void ub_reset_enter(struct ub_dev *sc, int try)
 #endif
 
 #if 0 /* We let them stop themselves. */
-       struct list_head *p;
        struct ub_lun *lun;
-       list_for_each(p, &sc->luns) {
-               lun = list_entry(p, struct ub_lun, link);
+       list_for_each_entry(lun, &sc->luns, link) {
                blk_stop_queue(lun->disk->queue);
        }
 #endif
@@ -1550,11 +1563,10 @@ static void ub_reset_enter(struct ub_dev *sc, int try)
        schedule_work(&sc->reset_work);
 }
 
-static void ub_reset_task(void *arg)
+static void ub_reset_task(struct work_struct *work)
 {
-       struct ub_dev *sc = arg;
+       struct ub_dev *sc = container_of(work, struct ub_dev, reset_work);
        unsigned long flags;
-       struct list_head *p;
        struct ub_lun *lun;
        int lkr, rc;
 
@@ -1565,16 +1577,14 @@ static void ub_reset_task(void *arg)
        }
 
        if (atomic_read(&sc->poison)) {
-               printk(KERN_NOTICE "%s: Not resetting disconnected device\n",
-                   sc->name); /* P3 This floods. Remove soon. XXX */
+               ;
        } else if ((sc->reset & 1) == 0) {
                ub_sync_reset(sc);
                msleep(700);    /* usb-storage sleeps 6s (!) */
                ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
                ub_probe_clear_stall(sc, sc->send_bulk_pipe);
        } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
-               printk(KERN_NOTICE "%s: Not resetting multi-interface device\n",
-                   sc->name); /* P3 This floods. Remove soon. XXX */
+               ;
        } else {
                if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
                        printk(KERN_NOTICE
@@ -1602,8 +1612,7 @@ static void ub_reset_task(void *arg)
        spin_lock_irqsave(sc->lock, flags);
        sc->reset = 0;
        tasklet_schedule(&sc->tasklet);
-       list_for_each(p, &sc->luns) {
-               lun = list_entry(p, struct ub_lun, link);
+       list_for_each_entry(lun, &sc->luns, link) {
                blk_start_queue(lun->disk->queue);
        }
        wake_up(&sc->reset_wait);
@@ -1648,15 +1657,11 @@ static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
 static int ub_bd_open(struct inode *inode, struct file *filp)
 {
        struct gendisk *disk = inode->i_bdev->bd_disk;
-       struct ub_lun *lun;
-       struct ub_dev *sc;
+       struct ub_lun *lun = disk->private_data;
+       struct ub_dev *sc = lun->udev;
        unsigned long flags;
        int rc;
 
-       if ((lun = disk->private_data) == NULL)
-               return -ENXIO;
-       sc = lun->udev;
-
        spin_lock_irqsave(&ub_lock, flags);
        if (atomic_read(&sc->poison)) {
                spin_unlock_irqrestore(&ub_lock, flags);
@@ -1711,7 +1716,7 @@ static int ub_bd_ioctl(struct inode *inode, struct file *filp,
        struct gendisk *disk = inode->i_bdev->bd_disk;
        void __user *usermem = (void __user *) arg;
 
-       return scsi_cmd_ioctl(filp, disk, cmd, usermem);
+       return scsi_cmd_ioctl(filp, disk->queue, disk, cmd, usermem);
 }
 
 /*
@@ -1820,10 +1825,8 @@ static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
        rc = ub_submit_scsi(sc, cmd);
        spin_unlock_irqrestore(sc->lock, flags);
 
-       if (rc != 0) {
-               printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
+       if (rc != 0)
                goto err_submit;
-       }
 
        wait_for_completion(&compl);
 
@@ -1867,9 +1870,8 @@ static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
        cmd->state = UB_CMDST_INIT;
        cmd->nsg = 1;
        sg = &cmd->sgv[0];
-       sg->page = virt_to_page(p);
-       sg->offset = (unsigned long)p & (PAGE_SIZE-1);
-       sg->length = 8;
+       sg_init_table(sg, UB_MAX_REQ_SG);
+       sg_set_page(sg, virt_to_page(p), 8, (unsigned long)p & (PAGE_SIZE-1));
        cmd->len = 8;
        cmd->lun = lun;
        cmd->done = ub_probe_done;
@@ -1881,20 +1883,16 @@ static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
        rc = ub_submit_scsi(sc, cmd);
        spin_unlock_irqrestore(sc->lock, flags);
 
-       if (rc != 0) {
-               printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
+       if (rc != 0)
                goto err_submit;
-       }
 
        wait_for_completion(&compl);
 
        if (cmd->error != 0) {
-               printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
                rc = -EIO;
                goto err_read;
        }
        if (cmd->act_len != 8) {
-               printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
                rc = -EIO;
                goto err_read;
        }
@@ -1908,7 +1906,6 @@ static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
        case 2048:      shift = 2;      break;
        case 4096:      shift = 3;      break;
        default:
-               printk("ub: Bad sector size %u\n", bsize); /* P3 */
                rc = -EDOM;
                goto err_inv_bsize;
        }
@@ -1928,7 +1925,7 @@ err_alloc:
 
 /*
  */
-static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
+static void ub_probe_urb_complete(struct urb *urb)
 {
        struct completion *cop = urb->context;
        complete(cop);
@@ -1962,9 +1959,6 @@ static int ub_sync_reset(struct ub_dev *sc)
 
        usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
            (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
-       sc->work_urb.actual_length = 0;
-       sc->work_urb.error_count = 0;
-       sc->work_urb.status = 0;
 
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
                printk(KERN_WARNING
@@ -2016,21 +2010,9 @@ static int ub_sync_getmaxlun(struct ub_dev *sc)
 
        usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
            (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
-       sc->work_urb.actual_length = 0;
-       sc->work_urb.error_count = 0;
-       sc->work_urb.status = 0;
 
-       if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
-               if (rc == -EPIPE) {
-                       printk("%s: Stall submitting GetMaxLUN, using 1 LUN\n",
-                            sc->name); /* P3 */
-               } else {
-                       printk(KERN_NOTICE
-                            "%s: Unable to submit GetMaxLUN (%d)\n",
-                            sc->name, rc);
-               }
+       if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0)
                goto err_submit;
-       }
 
        init_timer(&timer);
        timer.function = ub_probe_timeout;
@@ -2043,21 +2025,10 @@ static int ub_sync_getmaxlun(struct ub_dev *sc)
        del_timer_sync(&timer);
        usb_kill_urb(&sc->work_urb);
 
-       if ((rc = sc->work_urb.status) < 0) {
-               if (rc == -EPIPE) {
-                       printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
-                            sc->name); /* P3 */
-               } else {
-                       printk(KERN_NOTICE
-                            "%s: Error at GetMaxLUN (%d)\n",
-                            sc->name, rc);
-               }
+       if ((rc = sc->work_urb.status) < 0)
                goto err_io;
-       }
 
        if (sc->work_urb.actual_length != 1) {
-               printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
-                   sc->work_urb.actual_length); /* P3 */
                nluns = 0;
        } else {
                if ((nluns = *p) == 55) {
@@ -2068,8 +2039,6 @@ static int ub_sync_getmaxlun(struct ub_dev *sc)
                        if (nluns > UB_MAX_LUNS)
                                nluns = UB_MAX_LUNS;
                }
-               printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
-                   *p, nluns); /* P3 */
        }
 
        kfree(p);
@@ -2108,9 +2077,6 @@ static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
 
        usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
            (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
-       sc->work_urb.actual_length = 0;
-       sc->work_urb.error_count = 0;
-       sc->work_urb.status = 0;
 
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
                printk(KERN_WARNING
@@ -2159,10 +2125,13 @@ static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
                if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
                                == USB_ENDPOINT_XFER_BULK) {
                        /* BULK in or out? */
-                       if (ep->bEndpointAddress & USB_DIR_IN)
-                               ep_in = ep;
-                       else
-                               ep_out = ep;
+                       if (ep->bEndpointAddress & USB_DIR_IN) {
+                               if (ep_in == NULL)
+                                       ep_in = ep;
+                       } else {
+                               if (ep_out == NULL)
+                                       ep_out = ep;
+                       }
                }
        }
 
@@ -2206,7 +2175,7 @@ static int ub_probe(struct usb_interface *intf,
        usb_init_urb(&sc->work_urb);
        tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
        atomic_set(&sc->poison, 0);
-       INIT_WORK(&sc->reset_work, ub_reset_task, sc);
+       INIT_WORK(&sc->reset_work, ub_reset_task);
        init_waitqueue_head(&sc->reset_wait);
 
        init_timer(&sc->work_timer);
@@ -2221,7 +2190,12 @@ static int ub_probe(struct usb_interface *intf,
        // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
        usb_set_intfdata(intf, sc);
        usb_get_dev(sc->dev);
-       // usb_get_intf(sc->intf);      /* Do we need this? */
+       /*
+        * Since we give the interface struct to the block level through
+        * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
+        * oopses on close after a disconnect (kernels 2.6.16 and up).
+        */
+       usb_get_intf(sc->intf);
 
        snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
            sc->dev->bus->busnum, sc->dev->devnum);
@@ -2262,7 +2236,7 @@ static int ub_probe(struct usb_interface *intf,
         * has to succeed, so we clear checks with an additional one here.
         * In any case it's not our business how revaliadation is implemented.
         */
-       for (i = 0; i < 3; i++) {       /* Retries for benh's key */
+       for (i = 0; i < 3; i++) {  /* Retries for the schwag key from KS'04 */
                if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
                if (rc != 0x6) break;
                msleep(10);
@@ -2286,7 +2260,7 @@ static int ub_probe(struct usb_interface *intf,
 
 err_dev_desc:
        usb_set_intfdata(intf, NULL);
-       // usb_put_intf(sc->intf);
+       usb_put_intf(sc->intf);
        usb_put_dev(sc->dev);
        kfree(sc);
 err_core:
@@ -2296,7 +2270,7 @@ err_core:
 static int ub_probe_lun(struct ub_dev *sc, int lnum)
 {
        struct ub_lun *lun;
-       request_queue_t *q;
+       struct request_queue *q;
        struct gendisk *disk;
        int rc;
 
@@ -2310,7 +2284,6 @@ static int ub_probe_lun(struct ub_dev *sc, int lnum)
                goto err_id;
 
        lun->udev = sc;
-       list_add(&lun->link, &sc->luns);
 
        snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
            lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
@@ -2323,9 +2296,7 @@ static int ub_probe_lun(struct ub_dev *sc, int lnum)
        if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
                goto err_diskalloc;
 
-       lun->disk = disk;
        sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
-       sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
        disk->major = UB_MAJOR;
        disk->first_minor = lun->id * UB_PARTS_PER_LUN;
        disk->fops = &ub_bd_fops;
@@ -2345,7 +2316,9 @@ static int ub_probe_lun(struct ub_dev *sc, int lnum)
        blk_queue_max_sectors(q, UB_MAX_SECTORS);
        blk_queue_hardsect_size(q, lun->capacity.bsize);
 
+       lun->disk = disk;
        q->queuedata = lun;
+       list_add(&lun->link, &sc->luns);
 
        set_capacity(disk, lun->capacity.nsec);
        if (lun->removable)
@@ -2358,7 +2331,6 @@ static int ub_probe_lun(struct ub_dev *sc, int lnum)
 err_blkqinit:
        put_disk(disk);
 err_diskalloc:
-       list_del(&lun->link);
        ub_id_put(lun->id);
 err_id:
        kfree(lun);
@@ -2369,9 +2341,7 @@ err_alloc:
 static void ub_disconnect(struct usb_interface *intf)
 {
        struct ub_dev *sc = usb_get_intfdata(intf);
-       struct list_head *p;
        struct ub_lun *lun;
-       struct gendisk *disk;
        unsigned long flags;
 
        /*
@@ -2425,14 +2395,11 @@ static void ub_disconnect(struct usb_interface *intf)
        /*
         * Unregister the upper layer.
         */
-       list_for_each (p, &sc->luns) {
-               lun = list_entry(p, struct ub_lun, link);
-               disk = lun->disk;
-               if (disk->flags & GENHD_FL_UP)
-                       del_gendisk(disk);
+       list_for_each_entry(lun, &sc->luns, link) {
+               del_gendisk(lun->disk);
                /*
                 * I wish I could do:
-                *    set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
+                *    queue_flag_set(QUEUE_FLAG_DEAD, q);
                 * As it is, we rely on our internal poisoning and let
                 * the upper levels to spin furiously failing all the I/O.
                 */
@@ -2461,12 +2428,6 @@ static void ub_disconnect(struct usb_interface *intf)
         * and no URBs left in transit.
         */
 
-       usb_set_intfdata(intf, NULL);
-       // usb_put_intf(sc->intf);
-       sc->intf = NULL;
-       usb_put_dev(sc->dev);
-       sc->dev = NULL;
-
        ub_put(sc);
 }
 
@@ -2487,7 +2448,6 @@ static int __init ub_init(void)
 
        if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
                goto err_regblkdev;
-       devfs_mk_dir(DEVFS_NAME);
 
        if ((rc = usb_register(&ub_driver)) != 0)
                goto err_register;
@@ -2496,7 +2456,6 @@ static int __init ub_init(void)
        return 0;
 
 err_register:
-       devfs_remove(DEVFS_NAME);
        unregister_blkdev(UB_MAJOR, DRV_NAME);
 err_regblkdev:
        return rc;
@@ -2506,7 +2465,6 @@ static void __exit ub_exit(void)
 {
        usb_deregister(&ub_driver);
 
-       devfs_remove(DEVFS_NAME);
        unregister_blkdev(UB_MAJOR, DRV_NAME);
        usb_usual_clear_present(USB_US_TYPE_UB);
 }