Skip to contentSkip to conversation

usb: gadget: Add xilinx usb2 device support

PATCH Superseded Subbaraya Sundeep Bhatta 6 participants 9 messages Jul 22, 2014 – Sep 10, 2014 1 message not in the archive

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v4 2/2 usb: gadget: Add xilinx usb2 device support

This patch adds xilinx usb2 device driver support

---
Changes for v4:
- renamed xlnx,axi-usb2-device-4.00.a to xlnx,usb2-device-4.00.a
Changes for v3:
- Added pullup function
- Modified error handling
- Fixed all v2 comments except DMA interrupt handling since polling seems to be faster.
Please refer to below message thread:
http://www.kernelhub.org/?p=2&msg=481724

Changes for v2:
- Added Resume
- Added Remote wakeup
- Fixed v1 comments

 drivers/usb/gadget/Kconfig      |   14 +
 drivers/usb/gadget/Makefile     |    1 +
 drivers/usb/gadget/udc-xilinx.c | 2261 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 2276 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/gadget/udc-xilinx.c
drivers/usb/gadget/Kconfig +14 -0
index ba18e9c..ee919a9 100644
@@ -459,6 +459,20 @@ config USB_EG20T
 	  ML7213/ML7831 is companion chip for Intel Atom E6xx series.
 	  ML7213/ML7831 is completely compatible for Intel EG20T PCH.
 
+config USB_GADGET_XILINX
+	tristate "Xilinx USB Driver"
+	depends on COMPILE_TEST
+	help
+	  USB peripheral controller driver for Xilinx USB2 device.
+	  Xilinx USB2 device is a soft IP which supports both full
+	  and high speed USB 2.0 data transfers. It has seven configurable
+	  endpoints(bulk or interrupt or isochronous), as well as
+	  endpoint zero(for control transfers).
+
+	  Say "y" to link the driver statically, or "m" to build a
+	  dynamically linked module called "udc-xilinx" and force all
+	  gadget drivers to also be dynamically linked.
+
 #
 # LAST -- dummy/emulated controller
 #
drivers/usb/gadget/Makefile +1 -0
index 49514ea..dc6dd41 100644
@@ -35,6 +35,7 @@ obj-$(CONFIG_USB_FUSB300)	+= fusb300_udc.o
 obj-$(CONFIG_USB_FOTG210_UDC)	+= fotg210-udc.o
 obj-$(CONFIG_USB_MV_U3D)	+= mv_u3d_core.o
 obj-$(CONFIG_USB_GR_UDC)	+= gr_udc.o
+obj-$(CONFIG_USB_GADGET_XILINX)	+= udc-xilinx.o
 
 # USB Functions
 usb_f_acm-y			:= f_acm.o
drivers/usb/gadget/udc-xilinx.c new file +2261 -0

2,261 lines not on this page · raw message

Signature · 7 lines
1.7.4


--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 5 lines · Subbaraya Sundeep Bhatta
drivers/usb/gadget/udc-xilinx.c new file +5 -0
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/dma-mapping.h>
+#include "gadget_chips.h"
+#include <linux/interrupt.h>

drivers/usb/gadget/udc-xilinx.c line 33

Quoted 10 lines
drivers/usb/gadget/udc-xilinx.c new file +10 -0
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <linux/of_irq.h>
+#include <linux/prefetch.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb/gadget.h>
+

Normally we will put the includes in alphabetical because it looks good and readable.

But we have to include the local headers separately after all the main includes... #include <linux/delay.h>
#include <linux/device.h>
....
#include <linux/usb/gadget.h>

#include "gadget_chips.h"

(...)

drivers/usb/gadget/udc-xilinx.c line 1936

Quoted 9 lines
drivers/usb/gadget/udc-xilinx.c new file +8 -0
+	switch (udc->setupseqtx) {
+	case STATUS_PHASE:
+		switch (udc->setup.bRequest) {
+		case USB_REQ_SET_ADDRESS:
+			/* Set the address of the device.*/
+			udc->write_fn(udc->base_address,
+					XUSB_ADDRESS_OFFSET,
+					udc->setup.wValue);

Should match open parenthesis

udc->write_fn(udc->base_address,

	      XUSB_ADDRESS_OFFSET,
	      udc->setup.wValue);

drivers/usb/gadget/udc-xilinx.c line 1945

Quoted 9 lines
drivers/usb/gadget/udc-xilinx.c new file +9 -0
+			break;
+		case USB_REQ_SET_FEATURE:
+			if (udc->setup.bRequestType ==
+					USB_RECIP_DEVICE) {
+				if (udc->setup.wValue ==
+						USB_DEVICE_TEST_MODE)
+					udc->write_fn(udc->base_address,
+							XUSB_TESTMODE_OFFSET,
+							test_mode);

Dto

Quoted 59 lines
drivers/usb/gadget/udc-xilinx.c new file +59 -0
+			}
+			break;
+		}
+		req->usb_req.actual = req->usb_req.length;
+		xudc_done(ep0, req, 0);
+		break;
+	case DATA_PHASE:
+		if (!bytes_to_tx) {
+			/*
+			 * We're done with data transfer, next
+			 * will be zero length OUT with data toggle of
+			 * 1. Setup data_toggle.
+			 */
+			epcfgreg = udc->read_fn(udc->base_address +
+					ep0->offset);
+			epcfgreg |= XUSB_EP_CFG_DATA_TOGGLE_MASK;
+			udc->write_fn(udc->base_address, ep0->offset, epcfgreg);
+			udc->setupseqtx = STATUS_PHASE;
+		} else {
+			length = count = min_t(u32, bytes_to_tx,
+						EP0_MAX_PACKET);
+			/* Copy the data to be transmitted into the DPRAM. */
+			ep0rambase = (u8 __force *) (udc->base_address +
+					(ep0->rambase << 2));
+
+			buffer = req->usb_req.buf + req->usb_req.actual;
+			req->usb_req.actual = req->usb_req.actual + length;
+			memcpy((void *)ep0rambase, buffer, length);
+		}
+		udc->write_fn(udc->base_address, XUSB_EP_BUF0COUNT_OFFSET,
+				count);
+		udc->write_fn(udc->base_address, XUSB_BUFFREADY_OFFSET, 1);
+		break;
+	default:
+		break;
+	}
+}
+
+/**
+ * xudc_ctrl_ep_handler - Endpoint 0 interrupt handler.
+ * @udc: pointer to the udc structure.
+ * @intrstatus:	It's the mask value for the interrupt sources on endpoint 0.
+ *
+ * Processes the commands received during enumeration phase.
+ */
+static void xudc_ctrl_ep_handler(struct xusb_udc *udc, u32 intrstatus)
+{
+
+	if (intrstatus & XUSB_STATUS_SETUP_PACKET_MASK) {
+		xudc_handle_setup(udc);
+	} else {
+		if (intrstatus & XUSB_STATUS_FIFO_BUFF_RDY_MASK)
+			xudc_ep0_out(udc);
+		else if (intrstatus & XUSB_STATUS_FIFO_BUFF_FREE_MASK)
+			xudc_ep0_in(udc);
+	}
+}
+
+/**

drivers/usb/gadget/udc-xilinx.c line 2014

Quoted 10 lines
drivers/usb/gadget/udc-xilinx.c new file +10 -0
+ * xudc_nonctrl_ep_handler - Non control endpoint interrupt handler.
+ * @udc: pointer to the udc structure.
+ * @epnum: End point number for which the interrupt is to be processed
+ * @intrstatus:	mask value for interrupt sources of endpoints other
+ *		than endpoint 0.
+ *
+ * Processes the buffer completion interrupts.
+ */
+static void xudc_nonctrl_ep_handler(struct xusb_udc *udc, u8 epnum,
+		u32 intrstatus)

Should match open parenthesis:

static void xudc_nonctrl_ep_handler(struct xusb_udc *udc, u8 epnum,
u32 intrstatus)

Quoted 23 lines
drivers/usb/gadget/udc-xilinx.c new file +23 -0
+{
+
+	struct xusb_req *req;
+	struct xusb_ep *ep;
+
+	ep = &udc->ep[epnum];
+	/* Process the End point interrupts.*/
+	if (intrstatus & (XUSB_STATUS_EP0_BUFF1_COMP_MASK << epnum))
+		ep->buffer0ready = 0;
+	if (intrstatus & (XUSB_STATUS_EP0_BUFF2_COMP_MASK << epnum))
+		ep->buffer1ready = 0;
+
+	if (list_empty(&ep->queue))
+		return;
+
+	req = list_first_entry(&ep->queue, struct xusb_req, queue);
+
+	if (ep->is_in)
+		xudc_write_fifo(ep, req);
+	else
+		xudc_read_fifo(ep, req);
+}
+

(...)

drivers/usb/gadget/udc-xilinx.c line 2180

Quoted 3 lines
drivers/usb/gadget/udc-xilinx.c new file +3 -0
+
+	/* buffer for data of get_status request */
+	buff = kzalloc(2, GFP_KERNEL);

define proper macro for '2'. Also we can use devm_kzalloc().

Also one more thing: if we use kzalloc for allocating the 2 bytes no where i found that releasing the buffer on error path.

drivers/usb/gadget/udc-xilinx.c line 2193

Quoted 13 lines
drivers/usb/gadget/udc-xilinx.c new file +13 -0
+	if (buff == NULL) {
+		ret = -ENOMEM;
+		goto fail;
+	}
+	/* Dummy request ready, free this in remove */
+	udc->req->usb_req.buf = buff;
+
+	/* Set device address to 0.*/
+	udc->write_fn(udc->base_address, XUSB_ADDRESS_OFFSET, 0);
+
+	ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
+	if (ret)
+		goto fail;

If it fails we have to free the buff. see above..

Quoted 25 lines
drivers/usb/gadget/udc-xilinx.c new file +25 -0
+
+	udc->dev = &udc->gadget.dev;
+
+	/* Enable the interrupts.*/
+	ier = XUSB_STATUS_GLOBAL_INTR_MASK | XUSB_STATUS_INTR_EVENT_MASK |
+		XUSB_STATUS_FIFO_BUFF_RDY_MASK |
+		XUSB_STATUS_FIFO_BUFF_FREE_MASK |
+		XUSB_STATUS_SETUP_PACKET_MASK |
+		XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK;
+
+	udc->write_fn(udc->base_address, XUSB_IER_OFFSET, ier);
+
+	platform_set_drvdata(pdev, udc);
+
+	dev_dbg(&pdev->dev, "%s at 0x%08X mapped to 0x%08X %s\n",
+		driver_name, (u32)res->start,
+		(u32 __force)udc->base_address,
+		udc->dma_enabled ? "with DMA" : "without DMA");
+
+	return 0;
+fail:
+	dev_err(&pdev->dev, "probe failed, %d\n", ret);
+	return ret;
+}
+

drivers/usb/gadget/udc-xilinx.c line 2228

Quoted 10 lines
drivers/usb/gadget/udc-xilinx.c new file +10 -0
+/**
+ * xudc_remove - Releases the resources allocated during the initialization.
+ * @pdev: pointer to the platform device structure.
+ *
+ * Return: 0 always
+ */
+static int xudc_remove(struct platform_device *pdev)
+{
+	struct xusb_udc *udc = platform_get_drvdata(pdev);
+	void *buf = udc->req->usb_req.buf;

No need of creating another "void *". We can directly free it. (It is u8 *)

Quoted 12 lines
drivers/usb/gadget/udc-xilinx.c new file +12 -0
+
+	usb_del_gadget_udc(&udc->gadget);
+
+	/* free memory allocated for dummy request buffer */
+	kfree(buf);
+	/* free memory allocated for dummy request */
+	kfree(udc->req);
+
+	return 0;
+}
+
+/* Match table for of_platform binding */

drivers/usb/gadget/udc-xilinx.c line 2250

Quoted 10 lines
drivers/usb/gadget/udc-xilinx.c new file +10 -0
+static const struct of_device_id usb_of_match[] = {
+	{ .compatible = "xlnx,usb2-device-4.00.a", },
+	{ /* end of list */ },
+};
+MODULE_DEVICE_TABLE(of, usb_of_match);
+
+static struct platform_driver xudc_driver = {
+	.driver = {
+		.name = driver_name,
+		.owner = THIS_MODULE,

We can drop the owner field update... It updated automatically by module_platform_driver().

Please run checkpatch.pl on this patch. You may get more warnings and errors.

Signature · 7 lines
Regards,
Varka Bhadram.

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

Hi,

Quoted 29 lines · Varka Bhadram
Quoted 6 lines · Subbaraya Sundeep Bhatta
drivers/usb/gadget/udc-xilinx.c new file +5 -0
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/dma-mapping.h>
+#include "gadget_chips.h"
+#include <linux/interrupt.h>

drivers/usb/gadget/udc-xilinx.c line 33

Quoted 10 lines
drivers/usb/gadget/udc-xilinx.c new file +10 -0
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <linux/of_irq.h>
+#include <linux/prefetch.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb/gadget.h>
+

Normally we will put the includes in alphabetical because it looks good and readable.

But we have to include the local headers separately after all the main includes...
#include <linux/delay.h>
#include <linux/device.h>
....
#include <linux/usb/gadget.h>

Quoted 4 lines

#include "gadget_chips.h"

(...)

Ok

Quoted 12 lines

drivers/usb/gadget/udc-xilinx.c line 1936

Quoted 10 lines
drivers/usb/gadget/udc-xilinx.c new file +8 -0
+       switch (udc->setupseqtx) {
+       case STATUS_PHASE:
+               switch (udc->setup.bRequest) {
+               case USB_REQ_SET_ADDRESS:
+                       /* Set the address of the device.*/
+                       udc->write_fn(udc->base_address,
+                                       XUSB_ADDRESS_OFFSET,
+                                       udc->setup.wValue);
Quoted 1 line

Should match open parenthesis

not necessary, as per coding style spaces should not be used -- Outside of comments, documentation and except in Kconfig, spaces are never used for indentation.

Quoted 19 lines

udc->write_fn(udc->base_address,

              XUSB_ADDRESS_OFFSET,

              udc->setup.wValue);
Quoted 10 lines
drivers/usb/gadget/udc-xilinx.c new file +8 -0
+                       break;
+               case USB_REQ_SET_FEATURE:
+                       if (udc->setup.bRequestType ==
+                                       USB_RECIP_DEVICE) {
+                               if (udc->setup.wValue ==
+                                               USB_DEVICE_TEST_MODE)
+                                       udc->write_fn(udc->base_address,
+

XUSB_TESTMODE_OFFSET,
+ test_mode);

Quoted 1 line

Dto

not necessary

Quoted 72 lines
Quoted 72 lines
drivers/usb/gadget/udc-xilinx.c new file +17 -0
+                       }
+                       break;
+               }
+               req->usb_req.actual = req->usb_req.length;
+               xudc_done(ep0, req, 0);
+               break;
+       case DATA_PHASE:
+               if (!bytes_to_tx) {
+                       /*
+                        * We're done with data transfer, next
+                        * will be zero length OUT with data toggle of
+                        * 1. Setup data_toggle.
+                        */
+                       epcfgreg = udc->read_fn(udc->base_address +
+                                       ep0->offset);
+                       epcfgreg |= XUSB_EP_CFG_DATA_TOGGLE_MASK;
+                       udc->write_fn(udc->base_address, ep0->offset,

epcfgreg);

drivers/usb/gadget/udc-xilinx.c new file +5 -0
+                       udc->setupseqtx = STATUS_PHASE;
+               } else {
+                       length = count = min_t(u32, bytes_to_tx,
+                                               EP0_MAX_PACKET);
+                       /* Copy the data to be transmitted into the DPRAM.

*/

drivers/usb/gadget/udc-xilinx.c new file +5 -0
+                       ep0rambase = (u8 __force *) (udc->base_address +
+                                       (ep0->rambase << 2));
+
+                       buffer = req->usb_req.buf + req->usb_req.actual;
+                       req->usb_req.actual = req->usb_req.actual +

length;

drivers/usb/gadget/udc-xilinx.c new file +5 -0
+                       memcpy((void *)ep0rambase, buffer, length);
+               }
+               udc->write_fn(udc->base_address, XUSB_EP_BUF0COUNT_OFFSET,
+                               count);
+               udc->write_fn(udc->base_address, XUSB_BUFFREADY_OFFSET,

1);

drivers/usb/gadget/udc-xilinx.c new file +10 -0
+               break;
+       default:
+               break;
+       }
+}
+
+/**
+ * xudc_ctrl_ep_handler - Endpoint 0 interrupt handler.
+ * @udc: pointer to the udc structure.
+ * @intrstatus:        It's the mask value for the interrupt sources on

endpoint 0.
+ *
+ * Processes the commands received during enumeration phase.
+ */

drivers/usb/gadget/udc-xilinx.c new file +18 -0
+static void xudc_ctrl_ep_handler(struct xusb_udc *udc, u32 intrstatus)
+{
+
+       if (intrstatus & XUSB_STATUS_SETUP_PACKET_MASK) {
+               xudc_handle_setup(udc);
+       } else {
+               if (intrstatus & XUSB_STATUS_FIFO_BUFF_RDY_MASK)
+                       xudc_ep0_out(udc);
+               else if (intrstatus & XUSB_STATUS_FIFO_BUFF_FREE_MASK)
+                       xudc_ep0_in(udc);
+       }
+}
+
+/**
+ * xudc_nonctrl_ep_handler - Non control endpoint interrupt handler.
+ * @udc: pointer to the udc structure.
+ * @epnum: End point number for which the interrupt is to be processed
+ * @intrstatus:        mask value for interrupt sources of endpoints

other
+ * than endpoint 0.

Quoted 8 lines

drivers/usb/gadget/udc-xilinx.c line 2014

Quoted 5 lines

+ *
+ * Processes the buffer completion interrupts.
+ */

drivers/usb/gadget/udc-xilinx.c new file +2 -0
+static void xudc_nonctrl_ep_handler(struct xusb_udc *udc, u8 epnum,
+               u32 intrstatus)

Should match open parenthesis:

not necessary

Quoted 33 lines

static void xudc_nonctrl_ep_handler(struct xusb_udc *udc, u8 epnum,
u32 intrstatus)

Quoted 23 lines
drivers/usb/gadget/udc-xilinx.c new file +23 -0
+{
+
+       struct xusb_req *req;
+       struct xusb_ep *ep;
+
+       ep = &udc->ep[epnum];
+       /* Process the End point interrupts.*/
+       if (intrstatus & (XUSB_STATUS_EP0_BUFF1_COMP_MASK << epnum))
+               ep->buffer0ready = 0;
+       if (intrstatus & (XUSB_STATUS_EP0_BUFF2_COMP_MASK << epnum))
+               ep->buffer1ready = 0;
+
+       if (list_empty(&ep->queue))
+               return;
+
+       req = list_first_entry(&ep->queue, struct xusb_req, queue);
+
+       if (ep->is_in)
+               xudc_write_fifo(ep, req);
+       else
+               xudc_read_fifo(ep, req);
+}
+

(...)

Quoted 6 lines

drivers/usb/gadget/udc-xilinx.c line 2180

Quoted 3 lines
drivers/usb/gadget/udc-xilinx.c new file +3 -0
+
+       /* buffer for data of get_status request */
+       buff = kzalloc(2, GFP_KERNEL);

define proper macro for '2'. Also we can use devm_kzalloc().

Ok

Quoted 5 lines

Also one more thing: if we use kzalloc for allocating the 2 bytes no where i found that releasing the buffer on error path.

Quoted 16 lines

drivers/usb/gadget/udc-xilinx.c line 2193

Quoted 13 lines
drivers/usb/gadget/udc-xilinx.c new file +13 -0
+       if (buff == NULL) {
+               ret = -ENOMEM;
+               goto fail;
+       }
+       /* Dummy request ready, free this in remove */
+       udc->req->usb_req.buf = buff;
+
+       /* Set device address to 0.*/
+       udc->write_fn(udc->base_address, XUSB_ADDRESS_OFFSET, 0);
+
+       ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
+       if (ret)
+               goto fail;

If it fails we have to free the buff. see above..

Yeah will change.

Quoted 33 lines
Quoted 33 lines
drivers/usb/gadget/udc-xilinx.c new file +27 -0
+
+       udc->dev = &udc->gadget.dev;
+
+       /* Enable the interrupts.*/
+       ier = XUSB_STATUS_GLOBAL_INTR_MASK | XUSB_STATUS_INTR_EVENT_MASK |
+               XUSB_STATUS_FIFO_BUFF_RDY_MASK |
+               XUSB_STATUS_FIFO_BUFF_FREE_MASK |
+               XUSB_STATUS_SETUP_PACKET_MASK |
+               XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK;
+
+       udc->write_fn(udc->base_address, XUSB_IER_OFFSET, ier);
+
+       platform_set_drvdata(pdev, udc);
+
+       dev_dbg(&pdev->dev, "%s at 0x%08X mapped to 0x%08X %s\n",
+               driver_name, (u32)res->start,
+               (u32 __force)udc->base_address,
+               udc->dma_enabled ? "with DMA" : "without DMA");
+
+       return 0;
+fail:
+       dev_err(&pdev->dev, "probe failed, %d\n", ret);
+       return ret;
+}
+
+/**
+ * xudc_remove - Releases the resources allocated during the

initialization.
+ * @pdev: pointer to the platform device structure.
+ *
+ * Return: 0 always

Quoted 8 lines

drivers/usb/gadget/udc-xilinx.c line 2228

Quoted 5 lines

+ */

drivers/usb/gadget/udc-xilinx.c new file +4 -0
+static int xudc_remove(struct platform_device *pdev)
+{
+       struct xusb_udc *udc = platform_get_drvdata(pdev);
+       void *buf = udc->req->usb_req.buf;

No need of creating another "void *". We can directly free it. (It is u8 *)

Ofcourse but as per Felipe's comments declared local variables for readability

Quoted 20 lines
Quoted 20 lines
drivers/usb/gadget/udc-xilinx.c new file +18 -0
+
+       usb_del_gadget_udc(&udc->gadget);
+
+       /* free memory allocated for dummy request buffer */
+       kfree(buf);
+       /* free memory allocated for dummy request */
+       kfree(udc->req);
+
+       return 0;
+}
+
+/* Match table for of_platform binding */
+static const struct of_device_id usb_of_match[] = {
+       { .compatible = "xlnx,usb2-device-4.00.a", },
+       { /* end of list */ },
+};
+MODULE_DEVICE_TABLE(of, usb_of_match);
+
Quoted 8 lines

drivers/usb/gadget/udc-xilinx.c line 2250

Quoted 4 lines
drivers/usb/gadget/udc-xilinx.c new file +4 -0
+static struct platform_driver xudc_driver = {
+       .driver = {
+               .name = driver_name,
+               .owner = THIS_MODULE,

We can drop the owner field update... It updated automatically by module_platform_driver().

Ok

Quoted 4 lines

Please run checkpatch.pl on this patch. You may get more warnings and errors.

Yes ran before sending this out. 0 errors and 0 warnings.

Thanks,
Sundeep.B.S.

Quoted 7 lines

--
Regards,
Varka Bhadram.

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
In reply to Subbaraya Sundeep Bhatta

Hi,

Quoted 1 line · Subbaraya Sundeep Bhatta

This patch adds xilinx usb2 device driver support

Add some more information here, please. Copying the text from the Kconfig option is already a good start.

Quoted 5 lines
 drivers/usb/gadget/Kconfig      |   14 +
 drivers/usb/gadget/Makefile     |    1 +
 drivers/usb/gadget/udc-xilinx.c | 2261 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 2276 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/gadget/udc-xilinx.c

As your driver has a DT binding, you have to describe it in Documentation/devicetree/bindings.

drivers/usb/gadget/Kconfig config USB_EG20T · line 464

Quoted 9 lines
drivers/usb/gadget/Kconfig +3 -0
@@ -459,6 +459,20 @@ config USB_EG20T
 	  ML7213/ML7831 is companion chip for Intel Atom E6xx series.
 	  ML7213/ML7831 is completely compatible for Intel EG20T PCH.
 
+config USB_GADGET_XILINX
+	tristate "Xilinx USB Driver"
+	depends on COMPILE_TEST

Why would you depend on that?

Also, your code uses device tree functions unconditionally, which is fine, but it must hence depend on 'OF'.

Quoted 8 lines
drivers/usb/gadget/Kconfig +8 -0
+struct xusb_ep {
+	struct usb_ep ep_usb;
+	struct list_head queue;
+	struct xusb_udc *udc;
+	const struct usb_endpoint_descriptor *desc;
+	u32 rambase;
+	u32 offset;
+	char name[4];

drivers/usb/gadget/Kconfig config USB_EG20T

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+	u16 epnumber;
+	u16 maxpacket;
+	u16 buffer0count;
+	u16 buffer1count;
+	u8 buffer0ready;
+	u8 buffer1ready;
+	u8 eptype;
+	u8 curbufnum;
+	u8 is_in;
+	u8 is_iso;

Some of those could probably become booleans.

Quoted 12 lines
drivers/usb/gadget/Kconfig +12 -0
+struct xusb_udc {
+	struct usb_gadget gadget;
+	struct xusb_ep ep[8];
+	struct usb_gadget_driver *driver;
+	struct usb_ctrlrequest setup;
+	struct xusb_req *req;
+	struct device *dev;
+	u32 usb_state;
+	u32 remote_wkp;
+	u32 setupseqtx;
+	u32 setupseqrx;
+	void __iomem *base_address;

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+	spinlock_t lock;
+	bool dma_enabled;
+
+	unsigned int (*read_fn)(void __iomem *);
+	void (*write_fn)(void __iomem *, u32, u32);
+};
+
+/* Endpoint buffer start addresses in the core */
+static u32 rambase[8] = { 0x22, 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500,
+			0x1600 };

As stated by Peter, indenting such lines to match the position of the line before makes such code much prettier and more readable. It's also done in the majority of drivers.

This counts for many locations in your code.

drivers/usb/gadget/Kconfig

Quoted 7 lines
drivers/usb/gadget/Kconfig +7 -0
+/* Control endpoint configuration.*/
+static const struct usb_endpoint_descriptor config_bulk_out_desc = {
+	.bLength		= USB_DT_ENDPOINT_SIZE,
+	.bDescriptorType	= USB_DT_ENDPOINT,
+	.bEndpointAddress	= USB_DIR_OUT,
+	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
+	.wMaxPacketSize		= cpu_to_le16(64),

Why not use EP0_MAX_PACKET here?

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+static void xudc_wrstatus(struct xusb_udc *udc)
+{
+	struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
+	u32 epcfgreg;
+
+	epcfgreg = udc->read_fn(udc->base_address + ep0->offset)|
+			XUSB_EP_CFG_DATA_TOGGLE_MASK;
+	udc->write_fn(udc->base_address, ep0->offset, epcfgreg);
+	udc->write_fn(udc->base_address, ep0->offset + XUSB_EP_BUF0COUNT_OFFSET,
+			0);

Just a nit, but renaming 'base_address' to something like 'base' or 'addr' would help you get around or maximum line constraint here and in some other places.

Quoted 33 lines
drivers/usb/gadget/Kconfig +33 -0
+static int xudc_dma_send(struct xusb_ep *ep, struct xusb_req *req,
+		u8 *buffer, u32 length)
+{
+	u32 *eprambase;
+	dma_addr_t src;
+	dma_addr_t dst;
+	int ret;
+	struct xusb_udc *udc = ep->udc;
+
+	src = req->usb_req.dma + req->usb_req.actual;
+	if (req->usb_req.length)
+		dma_sync_single_for_device(udc->dev, src,
+						length, DMA_TO_DEVICE);
+	if (!ep->curbufnum && !ep->buffer0ready) {
+		/* Get the Buffer address and copy the transmit data.*/
+		eprambase = (u32 __force *)(udc->base_address +
+				ep->rambase);
+		dst = virt_to_phys(eprambase);
+		udc->write_fn(udc->base_address, ep->offset +
+				XUSB_EP_BUF0COUNT_OFFSET, length);
+		udc->write_fn(udc->base_address, XUSB_DMA_CONTROL_OFFSET,
+				XUSB_DMA_BRR_CTRL | (1 << ep->epnumber));
+		ep->buffer0ready = 1;
+		ep->curbufnum = 1;
+	} else if (ep->curbufnum && !ep->buffer1ready) {
+		/* Get the Buffer address and copy the transmit data.*/
+		eprambase = (u32 __force *)(udc->base_address +
+				ep->rambase + ep->ep_usb.maxpacket);
+		dst = virt_to_phys(eprambase);
+		udc->write_fn(udc->base_address, ep->offset +
+				XUSB_EP_BUF1COUNT_OFFSET, length);
+		udc->write_fn(udc->base_address, XUSB_DMA_CONTROL_OFFSET,
+				XUSB_DMA_BRR_CTRL | (1 << (ep->epnumber +

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+				XUSB_STATUS_EP_BUFF2_SHIFT)));
+		ep->buffer1ready = 1;
+		ep->curbufnum = 0;
+	} else {
+		/* None of ping pong buffers are ready currently .*/
+		return -EAGAIN;
+	}
+
+	ret = xudc_start_dma(ep, src, dst, length);
+	return ret;

return xudc_start_dma(ep, src, dst, length);

... so you don't need 'ret' at all.

Quoted 29 lines
drivers/usb/gadget/Kconfig +29 -0
+static int xudc_dma_receive(struct xusb_ep *ep, struct xusb_req *req,
+		u8 *buffer, u32 length)
+{
+	u32 *eprambase;
+	dma_addr_t src;
+	dma_addr_t dst;
+	int ret = 0;
+	struct xusb_udc *udc = ep->udc;
+
+	dst = req->usb_req.dma + req->usb_req.actual;
+
+	if (!ep->curbufnum && !ep->buffer0ready) {
+		/* Get the Buffer address and copy the transmit data */
+		eprambase = (u32 __force *)(udc->base_address +
+				ep->rambase);
+		src = virt_to_phys(eprambase);
+		udc->write_fn(udc->base_address, XUSB_DMA_CONTROL_OFFSET,
+				XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
+				(1 << ep->epnumber));
+		ep->buffer0ready = 1;
+		ep->curbufnum = 1;
+	} else if (ep->curbufnum && !ep->buffer1ready) {
+		/* Get the Buffer address and copy the transmit data */
+		eprambase = (u32 __force *)(udc->base_address +
+				ep->rambase + ep->ep_usb.maxpacket);
+		src = virt_to_phys(eprambase);
+		udc->write_fn(udc->base_address, XUSB_DMA_CONTROL_OFFSET,
+				XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
+				(1 << (ep->epnumber +

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+				XUSB_STATUS_EP_BUFF2_SHIFT)));
+		ep->buffer1ready = 1;
+		ep->curbufnum = 0;
+	} else {
+		/* None of the ping-pong buffers are ready currently */
+		return -EAGAIN;
+	}
+
+	ret = xudc_start_dma(ep, src, dst, length);
+	return ret;

Dito.

Quoted 26 lines
drivers/usb/gadget/Kconfig +26 -0
+static int xudc_eptxrx(struct xusb_ep *ep, struct xusb_req *req,
+		u8 *bufferptr, u32 bufferlen)
+{
+	u32 *eprambase;
+	u32 bytestosend;
+	u8 *temprambase;
+	int rc = 0;
+	struct xusb_udc *udc = ep->udc;
+
+	bytestosend = bufferlen;
+	if (udc->dma_enabled) {
+		if (ep->is_in)
+			rc = xudc_dma_send(ep, req, bufferptr, bufferlen);
+		else
+			rc = xudc_dma_receive(ep, req, bufferptr, bufferlen);
+		return rc;
+	}
+	/* Put the transmit buffer into the correct ping-pong buffer.*/
+	if (!ep->curbufnum && !ep->buffer0ready) {
+		/* Get the Buffer address and copy the transmit data.*/
+		eprambase = (u32 __force *)(udc->base_address + ep->rambase);
+		while (bytestosend > 3) {
+			if (ep->is_in)
+				*eprambase++ = *(u32 *)bufferptr;
+			else
+				*(u32 *)bufferptr = *eprambase++;

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+			bufferptr += 4;
+			bytestosend -= 4;
+		}
+		temprambase = (u8 *)eprambase;
+		while (bytestosend--) {
+			if (ep->is_in)
+				*temprambase++ = *bufferptr++;
+			else
+				*bufferptr++ = *temprambase++;
+		}

Why not use memcpy()?

Quoted 20 lines
drivers/usb/gadget/Kconfig +20 -0
+		/*
+		 * Set the Buffer count register with transmit length
+		 * and enable the buffer for transmission.
+		 */
+		if (ep->is_in)
+			udc->write_fn(udc->base_address, ep->offset +
+					XUSB_EP_BUF0COUNT_OFFSET, bufferlen);
+		udc->write_fn(udc->base_address, XUSB_BUFFREADY_OFFSET,
+				1 << ep->epnumber);
+		ep->buffer0ready = 1;
+		ep->curbufnum = 1;
+	} else if ((ep->curbufnum == 1) && (!ep->buffer1ready)) {
+		/* Get the Buffer address and copy the transmit data.*/
+		eprambase = (u32 __force *)(udc->base_address + ep->rambase +
+				ep->ep_usb.maxpacket);
+		while (bytestosend > 3) {
+			if (ep->is_in)
+				*eprambase++ = *(u32 *)bufferptr;
+			else
+				*(u32 *)bufferptr = *eprambase++;

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+			bufferptr += 4;
+			bytestosend -= 4;
+		}
+		temprambase = (u8 *)eprambase;
+		while (bytestosend--) {
+			if (ep->is_in)
+				*temprambase++ = *bufferptr++;
+			else
+				*bufferptr++ = *temprambase++;
+		}

memcpy()?

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+/**
+ * xudc_read_fifo - Reads the data from the given endpoint buffer.
+ * @ep: pointer to the usb device endpoint structure.
+ * @req: pointer to the usb request structure.
+ *
+ * Return: 0 if request is completed and -EAGAIN if not completed.
+ *
+ * Pulls OUT packet data from the endpoint buffer.
+ */
+static int xudc_read_fifo(struct xusb_ep *ep, struct xusb_req *req)

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+{
+	u8 *buf;
+	u32 is_short, count, bufferspace;
+	u8 bufoffset;
+	u8 two_pkts = 0;
+	int ret;
+	int retval = -EAGAIN;
+	struct xusb_udc *udc = ep->udc;
+
+	if ((ep->buffer0ready == 1) && (ep->buffer1ready == 1)) {

if (ep->buffer0ready && ep->buffer1ready)

is shorter and easier to read.

drivers/usb/gadget/Kconfig

Quoted 9 lines
drivers/usb/gadget/Kconfig +9 -0
+static int xudc_ep_set_halt(struct usb_ep *_ep, int value)
+{
+	struct xusb_ep *ep = to_xusb_ep(_ep);
+	struct xusb_udc *udc;
+	unsigned long flags;
+	u32 epcfgreg;
+
+	if (!_ep || (!ep->desc && ep->epnumber)) {
+		pr_debug("%s: bad ep or descriptor\n", __func__);

dev_dbg()

Quoted 30 lines
drivers/usb/gadget/Kconfig +30 -0
+static int __xudc_ep_enable(struct xusb_ep *ep,
+		const struct usb_endpoint_descriptor *desc)
+{
+	struct xusb_udc *udc = ep->udc;
+	u32 tmp;
+	u8 eptype = 0;
+	u32 epcfg;
+	u32 ier;
+
+	ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
+	/* Bit 3...0:endpoint number */
+	ep->epnumber = (desc->bEndpointAddress & 0x0f);
+	ep->desc = desc;
+	ep->ep_usb.desc = desc;
+	tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
+	ep->ep_usb.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
+
+	switch (tmp) {
+	case USB_ENDPOINT_XFER_CONTROL:
+		dev_dbg(udc->dev, "only one control endpoint\n");
+		/* NON- ISO */
+		eptype = 0;
+		return -EINVAL;
+	case USB_ENDPOINT_XFER_INT:
+		/* NON- ISO */
+		eptype = 0;
+		if (ep->ep_usb.maxpacket > 64)
+			goto bogus_max;
+		break;
+	case USB_ENDPOINT_XFER_BULK:

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+		/* NON- ISO */
+		eptype = 0;
+		switch (ep->ep_usb.maxpacket) {
+		case 8:
+		case 16:
+		case 32:
+		case 64:
+		case 512:
+			goto ok;
+		}

Nit: With is_power_of_2() and some min/max checks, you wouldn't need a switch here, and hence the jump label could go away.

Quoted 11 lines
drivers/usb/gadget/Kconfig +11 -0
+bogus_max:
+		dev_dbg(udc->dev, "bogus maxpacket %d\n",
+			ep->ep_usb.maxpacket);
+		return -EINVAL;
+	case USB_ENDPOINT_XFER_ISOC:
+		/* ISO */
+		eptype = 1;
+		ep->is_iso = 1;
+		break;
+	}
+ok:

...

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+static int xudc_ep_enable(struct usb_ep *_ep,
+		const struct usb_endpoint_descriptor *desc)
+{
+	struct xusb_ep *ep;
+	struct xusb_udc *udc;
+	unsigned long flags;
+	int ret;
+
+	if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
+		pr_debug("%s: bad ep or descriptor\n", __func__);

dev_dbg()

drivers/usb/gadget/Kconfig

Quoted 9 lines
drivers/usb/gadget/Kconfig +9 -0
+static int xudc_ep_disable(struct usb_ep *_ep)
+{
+	struct xusb_ep *ep;
+	unsigned long flags;
+	u32 epcfg;
+	struct xusb_udc *udc;
+
+	if (!_ep) {
+		pr_debug("%s: invalid ep\n", __func__);

dev_dbg()

Quoted 19 lines
drivers/usb/gadget/Kconfig +19 -0
+static int __xudc_ep0_queue(struct xusb_ep *ep0, struct xusb_req *req)
+{
+	struct xusb_udc *udc = ep0->udc;
+	u32 length;
+	u8 *corebuf;
+
+	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
+		dev_dbg(udc->dev, "%s, bogus device state\n",
+			__func__);
+		return -EINVAL;
+	}
+	if (!list_empty(&ep0->queue)) {
+		dev_dbg(udc->dev, "%s:ep0 busy\n", __func__);
+		return -EBUSY;
+	}
+
+	req->usb_req.status = -EINPROGRESS;
+	req->usb_req.actual = 0;
+

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+	list_add_tail(&req->queue, &ep0->queue);
+
+	if (udc->setup.bRequestType & USB_DIR_IN) {
+		prefetch(req->usb_req.buf);
+		length = req->usb_req.length;
+		corebuf = (void __force *) ((ep0->rambase << 2) +
+				udc->base_address);
+		length = req->usb_req.actual = min_t(u32, length,
+						EP0_MAX_PACKET);
+		memcpy((void *)corebuf, req->usb_req.buf, length);

No need for the cast.

Quoted 18 lines
drivers/usb/gadget/Kconfig +18 -0
+		udc->write_fn(udc->base_address, XUSB_EP_BUF0COUNT_OFFSET,
+				length);
+		udc->write_fn(udc->base_address, XUSB_BUFFREADY_OFFSET, 1);
+	} else {
+		if (udc->setup.wLength) {
+			/* Enable EP0 buffer to receive data */
+			udc->write_fn(udc->base_address,
+					XUSB_EP_BUF0COUNT_OFFSET, 0);
+			udc->write_fn(udc->base_address,
+					XUSB_BUFFREADY_OFFSET, 1);
+		} else {
+			xudc_wrstatus(udc);
+		}
+	}
+
+	return 0;
+}
+

...

Quoted 6 lines
drivers/usb/gadget/Kconfig +6 -0
+/**
+ * xudc_handle_setup - Processes the setup packet.
+ * @udc: pointer to the usb device controller structure.
+ *
+ * Process setup packet and delegate to gadget layer.
+ */

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+static void xudc_handle_setup(struct xusb_udc *udc)
+{
+	struct xusb_ep *ep0 = &udc->ep[0];
+	struct usb_ctrlrequest setup;
+	u32 *ep0rambase;
+
+	/* Load up the chapter 9 command buffer.*/
+	ep0rambase = (u32 __force *) (udc->base_address +
+				  XUSB_SETUP_PKT_ADDR_OFFSET);
+	memcpy((void *)&setup, (void *)ep0rambase, 8);

Dito.

Quoted 50 lines
drivers/usb/gadget/Kconfig +50 -0
+static void xudc_ep0_in(struct xusb_udc *udc)
+{
+	struct xusb_ep *ep0 = &udc->ep[0];
+	struct xusb_req *req;
+	unsigned int bytes_to_tx;
+	void *buffer;
+	u32 epcfgreg;
+	u16 count = 0;
+	u16 length;
+	u8 *ep0rambase;
+	u8 test_mode = udc->setup.wIndex >> 8;
+
+	req = list_first_entry(&ep0->queue, struct xusb_req, queue);
+	bytes_to_tx = req->usb_req.length - req->usb_req.actual;
+
+	switch (udc->setupseqtx) {
+	case STATUS_PHASE:
+		switch (udc->setup.bRequest) {
+		case USB_REQ_SET_ADDRESS:
+			/* Set the address of the device.*/
+			udc->write_fn(udc->base_address,
+					XUSB_ADDRESS_OFFSET,
+					udc->setup.wValue);
+			break;
+		case USB_REQ_SET_FEATURE:
+			if (udc->setup.bRequestType ==
+					USB_RECIP_DEVICE) {
+				if (udc->setup.wValue ==
+						USB_DEVICE_TEST_MODE)
+					udc->write_fn(udc->base_address,
+							XUSB_TESTMODE_OFFSET,
+							test_mode);
+			}
+			break;
+		}
+		req->usb_req.actual = req->usb_req.length;
+		xudc_done(ep0, req, 0);
+		break;
+	case DATA_PHASE:
+		if (!bytes_to_tx) {
+			/*
+			 * We're done with data transfer, next
+			 * will be zero length OUT with data toggle of
+			 * 1. Setup data_toggle.
+			 */
+			epcfgreg = udc->read_fn(udc->base_address +
+					ep0->offset);
+			epcfgreg |= XUSB_EP_CFG_DATA_TOGGLE_MASK;
+			udc->write_fn(udc->base_address, ep0->offset, epcfgreg);
+			udc->setupseqtx = STATUS_PHASE;

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+		} else {
+			length = count = min_t(u32, bytes_to_tx,
+						EP0_MAX_PACKET);
+			/* Copy the data to be transmitted into the DPRAM. */
+			ep0rambase = (u8 __force *) (udc->base_address +
+					(ep0->rambase << 2));
+
+			buffer = req->usb_req.buf + req->usb_req.actual;
+			req->usb_req.actual = req->usb_req.actual + length;
+			memcpy((void *)ep0rambase, buffer, length);

Dito.

+/**
+ * xudc_eps_init - initialize endpoints.
+ * @udc: pointer to the usb device controller structure.
+ */
+static void xudc_eps_init(struct xusb_udc *udc) +{
+ u32 ep_number;
+ char name[4];
+
+ INIT_LIST_HEAD(&udc->gadget.ep_list);
+
+ for (ep_number = 0; ep_number < XUSB_MAX_ENDPOINTS; ep_number++) {
+ struct xusb_ep *ep = &udc->ep[ep_number];
+
+ if (ep_number) {
+ list_add_tail(&ep->ep_usb.ep_list,
+ &udc->gadget.ep_list);
+ usb_ep_set_maxpacket_limit(&ep->ep_usb,
+ (unsigned short) ~0);
+ sprintf(name, "ep%d", ep_number);
+ strcpy(ep->name, name);
+ ep->ep_usb.name = ep->name;

Why not use ep->name directly? Also, I'd prefer snprintf() here, even if you're sure this can't overflow :)

Quoted 64 lines
drivers/usb/gadget/Kconfig +64 -0
+static int xudc_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct resource *res;
+	struct xusb_udc *udc;
+	struct xusb_ep *ep0;
+	int irq;
+	int ret;
+	u32 ier;
+	u8 *buff;
+
+	udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL);
+	if (!udc)
+		return -ENOMEM;
+
+	/* Map the registers */
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	udc->base_address = devm_ioremap_resource(&pdev->dev, res);
+	if (!udc->base_address)
+		return -ENOMEM;
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(&pdev->dev, "unable to get irq\n");
+		return irq;
+	}
+	ret = devm_request_irq(&pdev->dev, irq, xudc_irq, 0,
+				dev_name(&pdev->dev), udc);
+	if (ret < 0) {
+		dev_dbg(&pdev->dev, "unable to request irq %d", irq);
+		goto fail;
+	}
+
+	udc->dma_enabled = of_property_read_bool(np, "xlnx,has-builtin-dma");
+
+	/* Setup gadget structure */
+	udc->gadget.ops = &xusb_udc_ops;
+	udc->gadget.max_speed = USB_SPEED_HIGH;
+	udc->gadget.speed = USB_SPEED_UNKNOWN;
+	udc->gadget.ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO].ep_usb;
+	udc->gadget.name = driver_name;
+
+	spin_lock_init(&udc->lock);
+
+	/* Check for IP endianness */
+	udc->write_fn = xudc_write32_be;
+	udc->read_fn = xudc_read32_be;
+	udc->write_fn(udc->base_address, XUSB_TESTMODE_OFFSET, TEST_J);
+	if ((udc->read_fn(udc->base_address + XUSB_TESTMODE_OFFSET))
+			!= TEST_J) {
+		udc->write_fn = xudc_write32;
+		udc->read_fn = xudc_read32;
+	}
+	udc->write_fn(udc->base_address, XUSB_TESTMODE_OFFSET, 0);
+
+	xudc_eps_init(udc);
+
+	ep0 = &udc->ep[0];
+	/* Create a dummy request for GET_STATUS, SET_ADDRESS */
+	udc->req = container_of(xudc_ep_alloc_request(&ep0->ep_usb, GFP_KERNEL),
+				struct xusb_req, usb_req);
+	if (!udc->req) {
+		ret = -ENOMEM;
+		goto fail;

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+	}
+
+	/* buffer for data of get_status request */
+	buff = kzalloc(2, GFP_KERNEL);
+	if (buff == NULL) {
+		ret = -ENOMEM;
+		goto fail;
+	}
+	/* Dummy request ready, free this in remove */
+	udc->req->usb_req.buf = buff;

devm_kzalloc() solves this for you.

drivers/usb/gadget/Kconfig

Quoted 11 lines
drivers/usb/gadget/Kconfig +11 -0
+static int xudc_remove(struct platform_device *pdev)
+{
+	struct xusb_udc *udc = platform_get_drvdata(pdev);
+	void *buf = udc->req->usb_req.buf;
+
+	usb_del_gadget_udc(&udc->gadget);
+
+	/* free memory allocated for dummy request buffer */
+	kfree(buf);
+	/* free memory allocated for dummy request */
+	kfree(udc->req);

These comments don't really help anyone - it's pretty obvious what happens here :) But those will vanish with devm_kzalloc() anyway.

Thanks,
Daniel

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 10 lines · Daniel Mack
Quoted 5 lines · Subbaraya Sundeep Bhatta
 drivers/usb/gadget/Kconfig      |   14 +
 drivers/usb/gadget/Makefile     |    1 +
 drivers/usb/gadget/udc-xilinx.c | 2261 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 2276 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/gadget/udc-xilinx.c

As your driver has a DT binding, you have to describe it in Documentation/devicetree/bindings.

Ah, sorry. I missed patch 1/2 which does that.

Thanks,
Daniel

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
In reply to Daniel Mack

Hi Daniel,

Quoted 23 lines · Daniel Mack

Hi,

Quoted 1 line · Subbaraya Sundeep Bhatta

This patch adds xilinx usb2 device driver support

Add some more information here, please. Copying the text from the Kconfig option is already a good start.

Quoted 5 lines
 drivers/usb/gadget/Kconfig      |   14 +
 drivers/usb/gadget/Makefile     |    1 +
 drivers/usb/gadget/udc-xilinx.c | 2261 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 2276 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/gadget/udc-xilinx.c

As your driver has a DT binding, you have to describe it in Documentation/devicetree/bindings.

Quoted 6 lines
drivers/usb/gadget/Kconfig +0 -0
@@ -459,6 +459,20 @@ config USB_EG20T
        ML7213/ML7831 is companion chip for Intel Atom E6xx series.
        ML7213/ML7831 is completely compatible for Intel EG20T PCH.
 
Quoted 5 lines

drivers/usb/gadget/Kconfig config USB_EG20T · line 464

Quoted 3 lines
drivers/usb/gadget/Kconfig +3 -0
+config USB_GADGET_XILINX
+     tristate "Xilinx USB Driver"
+     depends on COMPILE_TEST

Why would you depend on that?

Felipe asked to make this since this is USB soft IP driver and its dependencies have tendency to grow.
Currently tested on arm and microblaze architectures.

Quoted 3 lines

Also, your code uses device tree functions unconditionally, which is fine, but it must hence depend on 'OF'.

Ok will add OF along with COMPILE_TEST

Quoted 20 lines
Quoted 9 lines
drivers/usb/gadget/Kconfig +8 -0
+struct xusb_ep {
+     struct usb_ep ep_usb;
+     struct list_head queue;
+     struct xusb_udc *udc;
+     const struct usb_endpoint_descriptor *desc;
+     u32 rambase;
+     u32 offset;
+     char name[4];

drivers/usb/gadget/Kconfig config USB_EG20T

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+     u16 epnumber;
+     u16 maxpacket;
+     u16 buffer0count;
+     u16 buffer1count;
+     u8 buffer0ready;
+     u8 buffer1ready;
+     u8 eptype;
+     u8 curbufnum;
+     u8 is_in;
+     u8 is_iso;
Quoted 1 line

Some of those could probably become booleans.

Ok

Quoted 28 lines
Quoted 13 lines
drivers/usb/gadget/Kconfig +12 -0
+struct xusb_udc {
+     struct usb_gadget gadget;
+     struct xusb_ep ep[8];
+     struct usb_gadget_driver *driver;
+     struct usb_ctrlrequest setup;
+     struct xusb_req *req;
+     struct device *dev;
+     u32 usb_state;
+     u32 remote_wkp;
+     u32 setupseqtx;
+     u32 setupseqrx;
+     void __iomem *base_address;

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+     spinlock_t lock;
+     bool dma_enabled;
+
+     unsigned int (*read_fn)(void __iomem *);
+     void (*write_fn)(void __iomem *, u32, u32);
+};
+
+/* Endpoint buffer start addresses in the core */
+static u32 rambase[8] = { 0x22, 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500,
+                     0x1600 };

As stated by Peter, indenting such lines to match the position of the line before makes such code much prettier and more readable. It's also done in the majority of drivers.

Quoted 1 line

This counts for many locations in your code.

Ok

Quoted 10 lines

drivers/usb/gadget/Kconfig

Quoted 8 lines
drivers/usb/gadget/Kconfig +7 -0
+/* Control endpoint configuration.*/
+static const struct usb_endpoint_descriptor config_bulk_out_desc = {
+     .bLength                = USB_DT_ENDPOINT_SIZE,
+     .bDescriptorType        = USB_DT_ENDPOINT,
+     .bEndpointAddress       = USB_DIR_OUT,
+     .bmAttributes           = USB_ENDPOINT_XFER_BULK,
+     .wMaxPacketSize         = cpu_to_le16(64),

Why not use EP0_MAX_PACKET here?

Ok

Quoted 15 lines

drivers/usb/gadget/Kconfig

Quoted 11 lines
drivers/usb/gadget/Kconfig +10 -0
+static void xudc_wrstatus(struct xusb_udc *udc)
+{
+     struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
+     u32 epcfgreg;
+
+     epcfgreg = udc->read_fn(udc->base_address + ep0->offset)|
+                     XUSB_EP_CFG_DATA_TOGGLE_MASK;
+     udc->write_fn(udc->base_address, ep0->offset, epcfgreg);
+     udc->write_fn(udc->base_address, ep0->offset + XUSB_EP_BUF0COUNT_OFFSET,
+                     0);

Just a nit, but renaming 'base_address' to something like 'base' or 'addr' would help you get around or maximum line constraint here and in some other places.

Ok i will do this

Quoted 47 lines
Quoted 34 lines
drivers/usb/gadget/Kconfig +33 -0
+static int xudc_dma_send(struct xusb_ep *ep, struct xusb_req *req,
+             u8 *buffer, u32 length)
+{
+     u32 *eprambase;
+     dma_addr_t src;
+     dma_addr_t dst;
+     int ret;
+     struct xusb_udc *udc = ep->udc;
+
+     src = req->usb_req.dma + req->usb_req.actual;
+     if (req->usb_req.length)
+             dma_sync_single_for_device(udc->dev, src,
+                                             length, DMA_TO_DEVICE);
+     if (!ep->curbufnum && !ep->buffer0ready) {
+             /* Get the Buffer address and copy the transmit data.*/
+             eprambase = (u32 __force *)(udc->base_address +
+                             ep->rambase);
+             dst = virt_to_phys(eprambase);
+             udc->write_fn(udc->base_address, ep->offset +
+                             XUSB_EP_BUF0COUNT_OFFSET, length);
+             udc->write_fn(udc->base_address, XUSB_DMA_CONTROL_OFFSET,
+                             XUSB_DMA_BRR_CTRL | (1 << ep->epnumber));
+             ep->buffer0ready = 1;
+             ep->curbufnum = 1;
+     } else if (ep->curbufnum && !ep->buffer1ready) {
+             /* Get the Buffer address and copy the transmit data.*/
+             eprambase = (u32 __force *)(udc->base_address +
+                             ep->rambase + ep->ep_usb.maxpacket);
+             dst = virt_to_phys(eprambase);
+             udc->write_fn(udc->base_address, ep->offset +
+                             XUSB_EP_BUF1COUNT_OFFSET, length);
+             udc->write_fn(udc->base_address, XUSB_DMA_CONTROL_OFFSET,
+                             XUSB_DMA_BRR_CTRL | (1 << (ep->epnumber +

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+                             XUSB_STATUS_EP_BUFF2_SHIFT)));
+             ep->buffer1ready = 1;
+             ep->curbufnum = 0;
+     } else {
+             /* None of ping pong buffers are ready currently .*/
+             return -EAGAIN;
+     }
+
+     ret = xudc_start_dma(ep, src, dst, length);
+     return ret;

return xudc_start_dma(ep, src, dst, length);

Quoted 1 line

... so you don't need 'ret' at all.

Ok

Quoted 73 lines
Quoted 30 lines
drivers/usb/gadget/Kconfig +29 -0
+static int xudc_dma_receive(struct xusb_ep *ep, struct xusb_req *req,
+             u8 *buffer, u32 length)
+{
+     u32 *eprambase;
+     dma_addr_t src;
+     dma_addr_t dst;
+     int ret = 0;
+     struct xusb_udc *udc = ep->udc;
+
+     dst = req->usb_req.dma + req->usb_req.actual;
+
+     if (!ep->curbufnum && !ep->buffer0ready) {
+             /* Get the Buffer address and copy the transmit data */
+             eprambase = (u32 __force *)(udc->base_address +
+                             ep->rambase);
+             src = virt_to_phys(eprambase);
+             udc->write_fn(udc->base_address, XUSB_DMA_CONTROL_OFFSET,
+                             XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
+                             (1 << ep->epnumber));
+             ep->buffer0ready = 1;
+             ep->curbufnum = 1;
+     } else if (ep->curbufnum && !ep->buffer1ready) {
+             /* Get the Buffer address and copy the transmit data */
+             eprambase = (u32 __force *)(udc->base_address +
+                             ep->rambase + ep->ep_usb.maxpacket);
+             src = virt_to_phys(eprambase);
+             udc->write_fn(udc->base_address, XUSB_DMA_CONTROL_OFFSET,
+                             XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
+                             (1 << (ep->epnumber +

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+                             XUSB_STATUS_EP_BUFF2_SHIFT)));
+             ep->buffer1ready = 1;
+             ep->curbufnum = 0;
+     } else {
+             /* None of the ping-pong buffers are ready currently */
+             return -EAGAIN;
+     }
+
+     ret = xudc_start_dma(ep, src, dst, length);
+     return ret;

Dito.

Quoted 30 lines
drivers/usb/gadget/Kconfig +30 -0
+static int xudc_eptxrx(struct xusb_ep *ep, struct xusb_req *req,
+             u8 *bufferptr, u32 bufferlen)
+{
+     u32 *eprambase;
+     u32 bytestosend;
+     u8 *temprambase;
+     int rc = 0;
+     struct xusb_udc *udc = ep->udc;
+
+     bytestosend = bufferlen;
+     if (udc->dma_enabled) {
+             if (ep->is_in)
+                     rc = xudc_dma_send(ep, req, bufferptr, bufferlen);
+             else
+                     rc = xudc_dma_receive(ep, req, bufferptr, bufferlen);
+             return rc;
+     }
+     /* Put the transmit buffer into the correct ping-pong buffer.*/
+     if (!ep->curbufnum && !ep->buffer0ready) {
+             /* Get the Buffer address and copy the transmit data.*/
+             eprambase = (u32 __force *)(udc->base_address + ep->rambase);
+             while (bytestosend > 3) {
+                     if (ep->is_in)
+                             *eprambase++ = *(u32 *)bufferptr;
+                     else
+                             *(u32 *)bufferptr = *eprambase++;
+                     bufferptr += 4;
+                     bytestosend -= 4;
+             }
+             temprambase = (u8 *)eprambase;
Quoted 8 lines

drivers/usb/gadget/Kconfig

Quoted 6 lines
drivers/usb/gadget/Kconfig +6 -0
+             while (bytestosend--) {
+                     if (ep->is_in)
+                             *temprambase++ = *bufferptr++;
+                     else
+                             *bufferptr++ = *temprambase++;
+             }

Why not use memcpy()?

Ok

Quoted 32 lines
Quoted 21 lines
drivers/usb/gadget/Kconfig +20 -0
+             /*
+              * Set the Buffer count register with transmit length
+              * and enable the buffer for transmission.
+              */
+             if (ep->is_in)
+                     udc->write_fn(udc->base_address, ep->offset +
+                                     XUSB_EP_BUF0COUNT_OFFSET, bufferlen);
+             udc->write_fn(udc->base_address, XUSB_BUFFREADY_OFFSET,
+                             1 << ep->epnumber);
+             ep->buffer0ready = 1;
+             ep->curbufnum = 1;
+     } else if ((ep->curbufnum == 1) && (!ep->buffer1ready)) {
+             /* Get the Buffer address and copy the transmit data.*/
+             eprambase = (u32 __force *)(udc->base_address + ep->rambase +
+                             ep->ep_usb.maxpacket);
+             while (bytestosend > 3) {
+                     if (ep->is_in)
+                             *eprambase++ = *(u32 *)bufferptr;
+                     else
+                             *(u32 *)bufferptr = *eprambase++;

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+                     bufferptr += 4;
+                     bytestosend -= 4;
+             }
+             temprambase = (u8 *)eprambase;
+             while (bytestosend--) {
+                     if (ep->is_in)
+                             *temprambase++ = *bufferptr++;
+                     else
+                             *bufferptr++ = *temprambase++;
+             }
Quoted 1 line

memcpy()?

Ok

Quoted 24 lines
Quoted 11 lines
drivers/usb/gadget/Kconfig +10 -0
+/**
+ * xudc_read_fifo - Reads the data from the given endpoint buffer.
+ * @ep: pointer to the usb device endpoint structure.
+ * @req: pointer to the usb request structure.
+ *
+ * Return: 0 if request is completed and -EAGAIN if not completed.
+ *
+ * Pulls OUT packet data from the endpoint buffer.
+ */
+static int xudc_read_fifo(struct xusb_ep *ep, struct xusb_req *req)

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+{
+     u8 *buf;
+     u32 is_short, count, bufferspace;
+     u8 bufoffset;
+     u8 two_pkts = 0;
+     int ret;
+     int retval = -EAGAIN;
+     struct xusb_udc *udc = ep->udc;
+
+     if ((ep->buffer0ready == 1) && (ep->buffer1ready == 1)) {

if (ep->buffer0ready && ep->buffer1ready)

Quoted 1 line

is shorter and easier to read.

Ok

Quoted 12 lines

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +9 -0
+static int xudc_ep_set_halt(struct usb_ep *_ep, int value)
+{
+     struct xusb_ep *ep = to_xusb_ep(_ep);
+     struct xusb_udc *udc;
+     unsigned long flags;
+     u32 epcfgreg;
+
+     if (!_ep || (!ep->desc && ep->epnumber)) {
+             pr_debug("%s: bad ep or descriptor\n", __func__);

dev_dbg()

device structure is in ep so first checking for proper ep and later use dev_dbg.

Quoted 36 lines
Quoted 36 lines
drivers/usb/gadget/Kconfig +35 -0
+static int __xudc_ep_enable(struct xusb_ep *ep,
+             const struct usb_endpoint_descriptor *desc)
+{
+     struct xusb_udc *udc = ep->udc;
+     u32 tmp;
+     u8 eptype = 0;
+     u32 epcfg;
+     u32 ier;
+
+     ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
+     /* Bit 3...0:endpoint number */
+     ep->epnumber = (desc->bEndpointAddress & 0x0f);
+     ep->desc = desc;
+     ep->ep_usb.desc = desc;
+     tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
+     ep->ep_usb.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
+
+     switch (tmp) {
+     case USB_ENDPOINT_XFER_CONTROL:
+             dev_dbg(udc->dev, "only one control endpoint\n");
+             /* NON- ISO */
+             eptype = 0;
+             return -EINVAL;
+     case USB_ENDPOINT_XFER_INT:
+             /* NON- ISO */
+             eptype = 0;
+             if (ep->ep_usb.maxpacket > 64)
+                     goto bogus_max;
+             break;
+     case USB_ENDPOINT_XFER_BULK:
+             /* NON- ISO */
+             eptype = 0;
+             switch (ep->ep_usb.maxpacket) {
+             case 8:
+             case 16:
Quoted 8 lines

drivers/usb/gadget/Kconfig

Quoted 5 lines
drivers/usb/gadget/Kconfig +5 -0
+             case 32:
+             case 64:
+             case 512:
+                     goto ok;
+             }

Nit: With is_power_of_2() and some min/max checks, you wouldn't need a switch here, and hence the jump label could go away.

Ok

Quoted 15 lines
Quoted 12 lines
drivers/usb/gadget/Kconfig +11 -0
+bogus_max:
+             dev_dbg(udc->dev, "bogus maxpacket %d\n",
+                     ep->ep_usb.maxpacket);
+             return -EINVAL;
+     case USB_ENDPOINT_XFER_ISOC:
+             /* ISO */
+             eptype = 1;
+             ep->is_iso = 1;
+             break;
+     }
+ok:

...

Quoted 12 lines

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+static int xudc_ep_enable(struct usb_ep *_ep,
+             const struct usb_endpoint_descriptor *desc)
+{
+     struct xusb_ep *ep;
+     struct xusb_udc *udc;
+     unsigned long flags;
+     int ret;
+
+     if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
+             pr_debug("%s: bad ep or descriptor\n", __func__);

dev_dbg()

device structure is in ep so first checking for proper ep and later use dev_dbg.

Quoted 12 lines

drivers/usb/gadget/Kconfig

Quoted 10 lines
drivers/usb/gadget/Kconfig +9 -0
+static int xudc_ep_disable(struct usb_ep *_ep)
+{
+     struct xusb_ep *ep;
+     unsigned long flags;
+     u32 epcfg;
+     struct xusb_udc *udc;
+
+     if (!_ep) {
+             pr_debug("%s: invalid ep\n", __func__);

dev_dbg()

device structure is in ep so first checking for proper ep and later use dev_dbg.

Quoted 24 lines
Quoted 24 lines
drivers/usb/gadget/Kconfig +23 -0
+static int __xudc_ep0_queue(struct xusb_ep *ep0, struct xusb_req *req)
+{
+     struct xusb_udc *udc = ep0->udc;
+     u32 length;
+     u8 *corebuf;
+
+     if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
+             dev_dbg(udc->dev, "%s, bogus device state\n",
+                     __func__);
+             return -EINVAL;
+     }
+     if (!list_empty(&ep0->queue)) {
+             dev_dbg(udc->dev, "%s:ep0 busy\n", __func__);
+             return -EBUSY;
+     }
+
+     req->usb_req.status = -EINPROGRESS;
+     req->usb_req.actual = 0;
+
+     list_add_tail(&req->queue, &ep0->queue);
+
+     if (udc->setup.bRequestType & USB_DIR_IN) {
+             prefetch(req->usb_req.buf);
Quoted 8 lines

drivers/usb/gadget/Kconfig

Quoted 6 lines
drivers/usb/gadget/Kconfig +6 -0
+             length = req->usb_req.length;
+             corebuf = (void __force *) ((ep0->rambase << 2) +
+                             udc->base_address);
+             length = req->usb_req.actual = min_t(u32, length,
+                                             EP0_MAX_PACKET);
+             memcpy((void *)corebuf, req->usb_req.buf, length);

No need for the cast.

Ok

Quoted 32 lines
Quoted 19 lines
drivers/usb/gadget/Kconfig +18 -0
+             udc->write_fn(udc->base_address, XUSB_EP_BUF0COUNT_OFFSET,
+                             length);
+             udc->write_fn(udc->base_address, XUSB_BUFFREADY_OFFSET, 1);
+     } else {
+             if (udc->setup.wLength) {
+                     /* Enable EP0 buffer to receive data */
+                     udc->write_fn(udc->base_address,
+                                     XUSB_EP_BUF0COUNT_OFFSET, 0);
+                     udc->write_fn(udc->base_address,
+                                     XUSB_BUFFREADY_OFFSET, 1);
+             } else {
+                     xudc_wrstatus(udc);
+             }
+     }
+
+     return 0;
+}
+

...

Quoted 10 lines
drivers/usb/gadget/Kconfig +10 -0
+/**
+ * xudc_handle_setup - Processes the setup packet.
+ * @udc: pointer to the usb device controller structure.
+ *
+ * Process setup packet and delegate to gadget layer.
+ */
+static void xudc_handle_setup(struct xusb_udc *udc)
+{
+     struct xusb_ep *ep0 = &udc->ep[0];
+     struct usb_ctrlrequest setup;
Quoted 8 lines

drivers/usb/gadget/Kconfig

Quoted 6 lines
drivers/usb/gadget/Kconfig +6 -0
+     u32 *ep0rambase;
+
+     /* Load up the chapter 9 command buffer.*/
+     ep0rambase = (u32 __force *) (udc->base_address +
+                               XUSB_SETUP_PKT_ADDR_OFFSET);
+     memcpy((void *)&setup, (void *)ep0rambase, 8);

Dito.

Ok

Quoted 55 lines
Quoted 55 lines
drivers/usb/gadget/Kconfig +54 -0
+static void xudc_ep0_in(struct xusb_udc *udc)
+{
+     struct xusb_ep *ep0 = &udc->ep[0];
+     struct xusb_req *req;
+     unsigned int bytes_to_tx;
+     void *buffer;
+     u32 epcfgreg;
+     u16 count = 0;
+     u16 length;
+     u8 *ep0rambase;
+     u8 test_mode = udc->setup.wIndex >> 8;
+
+     req = list_first_entry(&ep0->queue, struct xusb_req, queue);
+     bytes_to_tx = req->usb_req.length - req->usb_req.actual;
+
+     switch (udc->setupseqtx) {
+     case STATUS_PHASE:
+             switch (udc->setup.bRequest) {
+             case USB_REQ_SET_ADDRESS:
+                     /* Set the address of the device.*/
+                     udc->write_fn(udc->base_address,
+                                     XUSB_ADDRESS_OFFSET,
+                                     udc->setup.wValue);
+                     break;
+             case USB_REQ_SET_FEATURE:
+                     if (udc->setup.bRequestType ==
+                                     USB_RECIP_DEVICE) {
+                             if (udc->setup.wValue ==
+                                             USB_DEVICE_TEST_MODE)
+                                     udc->write_fn(udc->base_address,
+                                                     XUSB_TESTMODE_OFFSET,
+                                                     test_mode);
+                     }
+                     break;
+             }
+             req->usb_req.actual = req->usb_req.length;
+             xudc_done(ep0, req, 0);
+             break;
+     case DATA_PHASE:
+             if (!bytes_to_tx) {
+                     /*
+                      * We're done with data transfer, next
+                      * will be zero length OUT with data toggle of
+                      * 1. Setup data_toggle.
+                      */
+                     epcfgreg = udc->read_fn(udc->base_address +
+                                     ep0->offset);
+                     epcfgreg |= XUSB_EP_CFG_DATA_TOGGLE_MASK;
+                     udc->write_fn(udc->base_address, ep0->offset, epcfgreg);
+                     udc->setupseqtx = STATUS_PHASE;
+             } else {
+                     length = count = min_t(u32, bytes_to_tx,
+                                             EP0_MAX_PACKET);
+                     /* Copy the data to be transmitted into the DPRAM. */
Quoted 8 lines

drivers/usb/gadget/Kconfig

Quoted 6 lines
drivers/usb/gadget/Kconfig +6 -0
+                     ep0rambase = (u8 __force *) (udc->base_address +
+                                     (ep0->rambase << 2));
+
+                     buffer = req->usb_req.buf + req->usb_req.actual;
+                     req->usb_req.actual = req->usb_req.actual + length;
+                     memcpy((void *)ep0rambase, buffer, length);

Dito.

Ok

Quoted 24 lines
drivers/usb/gadget/udc-xilinx.c new file +22 -0
+/**
+ * xudc_eps_init - initialize endpoints.
+ * @udc: pointer to the usb device controller structure.
+ */
+static void xudc_eps_init(struct xusb_udc *udc)
+{
+       u32 ep_number;
+       char name[4];
+
+       INIT_LIST_HEAD(&udc->gadget.ep_list);
+
+       for (ep_number = 0; ep_number < XUSB_MAX_ENDPOINTS; ep_number++) {
+               struct xusb_ep *ep = &udc->ep[ep_number];
+
+               if (ep_number) {
+                       list_add_tail(&ep->ep_usb.ep_list,
+                                       &udc->gadget.ep_list);
+                       usb_ep_set_maxpacket_limit(&ep->ep_usb,
+                                       (unsigned short) ~0);
+                       sprintf(name, "ep%d", ep_number);
+                       strcpy(ep->name, name);
+                       ep->ep_usb.name = ep->name;
Quoted 2 lines

Why not use ep->name directly? Also, I'd prefer snprintf() here, even if you're sure this can't overflow :)

Ok

Quoted 69 lines
Quoted 69 lines
drivers/usb/gadget/Kconfig +68 -0
+static int xudc_probe(struct platform_device *pdev)
+{
+     struct device_node *np = pdev->dev.of_node;
+     struct resource *res;
+     struct xusb_udc *udc;
+     struct xusb_ep *ep0;
+     int irq;
+     int ret;
+     u32 ier;
+     u8 *buff;
+
+     udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL);
+     if (!udc)
+             return -ENOMEM;
+
+     /* Map the registers */
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+     udc->base_address = devm_ioremap_resource(&pdev->dev, res);
+     if (!udc->base_address)
+             return -ENOMEM;
+
+     irq = platform_get_irq(pdev, 0);
+     if (irq < 0) {
+             dev_err(&pdev->dev, "unable to get irq\n");
+             return irq;
+     }
+     ret = devm_request_irq(&pdev->dev, irq, xudc_irq, 0,
+                             dev_name(&pdev->dev), udc);
+     if (ret < 0) {
+             dev_dbg(&pdev->dev, "unable to request irq %d", irq);
+             goto fail;
+     }
+
+     udc->dma_enabled = of_property_read_bool(np, "xlnx,has-builtin-dma");
+
+     /* Setup gadget structure */
+     udc->gadget.ops = &xusb_udc_ops;
+     udc->gadget.max_speed = USB_SPEED_HIGH;
+     udc->gadget.speed = USB_SPEED_UNKNOWN;
+     udc->gadget.ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO].ep_usb;
+     udc->gadget.name = driver_name;
+
+     spin_lock_init(&udc->lock);
+
+     /* Check for IP endianness */
+     udc->write_fn = xudc_write32_be;
+     udc->read_fn = xudc_read32_be;
+     udc->write_fn(udc->base_address, XUSB_TESTMODE_OFFSET, TEST_J);
+     if ((udc->read_fn(udc->base_address + XUSB_TESTMODE_OFFSET))
+                     != TEST_J) {
+             udc->write_fn = xudc_write32;
+             udc->read_fn = xudc_read32;
+     }
+     udc->write_fn(udc->base_address, XUSB_TESTMODE_OFFSET, 0);
+
+     xudc_eps_init(udc);
+
+     ep0 = &udc->ep[0];
+     /* Create a dummy request for GET_STATUS, SET_ADDRESS */
+     udc->req = container_of(xudc_ep_alloc_request(&ep0->ep_usb, GFP_KERNEL),
+                             struct xusb_req, usb_req);
+     if (!udc->req) {
+             ret = -ENOMEM;
+             goto fail;
+     }
+
+     /* buffer for data of get_status request */
+     buff = kzalloc(2, GFP_KERNEL);
Quoted 8 lines

drivers/usb/gadget/Kconfig

Quoted 6 lines
drivers/usb/gadget/Kconfig +6 -0
+     if (buff == NULL) {
+             ret = -ENOMEM;
+             goto fail;
+     }
+     /* Dummy request ready, free this in remove */
+     udc->req->usb_req.buf = buff;

devm_kzalloc() solves this for you.

Ok

Quoted 15 lines

drivers/usb/gadget/Kconfig

Quoted 12 lines
drivers/usb/gadget/Kconfig +11 -0
+static int xudc_remove(struct platform_device *pdev)
+{
+     struct xusb_udc *udc = platform_get_drvdata(pdev);
+     void *buf = udc->req->usb_req.buf;
+
+     usb_del_gadget_udc(&udc->gadget);
+
+     /* free memory allocated for dummy request buffer */
+     kfree(buf);
+     /* free memory allocated for dummy request */
+     kfree(udc->req);

These comments don't really help anyone - it's pretty obvious what happens here :) But those will vanish with devm_kzalloc() anyway.

ok will use devm_kzalloc :)

Thank you,
Sundeep.B.S.

Quoted 11 lines

Thanks,
Daniel

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#7 lore raw
Quoted 26 lines · sundeep subbaraya

Hi Daniel,

Quoted 23 lines · Daniel Mack

Hi,

Quoted 1 line · Subbaraya Sundeep Bhatta

This patch adds xilinx usb2 device driver support

Add some more information here, please. Copying the text from the Kconfig option is already a good start.

Quoted 5 lines
 drivers/usb/gadget/Kconfig      |   14 +
 drivers/usb/gadget/Makefile     |    1 +
 drivers/usb/gadget/udc-xilinx.c | 2261 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 2276 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/gadget/udc-xilinx.c

As your driver has a DT binding, you have to describe it in Documentation/devicetree/bindings.

Quoted 6 lines
drivers/usb/gadget/Kconfig +0 -0
@@ -459,6 +459,20 @@ config USB_EG20T
        ML7213/ML7831 is companion chip for Intel Atom E6xx series.
        ML7213/ML7831 is completely compatible for Intel EG20T PCH.
 
Quoted 8 lines
Quoted 5 lines

drivers/usb/gadget/Kconfig config USB_EG20T · line 464

Quoted 3 lines
drivers/usb/gadget/Kconfig +3 -0
+config USB_GADGET_XILINX
+     tristate "Xilinx USB Driver"
+     depends on COMPILE_TEST

Why would you depend on that?

Felipe asked to make this since this is USB soft IP driver and its dependencies have tendency to grow.

that's not exactly what I asked :-) Usually you only add COMPILE_TEST when you have an ARCH dependency. So something like:

	depends on ARCH_ARM || COMPILE_TEST

would make it clear that this driver is only available on ARM, but when doing my build tests, I'd still be able to compile it on x86.

Quoted 4 lines
Quoted 2 lines

Also, your code uses device tree functions unconditionally, which is fine, but it must hence depend on 'OF'.

Ok will add OF along with COMPILE_TEST

so this would be:

	depends on OF || COMPILE_TEST
Signature · 1 line
balbi
#8 lore raw

Hi Felipe,

Quoted 36 lines · Felipe Balbi
Quoted 26 lines · sundeep subbaraya

Hi Daniel,

Quoted 23 lines · Daniel Mack

Hi,

Quoted 1 line · Subbaraya Sundeep Bhatta

This patch adds xilinx usb2 device driver support

Add some more information here, please. Copying the text from the Kconfig option is already a good start.

Quoted 5 lines
 drivers/usb/gadget/Kconfig      |   14 +
 drivers/usb/gadget/Makefile     |    1 +
 drivers/usb/gadget/udc-xilinx.c | 2261 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 2276 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/gadget/udc-xilinx.c

As your driver has a DT binding, you have to describe it in Documentation/devicetree/bindings.

Quoted 6 lines
drivers/usb/gadget/Kconfig +0 -0
@@ -459,6 +459,20 @@ config USB_EG20T
        ML7213/ML7831 is companion chip for Intel Atom E6xx series.
        ML7213/ML7831 is completely compatible for Intel EG20T PCH.
 
Quoted 8 lines
Quoted 5 lines

drivers/usb/gadget/Kconfig config USB_EG20T · line 464

Quoted 3 lines
drivers/usb/gadget/Kconfig +3 -0
+config USB_GADGET_XILINX
+     tristate "Xilinx USB Driver"
+     depends on COMPILE_TEST

Why would you depend on that?

Felipe asked to make this since this is USB soft IP driver and its dependencies have tendency to grow.

Quoted 7 lines

that's not exactly what I asked :-) Usually you only add COMPILE_TEST when you have an ARCH dependency. So something like:

        depends on ARCH_ARM || COMPILE_TEST

would make it clear that this driver is only available on ARM, but when doing my build tests, I'd still be able to compile it on x86.

Ok got it :)

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Also, your code uses device tree functions unconditionally, which is fine, but it must hence depend on 'OF'.

Ok will add OF along with COMPILE_TEST

so this would be:

        depends on OF || COMPILE_TEST

Ok

Thanks,
Sundeep

Quoted 3 lines

--
balbi

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
Quoted 9 lines · On Wednesday 10 September 2014 19:25:11 sundeep subbaraya wrote:
Quoted 7 lines

that's not exactly what I asked Usually you only add COMPILE_TEST when you have an ARCH dependency. So something like:

        depends on ARCH_ARM || COMPILE_TEST

would make it clear that this driver is only available on ARM, but when doing my build tests, I'd still be able to compile it on x86.

Ok got it

Most of the xilinx stuff also applies to MICROBLAZE however, so it may need to be

	ARM || MICROBLAZE || COMPILE_TEST

(not ARCH_ARM, btw).

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Also, your code uses device tree functions unconditionally, which is fine, but it must hence depend on 'OF'.

Ok will add OF along with COMPILE_TEST

so this would be:

        depends on OF || COMPILE_TEST

If it requires OF to build, you can use 'OF && (ARM || COMPILE_TEST)', but if there is no compile-time dependency, you can just leave out the 'depends on OF' completely. You can't even build a Zynq machine without OF enabled.

Arnd