Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usbd_log macro refactoring #57

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions lib/usbd/backend/usbd_dwc_otg.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ static void disable_all_non_ep0(usbd_device *dev)
{
unsigned i;

USBD_LOG_LN(USB_VIO, "USBD:disable endpoints");

for (i = 1; i < get_ep_count(dev); i++) {

REBASE(DWC_OTG_DOEPxINT, i) = 0xFFFF;
Expand Down Expand Up @@ -243,13 +245,19 @@ void dwc_otg_ep_prepare(usbd_device *dev, uint8_t addr,
if (IS_IN_ENDPOINT(addr)) {
/* FIXME: underflow */
dev->private_data.fifo_remaining -= fifo_word; /* IN */
USBD_LOGF_LN(USB_VIO_INIT, "USBD: EP%dIN avail %x"
, num, REBASE(DWC_OTG_DIEPxTXFSTS, num) );

REBASE(DWC_OTG_DIEPxTXF, num) = DWC_OTG_DIEPTXF_INEPTXFD(fifo_word) |
DWC_OTG_DIEPTXF_INEPTXSA(dev->private_data.fifo_remaining);

REBASE(DWC_OTG_DIEPxCTL, num) = DWC_OTG_DIEPCTL_SNAK |
DWC_OTG_DIEPCTL_SD0PID | eptyp_map[type] |
DWC_OTG_DIEPCTL_USBAEP | DWC_OTG_DIEPCTL_TXFNUM(num);

USBD_LOGF_LN(USB_VIO_INIT, "USBD: EP%dIN assign at %x[%x] avail %x"
, num, dev->private_data.fifo_remaining, fifo_word
, REBASE(DWC_OTG_DIEPxTXFSTS, num) );
} else {
if (type == USBD_EP_CONTROL) {
dev->private_data.fifo_rx_usage_overall += 13; /* Setup */
Expand Down Expand Up @@ -332,7 +340,7 @@ void dwc_otg_set_ep_stall(usbd_device *dev, uint8_t addr, bool stall)
{
uint8_t num = ENDPOINT_NUMBER(addr);

LOGF_LN("STALL endpoint 0x%"PRIx8" = %s", addr, stall ? "Yes" : "No");
USBD_LOGF_LN(USB_VIO, "STALL ep0x%"PRIx8" = %s", addr, stall ? "Yes" : "No");

/* DIEP0CTL, DIEPxCTL, DOEP0CTL, DOEPxCTL have same STALL layout */
volatile uint32_t *reg_ptr = IS_IN_ENDPOINT(addr) ?
Expand Down Expand Up @@ -389,12 +397,16 @@ static void urb_to_fifo_1pkt(usbd_device *dev, usbd_urb *urb)
size_t rem_len = transfer->length - transfer->transferred;

if (!rem_len) {
LOGF_LN("No more data to send URB %"PRIu64" (endpoint 0x%"PRIx8") "
"(intending ZLP?)", urb->id, transfer->ep_addr);
USBD_LOGF_LN(USB_VURB, "No more data to send URB %"PRIurb" (ep0x%"PRIx8") "
"(intending ZLP?)", view_urbid(urb->id), transfer->ep_addr);
return;
}

size_t tx_len = MIN(transfer->ep_size, rem_len);
USBD_LOGF_LN(USB_VIO2_URB, "USBD:tx URB%"PRIurb" (%d/%d) + %d"
, view_urbid(urb->id)
, transfer->transferred, transfer->length, tx_len
);

/* TX FIFO has enough space to write "tx_len" of data */
size_t tx_words = DIVIDE_AND_CEIL(tx_len, 4);
Expand Down Expand Up @@ -547,6 +559,9 @@ static void urb_submit_non_ep0(usbd_device *dev, usbd_urb *urb)
DWC_OTG_DIEPCTL_CNAK | DWC_OTG_DIEPCTL_TXFNUM(ep_num) |
eptyp_map[transfer->ep_type] | DWC_OTG_DIEPCTL_USBAEP;

USBD_LOGF_LN(USB_VIO, "USBD:submit urb%"PRIurb" on ep8%"PRIx8" DIEPxTSIZ=%x"
, view_urbid(urb->id), ep_num, REBASE(DWC_OTG_DIEPxTSIZ, ep_num) );

/* Push first packet to memory! */
if (transfer->length) {
/* Enable empty interrupt mask */
Expand Down Expand Up @@ -727,7 +742,7 @@ static void fifo_to_urb_1pkt(usbd_device *dev, usbd_urb *urb, uint16_t bcnt)

if (bcnt < transfer->ep_size) {
if (transfer->ep_type == USBD_EP_BULK) {
LOGF_LN("Short packet received for Bulk endpoint 0x%"PRIx8,
USBD_LOGF_LN(USB_VIO, "Short packet received for Bulk endpoint 0x%"PRIx8,
transfer->ep_addr);

if (transfer->flags & USBD_FLAG_SHORT_PACKET) {
Expand Down Expand Up @@ -775,7 +790,7 @@ static void handle_rxflvl_interrupt(usbd_device *dev)
[15] = "RESERVED_15"
};

LOGF_LN("GRXSTSP: rxstsp = %s, ep_num = %"PRIu8", bcnt = %"PRIu16,
USBD_LOGF_LN(USB_VIO, "GRXSTSP: %s, ep%"PRIu8", bcnt = %"PRIu16,
map_pktsts[DWC_OTG_GRXSTSP_PKTSTS_GET(rxstsp)], ep_num, bcnt);
#endif

Expand All @@ -790,19 +805,21 @@ static void handle_rxflvl_interrupt(usbd_device *dev)
} break;
case DWC_OTG_GRXSTSP_PKTSTS_SETUP: {
if (bcnt != 8) {
LOG_LN("SETUP packet in FIFO not equal to 8");
USBD_LOG_LN(USB_VSETUP,"SETUP packet in FIFO not equal to 8");
break;
}

struct usb_setup_data *setup_data = &dev->private_data.setup_data;
uint32_t *io = (void *) setup_data;
io[0] = REBASE(DWC_OTG_FIFO, 0);
io[1] = REBASE(DWC_OTG_FIFO, 0);
LOGF_LN("bmRequestType: 0x%02"PRIx8, setup_data->bmRequestType);
LOGF_LN("bRequest: 0x%02"PRIx8, setup_data->bRequest);
LOGF_LN("wValue: 0x%04"PRIx16, setup_data->wValue);
LOGF_LN("wIndex: 0x%04"PRIx16, setup_data->wIndex);
LOGF_LN("wLength: %"PRIu16, setup_data->wLength);
USBD_LOGF_LN(USB_VSETUP, "SETUP:reqType 0x%02"PRIx8"; Request 0x%02"PRIx8
";Value 0x%04"PRIx16";Index 0x%04"PRIx16
";Length: %"PRIu16
, setup_data->bmRequestType, setup_data->bRequest
, setup_data->wValue, setup_data->wIndex
, setup_data->wLength
);
} break;
case DWC_OTG_GRXSTSP_PKTSTS_SETUP_COMP: {
/* Enable Interrupt to receive the SETUP packet */
Expand All @@ -824,22 +841,22 @@ static void process_in_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)

if (REBASE(DWC_OTG_DIEPxINT, ep_num) & DWC_OTG_DIEPINT_EPDISD) {
REBASE(DWC_OTG_DIEPxINT, ep_num) = DWC_OTG_DIEPINT_EPDISD;
LOGF_LN("Endpoint disabled 0x%"PRIx8, ep_addr);
USBD_LOGF_LN(USB_VIO, "ep0x%"PRIx8" disabled", ep_addr);
}

usbd_urb *urb = usbd_find_active_urb(dev, ep_addr);

if (REBASE(DWC_OTG_DIEPxINT, ep_num) & DWC_OTG_DIEPINT_XFRC) {
REBASE(DWC_OTG_DIEPxINT, ep_num) = DWC_OTG_DIEPINT_XFRC;

LOGF_LN("Transfer Complete: endpoint 0x%"PRIx8, ep_addr);

if (!ep_num && urb != NULL && dev->private_data.ep0tsiz_pktcnt) {
/* We are still sending data! */

size_t rem = urb->transfer.length - urb->transfer.transferred;
uint32_t xfrsiz = MIN(urb->transfer.ep_size, rem);
dev->private_data.ep0tsiz_pktcnt--;
USBD_LOGF_LN(USB_VIO2, "USBD: new frame: ep%"PRIx8" len %d\n", ep_addr, xfrsiz);
//USBD_LOGF_LN(USB_VIO3, "USBD:IN%x:new0:DIEP3TSIZ=0x%x", ep_num, REBASE(DWC_OTG_DIEPxTSIZ, 3))

REBASE(DWC_OTG_DIEP0TSIZ) = DWC_OTG_DIEP0TSIZ_PKTCNT_1 |
DWC_OTG_DIEP0TSIZ_XFRSIZ(xfrsiz);
Expand All @@ -860,6 +877,7 @@ static void process_in_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)
/* Disable Interrupt */
REBASE(DWC_OTG_DAINTMSK) &= ~DWC_OTG_DAINTMSK_IEPM(ep_num);

USBD_LOGF_LN(USB_VIO2, "USBD: last frame: ep%"PRIx8"\n", ep_addr);
/* The URB has been processed, do the callback */
if (urb != NULL) {
usbd_urb_complete(dev, urb, USBD_SUCCESS);
Expand All @@ -871,7 +889,7 @@ static void process_in_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)

if (REBASE(DWC_OTG_DIEPxINT, ep_num) & DWC_OTG_DIEPINT_TXFE) {
/* Send more data */
LOGF_LN("Sending more data for endpoint 0x%"PRIx8, ep_addr);
USBD_LOGF_LN(USB_VIO2, "Sending more data for ep%"PRIx8, ep_addr);

if (urb != NULL) {
/* As per doc, before writing to FIFO, we need to write to CTL register.
Expand All @@ -883,7 +901,7 @@ static void process_in_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)
}

if (REBASE(DWC_OTG_DIEPxINT, ep_num) & DWC_OTG_DIEPINT_ITTXFE) {
LOGF_LN("Data IN Token received when endpoint 0x%"PRIx8" FIFO was empty",
USBD_LOGF_LN(USB_VIO2, "Data IN Token received when ep%"PRIx8" FIFO was empty",
ep_addr);
REBASE(DWC_OTG_DIEPxINT, ep_num) = DWC_OTG_DIEPINT_ITTXFE;
}
Expand All @@ -901,13 +919,12 @@ static void process_out_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)
const uint8_t ep_addr = ep_num;

if (REBASE(DWC_OTG_DOEPxINT, ep_num) & DWC_OTG_DOEPINT_EPDISD) {
LOGF_LN("Endpoint disabled 0x%"PRIx8, ep_addr);
USBD_LOGF_LN(USB_VIO, "ep0x%"PRIx8" disabled", ep_addr);
REBASE(DWC_OTG_DOEPxINT, ep_num) = DWC_OTG_DOEPINT_EPDISD;
}

if (REBASE(DWC_OTG_DOEPxINT, ep_num) & DWC_OTG_DOEPINT_BBLERR) {
LOGF_LN("Received more data than expected on endpoint 0x%"PRIx8,
ep_addr);
USBD_LOGF_LN(USB_VIO, "Received more data than expected on ep0x%"PRIx8,ep_addr);
usbd_urb *urb = usbd_find_active_urb(dev, ep_addr);
REBASE(DWC_OTG_DOEPxINT, ep_num) = DWC_OTG_DOEPINT_BBLERR;
premature_urb_complete(dev, urb, USBD_ERR_BABBLE);
Expand All @@ -916,11 +933,11 @@ static void process_out_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)
if (REBASE(DWC_OTG_DOEPxINT, ep_num) & DWC_OTG_DOEPINT_XFRC) {
REBASE(DWC_OTG_DOEPxINT, ep_num) = DWC_OTG_DOEPINT_XFRC;

LOGF_LN("Transfer Complete: endpoint 0x%"PRIx8, ep_addr);
usbd_urb *urb = usbd_find_active_urb(dev, ep_addr);

if (!ep_num && urb != NULL && dev->private_data.ep0tsiz_pktcnt) {
/* We are still expecting data! */
USBD_LOGF_LN(USB_VIO, "Transfer chunk Complete: ep0x%"PRIx8, ep_addr);

dev->private_data.ep0tsiz_pktcnt--;

Expand All @@ -930,6 +947,7 @@ static void process_out_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)

REBASE(DWC_OTG_DOEP0CTL) |= DWC_OTG_DOEP0CTL_EPENA;
} else {
USBD_LOGF_LN(USB_VIO, "Transfer Complete: ep0x%"PRIx8, ep_addr);
/* Set NAK on the endpoint */
REBASE(DWC_OTG_DOEPxCTL, ep_num) |= DWC_OTG_DOEPCTL_SNAK;

Expand All @@ -946,7 +964,7 @@ static void process_out_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)
}

if (REBASE(DWC_OTG_DOEPxINT, ep_num) & DWC_OTG_DOEPINT_STUP) {
LOGF_LN("Setup phase done for endpoint 0x%"PRIx8, ep_addr);
USBD_LOGF_LN(USB_VSETUP, "Setup phase done for ep0x%"PRIx8, ep_addr);
REBASE(DWC_OTG_DOEPxINT, ep_num) = DWC_OTG_DOEPINT_STUP;

REBASE(DWC_OTG_DOEPxTSIZ, ep_num) |= DWC_OTG_DOEPTSIZ_STUPCNT_3;
Expand All @@ -955,8 +973,7 @@ static void process_out_endpoint_interrupt(usbd_device *dev, uint8_t ep_num)

if (REBASE(DWC_OTG_DOEPxINT, ep_num) & DWC_OTG_DOEPINT_OTEPDIS) {
REBASE(DWC_OTG_DOEPxINT, ep_num) = DWC_OTG_DOEPINT_OTEPDIS;
LOGF_LN("Data OUT Token received when endpoint 0x%"PRIx8" was disable",
ep_addr);
USBD_LOGF_LN(USB_VIO, "Data OUT Token received when ep0x%"PRIx8" was disable", ep_addr);
}
}

Expand All @@ -980,6 +997,7 @@ static inline void alloc_fifo_for_ep0_only(usbd_device *dev)
void dwc_otg_poll(usbd_device *dev)
{
if (REBASE(DWC_OTG_GINTSTS) & DWC_OTG_GINTSTS_ENUMDNE) {
USBD_LOG_LN(USB_VIO_INIT,"USBD:enumerated");
REBASE(DWC_OTG_DCFG) &= ~DWC_OTG_DCFG_DAD_MASK;
disable_all_non_ep0(dev);
alloc_fifo_for_ep0_only(dev);
Expand Down
16 changes: 15 additions & 1 deletion lib/usbd/class/usbd_msc.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <unicore-mx/usbd/class/msc.h>
#include "../usbd_private.h"

#ifndef USB_VSETUP_MSC
#define USB_VSETUP_MSC USB_VSETUP
#endif

/*
* TODO:
* - Removable media support
Expand Down Expand Up @@ -230,6 +234,8 @@ static void scsi_write_10(usbd_msc *ms,
trans->current_block = 0;

trans->bytes_to_recv = trans->block_count << 9;
USBD_LOGF_LN(USB_MSC, "SCSI: write10 lba%x * %x"
, trans->lba_start, trans->block_count);
}
}

Expand All @@ -248,6 +254,8 @@ static void scsi_read_10(usbd_msc *ms,
/* both are in terms of 512 byte blocks, so shift by 9 */
trans->bytes_to_send = trans->block_count << 9;

USBD_LOGF_LN(USB_MSC, "SCSI: read10 lba%x * %x"
, trans->lba_start, trans->block_count);
set_sbc_status_good(ms);
}
}
Expand Down Expand Up @@ -406,6 +414,7 @@ static void scsi_command(usbd_msc *ms,
trans->byte_count = 0;
}

USBD_LOGF_LN(USB_VIO_MSC, "SCSI:cmd %x", trans->cbw.CBWCB[0]);
switch (trans->cbw.CBWCB[0]) {
case USB_MSC_SCSI_TEST_UNIT_READY:
case USB_MSC_SCSI_SEND_DIAGNOSTIC:
Expand Down Expand Up @@ -440,6 +449,7 @@ static void scsi_command(usbd_msc *ms,
scsi_write_10(ms, trans, event);
break;
default:
USBD_LOGF_LN(USB_VIO_MSC, "SCSI:cmd %x uncknown", trans->cbw.CBWCB[0]);
set_sbc_status(ms, SBC_SENSE_KEY_ILLEGAL_REQUEST,
SBC_ASC_INVALID_COMMAND_OPERATION_CODE,
SBC_ASCQ_NA);
Expand Down Expand Up @@ -791,18 +801,22 @@ bool usbd_msc_setup_ep0(usbd_msc *ms,
const uint8_t value = USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE;

if ((setup_data->bmRequestType & mask) == value) {
USBD_LOGF(USB_VSETUP_MSC, "USB:MSC:Req %x ", (int)setup_data->bRequest);
switch (setup_data->bRequest) {
case USB_MSC_REQ_BULK_ONLY_RESET:
USBD_LOG(USB_VSETUP_MSC,"BULK_ONLY_RESET\n");
/* Do any special reset code here. */
usbd_ep0_transfer(dev, setup_data, NULL, 0, NULL);
return true;
case USB_MSC_REQ_GET_MAX_LUN: {
USBD_LOG(USB_VSETUP_MSC,"MAX_LUN 0\n");
/* Return the number of LUNs. We use 0. */
static const uint8_t res = 0;
usbd_ep0_transfer(dev, setup_data, (void *) &res,
sizeof(res), NULL);
return true;
}}
}
}//switch (setup_data->bRequest)
}

return false;
Expand Down
6 changes: 6 additions & 0 deletions lib/usbd/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,9 @@ usbd_speed usbd_get_speed(usbd_device *dev)

/**@}*/

#if defined(USBD_DEBUG)
void usbd_log_call(const char *fname){
USBD_LOGF_LN(USB_VALL, "inside %s" , fname);
}
#endif

5 changes: 4 additions & 1 deletion lib/usbd/usbd_ep0.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,14 +790,17 @@ standard_request_endpoint(usbd_device *dev, struct usbd_control_arg *arg)
case USB_REQ_CLEAR_FEATURE:
if (arg->setup->wValue == USB_FEAT_ENDPOINT_HALT) {
uint8_t ep_addr = arg->setup->wIndex;
USBD_LOGF_LN(USB_VSETUP,"USBD:CLEAR_FEATURE - halt EP%x", ep_addr);
usbd_set_ep_dtog(dev, ep_addr, false);
usbd_set_ep_stall(dev, ep_addr, false);
return USBD_REQ_HANDLED;
}
break;
case USB_REQ_SET_FEATURE:
if (arg->setup->wValue == USB_FEAT_ENDPOINT_HALT) {
usbd_set_ep_stall(dev, arg->setup->wIndex, true);
uint8_t ep_addr = arg->setup->wIndex;
USBD_LOGF_LN(USB_VSETUP,"USBD:SET_FEATURE - halt EP%x", ep_addr);
usbd_set_ep_stall(dev, ep_addr, true);
return USBD_REQ_HANDLED;
}
break;
Expand Down
Loading