/* * The low performance USB storage driver (ub). * * Copyright (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net) * Copyright (C) 2004 Pete Zaitcev (zaitcev@yahoo.com) * * This work is a part of Linux kernel, is derived from it, * and is not licensed separately. See file COPYING for details. * * TODO (sorted by decreasing priority) * -- 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) * -- verify the 13 conditions and do bulk resets * -- 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 * -- Resove XXX's * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring. */ #include #include #include #include #include #include #include #define DRV_NAME "ub" #define UB_MAJOR 180 /* * The command state machine is the key model for understanding of this driver. * * The general rule is that all transitions are done towards the bottom * of the diagram, thus preventing any loops. * * An exception to that is how the STAT state is handled. A counter allows it * to be re-entered along the path marked with [C]. * * +--------+ * ! INIT ! * +--------+ * ! * ub_scsi_cmd_start fails ->--------------------------------------\ * ! ! * V ! * +--------+ ! * ! CMD ! ! * +--------+ ! * ! +--------+ ! * was -EPIPE -->-------------------------------->! CLEAR ! ! * ! +--------+ ! * ! ! ! * was error -->------------------------------------- ! --------->\ * ! ! ! * /--<-- cmd->dir == NONE ? ! ! * ! ! ! ! * ! V ! ! * ! +--------+ ! ! * ! ! DATA ! ! ! * ! +--------+ ! ! * ! ! +---------+ ! ! * ! was -EPIPE -->--------------->! CLR2STS ! ! ! * ! ! +---------+ ! ! * ! ! ! ! ! * ! ! was error -->---- ! --------->\ * ! was error -->--------------------- ! ------------- ! --------->\ * ! ! ! ! ! * ! V ! ! ! * \--->+--------+ ! ! ! * ! STAT !<--------------------------/ ! ! * /--->+--------+ ! ! * ! ! ! ! * [C] was -EPIPE -->-----------\ ! ! * ! ! ! ! ! * +<---- len == 0 ! ! ! * ! ! ! ! ! * ! was error -->--------------------------------------!---------->\ * ! ! ! ! ! * +<---- bad CSW ! ! ! * +<---- bad tag ! ! ! * ! ! V ! ! * ! ! +--------+ ! ! * ! ! ! CLRRS ! ! ! * ! ! +--------+ ! ! * ! ! ! ! ! * \------- ! --------------------[C]--------\ ! ! * ! ! ! ! * cmd->error---\ +--------+ ! ! * ! +--------------->! SENSE !<----------/ ! * STAT_FAIL----/ +--------+ ! * ! ! V * ! V +--------+ * \--------------------------------\--------------------->! DONE ! * +--------+ */ /* * This many LUNs per USB device. * Every one of them takes a host, see UB_MAX_HOSTS. */ #define UB_MAX_LUNS 9 /* */ #define UB_PARTS_PER_LUN 8 #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */ #define UB_SENSE_SIZE 18 /* */ /* command block wrapper */ struct bulk_cb_wrap { __le32 Signature; /* contains 'USBC' */ u32 Tag; /* unique per command id */ __le32 DataTransferLength; /* size of data */ u8 Flags; /* direction in bit 0 */ u8 Lun; /* LUN */ u8 Length; /* of of the CDB */ u8 CDB[UB_MAX_CDB_SIZE]; /* max command */ }; #define US_BULK_CB_WRAP_LEN 31 #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */ #define US_BULK_FLAG_IN 1 #define US_BULK_FLAG_OUT 0 /* command status wrapper */ struct bulk_cs_wrap { __le32 Signature; /* should = 'USBS' */ u32 Tag; /* same as original command */ __le32 Residue; /* amount not transferred */ u8 Status; /* see below */ }; #define US_BULK_CS_WRAP_LEN 13 #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */ #define US_BULK_STAT_OK 0 #define US_BULK_STAT_FAIL 1 #define US_BULK_STAT_PHASE 2 /* bulk-only class specific requests */ #define US_BULK_RESET_REQUEST 0xff #define US_BULK_GET_MAX_LUN 0xfe /* */ struct ub_dev; #define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */ #define UB_MAX_SECTORS 64 /* * A second is more than enough for a 32K transfer (UB_MAX_SECTORS) * even if a webcam hogs the bus, but some devices need time to spin up. */ #define UB_URB_TIMEOUT (HZ*2) #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */ #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */ #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */ /* * An instance of a SCSI command in transit. */ #define UB_DIR_NONE 0 #define UB_DIR_READ 1 #define UB_DIR_ILLEGAL2 2 #define UB_DIR_WRITE 3 #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \ (((c)==UB_DIR_READ)? 'r': 'n')) enum ub_scsi_cmd_state { UB_CMDST_INIT, /* Initial state */ UB_CMDST_CMD, /* Command submitted */ UB_CMDST_DATA, /* Data phase */ UB_CMDST_CLR2STS, /* Clearing before requesting status */ UB_CMDST_STAT, /* Status phase */ UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */ UB_CMDST_CLRRS, /* Clearing before retrying status */ UB_CMDST_SENSE, /* Sending Request Sense */ UB_CMDST_DONE /* Final state */ }; struct ub_scsi_cmd { unsigned char cdb[UB_MAX_CDB_SIZE]; unsigned char cdb_len; unsigned char dir; /* 0 - none, 1 - read, 3 - write. */ enum ub_scsi_cmd_state state; unsigned int tag; struct ub_scsi_cmd *next; int error; /* Return code - valid upon done */ unsigned int act_len; /* Return size */ unsigned char key, asc, ascq; /* May be valid if error==-EIO */ int stat_count; /* Retries getting status. */ unsigned int len; /* Requested length */ unsigned int current_sg; unsigned int nsg; /* sgv[nsg] */ struct scatterlist sgv[UB_MAX_REQ_SG]; struct ub_lun *lun; void (*done)(struct ub_dev *, struct ub_scsi_cmd *); void *back; }; struct ub_request { struct request *rq; unsigned int current_try; unsigned int nsg; /* sgv[nsg] */ struct scatterlist sgv[UB_MAX_REQ_SG]; }; /* */ struct ub_capacity { unsigned long nsec; /* Linux size - 512 byte sectors */ unsigned int bsize; /* Linux hardsect_size */ unsigned int bshift; /* Shift between 512 and hard sects */ }; /* * This is a direct take-off from linux/include/completion.h * The difference is that I do not wait on this thing, just poll. * When I want to wait (ub_probe), I just use the stock completion. * * Note that INIT_COMPLETION takes no lock. It is correct. But why * in the bloody hell that thing takes struct instead of pointer to struct * is quite beyond me. I just copied it from the stock completion. */ struct ub_completion { unsigned int done; spinlock_t lock; }; static inline void ub_init_completion(struct ub_completion *x) { x->done = 0; spin_lock_init(&x->lock); } #define UB_INIT_COMPLETION(x) ((x).done = 0) static void ub_complete(struct ub_completion *x) { unsigned long flags; spin_lock_irqsave(&x->lock, flags); x->done++; spin_unlock_irqrestore(&x->lock, flags); } static int ub_is_completed(struct ub_completion *x) { unsigned long flags; int ret; spin_lock_irqsave(&x->lock, flags); ret = x->done; spin_unlock_irqrestore(&x->lock, flags); return ret; } /* */ struct ub_scsi_cmd_queue { int qlen, qmax; struct ub_scsi_cmd *head, *tail; }; /* * The block device instance (one per LUN). */ struct ub_lun { struct ub_dev *udev; struct list_head link; struct gendisk *disk; int id; /* Host index */ int num; /* LUN number */ char name[16]; int changed; /* Media was changed */ int removable; int readonly; struct ub_request urq; /* Use Ingo's mempool if or when we have more than one command. */ /* * Currently we never need more than one command for the whole device. * However, giving every LUN a command is a cheap and automatic way * to enforce fairness between them. */ int cmda[1]; struct ub_scsi_cmd cmdv[1]; struct ub_capacity capacity; }; /* * The USB device instance. */ struct ub_dev { spinlock_t *lock; atomic_t poison; /* The USB device is disconnected */ int openc; /* protected by ub_lock! */ /* kref is too implicit for our taste */ int reset; /* Reset is running */ unsigned int tagcnt; char name[12]; struct usb_device *dev; struct usb_interface *intf; struct list_head luns; unsigned int send_bulk_pipe; /* cached pipe values */ unsigned int recv_bulk_pipe; unsigned int send_ctrl_pipe; unsigned int recv_ctrl_pipe; struct tasklet_struct tasklet; struct ub_scsi_cmd_queue cmd_queue; struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */ unsigned char top_sense[UB_SENSE_SIZE]; struct ub_completion work_done; struct urb work_urb; struct timer_list work_timer; int last_pipe; /* What might need clearing */ __le32 signature; /* Learned signature */ struct bulk_cb_wrap work_bcb; struct bulk_cs_wrap work_bcs; struct usb_ctrlrequest work_cr; struct work_struct reset_work; wait_queue_head_t reset_wait; int sg_stat[6]; }; /* */ static void ub_cleanup(struct ub_dev *sc); static int ub_request_fn_1(struct ub_lun *lun, struct request *rq); static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, struct ub_scsi_cmd *cmd, struct ub_request *urq); 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, unsigned int status); 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); 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); static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd); static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc); static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd); static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd); static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd); static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd); 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 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); static int ub_sync_reset(struct ub_dev *sc); static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe); static int ub_probe_lun(struct ub_dev *sc, int lnum); /* */ #ifdef CONFIG_USB_LIBUSUAL #define ub_usb_ids storage_usb_ids #else static struct usb_device_id ub_usb_ids[] = { { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) }, { } }; MODULE_DEVICE_TABLE(usb, ub_usb_ids); #endif /* CONFIG_USB_LIBUSUAL */ /* * Find me a way to identify "next free minor" for add_disk(), * and the array disappears the next day. However, the number of * hosts has something to do with the naming and /proc/partitions. * This has to be thought out in detail before changing. * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure. */ #define UB_MAX_HOSTS 26 static char ub_hostv[UB_MAX_HOSTS]; #define UB_QLOCK_NUM 5 static spinlock_t ub_qlockv[UB_QLOCK_NUM]; static int ub_qlock_next = 0; static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */ /* * The id allocator. * * This also stores the host for indexing by minor, which is somewhat dirty. */ static int ub_id_get(void) { unsigned long flags; int i; spin_lock_irqsave(&ub_lock, flags); for (i = 0; i < UB_MAX_HOSTS; i++) { if (ub_hostv[i] == 0) { ub_hostv[i] = 1; spin_unlock_irqrestore(&ub_lock, flags); return i; } } spin_unlock_irqrestore(&ub_lock, flags); return -1; } static void ub_id_put(int id) { unsigned long flags; if (id < 0 || id >= UB_MAX_HOSTS) { printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id); return; } spin_lock_irqsave(&ub_lock, flags); if (ub_hostv[id] == 0) { spin_unlock_irqrestore(&ub_lock, flags); printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id); return; } ub_hostv[id] = 0; spin_unlock_irqrestore(&ub_lock, flags); } /* * This is necessitated by the fact that blk_cleanup_queue does not * necesserily destroy the queue. Instead, it may merely decrease q->refcnt. * Since our blk_init_queue() passes a spinlock common with ub_dev, * we have life time issues when ub_cleanup frees ub_dev. */ static spinlock_t *ub_next_lock(void) { unsigned long flags; spinlock_t *ret; spin_lock_irqsave(&ub_lock, flags); ret = &ub_qlockv[ub_qlock_next]; ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM; spin_unlock_irqrestore(&ub_lock, flags); return ret; } /* * Downcount for deallocation. This rides on two assumptions: * - once something is poisoned, its refcount cannot grow * - opens cannot happen at this time (del_gendisk was done) * If the above is true, we can drop the lock, which we need for * blk_cleanup_queue(): the silly thing may attempt to sleep. * [Actually, it never needs to sleep for us, but it calls might_sleep()] */ static void ub_put(struct ub_dev *sc) { unsigned long flags; spin_lock_irqsave(&ub_lock, flags); --sc->openc; if (sc->openc == 0 && atomic_read(&sc->poison)) { spin_unlock_irqrestore(&ub_lock, flags); ub_cleanup(sc); } else { spin_unlock_irqrestore(&ub_lock, flags); } } /* * Final cleanup and deallocation. */ static void ub_cleanup(struct ub_dev *sc) { struct list_head *p; struct ub_lun *lun; request_queue_t *q; while (!list_empty(&sc->luns)) { p = sc->luns.next; lun = list_entry(p, struct ub_lun, link); list_del(p); /* I don't think queue can be NULL. But... Stolen from sx8.c */ if ((q = lun->disk->queue) != NULL) blk_cleanup_queue(q); /* * If we zero disk->private_data BEFORE put_disk, we have * to check for NULL all over the place in open, release, * check_media and revalidate, because the block level * semaphore is well inside the put_disk. * But we cannot zero after the call, because *disk is gone. * The sd.c is blatantly racy in this area. */ /* disk->private_data = NULL; */ put_disk(lun->disk); lun->disk = NULL; ub_id_put(lun->id); kfree(lun); } usb_set_intfdata(sc->intf, NULL); usb_put_intf(sc->intf); usb_put_dev(sc->dev); kfree(sc); } /* * The "command allocator". */ static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun) { struct ub_scsi_cmd *ret; if (lun->cmda[0]) return NULL; ret = &lun->cmdv[0]; lun->cmda[0] = 1; return ret; } static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd) { if (cmd != &lun->cmdv[0]) { printk(KERN_WARNING "%s: releasing a foreign cmd %p\n", lun->name, cmd); return; } if (!lun->cmda[0]) { printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name); return; } lun->cmda[0] = 0; } /* * The command queue. */ static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd) { struct ub_scsi_cmd_queue *t = &sc->cmd_queue; if (t->qlen++ == 0) { t->head = cmd; t->tail = cmd; } else { t->tail->next = cmd; t->tail = cmd; } if (t->qlen > t->qmax) t->qmax = t->qlen; } static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd) { struct ub_scsi_cmd_queue *t = &sc->cmd_queue; if (t->qlen++ == 0) { t->head = cmd; t->tail = cmd; } else { cmd->next = t->head; t->head = cmd; } if (t->qlen > t->qmax) t->qmax = t->qlen; } static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc) { struct ub_scsi_cmd_queue *t = &sc->cmd_queue; struct ub_scsi_cmd *cmd; if (t->qlen == 0) return NULL; if (--t->qlen == 0) t->tail = NULL; cmd = t->head; t->head = cmd->next; cmd->next = NULL; return cmd; } #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head) /* * The request function is our main entry point */ static void ub_request_fn(request_queue_t *q) { struct ub_lun *lun = q->queuedata; struct request *rq; while ((rq = elv_next_request(q)) != NULL) { if (ub_request_fn_1(lun, rq) != 0) { blk_stop_queue(q); break; } } } static int ub_request_fn_1(struct ub_lun *lun, struct request *rq) { struct ub_dev *sc = lun->udev; struct ub_scsi_cmd *cmd; struct ub_request *urq; int n_elem; if (atomic_read(&sc->poison)) { blkdev_dequeue_request(rq); ub_end_rq(rq, DID_NO_CONNECT << 16); return 0; } if (lun->changed && !blk_pc_request(rq)) { blkdev_dequeue_request(rq); ub_end_rq(rq, SAM_STAT_CHECK_CONDITION); return 0; } if (lun->urq.rq != NULL) return -1; if ((cmd = ub_get_cmd(lun)) == NULL) return -1; memset(cmd, 0, sizeof(struct ub_scsi_cmd)); blkdev_dequeue_request(rq); urq = &lun->urq; memset(urq, 0, sizeof(struct ub_request)); urq->rq = rq; /* * get scatterlist from block layer */ 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); goto drop; } if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */ printk(KERN_WARNING "%s: request with %d segments\n", lun->name, n_elem); goto drop; } urq->nsg = n_elem; sc->sg_stat[n_elem < 5 ? n_elem : 5]++; if (blk_pc_request(rq)) { ub_cmd_build_packet(sc, lun, cmd, urq); } else { ub_cmd_build_block(sc, lun, cmd, urq); } cmd->state = UB_CMDST_INIT; cmd->lun = lun; cmd->done = ub_rw_cmd_done; cmd->back = urq; cmd->tag = sc->tagcnt++; if (ub_submit_scsi(sc, cmd) != 0) goto drop; return 0; drop: ub_put_cmd(lun, cmd); ub_end_rq(rq, DID_ERROR << 16); return 0; } static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, struct ub_scsi_cmd *cmd, struct ub_request *urq) { struct request *rq = urq->rq; unsigned int block, nblks; if (rq_data_dir(rq) == WRITE) cmd->dir = UB_DIR_WRITE; else cmd->dir = UB_DIR_READ; cmd->nsg = urq->nsg; memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg); /* * build the command * * The call to blk_queue_hardsect_size() guarantees that request * is aligned, but it is given in terms of 512 byte units, always. */ block = rq->sector >> lun->capacity.bshift; nblks = rq->nr_sectors >> lun->capacity.bshift; cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10; /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */ cmd->cdb[2] = block >> 24; cmd->cdb[3] = block >> 16; cmd->cdb[4] = block >> 8; cmd->cdb[5] = block; cmd->cdb[7] = nblks >> 8; cmd->cdb[8] = nblks; cmd->cdb_len = 10; cmd->len = rq->nr_sectors * 512; } static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun, struct ub_scsi_cmd *cmd, struct ub_request *urq) { struct request *rq = urq->rq; if (rq->data_len == 0) { cmd->dir = UB_DIR_NONE; } else { if (rq_data_dir(rq) == WRITE) cmd->dir = UB_DIR_WRITE; else cmd->dir = UB_DIR_READ; } cmd->nsg = urq->nsg; memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg); memcpy(&cmd->cdb, rq->cmd, rq->cmd_len); cmd->cdb_len = rq->cmd_len; cmd->len = rq->data_len; } 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; unsigned int scsi_status; rq = urq->rq; if (cmd->error == 0) { 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 (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) scsi_status = SAM_STAT_CHECK_CONDITION; else 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; ub_put_cmd(lun, cmd); ub_end_rq(rq, scsi_status); blk_start_queue(lun->disk->queue); } static void ub_end_rq(struct request *rq, unsigned int scsi_status) { int uptodate; if (scsi_status == 0) { uptodate = 1; } else { uptodate = 0; rq->errors = scsi_status; } end_that_request_first(rq, uptodate, rq->hard_nr_sectors); end_that_request_last(rq, uptodate); } static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun, struct ub_request *urq, struct ub_scsi_cmd *cmd) { if (atomic_read(&sc->poison)) return -ENXIO; ub_reset_enter(sc, urq->current_try); if (urq->current_try >= 3) return -EIO; urq->current_try++; /* 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); memset(cmd, 0, sizeof(struct ub_scsi_cmd)); ub_cmd_build_block(sc, lun, cmd, urq); cmd->state = UB_CMDST_INIT; cmd->lun = lun; cmd->done = ub_rw_cmd_done; cmd->back = urq; cmd->tag = sc->tagcnt++; #if 0 /* Wasteful */ return ub_submit_scsi(sc, cmd); #else ub_cmdq_add(sc, cmd); return 0; #endif } /* * Submit a regular SCSI operation (not an auto-sense). * * The Iron Law of Good Submit Routine is: * Zero return - callback is done, Nonzero return - callback is not done. * No exceptions. * * Host is assumed locked. */ static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd) { if (cmd->state != UB_CMDST_INIT || (cmd->dir != UB_DIR_NONE && cmd->len == 0)) { return -EINVAL; } ub_cmdq_add(sc, cmd); /* * We can call ub_scsi_dispatch(sc) right away here, but it's a little * safer to jump to a tasklet, in case upper layers do something silly. */ tasklet_schedule(&sc->tasklet); return 0; } /* * Submit the first URB for the queued command. * This function does not deal with queueing in any way. */ static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd) { struct bulk_cb_wrap *bcb; int rc; bcb = &sc->work_bcb; /* * ``If the allocation length is eighteen or greater, and a device * server returns less than eithteen bytes of data, the application * client should assume that the bytes not transferred would have been * zeroes had the device server returned those bytes.'' * * We zero sense for all commands so that when a packet request * fails it does not return a stale sense. */ memset(&sc->top_sense, 0, UB_SENSE_SIZE); /* set up the command wrapper */ bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); bcb->Tag = cmd->tag; /* Endianness is not important */ bcb->DataTransferLength = cpu_to_le32(cmd->len); bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0; bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0; bcb->Length = cmd->cdb_len; /* copy the command payload */ memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE); UB_INIT_COMPLETION(sc->work_done); sc->last_pipe = sc->send_bulk_pipe; 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); return rc; } sc->work_timer.expires = jiffies + UB_URB_TIMEOUT; add_timer(&sc->work_timer); cmd->state = UB_CMDST_CMD; return 0; } /* * Timeout handler. */ static void ub_urb_timeout(unsigned long arg) { struct ub_dev *sc = (struct ub_dev *) arg; unsigned long flags; spin_lock_irqsave(sc->lock, flags); if (!ub_is_completed(&sc->work_done)) usb_unlink_urb(&sc->work_urb); spin_unlock_irqrestore(sc->lock, flags); } /* * Completion routine for the work URB. * * This can be called directly from usb_submit_urb (while we have * 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 ub_dev *sc = urb->context; ub_complete(&sc->work_done); tasklet_schedule(&sc->tasklet); } static void ub_scsi_action(unsigned long _dev) { struct ub_dev *sc = (struct ub_dev *) _dev; unsigned long flags; spin_lock_irqsave(sc->lock, flags); ub_scsi_dispatch(sc); spin_unlock_irqrestore(sc->lock, flags); } static void ub_scsi_dispatch(struct ub_dev *sc) { struct ub_scsi_cmd *cmd; int rc; while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) { if (cmd->state == UB_CMDST_DONE) { ub_cmdq_pop(sc); (*cmd->done)(sc, cmd); } else if (cmd->state == UB_CMDST_INIT) { if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0) break; cmd->error = rc; cmd->state = UB_CMDST_DONE; } else { if (!ub_is_completed(&sc->work_done)) break; del_timer(&sc->work_timer); ub_scsi_urb_compl(sc, cmd); } } } static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd) { struct urb *urb = &sc->work_urb; struct bulk_cs_wrap *bcs; int len; int rc; if (atomic_read(&sc->poison)) { ub_state_done(sc, cmd, -ENODEV); return; } if (cmd->state == UB_CMDST_CLEAR) { if (urb->status == -EPIPE) { /* * STALL while clearning STALL. * The control pipe clears itself - nothing to do. */ printk(KERN_NOTICE "%s: stall on control pipe\n", sc->name); goto Bad_End; } How to Get Your Change Into the Linux Kernel or Care And Operation Of Your Linus Torvalds For a person or company who wishes to submit a change to the Linux kernel, the process can sometimes be daunting if you're not familiar with "the system." This text is a collection of suggestions which can greatly increase the chances of your change being accepted. Read Documentation/SubmitChecklist for a list of items to check before submitting code. If you are submitting a driver, also read Documentation/SubmittingDrivers. -------------------------------------------- SECTION 1 - CREATING AND SENDING YOUR CHANGE -------------------------------------------- 1) "diff -up" ------------ Use "diff -up" or "diff -uprN" to create patches. All changes to the Linux kernel occur in the form of patches, as generated by diff(1). When creating your patch, make sure to create it in "unified diff" format, as supplied by the '-u' argument to diff(1). Also, please use the '-p' argument which shows which C function each change is in - that makes the resultant diff a lot easier to read. Patches should be based in the root kernel source directory, not in any lower subdirectory. To create a patch for a single file, it is often sufficient to do: SRCTREE= linux-2.6 MYFILE= drivers/net/mydriver.c cd $SRCTREE cp $MYFILE $MYFILE.orig vi $MYFILE # make your change cd .. diff -up $SRCTREE/$MYFILE{.orig,} > /tmp/patch To create a patch for multiple files, you should unpack a "vanilla", or unmodified kernel source tree, and generate a diff against your own source tree. For example: MYSRC= /devel/linux-2.6 tar xvfz linux-2.6.12.tar.gz mv linux-2.6.12 linux-2.6.12-vanilla diff -uprN -X linux-2.6.12-vanilla/Documentation/dontdiff \ linux-2.6.12-vanilla $MYSRC > /tmp/patch "dontdiff" is a list of files which are generated by the kernel during the build process, and should be ignored in any diff(1)-generated patch. The "dontdiff" file is included in the kernel tree in 2.6.12 and later. For earlier kernel versions, you can get it from <http://www.xenotime.net/linux/doc/dontdiff>. Make sure your patch does not include any extra files which do not belong in a patch submission. Make sure to review your patch -after- generated it with diff(1), to ensure accuracy. If your changes produce a lot of deltas, you may want to look into splitting them into individual patches which modify things in logical stages. This will facilitate easier reviewing by other kernel developers, very important if you want your patch accepted. There are a number of scripts which can aid in this: Quilt: http://savannah.nongnu.org/projects/quilt Andrew Morton's patch scripts: http://userweb.kernel.org/~akpm/stuff/patch-scripts.tar.gz Instead of these scripts, quilt is the recommended patch management tool (see above). 2) Describe your changes. Describe the technical detail of the change(s) your patch includes. Be as specific as possible. The WORST descriptions possible include things like "update driver X", "bug fix for driver X", or "this patch includes updates for subsystem X. Please apply." The maintainer will thank you if you write your patch description in a form which can be easily pulled into Linux's source code management system, git, as a "commit log". See #15, below. If your description starts to get long, that's a sign that you probably need to split up your patch. See #3, next. When you submit or resubmit a patch or patch series, include the complete patch description and justification for it. Don't just say that this is version N of the patch (series). Don't expect the patch merger to refer back to earlier patch versions or referenced URLs to find the patch description and put that into the patch. I.e., the patch (series) and its description should be self-contained. This benefits both the patch merger(s) and reviewers. Some reviewers probably didn't even receive earlier versions of the patch. If the patch fixes a logged bug entry, refer to that bug entry by number and URL. 3) Separate your changes. Separate _logical changes_ into a single patch file. For example, if your changes include both bug fixes and performance enhancements for a single driver, separate those changes into two or more patches. If your changes include an API update, and a new driver which uses that new API, separate those into two patches. On the other hand, if you make a single change to numerous files, group those changes into a single patch. Thus a single logical change is contained within a single patch. If one patch depends on another patch in order for a change to be complete, that is OK. Simply note "this patch depends on patch X" in your patch description. If you cannot condense your patch set into a smaller set of patches, then only post say 15 or so at a time and wait for review and integration. 4) Style check your changes. Check your patch for basic style violations, details of which can be found in Documentation/CodingStyle. Failure to do so simply wastes the reviewers time and will get your patch rejected, probably without even being read. At a minimum you should check your patches with the patch style checker prior to submission (scripts/checkpatch.pl). You should be able to justify all violations that remain in your patch. 5) Select e-mail destination. Look through the MAINTAINERS file and the source code, and determine if your change applies to a specific subsystem of the kernel, with an assigned maintainer. If so, e-mail that person. The script scripts/get_maintainer.pl can be very useful at this step. If no maintainer is listed, or the maintainer does not respond, send your patch to the primary Linux kernel developer's mailing list, linux-kernel@vger.kernel.org. Most kernel developers monitor this e-mail list, and can comment on your changes. Do not send more than 15 patches at once to the vger mailing lists!!! Linus Torvalds is the final arbiter of all changes accepted into the Linux kernel. His e-mail address is <torvalds@linux-foundation.org>. He gets a lot of e-mail, so typically you should do your best to -avoid- sending him e-mail. Patches which are bug fixes, are "obvious" changes, or similarly require little discussion should be sent or CC'd to Linus. Patches which require discussion or do not have a clear advantage should usually be sent first to linux-kernel. Only after the patch is discussed should the patch then be submitted to Linus. 6) Select your CC (e-mail carbon copy) list. Unless you have a reason NOT to do so, CC linux-kernel@vger.kernel.org. Other kernel developers besides Linus need to be aware of your change, so that they may comment on it and offer code review and suggestions. linux-kernel is the primary Linux kernel developer mailing list. Other mailing lists are available for specific subsystems, such as USB, framebuffer devices, the VFS, the SCSI subsystem, etc. See the MAINTAINERS file for a mailing list that relates specifically to your change. Majordomo lists of VGER.KERNEL.ORG at: <http://vger.kernel.org/vger-lists.html> If changes affect userland-kernel interfaces, please send the MAN-PAGES maintainer (as listed in the MAINTAINERS file) a man-pages patch, or at least a notification of the change, so that some information makes its way into the manual pages. Even if the maintainer did not respond in step #5, make sure to ALWAYS copy the maintainer when you change their code. For small patches you may want to CC the Trivial Patch Monkey trivial@kernel.org which collects "trivial" patches. Have a look into the MAINTAINERS file for its current manager. Trivial patches must qualify for one of the following rules: Spelling fixes in documentation Spelling fixes which could break grep(1) Warning fixes (cluttering with useless warnings is bad) Compilation fixes (only if they are actually correct) Runtime fixes (only if they actually fix things) Removing use of deprecated functions/macros (eg. check_region) Contact detail and documentation fixes Non-portable code replaced by portable code (even in arch-specific, since people copy, as long as it's trivial) Any fix by the author/maintainer of the file (ie. patch monkey in re-transmission mode) 7) No MIME, no links, no compression, no attachments. Just plain text. Linus and other kernel developers need to be able to read and comment on the changes you are submitting. It is important for a kernel developer to be able to "quote" your changes, using standard e-mail tools, so that they may comment on specific portions of your code. For this reason, all patches should be submitting e-mail "inline". WARNING: Be wary of your editor's word-wrap corrupting your patch, if you choose to cut-n-paste your patch. Do not attach the patch as a MIME attachment, compressed or not. Many popular e-mail applications will not always transmit a MIME attachment as plain text, making it impossible to comment on your code. A MIME attachment also takes Linus a bit more time to process, decreasing the likelihood of your MIME-attached change being accepted. Exception: If your mailer is mangling patches then someone may ask you to re-send them using MIME. See Documentation/email-clients.txt for hints about configuring your e-mail client so that it sends your patches untouched. 8) E-mail size. When sending patches to Linus, always follow step #7. Large changes are not appropriate for mailing lists, and some maintainers. If your patch, uncompressed, exceeds 300 kB in size, it is preferred that you store your patch on an Internet-accessible server, and provide instead a URL (link) pointing to your patch. 9) Name your kernel version. It is important to note, either in the subject line or in the patch description, the kernel version to which this patch applies. If the patch does not apply cleanly to the latest kernel version, Linus will not apply it. 10) Don't get discouraged. Re-submit. After you have submitted your change, be patient and wait. If Linus likes your change and applies it, it will appear in the next version of the kernel that he releases. However, if your change doesn't appear in the next version of the kernel, there could be any number of reasons. It's YOUR job to narrow down those reasons, correct what was wrong, and submit your updated change. It is quite common for Linus to "drop" your patch without comment. That's the nature of the system. If he drops your patch, it could be due to * Your patch did not apply cleanly to the latest kernel version. * Your patch was not sufficiently discussed on linux-kernel. * A style issue (see section 2). * An e-mail formatting issue (re-read this section). * A technical problem with your change. * He gets tons of e-mail, and yours got lost in the shuffle. * You are being annoying. When in doubt, solicit comments on linux-kernel mailing list. 11) Include PATCH in the subject Due to high e-mail traffic to Linus, and to linux-kernel, it is common convention to prefix your subject line with [PATCH]. This lets Linus and other kernel developers more easily distinguish patches from other e-mail discussions. 12) Sign your work To improve tracking of who did what, especially with patches that can percolate to their final resting place in the kernel through several layers of maintainers, we've introduced a "sign-off" procedure on patches that are being emailed around. The sign-off is a simple line at the end of the explanation for the patch, which certifies that you wrote it or otherwise have the right to pass it on as an open-source patch. The rules are pretty simple: if you can certify the below: Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. then you just add a line saying Signed-off-by: Random J Developer <random@developer.example.org> using your real name (sorry, no pseudonyms or anonymous contributions.) Some people also put extra tags at the end. They'll just be ignored for now, but you can do this to mark internal company procedures or just point out some special detail about the sign-off. If you are a subsystem or branch maintainer, sometimes you need to slightly modify patches you receive in order to merge them, because the code is not exactly the same in your tree and the submitters'. If you stick strictly to rule (c), you should ask the submitter to rediff, but this is a totally counter-productive waste of time and energy. Rule (b) allows you to adjust the code, but then it is very impolite to change one submitter's code and make him endorse your bugs. To solve this problem, it is recommended that you add a line between the last Signed-off-by header and yours, indicating the nature of your changes. While there is nothing mandatory about this, it seems like prepending the description with your mail and/or name, all enclosed in square brackets, is noticeable enough to make it obvious that you are responsible for last-minute changes. Example : Signed-off-by: Random J Developer <random@developer.example.org> [lucky@maintainer.example.org: struct foo moved from foo.c to foo.h] Signed-off-by: Lucky K Maintainer <lucky@maintainer.example.org> This practise is particularly helpful if you maintain a stable branch and want at the same time to credit the author, track changes, merge the fix, and protect the submitter from complaints. Note that under no circumstances can you change the author's identity (the From header), as it is the one which appears in the changelog. Special note to back-porters: It seems to be a common and useful practise to insert an indication of the origin of a patch at the top of the commit message (just after the subject line) to facilitate tracking. For instance, here's what we see in 2.6-stable : Date: Tue May 13 19:10:30 2008 +0000 SCSI: libiscsi regression in 2.6.25: fix nop timer handling commit 4cf1043593db6a337f10e006c23c69e5fc93e722 upstream And here's what appears in 2.4 : Date: Tue May 13 22:12:27 2008 +0200 wireless, airo: waitbusy() won't delay [backport of 2.6 commit b7acbdfbd1f277c1eb23f344f899cfa4cd0bf36a] Whatever the format, this information provides a valuable help to people tracking your trees, and to people trying to trouble-shoot bugs in your tree. 13) When to use Acked-by: and Cc: The Signed-off-by: tag indicates that the signer was involved in the development of the patch, or that he/she was in the patch's delivery path. If a person was not directly involved in the preparation or handling of a patch but wishes to signify and record their approval of it then they can arrange to have an Acked-by: line added to the patch's changelog. Acked-by: is often used by the maintainer of the affected code when that maintainer neither contributed to nor forwarded the patch. Acked-by: is not as formal as Signed-off-by:. It is a record that the acker has at least reviewed the patch and has indicated acceptance. Hence patch mergers will sometimes manually convert an acker's "yep, looks good to me" into an Acked-by:. Acked-by: does not necessarily indicate acknowledgement of the entire patch. For example, if a patch affects multiple subsystems and has an Acked-by: from one subsystem maintainer then this usually indicates acknowledgement of just the part which affects that maintainer's code. Judgement should be used here. When in doubt people should refer to the original discussion in the mailing list archives. If a person has had the opportunity to comment on a patch, but has not provided such comments, you may optionally add a "Cc:" tag to the patch. This is the only tag which might be added without an explicit action by the person it names. This tag documents that potentially interested parties have been included in the discussion 14) Using Reported-by:, Tested-by: and Reviewed-by: If this patch fixes a problem reported by somebody else, consider adding a Reported-by: tag to credit the reporter for their contribution. Please note that this tag should not be added without the reporter's permission, especially if the problem was not reported in a public forum. That said, if we diligently credit our bug reporters, they will, hopefully, be inspired to help us again in the future. A Tested-by: tag indicates that the patch has been successfully tested (in some environment) by the person named. This tag informs maintainers that some testing has been performed, provides a means to locate testers for future patches, and ensures credit for the testers. Reviewed-by:, instead, indicates that the patch has been reviewed and found acceptable according to the Reviewer's Statement: Reviewer's statement of oversight By offering my Reviewed-by: tag, I state that: (a) I have carried out a technical review of this patch to evaluate its appropriateness and readiness for inclusion into the mainline kernel. (b) Any problems, concerns, or questions relating to the patch have been communicated back to the submitter. I am satisfied with the submitter's response to my comments. (c) While there may be things that could be improved with this submission, I believe that it is, at this time, (1) a worthwhile modification to the kernel, and (2) free of known issues which would argue against its inclusion. (d) While I have reviewed the patch and believe it to be sound, I do not (unless explicitly stated elsewhere) make any warranties or guarantees that it will achieve its stated purpose or function properly in any given situation. A Reviewed-by tag is a statement of opinion that the patch is an appropriate modification of the kernel without any remaining serious technical issues. Any interested reviewer (who has done the work) can offer a Reviewed-by tag for a patch. This tag serves to give credit to reviewers and to inform maintainers of the degree of review which has been done on the patch. Reviewed-by: tags, when supplied by reviewers known to understand the subject area and to perform thorough reviews, will normally increase the likelihood of your patch getting into the kernel. 15) The canonical patch format The canonical patch subject line is: Subject: [PATCH 001/123] subsystem: summary phrase The canonical patch message body contains the following: - A "from" line specifying the patch author. - An empty line. - The body of the explanation, which will be copied to the permanent changelog to describe this patch. - The "Signed-off-by:" lines, described above, which will also go in the changelog. - A marker line containing simply "---". - Any additional comments not suitable for the changelog. - The actual patch (diff output). The Subject line format makes it very easy to sort the emails alphabetically by subject line - pretty much any email reader will support that - since because the sequence number is zero-padded, the numerical and alphabetic sort is the same. The "subsystem" in the email's Subject should identify which area or subsystem of the kernel is being patched. The "summary phrase" in the email's Subject should concisely describe the patch which that email contains. The "summary phrase" should not be a filename. Do not use the same "summary phrase" for every patch in a whole patch series (where a "patch series" is an ordered sequence of multiple, related patches). Bear in mind that the "summary phrase" of your email becomes a globally-unique identifier for that patch. It propagates all the way into the git changelog. The "summary phrase" may later be used in developer discussions which refer to the patch. People will want to google for the "summary phrase" to read discussion regarding that patch. It will also be the only thing that people may quickly see when, two or three months later, they are going through perhaps thousands of patches using tools such as "gitk" or "git log --oneline". For these reasons, the "summary" must be no more than 70-75 characters, and it must describe both what the patch changes, as well as why the patch might be necessary. It is challenging to be both succinct and descriptive, but that is what a well-written summary should do. The "summary phrase" may be prefixed by tags enclosed in square brackets: "Subject: [PATCH tag] <summary phrase>". The tags are not considered part of the summary phrase, but describe how the patch should be treated. Common tags might include a version descriptor if the multiple versions of the patch have been sent out in response to comments (i.e., "v1, v2, v3"), or "RFC" to indicate a request for comments. If there are four patches in a patch series the individual patches may be numbered like this: 1/4, 2/4, 3/4, 4/4. This assures that developers understand the order in which the patches should be applied and that they have reviewed or applied all of the patches in the patch series. A couple of example Subjects: Subject: [patch 2/5] ext2: improve scalability of bitmap searching Subject: [PATCHv2 001/207] x86: fix eflags tracking The "from" line must be the very first line in the message body, and has the form: From: Original Author <author@example.com> The "from" line specifies who will be credited as the author of the patch in the permanent changelog. If the "from" line is missing, then the "From:" line from the email header will be used to determine the patch author in the changelog. The explanation body will be committed to the permanent source changelog, so should make sense to a competent reader who has long since forgotten the immediate details of the discussion that might have led to this patch. Including symptoms of the failure which the patch addresses (kernel log messages, oops messages, etc.) is especially useful for people who might be searching the commit logs looking for the applicable patch. If a patch fixes a compile failure, it may not be necessary to include _all_ of the compile failures; just enough that it is likely that someone searching for the patch can find it. As in the "summary phrase", it is important to be both succinct as well as descriptive. The "---" marker line serves the essential purpose of marking for patch handling tools where the changelog message ends. One good use for the additional comments after the "---" marker is for a diffstat, to show what files have changed, and the number of inserted and deleted lines per file. A diffstat is especially useful on bigger patches. Other comments relevant only to the moment or the maintainer, not suitable for the permanent changelog, should also go here. A good example of such comments might be "patch changelogs" which describe what has changed between the v1 and v2 version of the patch. If you are going to include a diffstat after the "---" marker, please use diffstat options "-p 1 -w 70" so that filenames are listed from the top of the kernel source tree and don't use too much horizontal space (easily fit in 80 columns, maybe with some indentation). See more details on the proper patch format in the following references. 16) Sending "git pull" requests (from Linus emails) Please write the git repo address and branch name alone on the same line so that I can't even by mistake pull from the wrong branch, and so that a triple-click just selects the whole thing. So the proper format is something along the lines of: "Please pull from git://jdelvare.pck.nerim.net/jdelvare-2.6 i2c-for-linus to get these changes:" so that I don't have to hunt-and-peck for the address and inevitably get it wrong (actually, I've only gotten it wrong a few times, and checking against the diffstat tells me when I get it wrong, but I'm just a lot more comfortable when I don't have to "look for" the right thing to pull, and double-check that I have the right branch-name). Please use "git diff -M --stat --summary" to generate the diffstat: the -M enables rename detection, and the summary enables a summary of new/deleted or renamed files. With rename detection, the statistics are rather different [...] because git will notice that a fair number of the changes are renames. ----------------------------------- SECTION 2 - HINTS, TIPS, AND TRICKS ----------------------------------- This section lists many of the common "rules" associated with code submitted to the kernel. There are always exceptions... but you must have a really good reason for doing so. You could probably call this section Linus Computer Science 101. 1) Read Documentation/CodingStyle Nuff said. If your code deviates too much from this, it is likely to be rejected without further review, and without comment. One significant exception is when moving code from one file to another -- in this case you should not modify the moved code at all in the same patch which moves it. This clearly delineates the act of moving the code and your changes. This greatly aids review of the actual differences and allows tools to better track the history of the code itself. Check your patches with the patch style checker prior to submission (scripts/checkpatch.pl). The style checker should be viewed as a guide not as the final word. If your code looks better with a violation then its probably best left alone. The checker reports at three levels: - ERROR: things that are very likely to be wrong - WARNING: things requiring careful review - CHECK: things requiring thought You should be able to justify all violations that remain in your patch. 2) #ifdefs are ugly Code cluttered with ifdefs is difficult to read and maintain. Don't do it. Instead, put your ifdefs in a header, and conditionally define 'static inline' functions, or macros, which are used in the code. Let the compiler optimize away the "no-op" case. Simple example, of poor code: dev = alloc_etherdev (sizeof(struct funky_private)); if (!dev) return -ENODEV; #ifdef CONFIG_NET_FUNKINESS init_funky_net(dev); #endif Cleaned-up example: (in header) #ifndef CONFIG_NET_FUNKINESS static inline void init_funky_net (struct net_device *d) {} #endif (in the code itself) dev = alloc_etherdev (sizeof(struct funky_private)); if (!dev) return -ENODEV; init_funky_net(dev); 3) 'static inline' is better than a macro Static inline functions are greatly preferred over macros. They provide type safety, have no length limitations, no formatting limitations, and under gcc they are as cheap as macros. Macros should only be used for cases where a static inline is clearly suboptimal [there are a few, isolated cases of this in fast paths], or where it is impossible to use a static inline function [such as string-izing]. 'static inline' is preferred over 'static __inline__', 'extern inline', and 'extern __inline__'. 4) Don't over-design. Don't try to anticipate nebulous future cases which may or may not be useful: "Make it as simple as you can, and no simpler." ---------------------- SECTION 3 - REFERENCES ---------------------- Andrew Morton, "The perfect patch" (tpp). <http://userweb.kernel.org/~akpm/stuff/tpp.txt> Jeff Garzik, "Linux kernel patch submission format". <http://linux.yyz.us/patch-format.html> Greg Kroah-Hartman, "How to piss off a kernel subsystem maintainer". <http://www.kroah.com/log/linux/maintainer.html> <http://www.kroah.com/log/linux/maintainer-02.html> <http://www.kroah.com/log/linux/maintainer-03.html> <http://www.kroah.com/log/linux/maintainer-04.html> <http://www.kroah.com/log/linux/maintainer-05.html> NO!!!! No more huge patch bombs to linux-kernel@vger.kernel.org people! <http://marc.theaimsgroup.com/?l=linux-kernel&m=112112749912944&w=2> Kernel Documentation/CodingStyle: <http://users.sosdg.org/~qiyong/lxr/source/Documentation/CodingStyle> Linus Torvalds's mail on the canonical patch format: <http://lkml.org/lkml/2005/4/7/183> Andi Kleen, "On submitting kernel patches" Some strategies to get difficult or controversial changes in. http://halobates.de/on-submitting-patches.pdf -- LL) goto err_blkqinit; disk->queue = q; blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH); blk_queue_max_hw_segments(q, UB_MAX_REQ_SG); blk_queue_max_phys_segments(q, UB_MAX_REQ_SG); blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */ 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) disk->flags |= GENHD_FL_REMOVABLE; add_disk(disk); return 0; err_blkqinit: put_disk(disk); err_diskalloc: ub_id_put(lun->id); err_id: kfree(lun); err_alloc: return rc; } static void ub_disconnect(struct usb_interface *intf) { struct ub_dev *sc = usb_get_intfdata(intf); struct list_head *p; struct ub_lun *lun; unsigned long flags; /* * Prevent ub_bd_release from pulling the rug from under us. * XXX This is starting to look like a kref. * XXX Why not to take this ref at probe time? */ spin_lock_irqsave(&ub_lock, flags); sc->openc++; spin_unlock_irqrestore(&ub_lock, flags); /* * Fence stall clearnings, operations triggered by unlinkings and so on. * We do not attempt to unlink any URBs, because we do not trust the * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway. */ atomic_set(&sc->poison, 1); /* * Wait for reset to end, if any. */ wait_event(sc->reset_wait, !sc->reset); /* * Blow away queued commands. * * Actually, this never works, because before we get here * the HCD terminates outstanding URB(s). It causes our * SCSI command queue to advance, commands fail to submit, * and the whole queue drains. So, we just use this code to * print warnings. */ spin_lock_irqsave(sc->lock, flags); { struct ub_scsi_cmd *cmd; int cnt = 0; while ((cmd = ub_cmdq_peek(sc)) != NULL) { cmd->error = -ENOTCONN; cmd->state = UB_CMDST_DONE; ub_cmdq_pop(sc); (*cmd->done)(sc, cmd); cnt++; } if (cnt != 0) { printk(KERN_WARNING "%s: " "%d was queued after shutdown\n", sc->name, cnt); } } spin_unlock_irqrestore(sc->lock, flags); /* * Unregister the upper layer. */ list_for_each (p, &sc->luns) { lun = list_entry(p, struct ub_lun, link); del_gendisk(lun->disk); /* * I wish I could do: * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags); * As it is, we rely on our internal poisoning and let * the upper levels to spin furiously failing all the I/O. */ } /* * Testing for -EINPROGRESS is always a bug, so we are bending * the rules a little. */ spin_lock_irqsave(sc->lock, flags); if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */ printk(KERN_WARNING "%s: " "URB is active after disconnect\n", sc->name); } spin_unlock_irqrestore(sc->lock, flags); /* * There is virtually no chance that other CPU runs times so long * after ub_urb_complete should have called del_timer, but only if HCD * didn't forget to deliver a callback on unlink. */ del_timer_sync(&sc->work_timer); /* * At this point there must be no commands coming from anyone * and no URBs left in transit. */ ub_put(sc); } static struct usb_driver ub_driver = { .name = "ub", .probe = ub_probe, .disconnect = ub_disconnect, .id_table = ub_usb_ids, }; static int __init ub_init(void) { int rc; int i; for (i = 0; i < UB_QLOCK_NUM; i++) spin_lock_init(&ub_qlockv[i]); if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0) goto err_regblkdev; if ((rc = usb_register(&ub_driver)) != 0) goto err_register; usb_usual_set_present(USB_US_TYPE_UB); return 0; err_register: unregister_blkdev(UB_MAJOR, DRV_NAME); err_regblkdev: return rc; } static void __exit ub_exit(void) { usb_deregister(&ub_driver); unregister_blkdev(UB_MAJOR, DRV_NAME); usb_usual_clear_present(USB_US_TYPE_UB); } module_init(ub_init); module_exit(ub_exit); MODULE_LICENSE("GPL");