In the final version, many fsg_dev structures will (be able to) refer to a single fsg_common structure and so it is required to move common data to another object which can be shared. Situation where many fsg_dev structures refer single fsg_common structure is when a single instance of MSF is used in several USB configurations. Signed-off-by: Michal Nazarewicz <m.nazarewicz@xxxxxxxxxxx> --- drivers/usb/gadget/f_mass_storage.c | 324 ++++++++++++++++++----------------- 1 files changed, 169 insertions(+), 155 deletions(-) diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index 80a282a..9896f57 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -326,14 +326,32 @@ MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk"); /*-------------------------------------------------------------------------*/ +/* Data shared by all the FSG instances. */ +struct fsg_common { + /* filesem protects: backing files in use */ + struct rw_semaphore filesem; + + struct fsg_buffhd *next_buffhd_to_fill; + struct fsg_buffhd *next_buffhd_to_drain; + struct fsg_buffhd buffhds[FSG_NUM_BUFFERS]; + + int cmnd_size; + u8 cmnd[MAX_COMMAND_SIZE]; + + unsigned int nluns; + unsigned int lun; + struct fsg_lun *luns; + struct fsg_lun *curlun; +}; + + struct fsg_dev { - /* lock protects: state, all the req_busy's, and cbbuf_cmnd */ + struct fsg_common *common; + + /* lock protects: state, all the req_busy's */ spinlock_t lock; struct usb_gadget *gadget; - /* filesem protects: backing files in use */ - struct rw_semaphore filesem; - /* reference counting: wait until all LUNs are released */ struct kref ref; @@ -362,27 +380,16 @@ struct fsg_dev { struct usb_ep *bulk_in; struct usb_ep *bulk_out; - struct fsg_buffhd *next_buffhd_to_fill; - struct fsg_buffhd *next_buffhd_to_drain; - struct fsg_buffhd buffhds[FSG_NUM_BUFFERS]; - int thread_wakeup_needed; struct completion thread_notifier; struct task_struct *thread_task; - int cmnd_size; - u8 cmnd[MAX_COMMAND_SIZE]; enum data_direction data_dir; u32 data_size; u32 data_size_from_cmnd; u32 tag; - unsigned int lun; u32 residue; u32 usb_amount_left; - - unsigned int nluns; - struct fsg_lun *luns; - struct fsg_lun *curlun; }; @@ -421,7 +428,7 @@ static struct usb_gadget_driver fsg_driver; static void dump_cdb(struct fsg_dev *fsg) { print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, - 16, 1, fsg->cmnd, fsg->cmnd_size, 0); + 16, 1, fsg->common->cmnd, fsg->common->cmnd_size, 0); } #else @@ -653,7 +660,7 @@ static int class_setup_req(struct fsg_dev *fsg, if (w_index != 0 || w_value != 0) return -EDOM; VDBG(fsg, "get max LUN\n"); - *(u8 *) req->buf = fsg->nluns - 1; + *(u8 *) req->buf = fsg->common->nluns - 1; return 1; } @@ -882,7 +889,7 @@ static int sleep_thread(struct fsg_dev *fsg) static int do_read(struct fsg_dev *fsg) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; u32 lba; struct fsg_buffhd *bh; int rc; @@ -894,15 +901,15 @@ static int do_read(struct fsg_dev *fsg) /* Get the starting Logical Block Address and check that it's * not too big */ - if (fsg->cmnd[0] == SC_READ_6) - lba = get_unaligned_be24(&fsg->cmnd[1]); + if (fsg->common->cmnd[0] == SC_READ_6) + lba = get_unaligned_be24(&fsg->common->cmnd[1]); else { - lba = get_unaligned_be32(&fsg->cmnd[2]); + lba = get_unaligned_be32(&fsg->common->cmnd[2]); /* We allow DPO (Disable Page Out = don't save data in the * cache) and FUA (Force Unit Access = don't read from the * cache), but we don't implement them. */ - if ((fsg->cmnd[1] & ~0x18) != 0) { + if ((fsg->common->cmnd[1] & ~0x18) != 0) { curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; } @@ -937,7 +944,7 @@ static int do_read(struct fsg_dev *fsg) partial_page); /* Wait for the next buffer to become available */ - bh = fsg->next_buffhd_to_fill; + bh = fsg->common->next_buffhd_to_fill; while (bh->state != BUF_STATE_EMPTY) { rc = sleep_thread(fsg); if (rc) @@ -997,7 +1004,7 @@ static int do_read(struct fsg_dev *fsg) bh->inreq->zero = 0; start_transfer(fsg, fsg->bulk_in, bh->inreq, &bh->inreq_busy, &bh->state); - fsg->next_buffhd_to_fill = bh->next; + fsg->common->next_buffhd_to_fill = bh->next; } return -EIO; // No default reply @@ -1008,7 +1015,7 @@ static int do_read(struct fsg_dev *fsg) static int do_write(struct fsg_dev *fsg) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; u32 lba; struct fsg_buffhd *bh; int get_some_more; @@ -1029,20 +1036,20 @@ static int do_write(struct fsg_dev *fsg) /* Get the starting Logical Block Address and check that it's * not too big */ - if (fsg->cmnd[0] == SC_WRITE_6) - lba = get_unaligned_be24(&fsg->cmnd[1]); + if (fsg->common->cmnd[0] == SC_WRITE_6) + lba = get_unaligned_be24(&fsg->common->cmnd[1]); else { - lba = get_unaligned_be32(&fsg->cmnd[2]); + lba = get_unaligned_be32(&fsg->common->cmnd[2]); /* We allow DPO (Disable Page Out = don't save data in the * cache) and FUA (Force Unit Access = write directly to the * medium). We don't implement DPO; we implement FUA by * performing synchronous output. */ - if ((fsg->cmnd[1] & ~0x18) != 0) { + if ((fsg->common->cmnd[1] & ~0x18) != 0) { curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; } - if (fsg->cmnd[1] & 0x08) { // FUA + if (fsg->common->cmnd[1] & 0x08) { // FUA spin_lock(&curlun->filp->f_lock); curlun->filp->f_flags |= O_SYNC; spin_unlock(&curlun->filp->f_lock); @@ -1061,7 +1068,7 @@ static int do_write(struct fsg_dev *fsg) while (amount_left_to_write > 0) { /* Queue a request for more data from the host */ - bh = fsg->next_buffhd_to_fill; + bh = fsg->common->next_buffhd_to_fill; if (bh->state == BUF_STATE_EMPTY && get_some_more) { /* Figure out how much we want to get: @@ -1112,17 +1119,17 @@ static int do_write(struct fsg_dev *fsg) bh->outreq->short_not_ok = 1; start_transfer(fsg, fsg->bulk_out, bh->outreq, &bh->outreq_busy, &bh->state); - fsg->next_buffhd_to_fill = bh->next; + fsg->common->next_buffhd_to_fill = bh->next; continue; } /* Write the received data to the backing file */ - bh = fsg->next_buffhd_to_drain; + bh = fsg->common->next_buffhd_to_drain; if (bh->state == BUF_STATE_EMPTY && !get_some_more) break; // We stopped early if (bh->state == BUF_STATE_FULL) { smp_rmb(); - fsg->next_buffhd_to_drain = bh->next; + fsg->common->next_buffhd_to_drain = bh->next; bh->state = BUF_STATE_EMPTY; /* Did something go wrong with the transfer? */ @@ -1197,7 +1204,7 @@ static int do_write(struct fsg_dev *fsg) static int do_synchronize_cache(struct fsg_dev *fsg) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; int rc; /* We ignore the requested LBA and write out all file's @@ -1223,10 +1230,10 @@ static void invalidate_sub(struct fsg_lun *curlun) static int do_verify(struct fsg_dev *fsg) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; u32 lba; u32 verification_length; - struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; + struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill; loff_t file_offset, file_offset_tmp; u32 amount_left; unsigned int amount; @@ -1234,7 +1241,7 @@ static int do_verify(struct fsg_dev *fsg) /* Get the starting Logical Block Address and check that it's * not too big */ - lba = get_unaligned_be32(&fsg->cmnd[2]); + lba = get_unaligned_be32(&fsg->common->cmnd[2]); if (lba >= curlun->num_sectors) { curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; return -EINVAL; @@ -1242,12 +1249,12 @@ static int do_verify(struct fsg_dev *fsg) /* We allow DPO (Disable Page Out = don't save data in the * cache) but we don't implement it. */ - if ((fsg->cmnd[1] & ~0x10) != 0) { + if ((fsg->common->cmnd[1] & ~0x10) != 0) { curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; } - verification_length = get_unaligned_be16(&fsg->cmnd[7]); + verification_length = get_unaligned_be16(&fsg->common->cmnd[7]); if (unlikely(verification_length == 0)) return -EIO; // No default reply @@ -1327,7 +1334,7 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh) static char product_disk_id[] = "File-Stor Gadget"; static char product_cdrom_id[] = "File-CD Gadget "; - if (!fsg->curlun) { // Unsupported LUNs are okay + if (!fsg->common->curlun) { // Unsupported LUNs are okay fsg->bad_lun_okay = 1; memset(buf, 0, 36); buf[0] = 0x7f; // Unsupported, no device-type @@ -1353,7 +1360,7 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh) static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; u8 *buf = (u8 *) bh->buf; u32 sd, sdinfo; int valid; @@ -1407,9 +1414,9 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) { - struct fsg_lun *curlun = fsg->curlun; - u32 lba = get_unaligned_be32(&fsg->cmnd[2]); - int pmi = fsg->cmnd[8]; + struct fsg_lun *curlun = fsg->common->curlun; + u32 lba = get_unaligned_be32(&fsg->common->cmnd[2]); + int pmi = fsg->common->cmnd[8]; u8 *buf = (u8 *) bh->buf; /* Check the PMI and LBA fields */ @@ -1427,12 +1434,12 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) { - struct fsg_lun *curlun = fsg->curlun; - int msf = fsg->cmnd[1] & 0x02; - u32 lba = get_unaligned_be32(&fsg->cmnd[2]); + struct fsg_lun *curlun = fsg->common->curlun; + int msf = fsg->common->cmnd[1] & 0x02; + u32 lba = get_unaligned_be32(&fsg->common->cmnd[2]); u8 *buf = (u8 *) bh->buf; - if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */ + if ((fsg->common->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */ curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; } @@ -1450,12 +1457,12 @@ static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) { - struct fsg_lun *curlun = fsg->curlun; - int msf = fsg->cmnd[1] & 0x02; - int start_track = fsg->cmnd[6]; + struct fsg_lun *curlun = fsg->common->curlun; + int msf = fsg->common->cmnd[1] & 0x02; + int start_track = fsg->common->cmnd[6]; u8 *buf = (u8 *) bh->buf; - if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ + if ((fsg->common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ start_track > 1) { curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; @@ -1478,8 +1485,8 @@ static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) { - struct fsg_lun *curlun = fsg->curlun; - int mscmnd = fsg->cmnd[0]; + struct fsg_lun *curlun = fsg->common->curlun; + int mscmnd = fsg->common->cmnd[0]; u8 *buf = (u8 *) bh->buf; u8 *buf0 = buf; int pc, page_code; @@ -1487,12 +1494,12 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) int valid_page = 0; int len, limit; - if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD + if ((fsg->common->cmnd[1] & ~0x08) != 0) { // Mask away DBD curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; } - pc = fsg->cmnd[2] >> 6; - page_code = fsg->cmnd[2] & 0x3f; + pc = fsg->common->cmnd[2] >> 6; + page_code = fsg->common->cmnd[2] & 0x3f; if (pc == 3) { curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED; return -EINVAL; @@ -1560,7 +1567,7 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) static int do_start_stop(struct fsg_dev *fsg) { if (!mod_data.removable) { - fsg->curlun->sense_data = SS_INVALID_COMMAND; + fsg->common->curlun->sense_data = SS_INVALID_COMMAND; return -EINVAL; } return 0; @@ -1569,7 +1576,7 @@ static int do_start_stop(struct fsg_dev *fsg) static int do_prevent_allow(struct fsg_dev *fsg) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; int prevent; if (!mod_data.removable) { @@ -1577,8 +1584,8 @@ static int do_prevent_allow(struct fsg_dev *fsg) return -EINVAL; } - prevent = fsg->cmnd[4] & 0x01; - if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent + prevent = fsg->common->cmnd[4] & 0x01; + if ((fsg->common->cmnd[4] & ~0x01) != 0) { // Mask away Prevent curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; } @@ -1593,7 +1600,7 @@ static int do_prevent_allow(struct fsg_dev *fsg) static int do_read_format_capacities(struct fsg_dev *fsg, struct fsg_buffhd *bh) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; u8 *buf = (u8 *) bh->buf; buf[0] = buf[1] = buf[2] = 0; @@ -1610,7 +1617,7 @@ static int do_read_format_capacities(struct fsg_dev *fsg, static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; /* We don't support MODE SELECT */ curlun->sense_data = SS_INVALID_COMMAND; @@ -1667,7 +1674,7 @@ static int wedge_bulk_in_endpoint(struct fsg_dev *fsg) static int pad_with_zeros(struct fsg_dev *fsg) { - struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; + struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill; u32 nkeep = bh->inreq->length; u32 nsend; int rc; @@ -1689,7 +1696,7 @@ static int pad_with_zeros(struct fsg_dev *fsg) bh->inreq->zero = 0; start_transfer(fsg, fsg->bulk_in, bh->inreq, &bh->inreq_busy, &bh->state); - bh = fsg->next_buffhd_to_fill = bh->next; + bh = fsg->common->next_buffhd_to_fill = bh->next; fsg->usb_amount_left -= nsend; nkeep = 0; } @@ -1702,14 +1709,15 @@ static int throw_away_data(struct fsg_dev *fsg) u32 amount; int rc; - while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY || - fsg->usb_amount_left > 0) { + for (bh = fsg->common->next_buffhd_to_drain; + bh->state != BUF_STATE_EMPTY || fsg->usb_amount_left > 0; + bh = fsg->common->next_buffhd_to_drain) { /* Throw away the data in a filled buffer */ if (bh->state == BUF_STATE_FULL) { smp_rmb(); bh->state = BUF_STATE_EMPTY; - fsg->next_buffhd_to_drain = bh->next; + fsg->common->next_buffhd_to_drain = bh->next; /* A short packet or an error ends everything */ if (bh->outreq->actual != bh->outreq->length || @@ -1721,7 +1729,7 @@ static int throw_away_data(struct fsg_dev *fsg) } /* Try to submit another request if we need one */ - bh = fsg->next_buffhd_to_fill; + bh = fsg->common->next_buffhd_to_fill; if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) { amount = min(fsg->usb_amount_left, FSG_BUFLEN); @@ -1732,7 +1740,7 @@ static int throw_away_data(struct fsg_dev *fsg) bh->outreq->short_not_ok = 1; start_transfer(fsg, fsg->bulk_out, bh->outreq, &bh->outreq_busy, &bh->state); - fsg->next_buffhd_to_fill = bh->next; + fsg->common->next_buffhd_to_fill = bh->next; fsg->usb_amount_left -= amount; continue; } @@ -1748,7 +1756,7 @@ static int throw_away_data(struct fsg_dev *fsg) static int finish_reply(struct fsg_dev *fsg) { - struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; + struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill; int rc = 0; switch (fsg->data_dir) { @@ -1776,7 +1784,7 @@ static int finish_reply(struct fsg_dev *fsg) bh->inreq->zero = 0; start_transfer(fsg, fsg->bulk_in, bh->inreq, &bh->inreq_busy, &bh->state); - fsg->next_buffhd_to_fill = bh->next; + fsg->common->next_buffhd_to_fill = bh->next; /* For Bulk-only, if we're allowed to stall then send the * short packet and halt the bulk-in endpoint. If we can't @@ -1785,7 +1793,7 @@ static int finish_reply(struct fsg_dev *fsg) bh->inreq->zero = 1; start_transfer(fsg, fsg->bulk_in, bh->inreq, &bh->inreq_busy, &bh->state); - fsg->next_buffhd_to_fill = bh->next; + fsg->common->next_buffhd_to_fill = bh->next; rc = halt_bulk_in_endpoint(fsg); } else { rc = pad_with_zeros(fsg); @@ -1830,7 +1838,7 @@ static int finish_reply(struct fsg_dev *fsg) static int send_status(struct fsg_dev *fsg) { - struct fsg_lun *curlun = fsg->curlun; + struct fsg_lun *curlun = fsg->common->curlun; struct fsg_buffhd *bh; struct bulk_cs_wrap *csw; int rc; @@ -1838,7 +1846,7 @@ static int send_status(struct fsg_dev *fsg) u32 sd, sdinfo = 0; /* Wait for the next buffer to become available */ - bh = fsg->next_buffhd_to_fill; + bh = fsg->common->next_buffhd_to_fill; while (bh->state != BUF_STATE_EMPTY) { rc = sleep_thread(fsg); if (rc) @@ -1879,7 +1887,7 @@ static int send_status(struct fsg_dev *fsg) start_transfer(fsg, fsg->bulk_in, bh->inreq, &bh->inreq_busy, &bh->state); - fsg->next_buffhd_to_fill = bh->next; + fsg->common->next_buffhd_to_fill = bh->next; return 0; } @@ -1893,7 +1901,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, int needs_medium, const char *name) { int i; - int lun = fsg->cmnd[1] >> 5; + int lun = fsg->common->cmnd[1] >> 5; static const char dirletter[4] = {'u', 'o', 'i', 'n'}; char hdlen[20]; struct fsg_lun *curlun; @@ -1904,7 +1912,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, fsg->data_size); VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", name, cmnd_size, dirletter[(int) data_dir], - fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen); + fsg->data_size_from_cmnd, fsg->common->cmnd_size, hdlen); /* We can't reply at all until we know the correct data direction * and size. */ @@ -1933,7 +1941,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, } /* Verify the length of the command itself */ - if (cmnd_size != fsg->cmnd_size) { + if (cmnd_size != fsg->common->cmnd_size) { /* Special case workaround: There are plenty of buggy SCSI * implementations. Many have issues with cbw->Length @@ -1947,11 +1955,11 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, * REQUEST SENSE with cbw->Length == 10 where it should * be 6 as well. */ - if (cmnd_size <= fsg->cmnd_size) { + if (cmnd_size <= fsg->common->cmnd_size) { DBG(fsg, "%s is buggy! Expected length %d " - "but we got %d\n", name, - cmnd_size, fsg->cmnd_size); - cmnd_size = fsg->cmnd_size; + "but we got %d\n", name, + cmnd_size, fsg->common->cmnd_size); + cmnd_size = fsg->common->cmnd_size; } else { fsg->phase_error = 1; return -EINVAL; @@ -1959,27 +1967,27 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, } /* Check that the LUN values are consistent */ - if (fsg->lun != lun) + if (fsg->common->lun != lun) DBG(fsg, "using LUN %d from CBW, not LUN %d from CDB\n", - fsg->lun, lun); + fsg->common->lun, lun); /* Check the LUN */ - if (fsg->lun >= 0 && fsg->lun < fsg->nluns) { - fsg->curlun = curlun = &fsg->luns[fsg->lun]; - if (fsg->cmnd[0] != SC_REQUEST_SENSE) { + if (fsg->common->lun >= 0 && fsg->common->lun < fsg->common->nluns) { + fsg->common->curlun = curlun = &fsg->common->luns[fsg->common->lun]; + if (fsg->common->cmnd[0] != SC_REQUEST_SENSE) { curlun->sense_data = SS_NO_SENSE; curlun->sense_data_info = 0; curlun->info_valid = 0; } } else { - fsg->curlun = curlun = NULL; + fsg->common->curlun = curlun = NULL; fsg->bad_lun_okay = 0; /* INQUIRY and REQUEST SENSE commands are explicitly allowed * to use unsupported LUNs; all others may not. */ - if (fsg->cmnd[0] != SC_INQUIRY && - fsg->cmnd[0] != SC_REQUEST_SENSE) { - DBG(fsg, "unsupported LUN %d\n", fsg->lun); + if (fsg->common->cmnd[0] != SC_INQUIRY && + fsg->common->cmnd[0] != SC_REQUEST_SENSE) { + DBG(fsg, "unsupported LUN %d\n", fsg->common->lun); return -EINVAL; } } @@ -1987,17 +1995,17 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, /* If a unit attention condition exists, only INQUIRY and * REQUEST SENSE commands are allowed; anything else must fail. */ if (curlun && curlun->unit_attention_data != SS_NO_SENSE && - fsg->cmnd[0] != SC_INQUIRY && - fsg->cmnd[0] != SC_REQUEST_SENSE) { + fsg->common->cmnd[0] != SC_INQUIRY && + fsg->common->cmnd[0] != SC_REQUEST_SENSE) { curlun->sense_data = curlun->unit_attention_data; curlun->unit_attention_data = SS_NO_SENSE; return -EINVAL; } /* Check that only command bytes listed in the mask are non-zero */ - fsg->cmnd[1] &= 0x1f; // Mask away the LUN + fsg->common->cmnd[1] &= 0x1f; // Mask away the LUN for (i = 1; i < cmnd_size; ++i) { - if (fsg->cmnd[i] && !(mask & (1 << i))) { + if (fsg->common->cmnd[i] && !(mask & (1 << i))) { if (curlun) curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; @@ -2026,7 +2034,7 @@ static int do_scsi_command(struct fsg_dev *fsg) dump_cdb(fsg); /* Wait for the next buffer to become available for data or status */ - bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill; + bh = fsg->common->next_buffhd_to_drain = fsg->common->next_buffhd_to_fill; while (bh->state != BUF_STATE_EMPTY) { rc = sleep_thread(fsg); if (rc) @@ -2035,11 +2043,11 @@ static int do_scsi_command(struct fsg_dev *fsg) fsg->phase_error = 0; fsg->short_packet_received = 0; - down_read(&fsg->filesem); // We're using the backing file - switch (fsg->cmnd[0]) { + down_read(&fsg->common->filesem); // We're using the backing file + switch (fsg->common->cmnd[0]) { case SC_INQUIRY: - fsg->data_size_from_cmnd = fsg->cmnd[4]; + fsg->data_size_from_cmnd = fsg->common->cmnd[4]; if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, (1<<4), 0, "INQUIRY")) == 0) @@ -2047,7 +2055,7 @@ static int do_scsi_command(struct fsg_dev *fsg) break; case SC_MODE_SELECT_6: - fsg->data_size_from_cmnd = fsg->cmnd[4]; + fsg->data_size_from_cmnd = fsg->common->cmnd[4]; if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, (1<<1) | (1<<4), 0, "MODE SELECT(6)")) == 0) @@ -2055,7 +2063,7 @@ static int do_scsi_command(struct fsg_dev *fsg) break; case SC_MODE_SELECT_10: - fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, (1<<1) | (3<<7), 0, "MODE SELECT(10)")) == 0) @@ -2063,7 +2071,7 @@ static int do_scsi_command(struct fsg_dev *fsg) break; case SC_MODE_SENSE_6: - fsg->data_size_from_cmnd = fsg->cmnd[4]; + fsg->data_size_from_cmnd = fsg->common->cmnd[4]; if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, (1<<1) | (1<<2) | (1<<4), 0, "MODE SENSE(6)")) == 0) @@ -2071,7 +2079,7 @@ static int do_scsi_command(struct fsg_dev *fsg) break; case SC_MODE_SENSE_10: - fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, (1<<1) | (1<<2) | (3<<7), 0, "MODE SENSE(10)")) == 0) @@ -2087,7 +2095,7 @@ static int do_scsi_command(struct fsg_dev *fsg) break; case SC_READ_6: - i = fsg->cmnd[4]; + i = fsg->common->cmnd[4]; fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, (7<<1) | (1<<4), 1, @@ -2097,7 +2105,7 @@ static int do_scsi_command(struct fsg_dev *fsg) case SC_READ_10: fsg->data_size_from_cmnd = - get_unaligned_be16(&fsg->cmnd[7]) << 9; + get_unaligned_be16(&fsg->common->cmnd[7]) << 9; if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, (1<<1) | (0xf<<2) | (3<<7), 1, "READ(10)")) == 0) @@ -2106,7 +2114,7 @@ static int do_scsi_command(struct fsg_dev *fsg) case SC_READ_12: fsg->data_size_from_cmnd = - get_unaligned_be32(&fsg->cmnd[6]) << 9; + get_unaligned_be32(&fsg->common->cmnd[6]) << 9; if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST, (1<<1) | (0xf<<2) | (0xf<<6), 1, "READ(12)")) == 0) @@ -2124,7 +2132,7 @@ static int do_scsi_command(struct fsg_dev *fsg) case SC_READ_HEADER: if (!mod_data.cdrom) goto unknown_cmnd; - fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, (3<<7) | (0x1f<<1), 1, "READ HEADER")) == 0) @@ -2134,7 +2142,7 @@ static int do_scsi_command(struct fsg_dev *fsg) case SC_READ_TOC: if (!mod_data.cdrom) goto unknown_cmnd; - fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, (7<<6) | (1<<1), 1, "READ TOC")) == 0) @@ -2142,7 +2150,7 @@ static int do_scsi_command(struct fsg_dev *fsg) break; case SC_READ_FORMAT_CAPACITIES: - fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, (3<<7), 1, "READ FORMAT CAPACITIES")) == 0) @@ -2150,7 +2158,7 @@ static int do_scsi_command(struct fsg_dev *fsg) break; case SC_REQUEST_SENSE: - fsg->data_size_from_cmnd = fsg->cmnd[4]; + fsg->data_size_from_cmnd = fsg->common->cmnd[4]; if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, (1<<4), 0, "REQUEST SENSE")) == 0) @@ -2191,7 +2199,7 @@ static int do_scsi_command(struct fsg_dev *fsg) break; case SC_WRITE_6: - i = fsg->cmnd[4]; + i = fsg->common->cmnd[4]; fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, (7<<1) | (1<<4), 1, @@ -2201,7 +2209,7 @@ static int do_scsi_command(struct fsg_dev *fsg) case SC_WRITE_10: fsg->data_size_from_cmnd = - get_unaligned_be16(&fsg->cmnd[7]) << 9; + get_unaligned_be16(&fsg->common->cmnd[7]) << 9; if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, (1<<1) | (0xf<<2) | (3<<7), 1, "WRITE(10)")) == 0) @@ -2210,7 +2218,7 @@ static int do_scsi_command(struct fsg_dev *fsg) case SC_WRITE_12: fsg->data_size_from_cmnd = - get_unaligned_be32(&fsg->cmnd[6]) << 9; + get_unaligned_be32(&fsg->common->cmnd[6]) << 9; if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, (1<<1) | (0xf<<2) | (0xf<<6), 1, "WRITE(12)")) == 0) @@ -2230,15 +2238,15 @@ static int do_scsi_command(struct fsg_dev *fsg) default: unknown_cmnd: fsg->data_size_from_cmnd = 0; - sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]); - if ((reply = check_command(fsg, fsg->cmnd_size, + sprintf(unknown, "Unknown x%02x", fsg->common->cmnd[0]); + if ((reply = check_command(fsg, fsg->common->cmnd_size, DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) { - fsg->curlun->sense_data = SS_INVALID_COMMAND; + fsg->common->curlun->sense_data = SS_INVALID_COMMAND; reply = -EINVAL; } break; } - up_read(&fsg->filesem); + up_read(&fsg->common->filesem); if (reply == -EINTR || signal_pending(current)) return -EINTR; @@ -2307,8 +2315,8 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) } /* Save the command for later */ - fsg->cmnd_size = cbw->Length; - memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size); + fsg->common->cmnd_size = cbw->Length; + memcpy(fsg->common->cmnd, cbw->CDB, fsg->common->cmnd_size); if (cbw->Flags & USB_BULK_IN_FLAG) fsg->data_dir = DATA_DIR_TO_HOST; else @@ -2316,7 +2324,7 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) fsg->data_size = le32_to_cpu(cbw->DataTransferLength); if (fsg->data_size == 0) fsg->data_dir = DATA_DIR_NONE; - fsg->lun = cbw->Lun; + fsg->common->lun = cbw->Lun; fsg->tag = cbw->Tag; return 0; } @@ -2328,7 +2336,7 @@ static int get_next_command(struct fsg_dev *fsg) int rc = 0; /* Wait for the next buffer to become available */ - bh = fsg->next_buffhd_to_fill; + bh = fsg->common->next_buffhd_to_fill; while (bh->state != BUF_STATE_EMPTY) { rc = sleep_thread(fsg); if (rc) @@ -2400,7 +2408,7 @@ static int do_set_interface(struct fsg_dev *fsg, int altsetting) reset: /* Deallocate the requests */ for (i = 0; i < FSG_NUM_BUFFERS; ++i) { - struct fsg_buffhd *bh = &fsg->buffhds[i]; + struct fsg_buffhd *bh = &fsg->common->buffhds[i]; if (bh->inreq) { usb_ep_free_request(fsg->bulk_in, bh->inreq); @@ -2445,7 +2453,7 @@ reset: /* Allocate the requests */ for (i = 0; i < FSG_NUM_BUFFERS; ++i) { - struct fsg_buffhd *bh = &fsg->buffhds[i]; + struct fsg_buffhd *bh = &fsg->common->buffhds[i]; if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0) goto reset; @@ -2458,8 +2466,8 @@ reset: } fsg->running = 1; - for (i = 0; i < fsg->nluns; ++i) - fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED; + for (i = 0; i < fsg->common->nluns; ++i) + fsg->common->luns[i].unit_attention_data = SS_RESET_OCCURRED; return rc; } @@ -2511,7 +2519,6 @@ static void handle_exception(struct fsg_dev *fsg) siginfo_t info; int sig; int i; - int num_active; struct fsg_buffhd *bh; enum fsg_state old_state; u8 new_config; @@ -2534,7 +2541,7 @@ static void handle_exception(struct fsg_dev *fsg) /* Cancel all the pending transfers */ for (i = 0; i < FSG_NUM_BUFFERS; ++i) { - bh = &fsg->buffhds[i]; + bh = &fsg->common->buffhds[i]; if (bh->inreq_busy) usb_ep_dequeue(fsg->bulk_in, bh->inreq); if (bh->outreq_busy) @@ -2543,9 +2550,9 @@ static void handle_exception(struct fsg_dev *fsg) /* Wait until everything is idle */ for (;;) { - num_active = 0; + int num_active = 0; for (i = 0; i < FSG_NUM_BUFFERS; ++i) { - bh = &fsg->buffhds[i]; + bh = &fsg->common->buffhds[i]; num_active += bh->inreq_busy + bh->outreq_busy; } if (num_active == 0) @@ -2565,11 +2572,11 @@ static void handle_exception(struct fsg_dev *fsg) spin_lock_irq(&fsg->lock); for (i = 0; i < FSG_NUM_BUFFERS; ++i) { - bh = &fsg->buffhds[i]; + bh = &fsg->common->buffhds[i]; bh->state = BUF_STATE_EMPTY; } - fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain = - &fsg->buffhds[0]; + fsg->common->next_buffhd_to_fill = fsg->common->next_buffhd_to_drain = + &fsg->common->buffhds[0]; exception_req_tag = fsg->exception_req_tag; new_config = fsg->new_config; @@ -2578,8 +2585,8 @@ static void handle_exception(struct fsg_dev *fsg) if (old_state == FSG_STATE_ABORT_BULK_OUT) fsg->state = FSG_STATE_STATUS_PHASE; else { - for (i = 0; i < fsg->nluns; ++i) { - curlun = &fsg->luns[i]; + for (i = 0; i < fsg->common->nluns; ++i) { + curlun = &fsg->common->luns[i]; curlun->prevent_medium_removal = 0; curlun->sense_data = curlun->unit_attention_data = SS_NO_SENSE; @@ -2616,8 +2623,8 @@ static void handle_exception(struct fsg_dev *fsg) /* Technically this should go here, but it would only be * a waste of time. Ditto for the INTERFACE_CHANGE and * CONFIG_CHANGE cases. */ - // for (i = 0; i < fsg->nluns; ++i) - // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED; + // for (i = 0; i < fsg->common->nluns; ++i) + // fsg->common->luns[i].unit_attention_data = SS_RESET_OCCURRED; break; case FSG_STATE_INTERFACE_CHANGE: @@ -2641,8 +2648,8 @@ static void handle_exception(struct fsg_dev *fsg) break; case FSG_STATE_DISCONNECT: - for (i = 0; i < fsg->nluns; ++i) - fsg_lun_fsync_sub(fsg->luns + i); + for (i = 0; i < fsg->common->nluns; ++i) + fsg_lun_fsync_sub(&fsg->common->luns[i]); do_set_config(fsg, 0); // Unconfigured state break; @@ -2743,7 +2750,7 @@ static void fsg_release(struct kref *ref) { struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref); - kfree(fsg->luns); + kfree(fsg->common->luns); kfree(fsg); } @@ -2767,8 +2774,8 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) clear_bit(REGISTERED, &fsg->atomic_bitflags); /* Unregister the sysfs attribute files and the LUNs */ - for (i = 0; i < fsg->nluns; ++i) { - curlun = &fsg->luns[i]; + for (i = 0; i < fsg->common->nluns; ++i) { + curlun = &fsg->common->luns[i]; if (curlun->registered) { device_remove_file(&curlun->dev, &dev_attr_ro); device_remove_file(&curlun->dev, &dev_attr_file); @@ -2789,7 +2796,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) /* Free the data buffers */ for (i = 0; i < FSG_NUM_BUFFERS; ++i) - kfree(fsg->buffhds[i].buf); + kfree(fsg->common->buffhds[i].buf); /* Free the request and buffer for endpoint 0 */ if (req) { @@ -2870,15 +2877,15 @@ static int __init fsg_bind(struct usb_gadget *gadget) /* Create the LUNs, open their backing files, and register the * LUN devices in sysfs. */ - fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL); - if (!fsg->luns) { + fsg->common->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL); + if (!fsg->common->luns) { rc = -ENOMEM; goto out; } - fsg->nluns = i; + fsg->common->nluns = i; - for (i = 0; i < fsg->nluns; ++i) { - curlun = &fsg->luns[i]; + for (i = 0; i < fsg->common->nluns; ++i) { + curlun = &fsg->common->luns[i]; curlun->cdrom = !!mod_data.cdrom; curlun->ro = mod_data.cdrom || mod_data.ro[i]; curlun->initially_ro = curlun->ro; @@ -2960,7 +2967,7 @@ static int __init fsg_bind(struct usb_gadget *gadget) /* Allocate the data buffers */ for (i = 0; i < FSG_NUM_BUFFERS; ++i) { - struct fsg_buffhd *bh = &fsg->buffhds[i]; + struct fsg_buffhd *bh = &fsg->common->buffhds[i]; /* Allocate for the bulk-in endpoint. We assume that * the buffer will also work with the bulk-out (and @@ -2970,7 +2977,7 @@ static int __init fsg_bind(struct usb_gadget *gadget) goto out; bh->next = bh + 1; } - fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0]; + fsg->common->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->common->buffhds[0]; /* This should reflect the actual gadget power source */ usb_gadget_set_selfpowered(gadget); @@ -2998,11 +3005,11 @@ static int __init fsg_bind(struct usb_gadget *gadget) } INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); - INFO(fsg, "Number of LUNs=%d\n", fsg->nluns); + INFO(fsg, "Number of LUNs=%d\n", fsg->common->nluns); pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); - for (i = 0; i < fsg->nluns; ++i) { - curlun = &fsg->luns[i]; + for (i = 0; i < fsg->common->nluns; ++i) { + curlun = &fsg->common->luns[i]; if (fsg_lun_is_open(curlun)) { p = NULL; if (pathbuf) { @@ -3071,8 +3078,15 @@ static int __init fsg_alloc(void) fsg = kzalloc(sizeof *fsg, GFP_KERNEL); if (!fsg) return -ENOMEM; + + fsg->common = kzalloc(sizeof *fsg->common, GFP_KERNEL); + if (!fsg->common) { + kfree(fsg); + return -ENOMEM; + } + spin_lock_init(&fsg->lock); - init_rwsem(&fsg->filesem); + init_rwsem(&fsg->common->filesem); kref_init(&fsg->ref); init_completion(&fsg->thread_notifier); -- 1.6.3.3 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html