Moved code initialising fsg_common structure to fsg_common_init() function which is called from fsg_bind(). Moroever, changed reference counting mechanism: fsg_common has a reference counter which counts how many fsg_dev structures uses it. When this reaches zero fsg_common_release() is run which unregisters LUN devices and frees memory. Signed-off-by: Michal Nazarewicz <m.nazarewicz@xxxxxxxxxxx> --- drivers/usb/gadget/f_mass_storage.c | 345 +++++++++++++++++++---------------- 1 files changed, 187 insertions(+), 158 deletions(-) diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index c075766..4a9cfcf 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -330,6 +330,8 @@ MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk"); /* Data shared by all the FSG instances. */ struct fsg_common { + struct usb_gadget *gadget; + /* filesem protects: backing files in use */ struct rw_semaphore filesem; @@ -344,6 +346,8 @@ struct fsg_common { unsigned int lun; struct fsg_lun *luns; struct fsg_lun *curlun; + + struct kref ref; }; @@ -354,9 +358,6 @@ struct fsg_dev { spinlock_t lock; struct usb_gadget *gadget; - /* reference counting: wait until all LUNs are released */ - struct kref ref; - struct usb_ep *ep0; // Handy copy of gadget->ep0 struct usb_request *ep0req; // For control responses unsigned int ep0_req_tag; @@ -2737,7 +2738,7 @@ static int fsg_main_thread(void *fsg_) } -/*-------------------------------------------------------------------------*/ +/*************************** DEVICE ATTRIBUTES ***************************/ /* The write permissions and store_xxx pointers are set in fsg_bind() */ @@ -2745,47 +2746,182 @@ static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL); static DEVICE_ATTR(file, 0444, fsg_show_file, NULL); -/*-------------------------------------------------------------------------*/ +/****************************** FSG COMMON ******************************/ + +static void fsg_common_release(struct kref *ref); -static void fsg_release(struct kref *ref) +static void fsg_lun_release(struct device *dev) { - struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref); + /* Nothing needs to be done */ +} - kfree(fsg->common->luns); - kfree(fsg); +static inline void fsg_common_get(struct fsg_common *common) +{ + kref_get(&common->ref); +} + +static inline void fsg_common_put(struct fsg_common *common) +{ + kref_put(&common->ref, fsg_common_release); +} + + +static struct fsg_common *fsg_common_init(struct usb_gadget *gadget) +{ + struct fsg_common *common; + struct fsg_buffhd *bh; + struct fsg_lun *curlun; + int nluns, i, rc; + + /* Find out how many LUNs there should be */ + nluns = mod_data.nluns; + if (nluns == 0) + nluns = max(mod_data.num_filenames, 1u); + if (nluns < 1 || nluns > FSG_MAX_LUNS) { + dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns); + return ERR_PTR(-EINVAL); + } + + common = kzalloc(sizeof *common, GFP_KERNEL); + if (!common) + return ERR_PTR(-ENOMEM); + common->gadget = gadget; + + /* Create the LUNs, open their backing files, and register the + * LUN devices in sysfs. */ + curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL); + if (!curlun) { + kfree(common); + return ERR_PTR(-ENOMEM); + } + common->luns = curlun; + + init_rwsem(&common->filesem); + + for (i = 0; i < nluns; ++i, ++curlun) { + curlun->cdrom = !!mod_data.cdrom; + curlun->ro = mod_data.cdrom || mod_data.ro[i]; + curlun->removable = mod_data.removable; + curlun->dev.release = fsg_lun_release; + curlun->dev.parent = &gadget->dev; + curlun->dev.driver = &fsg_driver.driver; + dev_set_drvdata(&curlun->dev, &common->filesem); + dev_set_name(&curlun->dev,"%s-lun%d", + dev_name(&gadget->dev), i); + + rc = device_register(&curlun->dev); + if (rc) { + INFO(common, "failed to register LUN%d: %d\n", i, rc); + common->nluns = i; + goto error_release; + } + + rc = device_create_file(&curlun->dev, &dev_attr_ro); + if (rc) + goto error_luns; + rc = device_create_file(&curlun->dev, &dev_attr_file); + if (rc) + goto error_luns; + + if (mod_data.file[i] && *mod_data.file[i]) { + rc = fsg_lun_open(curlun, mod_data.file[i]); + if (rc) + goto error_luns; + } else if (!mod_data.removable) { + ERROR(common, "no file given for LUN%d\n", i); + rc = -EINVAL; + goto error_luns; + } + } + common->nluns = nluns; + + + /* Data buffers cyclic list */ + /* Buffers in buffhds are static -- no need for additional + * allocation. */ + bh = common->buffhds; + i = FSG_NUM_BUFFERS - 1; + do { + bh->next = bh + 1; + } while (++bh, --i); + bh->next = common->buffhds; + + + /* Release */ + if (mod_data.release == 0xffff) { // Parameter wasn't set + int gcnum; + + /* The sa1100 controller is not supported */ + if (gadget_is_sa1100(gadget)) + gcnum = -1; + else + gcnum = usb_gadget_controller_number(gadget); + if (gcnum >= 0) + mod_data.release = 0x0300 + gcnum; + else { + WARNING(common, "controller '%s' not recognized\n", + gadget->name); + WARNING(common, "controller '%s' not recognized\n", + gadget->name); + mod_data.release = 0x0399; + } + } + + + /* Some peripheral controllers are known not to be able to + * halt bulk endpoints correctly. If one of them is present, + * disable stalls. + */ + if (gadget_is_sh(fsg->gadget) || gadget_is_at91(fsg->gadget)) + mod_data.can_stall = 0; + + + kref_init(&common->ref); + return common; + + +error_luns: + common->nluns = i + 1; +error_release: + /* Call fsg_common_release() directly, ref is not initialised */ + fsg_common_release(&common->ref); + return ERR_PTR(rc); } -static void lun_release(struct device *dev) + +static void fsg_common_release(struct kref *ref) { - struct rw_semaphore *filesem = dev_get_drvdata(dev); - struct fsg_dev *fsg = - container_of(filesem, struct fsg_dev, filesem); + struct fsg_common *common = + container_of(ref, struct fsg_common, ref); + unsigned i = common->nluns; + struct fsg_lun *lun = common->luns; - kref_put(&fsg->ref, fsg_release); + /* Beware tempting for -> do-while optimization: when in error + * recovery nluns may be zero. */ + + for (; i; --i, ++lun) { + device_remove_file(&lun->dev, &dev_attr_ro); + device_remove_file(&lun->dev, &dev_attr_file); + fsg_lun_close(lun); + device_unregister(&lun->dev); + } + + kfree(common->luns); + kfree(common); } + +/*-------------------------------------------------------------------------*/ + + static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) { struct fsg_dev *fsg = get_gadget_data(gadget); - int i; - struct fsg_lun *curlun; struct usb_request *req = fsg->ep0req; DBG(fsg, "unbind\n"); clear_bit(REGISTERED, &fsg->atomic_bitflags); - /* Unregister the sysfs attribute files and the LUNs */ - 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); - fsg_lun_close(curlun); - device_unregister(&curlun->dev); - curlun->registered = 0; - } - } - /* If the thread isn't already dead, tell it to exit now */ if (fsg->state != FSG_STATE_TERMINATED) { raise_exception(fsg, FSG_STATE_EXIT); @@ -2801,13 +2937,15 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) usb_ep_free_request(fsg->ep0, req); } + fsg_common_put(fsg->common); + kfree(fsg); set_gadget_data(gadget, NULL); } static int __init fsg_bind(struct usb_gadget *gadget) { - struct fsg_dev *fsg = the_fsg; + struct fsg_dev *fsg; int rc; int i; struct fsg_lun *curlun; @@ -2815,19 +2953,27 @@ static int __init fsg_bind(struct usb_gadget *gadget) struct usb_request *req; char *pathbuf, *p; + /* Allocate */ + fsg = kzalloc(sizeof *fsg, GFP_KERNEL); + if (!fsg) + return -ENOMEM; + + /* Initialise common */ + fsg->common = fsg_common_init(gadget); + if (IS_ERR(fsg->common)) + return PTR_ERR(fsg->common); + + /* Basic parameters */ fsg->gadget = gadget; set_gadget_data(gadget, fsg); fsg->ep0 = gadget->ep0; fsg->ep0->driver_data = fsg; - /* Some peripheral controllers are known not to be able to - * halt bulk endpoints correctly. If one of them is present, - * disable stalls. - */ - if (gadget_is_sh(fsg->gadget) || gadget_is_at91(fsg->gadget)) - mod_data.can_stall = 0; + spin_lock_init(&fsg->lock); + init_completion(&fsg->thread_notifier); - if (mod_data.removable) { // Enable the store_xxx attributes + /* Enable the store_xxx attributes */ + if (mod_data.removable) { dev_attr_file.attr.mode = 0644; dev_attr_file.store = fsg_store_file; if (!mod_data.cdrom) { @@ -2836,78 +2982,6 @@ static int __init fsg_bind(struct usb_gadget *gadget) } } - if (mod_data.release == 0xffff) { // Parameter wasn't set - /* The sa1100 controller is not supported */ - if (gadget_is_sa1100(fsg->gadget)) - i = -1; - else - i = usb_gadget_controller_number(fsg->gadget); - if (i >= 0) - mod_data.release = 0x0300 + i; - else { - WARNING(fsg, "controller '%s' not recognized\n", - fsg->gadget->name); - mod_data.release = 0x0399; - } - } - - /* Find out how many LUNs there should be */ - i = mod_data.nluns; - if (i == 0) - i = max(mod_data.num_filenames, 1u); - if (i > FSG_MAX_LUNS) { - ERROR(fsg, "invalid number of LUNs: %d\n", i); - rc = -EINVAL; - goto out; - } - - /* Create the LUNs, open their backing files, and register the - * LUN devices in sysfs. */ - fsg->common->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL); - if (!fsg->common->luns) { - rc = -ENOMEM; - goto out; - } - fsg->common->nluns = 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->removable = mod_data.removable; - 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_name(&curlun->dev,"%s-lun%d", - dev_name(&gadget->dev), i); - - if ((rc = device_register(&curlun->dev)) != 0) { - INFO(fsg, "failed to register LUN%d: %d\n", i, rc); - goto out; - } - if ((rc = device_create_file(&curlun->dev, - &dev_attr_ro)) != 0 || - (rc = device_create_file(&curlun->dev, - &dev_attr_file)) != 0) { - device_unregister(&curlun->dev); - goto out; - } - curlun->registered = 1; - kref_get(&fsg->ref); - - if (mod_data.file[i] && *mod_data.file[i]) { - if ((rc = fsg_lun_open(curlun, - mod_data.file[i])) != 0) - goto out; - } else if (!mod_data.removable) { - ERROR(fsg, "no file given for LUN%d\n", i); - rc = -EINVAL; - goto out; - } - } - /* Find all the endpoints we will use */ usb_ep_autoconfig_reset(gadget); ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc); @@ -3005,6 +3079,8 @@ static int __init fsg_bind(struct usb_gadget *gadget) /* Tell the thread to start working */ wake_up_process(fsg->thread_task); + + the_fsg = fsg; return 0; autoconf_fail: @@ -3043,65 +3119,18 @@ static struct usb_gadget_driver fsg_driver = { }; -static int __init fsg_alloc(void) -{ - struct fsg_dev *fsg; - struct fsg_buffhd *bh; - unsigned i; - - 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; - } - - bh = fsg->common->buffhds; - i = FSG_NUM_BUFFERS - 1; - do { - bh->next = bh + 1; - } while (++bh, --i); - bh->next = fsg->common->buffhds; - - spin_lock_init(&fsg->lock); - init_rwsem(&fsg->common->filesem); - kref_init(&fsg->ref); - init_completion(&fsg->thread_notifier); - - the_fsg = fsg; - return 0; -} - - static int __init fsg_init(void) { - int rc; - struct fsg_dev *fsg; - - if ((rc = fsg_alloc()) != 0) - return rc; - fsg = the_fsg; - if ((rc = usb_gadget_register_driver(&fsg_driver)) != 0) - kref_put(&fsg->ref, fsg_release); - return rc; + return usb_gadget_register_driver(&fsg_driver); } module_init(fsg_init); static void __exit fsg_cleanup(void) { - struct fsg_dev *fsg = the_fsg; - /* Unregister the driver iff the thread hasn't already done so */ - if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags)) + if (the_fsg && + test_and_clear_bit(REGISTERED, &the_fsg->atomic_bitflags)) usb_gadget_unregister_driver(&fsg_driver); - - /* Wait for the thread to finish up */ - wait_for_completion(&fsg->thread_notifier); - - kref_put(&fsg->ref, fsg_release); } module_exit(fsg_cleanup); -- 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