All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lukasz Majewski <l.majewski@samsung.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 2/7] dfu:usb: DFU USB function (f_dfu) support for g_dnl composite gadget
Date: Wed, 04 Jul 2012 10:39:56 +0200	[thread overview]
Message-ID: <20120704103956.6a390cc6@lmajewski.digital.local> (raw)
In-Reply-To: <201207032321.56085.marex@denx.de>

Hi Marek,

> Dear Lukasz Majewski,
> 
> > Support for f_dfu USB function.
> > 
> > Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
> > Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
> > Cc: Marek Vasut <marex@denx.de>
> > ---
> [...]
> > +
> > +static const char dfu_name[] = "Device Firmware Upgrade";
> > +
> > +/* static strings, in UTF-8 */
> > +/*
> > + * dfu_genericiguration specific
> 
> 
> generi....what?

Misspelled, fixed.
> 
> > + */
> > +static struct usb_string strings_dfu_generic[] = {
> > +	[0].s = dfu_name,
> > +	{  }			/* end of list */
> > +};
> > +
> > +static struct usb_gadget_strings stringtab_dfu_generic = {
> > +	.language	= 0x0409,	/* en-us */
> > +	.strings	= strings_dfu_generic,
> > +};
> > +
> > +static struct usb_gadget_strings *dfu_generic_strings[] = {
> > +	&stringtab_dfu_generic,
> > +	NULL,
> > +};
> > +
> > +/*
> > + * usb_function specific
> > + */
> > +static struct usb_gadget_strings stringtab_dfu = {
> > +	.language	= 0x0409,	/* en-us */
> > +	/*
> > +	 * .strings
> > +	 *
> > +	 * assigned during initialization,
> > +	 * depends on number of flash entities
> > +	 *
> > +	 */
> > +};
> > +
> > +static struct usb_gadget_strings *dfu_strings[] = {
> > +	&stringtab_dfu,
> > +	NULL,
> > +};
> > +
> > +/*------------------------------------------------------------------------
> > -*/ +
> > +static void dnload_request_complete(struct usb_ep *ep, struct
> > usb_request *req) +{
> > +	struct f_dfu *f_dfu = req->context;
> > +
> > +	dfu_write(dfu_get_entity(f_dfu->altsetting), req->buf,
> > +		  req->length, f_dfu->blk_seq_num);
> > +
> > +	if (req->length == 0) {
> > +		puts("DOWNLOAD ... OK\n");
> > +		puts("Ctrl+C to exit ...\n");
> > +	}
> > +}
> > +
> > +static void handle_getstatus(struct usb_request *req)
> > +{
> > +	struct dfu_status *dstat = (struct dfu_status *)req->buf;
> > +	struct f_dfu *f_dfu = req->context;
> > +
> > +	switch (f_dfu->dfu_state) {
> > +	case DFU_STATE_dfuDNLOAD_SYNC:
> 
> What's this crazy cammel case in here ? :-)

I know, that camel case descriptions are not welcome :-),
but those are written in this way for purpose.

dfuDNLOAD_SYNC is the exact state name mentioned at DFU specification.
Other states - like dfuDNBUSY are exactly the same.

From mine experience it is more readable. Please consider for
example the Fig. A1 - Interface state transition diagram from page 28
at DFU_1.1.pdf spec (link below).

http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf

> 
> > +	case DFU_STATE_dfuDNBUSY:
> > +		f_dfu->dfu_state = DFU_STATE_dfuDNLOAD_IDLE;
> > +		break;
> > +	case DFU_STATE_dfuMANIFEST_SYNC:
> > +		break;
> > +	default:
> > +		break;
> > +	}
> > +
> > +	/* send status response */
> > +	dstat->bStatus = f_dfu->dfu_status;
> > +	/* FIXME: set dstat->bwPollTimeout */
> 
> FIXME ... so fix it ;-)
This is not u-boot related - will be removed 

> 
> > +	dstat->bState = f_dfu->dfu_state;
> > +	dstat->iString = 0;
> > +}
> > +
> > +static void handle_getstate(struct usb_request *req)
> > +{
> > +	struct f_dfu *f_dfu = req->context;
> > +
> > +	((u8 *)req->buf)[0] = f_dfu->dfu_state & 0xff;
> 
> Now ... this is ubercrazy ... can't this be made without this
> typecasting voodoo?

The problem is that req->buf is a void pointer. And the goal is to
store dfu state at 8 bits.

> 
> > +	req->actual = sizeof(u8);
> > +}
> > +
> [...]
> > +static int handle_dnload(struct usb_gadget *gadget, u16 len)
> > +{
> > +	struct usb_composite_dev *cdev = get_gadget_data(gadget);
> > +	struct usb_request *req = cdev->req;
> > +	struct f_dfu *f_dfu = req->context;
> > +
> > +	if (len == 0)
> > +		f_dfu->dfu_state = DFU_STATE_dfuMANIFEST_SYNC;
> > +
> > +	req->complete = dnload_request_complete;
> > +
> > +	return len;
> > +}
> 
> One newline too much below.
Fixed.
> 
> > +
> > +
> > +static int
> > +dfu_handle(struct usb_function *f, const struct usb_ctrlrequest
> > *ctrl) +{
> > +	struct usb_gadget *gadget = f->config->cdev->gadget;
> > +	struct usb_request *req = f->config->cdev->req;
> > +	struct f_dfu *f_dfu = f->config->cdev->req->context;
> > +	u16 len = le16_to_cpu(ctrl->wLength);
> > +	u16 w_value = le16_to_cpu(ctrl->wValue);
> > +	int value = 0;
> > +	int standard;
> > +
> > +	standard = (ctrl->bRequestType & USB_TYPE_MASK)
> > +						==
> > USB_TYPE_STANDARD; +
> > +	debug("w_value: 0x%x len: 0x%x\n", w_value, len);
> > +	debug("standard: 0x%x ctrl->bRequest: 0x%x
> > f_dfu->dfu_state: 0x%x\n",
> > +	       standard, ctrl->bRequest, f_dfu->dfu_state);
> > +
> > +	if (!standard) {
> 
> This function doesn't fit on my screen ... that means it's waaaay too
> long and shall be split into more functions ;-)
> 
> That's also remove the need for your crazy conditional assignment of
> standard.

You are correct :-). I will split the if(!standard) clause to two
separate. It shall improve readability.

> 
> > +		switch (f_dfu->dfu_state) {
> > +		case DFU_STATE_appIDLE:
> > +			switch (ctrl->bRequest) {
> > +			case USB_REQ_DFU_GETSTATUS:
> > +				handle_getstatus(req);
> > +				value = RET_STAT_LEN;
> > +				break;
> [...]
> 
> > +
> > +/*------------------------------------------------------------------------
> > -*/ +
> > +static int
> > +dfu_prepare_strings(struct f_dfu *f_dfu, int n)
> > +{
> > +	struct dfu_entity *de = NULL;
> > +	int i = 0;
> > +
> > +	f_dfu->strings = calloc(((n + 1) * sizeof(struct
> > usb_string)), 1);
> 
> calloc(n, 1) ... that's the same as malloc(), isn't it ?

I now wonder how mine mind produced this :-)

Correct version:

f_dfu->strings = calloc(sizeof(struct usb_string), n + 1);

I prefer calloc, since it delivers zeroed memory.

> 
> > +	if (!f_dfu->strings)
> > +		goto enomem;
> > +
> > +	for (i = 0; i < n; ++i) {
> > +		de = dfu_get_entity(i);
> > +		f_dfu->strings[i].s = de->name;
> > +	}
> > +
> > +	f_dfu->strings[i].id = 0;
> > +	f_dfu->strings[i].s = NULL;
> > +
> > +	return 0;
> > +
> > +enomem:
> > +	while (i)
> > +		f_dfu->strings[--i].s = NULL;
> > +
> > +	kfree(f_dfu->strings);
> > +
> > +	return -ENOMEM;
> > +}
> > +
> > +static int dfu_prepare_function(struct f_dfu *f_dfu, int n)
> > +{
> > +	struct usb_interface_descriptor *d;
> > +	int i = 0;
> > +
> > +	f_dfu->function = calloc(n * sizeof(struct
> > usb_descriptor_header *), 1);
> 
> DITTO

As above.
> 
> > +	if (!f_dfu->function)
> > +		goto enomem;
> > +
> > +	for (i = 0; i < n; ++i) {
> > +		d = kzalloc(sizeof(*d), GFP_KERNEL);
> 
> Why use the kernel alternatives ? just use malloc()/free() ?

I will use calloc(sizeof(*d), 1) to receive cleared memory.
> 
> > +		if (!d)
> > +			goto enomem;
> > +
> > +		d->bLength =		sizeof(*d);
> > +		d->bDescriptorType =	USB_DT_INTERFACE;
> > +		d->bAlternateSetting =	i;
> > +		d->bNumEndpoints =	0;
> > +		d->bInterfaceClass =	USB_CLASS_APP_SPEC;
> > +		d->bInterfaceSubClass =	1;
> > +		d->bInterfaceProtocol =	2;
> > +
> > +		f_dfu->function[i] = (struct usb_descriptor_header
> > *)d;
> > +	}
> > +	f_dfu->function[i] = NULL;
> > +
> > +	return 0;
> > +
> > +enomem:
> > +	while (i) {
> > +		kfree(f_dfu->function[--i]);
> > +		f_dfu->function[i] = NULL;
> > +	}
> > +	kfree(f_dfu->function);
> > +
> > +	return -ENOMEM;
> > +}
> > +
> > +static int dfu_bind(struct usb_configuration *c, struct
> > usb_function *f) +{
> > +	struct usb_composite_dev *cdev = c->cdev;
> > +	struct f_dfu *f_dfu = func_to_dfu(f);
> > +	int alt_num = dfu_get_alt_number();
> > +	int rv, id, i;
> > +
> > +	id = usb_interface_id(c, f);
> > +	if (id < 0)
> > +		return id;
> > +	dfu_intf_runtime.bInterfaceNumber = id;
> > +
> > +	f_dfu->dfu_state = DFU_STATE_appIDLE;
> > +	f_dfu->dfu_status = DFU_STATUS_OK;
> > +
> > +	rv = dfu_prepare_function(f_dfu, alt_num);
> > +	if (rv)
> > +		goto error;
> > +
> > +	rv = dfu_prepare_strings(f_dfu, alt_num);
> > +	if (rv)
> > +		goto error;
> > +	for (i = 0; i < alt_num; i++) {
> > +		id = usb_string_id(cdev);
> > +		if (id < 0)
> > +			return id;
> > +		f_dfu->strings[i].id = id;
> > +		((struct usb_interface_descriptor
> > *)f_dfu->function[i])
> > +			->iInterface = id;
> > +	}
> > +
> > +	stringtab_dfu.strings = f_dfu->strings;
> > +
> > +	cdev->req->context = f_dfu;
> > +
> > +	return 0;
> > +error:
> > +	return rv;
> 
> So just return the retval ...

Fixed.
> 
> > +}
> > +
> 
> Every time I review patches and find multiple small issues, I feel
> bad :-(

All will be OK :-)



-- 
Best regards,

Lukasz Majewski

Samsung Poland R&D Center | Linux Platform Group

  reply	other threads:[~2012-07-04  8:39 UTC|newest]

Thread overview: 147+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-03  9:38 [U-Boot] [PATCH 0/7] dfu:usb: Composite USB download gadget with DFU function Lukasz Majewski
2012-07-03  9:38 ` [U-Boot] [PATCH 1/7] dfu:usb: Support for g_dnl composite download gadget Lukasz Majewski
2012-07-03 18:41   ` Marek Vasut
2012-07-04  7:42     ` Lukasz Majewski
2012-07-20  4:14   ` Mike Frysinger
2012-07-23 15:25     ` Lukasz Majewski
2012-07-24 17:50       ` Mike Frysinger
2012-07-03  9:38 ` [U-Boot] [PATCH 2/7] dfu:usb: DFU USB function (f_dfu) support for g_dnl composite gadget Lukasz Majewski
2012-07-03 21:21   ` Marek Vasut
2012-07-04  8:39     ` Lukasz Majewski [this message]
2012-07-04 14:35       ` Marek Vasut
2012-07-04 15:04         ` Lukasz Majewski
2012-07-04 16:21           ` Marek Vasut
2012-07-03  9:38 ` [U-Boot] [PATCH 3/7] dfu: DFU backend implementation Lukasz Majewski
2012-07-03 21:28   ` Marek Vasut
2012-07-04  8:56     ` Lukasz Majewski
2012-07-04 14:36       ` Marek Vasut
2012-07-04 15:07         ` Lukasz Majewski
2012-07-04 16:22           ` Marek Vasut
2012-07-20  4:32   ` Mike Frysinger
2012-07-23 16:11     ` Lukasz Majewski
2012-07-03  9:38 ` [U-Boot] [PATCH 4/7] dfu: MMC specific routines for DFU operation Lukasz Majewski
2012-07-03 21:29   ` Marek Vasut
2012-07-03 21:55     ` Tom Rini
2012-07-03 22:01       ` Marek Vasut
2012-07-03 22:06         ` Tom Rini
2012-07-03 22:31           ` Marek Vasut
2012-07-03 22:33             ` Tom Rini
2012-07-03 23:07               ` Stephen Warren
2012-07-03 23:38                 ` Tom Rini
2012-07-03 23:58                   ` Stephen Warren
2012-07-04  0:13               ` Marek Vasut
2012-07-20  4:25                 ` Mike Frysinger
2012-07-04  9:10         ` Lukasz Majewski
2012-07-04 14:38           ` Marek Vasut
2012-07-04 15:13             ` Lukasz Majewski
2012-07-03  9:38 ` [U-Boot] [PATCH 5/7] dfu:cmd: Support for DFU u-boot command Lukasz Majewski
2012-07-03 21:32   ` Marek Vasut
2012-07-04  9:28     ` Lukasz Majewski
2012-07-04 14:39       ` Marek Vasut
2012-07-20  4:23         ` Mike Frysinger
2012-07-20 11:33           ` Marek Vasut
2012-07-20 14:43             ` Mike Frysinger
2012-07-20 21:11               ` Marek Vasut
2012-07-21 17:20                 ` Mike Frysinger
2012-07-21 17:21                   ` Marek Vasut
2012-07-20  4:22     ` Mike Frysinger
2012-07-20 11:35       ` Marek Vasut
2012-07-20  4:20   ` Mike Frysinger
2012-07-23 16:01     ` Lukasz Majewski
2012-07-24 18:00       ` Mike Frysinger
2012-07-24 20:48         ` Lukasz Majewski
2012-07-03  9:38 ` [U-Boot] [PATCH 6/7] arm:trats: Support for USB UDC driver at TRATS board Lukasz Majewski
2012-07-04  0:20   ` Minkyu Kang
2012-07-04  9:33     ` Lukasz Majewski
2012-07-03  9:38 ` [U-Boot] [PATCH 7/7] arm:trats: Enable g_dnl composite USB gadget with embedded DFU function on TRATS Lukasz Majewski
2012-07-04  0:22   ` Minkyu Kang
2012-07-03 12:52 ` [U-Boot] [PATCH 0/7] dfu:usb: Composite USB download gadget with DFU function Otavio Salvador
2012-07-03 12:59   ` Marek Vasut
2012-07-04 15:48 ` [U-Boot] [PATCH v2 " Lukasz Majewski
2012-07-04 15:48   ` [U-Boot] [PATCH v2 1/7] dfu:usb: Support for g_dnl composite download gadget Lukasz Majewski
2012-07-09 16:30     ` Marek Vasut
2012-07-04 15:48   ` [U-Boot] [PATCH v2 2/7] dfu:usb: DFU USB function (f_dfu) support for g_dnl composite gadget Lukasz Majewski
2012-07-09 16:34     ` Marek Vasut
2012-07-04 15:48   ` [U-Boot] [PATCH v2 3/7] dfu: DFU backend implementation Lukasz Majewski
2012-07-09 16:35     ` Marek Vasut
2012-07-27 11:58     ` Wolfgang Denk
2012-07-27 13:15       ` Lukasz Majewski
2012-07-27 13:35         ` Wolfgang Denk
2012-07-27 13:47           ` Lukasz Majewski
2012-07-04 15:48   ` [U-Boot] [PATCH v2 4/7] dfu: MMC specific routines for DFU operation Lukasz Majewski
2012-07-09 16:36     ` Marek Vasut
2012-07-10  8:45     ` Tom Rini
2012-07-10 10:38       ` Lukasz Majewski
2012-07-11 11:54         ` Tom Rini
2012-07-12 12:39           ` Lukasz Majewski
2012-07-12 12:46             ` Tom Rini
2012-07-13 10:29               ` Marek Vasut
2012-07-13 21:27                 ` Andy Fleming
2012-07-27 12:36     ` Wolfgang Denk
2012-07-27 12:43       ` Marek Vasut
2012-07-27 12:57         ` Wolfgang Denk
2012-07-27 13:15           ` Marek Vasut
2012-07-27 13:38             ` Wolfgang Denk
2012-07-27 13:33       ` Lukasz Majewski
2012-07-27 13:47         ` Wolfgang Denk
2012-07-04 15:48   ` [U-Boot] [PATCH v2 5/7] dfu:cmd: Support for DFU u-boot command Lukasz Majewski
2012-07-04 15:48   ` [U-Boot] [PATCH v2 6/7] arm:trats: Support for USB UDC driver at TRATS board Lukasz Majewski
2012-07-04 15:48   ` [U-Boot] [PATCH v2 7/7] arm:trats: Enable g_dnl composite USB gadget with embedded DFU function on TRATS Lukasz Majewski
2012-07-09 11:28   ` [U-Boot] [PATCH v2 0/7] dfu:usb: Composite USB download gadget with DFU function Lukasz Majewski
2012-07-09 11:46     ` Tom Rini
2012-07-09 16:25       ` Marek Vasut
2012-07-10  8:27         ` Lukasz Majewski
2012-07-10  9:28           ` Marek Vasut
2012-07-18 12:51 ` [U-Boot] [PATCH " Marek Vasut
2012-07-23  7:57   ` Lukasz Majewski
2012-07-23 10:57     ` Marek Vasut
2012-07-31  6:36 ` [U-Boot] [PATCH v3 0/7] dfu:usb: DFU support via USB Download gadget Lukasz Majewski
2012-07-31  6:36   ` [U-Boot] [PATCH v3 1/7] dfu:usb: Support for g_dnl composite download gadget Lukasz Majewski
2012-08-01 22:40     ` Mike Frysinger
2012-08-02  9:55       ` Lukasz Majewski
2012-07-31  6:36   ` [U-Boot] [PATCH v3 2/7] dfu:usb: DFU USB function (f_dfu) support for g_dnl composite gadget Lukasz Majewski
2012-08-01 22:45     ` Mike Frysinger
2012-08-02 10:54       ` Lukasz Majewski
2012-07-31  6:36   ` [U-Boot] [PATCH v3 3/7] dfu: DFU backend implementation Lukasz Majewski
2012-08-01 22:57     ` Mike Frysinger
2012-08-02 13:55       ` Lukasz Majewski
2012-08-03 23:19         ` Mike Frysinger
2012-08-04  7:47           ` Marek Vasut
2012-08-04 16:28             ` Mike Frysinger
2012-07-31  6:37   ` [U-Boot] [PATCH v3 4/7] dfu: MMC specific routines for DFU operation Lukasz Majewski
2012-08-01 23:00     ` Mike Frysinger
2012-08-02 14:47       ` Lukasz Majewski
2012-07-31  6:37   ` [U-Boot] [PATCH v3 5/7] dfu:cmd: Support for DFU u-boot command Lukasz Majewski
2012-07-31 17:14     ` Stephen Warren
2012-08-01  7:16       ` Lukasz Majewski
2012-08-01 17:13         ` Stephen Warren
2012-08-02  8:31           ` Lukasz Majewski
2012-08-02 15:52             ` Stephen Warren
2012-08-03  6:13               ` Lukasz Majewski
2012-08-03 15:32                 ` Stephen Warren
2012-08-06  7:13                   ` Lukasz Majewski
2012-08-01 18:04     ` Mike Frysinger
2012-08-02  7:16       ` Marek Vasut
2012-08-02 15:28         ` Lukasz Majewski
2012-08-02 17:47         ` Mike Frysinger
2012-07-31  6:37   ` [U-Boot] [PATCH v3 6/7] arm:trats: Support for USB UDC driver at TRATS board Lukasz Majewski
2012-07-31  8:31     ` Minkyu Kang
2012-07-31  6:37   ` [U-Boot] [PATCH v3 7/7] arm:trats: Enable g_dnl composite USB gadget with embedded DFU function on TRATS Lukasz Majewski
2012-07-31  8:32     ` Minkyu Kang
2012-08-03  7:45 ` [U-Boot] [PATCH v4 0/7] dfu:usb: DFU support via USB Download gadget Lukasz Majewski
2012-08-03  7:45   ` [U-Boot] [PATCH v4 1/7] dfu:usb: Support for g_dnl composite download gadget Lukasz Majewski
2012-08-03  7:45   ` [U-Boot] [PATCH v4 2/7] dfu:usb: DFU USB function (f_dfu) support for g_dnl composite gadget Lukasz Majewski
2012-08-03  7:45   ` [U-Boot] [PATCH v4 3/7] dfu: DFU backend implementation Lukasz Majewski
2012-08-03  7:45   ` [U-Boot] [PATCH v4 4/7] dfu: MMC specific routines for DFU operation Lukasz Majewski
2012-08-03  7:45   ` [U-Boot] [PATCH v4 5/7] dfu:cmd: Support for DFU u-boot command Lukasz Majewski
2012-08-03  7:45   ` [U-Boot] [PATCH v4 6/7] arm:trats: Support for USB UDC driver at TRATS board Lukasz Majewski
2012-08-03  7:45   ` [U-Boot] [PATCH v4 7/7] arm:trats: Enable g_dnl composite USB gadget with embedded DFU function on TRATS Lukasz Majewski
2012-08-06 12:41 ` [U-Boot] [PATCH v5 0/7] dfu:usb: DFU support via USB Download gadget Lukasz Majewski
2012-08-06 12:41   ` [U-Boot] [PATCH v5 1/7] dfu:usb: Support for g_dnl composite download gadget Lukasz Majewski
2012-08-06 12:41   ` [U-Boot] [PATCH v5 2/7] dfu:usb: DFU USB function (f_dfu) support for g_dnl composite gadget Lukasz Majewski
2012-08-06 12:41   ` [U-Boot] [PATCH v5 3/7] dfu: DFU backend implementation Lukasz Majewski
2012-08-06 12:41   ` [U-Boot] [PATCH v5 4/7] dfu: MMC specific routines for DFU operation Lukasz Majewski
2012-08-06 12:41   ` [U-Boot] [PATCH v5 5/7] dfu:cmd: Support for DFU u-boot command Lukasz Majewski
2012-08-06 12:41   ` [U-Boot] [PATCH v5 6/7] arm:trats: Support for USB UDC driver at TRATS board Lukasz Majewski
2012-08-06 12:41   ` [U-Boot] [PATCH v5 7/7] arm:trats: Enable g_dnl composite USB gadget with embedded DFU function on TRATS Lukasz Majewski
2012-08-06 20:31   ` [U-Boot] [PATCH v5 0/7] dfu:usb: DFU support via USB Download gadget Marek Vasut

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120704103956.6a390cc6@lmajewski.digital.local \
    --to=l.majewski@samsung.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.