Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6313caac authored by Andrzej Pietrasiewicz's avatar Andrzej Pietrasiewicz Committed by Felipe Balbi
Browse files

usb: gadget: f_mass_storage: create fsg_common_set_num_buffers for use in fsg_common_init



fsg_common_init is a lengthy function. Factor a portion of it out.

Signed-off-by: default avatarAndrzej Pietrasiewicz <andrzej.p@samsung.com>
Signed-off-by: default avatarKyungmin Park <kyungmin.park@samsung.com>
Acked-by: default avatarMichal Nazarewicz <mina86@mina86.com>
Signed-off-by: default avatarFelipe Balbi <balbi@ti.com>
parent b24650df
Loading
Loading
Loading
Loading
+46 −26
Original line number Original line Diff line number Diff line
@@ -2681,6 +2681,49 @@ static void _fsg_common_free_buffers(struct fsg_buffhd *buffhds, unsigned n)
	}
	}
}
}


int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
{
	struct fsg_buffhd *bh, *buffhds;
	int i, rc;

	rc = fsg_num_buffers_validate(n);
	if (rc != 0)
		return rc;

	buffhds = kcalloc(n, sizeof(*buffhds), GFP_KERNEL);
	if (!buffhds)
		return -ENOMEM;

	/* Data buffers cyclic list */
	bh = buffhds;
	i = n;
	goto buffhds_first_it;
	do {
		bh->next = bh + 1;
		++bh;
buffhds_first_it:
		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
		if (unlikely(!bh->buf))
			goto error_release;
	} while (--i);
	bh->next = buffhds;

	_fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
	common->fsg_num_buffers = n;
	common->buffhds = buffhds;

	return 0;

error_release:
	/*
	 * "buf"s pointed to by heads after n - i are NULL
	 * so releasing them won't hurt
	 */
	_fsg_common_free_buffers(buffhds, n);

	return -ENOMEM;
}

static inline void fsg_common_remove_sysfs(struct fsg_lun *lun)
static inline void fsg_common_remove_sysfs(struct fsg_lun *lun)
{
{
	device_remove_file(&lun->dev, &dev_attr_nofua);
	device_remove_file(&lun->dev, &dev_attr_nofua);
@@ -2714,17 +2757,12 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
				   struct fsg_config *cfg)
				   struct fsg_config *cfg)
{
{
	struct usb_gadget *gadget = cdev->gadget;
	struct usb_gadget *gadget = cdev->gadget;
	struct fsg_buffhd *bh;
	struct fsg_lun **curlun_it;
	struct fsg_lun **curlun_it;
	struct fsg_lun_config *lcfg;
	struct fsg_lun_config *lcfg;
	struct usb_string *us;
	struct usb_string *us;
	int nluns, i, rc;
	int nluns, i, rc;
	char *pathbuf;
	char *pathbuf;


	rc = fsg_num_buffers_validate(cfg->fsg_num_buffers);
	if (rc != 0)
		return ERR_PTR(rc);

	/* Find out how many LUNs there should be */
	/* Find out how many LUNs there should be */
	nluns = cfg->nluns;
	nluns = cfg->nluns;
	if (nluns < 1 || nluns > FSG_MAX_LUNS) {
	if (nluns < 1 || nluns > FSG_MAX_LUNS) {
@@ -2738,15 +2776,12 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
	fsg_common_set_sysfs(common, true);
	fsg_common_set_sysfs(common, true);
	common->state = FSG_STATE_IDLE;
	common->state = FSG_STATE_IDLE;


	common->fsg_num_buffers = cfg->fsg_num_buffers;
	rc = fsg_common_set_num_buffers(common, cfg->fsg_num_buffers);
	common->buffhds = kcalloc(common->fsg_num_buffers,
	if (rc) {
				  sizeof *(common->buffhds), GFP_KERNEL);
	if (!common->buffhds) {
		if (common->free_storage_on_release)
		if (common->free_storage_on_release)
			kfree(common);
			kfree(common);
		return ERR_PTR(-ENOMEM);
		return ERR_PTR(rc);
	}
	}

	common->ops = cfg->ops;
	common->ops = cfg->ops;
	common->private_data = cfg->private_data;
	common->private_data = cfg->private_data;


@@ -2833,21 +2868,6 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,
	}
	}
	common->nluns = nluns;
	common->nluns = nluns;


	/* Data buffers cyclic list */
	bh = common->buffhds;
	i = common->fsg_num_buffers;
	goto buffhds_first_it;
	do {
		bh->next = bh + 1;
		++bh;
buffhds_first_it:
		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
		if (unlikely(!bh->buf)) {
			rc = -ENOMEM;
			goto error_release;
		}
	} while (--i);
	bh->next = common->buffhds;


	/* Prepare inquiryString */
	/* Prepare inquiryString */
	i = get_default_bcdDevice();
	i = get_default_bcdDevice();
+2 −0
Original line number Original line Diff line number Diff line
@@ -104,6 +104,8 @@ struct fsg_common *fsg_common_init(struct fsg_common *common,


void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs);
void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs);


int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n);

void fsg_config_from_params(struct fsg_config *cfg,
void fsg_config_from_params(struct fsg_config *cfg,
			    const struct fsg_module_parameters *params,
			    const struct fsg_module_parameters *params,
			    unsigned int fsg_num_buffers);
			    unsigned int fsg_num_buffers);