drivers: usb: nrf5: Fix cdc_acm OUT endpoint read size issue

Problem:
cdc_acm sample outputs junk characters to the console when any key
is pressed. It is supposed to echo the same character as the input.

Analysis:
nRF52840 USB driver always reads 64 bytes from the USB peripheral's
local buffer irrespective of the data length. Even when 1 byte
is sent by the host, the usb driver receives as 64 bytes.

Since the maximum packet size value had been used when setting up
the EasyDMA's MAXCNT register for OUT transactions, DMA transffered
the entire 64 bytes of it's local buffer into the data RAM
irrespective of the OUT data length, containing garbage at the end
of useful payload. For some applications like hci_usb the extra
bytes were being ignored. But for cdc_acm, the entire 64 bytes
is getting echoed back displaying garbled characters on the terminal.

Fix:
For OUT endpoints, the amount of data received by USBD to its local
buffer should be read using SIZE.EPOUT[x] registers in the case of
BULK and INTERRUPT endpoints and SIZE.ISOOUT register for ISOOUT
endpoint. This value should be set to the EasyDMA maxcnt register
to DMA into RAM buffer. Fix this issue for Bulk, Interrupt and
ISOOUT endpoints.

Signed-off-by: Sundar Subramaniyan <sundar.subramaniyan@gmail.com>
diff --git a/drivers/usb/device/usb_dc_nrf5.c b/drivers/usb/device/usb_dc_nrf5.c
index 34ce060..bcb1d21 100644
--- a/drivers/usb/device/usb_dc_nrf5.c
+++ b/drivers/usb/device/usb_dc_nrf5.c
@@ -1307,18 +1307,18 @@
 	case EP_DATA_RECV:
 	{
 		struct nrf5_usbd_ctx *ctx = get_usbd_ctx();
+		u8_t addr = ep_ctx->cfg.addr;
 
 		/**
 		 * We have some OUT BULK/INTERRUT data received
 		 * into the USBD's local buffer. Let's grab it.
 		 */
-		nrf_usbd_ep_easydma_set(ep_ctx->cfg.addr,
-					(u32_t)ep_ctx->buf.data,
-					ep_ctx->cfg.max_sz);
+		nrf_usbd_ep_easydma_set(addr, (u32_t)ep_ctx->buf.data,
+					nrf_usbd_epout_size_get(addr));
 
 		/* Only one DMA can happen at a time */
 		k_sem_take(&ctx->dma_in_use, K_FOREVER);
-		start_epout_task(ep_ctx->cfg.addr);
+		start_epout_task(addr);
 	}
 		break;
 	case EP_WRITE_COMPLETE:
@@ -1413,6 +1413,7 @@
 	case EP_SOF:
 	{
 		struct nrf5_usbd_ctx *ctx = get_usbd_ctx();
+		u8_t addr = ep_ctx->cfg.addr;
 
 		if (NRF_USBD_EPOUT_CHECK(ep_ctx->cfg.addr)) {
 			/**
@@ -1426,18 +1427,21 @@
 			 * USB bus?
 			 */
 			if (ep_ctx->buf.len) {
-				nrf_usbd_ep_easydma_set(ep_ctx->cfg.addr,
+				u32_t maxcnt;
+
+				maxcnt = nrf_usbd_episoout_size_get(addr);
+				nrf_usbd_ep_easydma_set(addr,
 							(u32_t)ep_ctx->buf.data,
-							ep_ctx->cfg.max_sz);
+							maxcnt);
 
 				/* Only one DMA can happen at a time */
 				k_sem_take(&ctx->dma_in_use, K_FOREVER);
-				start_epout_task(ep_ctx->cfg.addr);
+				start_epout_task(addr);
 			}
 		} else {
 			/** Is buffer available? */
 			if (!ep_ctx->buf.len) {
-				nrf_usbd_ep_easydma_set(ep_ctx->cfg.addr,
+				nrf_usbd_ep_easydma_set(addr,
 							(u32_t)ep_ctx->buf.data,
 							ep_ctx->cfg.max_sz);
 
@@ -1449,7 +1453,7 @@
 				 * the sem back in ISR?
 				 */
 				k_sem_take(&ctx->dma_in_use, K_FOREVER);
-				start_epin_task(ep_ctx->cfg.addr);
+				start_epin_task(addr);
 			}
 		}
 	}