/*-------------------------------------------------------------------------*/
+/* 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;
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;
};
typedef void (*fsg_routine_t)(struct fsg_dev *);
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;
}
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;
/* 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;
}
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)
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
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;
/* 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);
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:
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? */
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
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;
/* 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;
/* 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
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
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;
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 */
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;
}
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;
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;
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;
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;
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) {
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;
}
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;
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;
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;
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;
}
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 ||
}
/* 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);
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;
}
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) {
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
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);
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;
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)
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;
}
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;
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. */
}
/* 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
* 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;
}
/* 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;
}
}
/* 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;
int i;
static char unknown[16];
- dump_cdb(fsg);
+ dump_cdb(fsg->common);
/* 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)
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)
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)
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)
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)
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)
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,
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)
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)
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)
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)
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)
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)
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,
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)
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)
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;
}
/* 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
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;
}
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)
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);
/* 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;
}
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;
}
siginfo_t info;
int sig;
int i;
- int num_active;
struct fsg_buffhd *bh;
enum fsg_state old_state;
u8 new_config;
/* 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)
/* 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)
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;
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;
/* 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:
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;
/*-------------------------------------------------------------------------*/
-static void fsg_release(struct kref *ref)
+static void fsg_release(struct fsg_dev *fsg)
{
- struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
-
- kfree(fsg->luns);
+ kfree(fsg->common->luns);
kfree(fsg);
}
static void lun_release(struct device *dev)
{
- struct rw_semaphore *filesem = dev_get_drvdata(dev);
- struct fsg_dev *fsg =
- container_of(filesem, struct fsg_dev, filesem);
-
- kref_put(&fsg->ref, fsg_release);
}
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);
/* 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) {
/* 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;
curlun->dev.release = lun_release;
curlun->dev.parent = &gadget->dev;
curlun->dev.driver = &fsg_driver.driver;
- dev_set_drvdata(&curlun->dev, &fsg->filesem);
+ dev_set_drvdata(&curlun->dev, &fsg->common->filesem);
dev_set_name(&curlun->dev,"%s-lun%d",
dev_name(&gadget->dev), i);
goto out;
}
curlun->registered = 1;
- kref_get(&fsg->ref);
if (mod_data.file[i] && *mod_data.file[i]) {
if ((rc = fsg_lun_open(curlun,
/* 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
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);
}
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) {
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);
- kref_init(&fsg->ref);
+ init_rwsem(&fsg->common->filesem);
init_completion(&fsg->thread_notifier);
the_fsg = fsg;
return rc;
fsg = the_fsg;
if ((rc = usb_gadget_register_driver(&fsg_driver)) != 0)
- kref_put(&fsg->ref, fsg_release);
+ fsg_release(fsg);
return rc;
}
module_init(fsg_init);
/* Wait for the thread to finish up */
wait_for_completion(&fsg->thread_notifier);
- kref_put(&fsg->ref, fsg_release);
+ fsg_release(fsg);
}
module_exit(fsg_cleanup);