Commit 8876f5e7 authored by Michal Nazarewicz's avatar Michal Nazarewicz Committed by Greg Kroah-Hartman

USB: gadget: f_mass_storage: added eject callback

Added pre_eject() and post_eject() callbacks which are
called before and after removable logical unit is ejected.
The first can prevent logical unit from being ejected.

This commit also changes the way callbacks are passed to
the function from gadget.  A fsg_operations structure has
been created which lists all callbacks -- this is passed
to the fsg_config.

This is important because it changes the way thread_exits()
callback is passed.
Signed-off-by: default avatarMichal Nazarewicz <m.nazarewicz@samsung.com>
Signed-off-by: default avatarKyungmin Park <kyungmin.park@samsung.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 3f3e12d0
...@@ -316,6 +316,27 @@ static const char fsg_string_interface[] = "Mass Storage"; ...@@ -316,6 +316,27 @@ static const char fsg_string_interface[] = "Mass Storage";
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
struct fsg_dev; struct fsg_dev;
struct fsg_common;
/* FSF callback functions */
struct fsg_operations {
/* Callback function to call when thread exits. If no
* callback is set or it returns value lower then zero MSF
* will force eject all LUNs it operates on (including those
* marked as non-removable or with prevent_medium_removal flag
* set). */
int (*thread_exits)(struct fsg_common *common);
/* Called prior to ejection. Negative return means error,
* zero means to continue with ejection, positive means not to
* eject. */
int (*pre_eject)(struct fsg_common *common,
struct fsg_lun *lun, int num);
/* Called after ejection. Negative return means error, zero
* or positive is just a success. */
int (*post_eject)(struct fsg_common *common,
struct fsg_lun *lun, int num);
};
/* Data shared by all the FSG instances. */ /* Data shared by all the FSG instances. */
...@@ -368,8 +389,8 @@ struct fsg_common { ...@@ -368,8 +389,8 @@ struct fsg_common {
struct completion thread_notifier; struct completion thread_notifier;
struct task_struct *thread_task; struct task_struct *thread_task;
/* Callback function to call when thread exits. */ /* Callback functions. */
int (*thread_exits)(struct fsg_common *common); const struct fsg_operations *ops;
/* Gadget's private data. */ /* Gadget's private data. */
void *private_data; void *private_data;
...@@ -393,12 +414,8 @@ struct fsg_config { ...@@ -393,12 +414,8 @@ struct fsg_config {
const char *lun_name_format; const char *lun_name_format;
const char *thread_name; const char *thread_name;
/* Callback function to call when thread exits. If no /* Callback functions. */
* callback is set or it returns value lower then zero MSF const struct fsg_operations *ops;
* will force eject all LUNs it operates on (including those
* marked as non-removable or with prevent_medium_removal flag
* set). */
int (*thread_exits)(struct fsg_common *common);
/* Gadget's private data. */ /* Gadget's private data. */
void *private_data; void *private_data;
...@@ -434,6 +451,7 @@ static inline int __fsg_is_set(struct fsg_common *common, ...@@ -434,6 +451,7 @@ static inline int __fsg_is_set(struct fsg_common *common,
if (common->fsg) if (common->fsg)
return 1; return 1;
ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
WARN_ON(1);
return 0; return 0;
} }
...@@ -1392,43 +1410,55 @@ static int do_start_stop(struct fsg_common *common) ...@@ -1392,43 +1410,55 @@ static int do_start_stop(struct fsg_common *common)
} else if (!curlun->removable) { } else if (!curlun->removable) {
curlun->sense_data = SS_INVALID_COMMAND; curlun->sense_data = SS_INVALID_COMMAND;
return -EINVAL; return -EINVAL;
} } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
(common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
loej = common->cmnd[4] & 0x02;
start = common->cmnd[4] & 0x01;
/* eject code from file_storage.c:do_start_stop() */
if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
(common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
curlun->sense_data = SS_INVALID_FIELD_IN_CDB; curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
return -EINVAL; return -EINVAL;
} }
if (!start) { loej = common->cmnd[4] & 0x02;
/* Are we allowed to unload the media? */ start = common->cmnd[4] & 0x01;
if (curlun->prevent_medium_removal) {
LDBG(curlun, "unload attempt prevented\n");
curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
return -EINVAL;
}
if (loej) { /* Simulate an unload/eject */
up_read(&common->filesem);
down_write(&common->filesem);
fsg_lun_close(curlun);
up_write(&common->filesem);
down_read(&common->filesem);
}
} else {
/* Our emulation doesn't support mounting; the medium is /* Our emulation doesn't support mounting; the medium is
* available for use as soon as it is loaded. */ * available for use as soon as it is loaded. */
if (start) {
if (!fsg_lun_is_open(curlun)) { if (!fsg_lun_is_open(curlun)) {
curlun->sense_data = SS_MEDIUM_NOT_PRESENT; curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
return -EINVAL; return -EINVAL;
} }
return 0;
} }
return 0;
/* Are we allowed to unload the media? */
if (curlun->prevent_medium_removal) {
LDBG(curlun, "unload attempt prevented\n");
curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
return -EINVAL;
}
if (!loej)
return 0;
/* Simulate an unload/eject */
if (common->ops && common->ops->pre_eject) {
int r = common->ops->pre_eject(common, curlun,
curlun - common->luns);
if (unlikely(r < 0))
return r;
else if (r)
return 0;
}
up_read(&common->filesem);
down_write(&common->filesem);
fsg_lun_close(curlun);
up_write(&common->filesem);
down_read(&common->filesem);
return common->ops && common->ops->post_eject
? min(0, common->ops->post_eject(common, curlun,
curlun - common->luns))
: 0;
} }
...@@ -2607,7 +2637,8 @@ static int fsg_main_thread(void *common_) ...@@ -2607,7 +2637,8 @@ static int fsg_main_thread(void *common_)
common->thread_task = NULL; common->thread_task = NULL;
spin_unlock_irq(&common->lock); spin_unlock_irq(&common->lock);
if (!common->thread_exits || common->thread_exits(common) < 0) { if (!common->ops || !common->ops->thread_exits
|| common->ops->thread_exits(common) < 0) {
struct fsg_lun *curlun = common->luns; struct fsg_lun *curlun = common->luns;
unsigned i = common->nluns; unsigned i = common->nluns;
...@@ -2683,6 +2714,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, ...@@ -2683,6 +2714,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
common->free_storage_on_release = 0; common->free_storage_on_release = 0;
} }
common->ops = cfg->ops;
common->private_data = cfg->private_data; common->private_data = cfg->private_data;
common->gadget = gadget; common->gadget = gadget;
...@@ -2804,7 +2836,6 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, ...@@ -2804,7 +2836,6 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
/* Tell the thread to start working */ /* Tell the thread to start working */
common->thread_exits = cfg->thread_exits;
common->thread_task = common->thread_task =
kthread_create(fsg_main_thread, common, kthread_create(fsg_main_thread, common,
OR(cfg->thread_name, "file-storage")); OR(cfg->thread_name, "file-storage"));
...@@ -3100,8 +3131,8 @@ fsg_config_from_params(struct fsg_config *cfg, ...@@ -3100,8 +3131,8 @@ fsg_config_from_params(struct fsg_config *cfg,
cfg->product_name = 0; cfg->product_name = 0;
cfg->release = 0xffff; cfg->release = 0xffff;
cfg->thread_exits = 0; cfg->ops = NULL;
cfg->private_data = 0; cfg->private_data = NULL;
/* Finalise */ /* Finalise */
cfg->can_stall = params->stall; cfg->can_stall = params->stall;
......
...@@ -143,6 +143,9 @@ static int msg_thread_exits(struct fsg_common *common) ...@@ -143,6 +143,9 @@ static int msg_thread_exits(struct fsg_common *common)
static int __init msg_do_config(struct usb_configuration *c) static int __init msg_do_config(struct usb_configuration *c)
{ {
static const struct fsg_operations ops = {
.thread_exits = msg_thread_exits,
};
static struct fsg_common common; static struct fsg_common common;
struct fsg_common *retp; struct fsg_common *retp;
...@@ -155,7 +158,7 @@ static int __init msg_do_config(struct usb_configuration *c) ...@@ -155,7 +158,7 @@ static int __init msg_do_config(struct usb_configuration *c)
} }
fsg_config_from_params(&config, &mod_data); fsg_config_from_params(&config, &mod_data);
config.thread_exits = msg_thread_exits; config.ops = &ops;
retp = fsg_common_init(&common, c->cdev, &config); retp = fsg_common_init(&common, c->cdev, &config);
if (IS_ERR(retp)) if (IS_ERR(retp))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment