diff options
author | NIIBE Yutaka <gniibe@fsij.org> | 2017-10-10 12:09:06 +0900 |
---|---|---|
committer | NIIBE Yutaka <gniibe@fsij.org> | 2017-10-10 12:16:03 +0900 |
commit | a34937453c35c2d836748aa1b1fcd7a971cd90eb (patch) | |
tree | 3658b8f114ef8a7ac426e4906845e46c38220e2c | |
parent | dd54b5ff202e3b1bc863e32fb3eba7633a7aeb01 (diff) |
New: debug option.
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | mcu/sys-gnu-linux.c | 20 | ||||
-rw-r--r-- | mcu/sys-gnu-linux.h | 6 | ||||
-rw-r--r-- | mcu/usb-usbip.c | 166 |
4 files changed, 145 insertions, 51 deletions
@@ -1,5 +1,9 @@ 2017-10-09 NIIBE Yutaka <gniibe@fsij.org> + * mcu/sys-gnu-linux.h (debug): New. + * mcu/sys-gnu-linux.c: Use debug. + * mcu/usb-usbip.c: Use debug. + * example-cdc/sample.ld (.process_stack, .main_stack): Add NOLOAD. 2017-09-29 NIIBE Yutaka <gniibe@fsij.org> diff --git a/mcu/sys-gnu-linux.c b/mcu/sys-gnu-linux.c index 1b4dbfa..332003e 100644 --- a/mcu/sys-gnu-linux.c +++ b/mcu/sys-gnu-linux.c @@ -11,6 +11,7 @@ #include <sys/mman.h> #include "board.h" +#include "sys.h" const uint8_t sys_version[8] = { 3*2+2, /* bLength */ @@ -24,10 +25,13 @@ const uint32_t sys_board_id = BOARD_ID; const uint8_t sys_board_name[] = BOARD_NAME; #endif +int debug; + void set_led (int on) { - puts (on ? "*": ""); + if ((debug & DEBUG_LED)) + puts (on ? "*": ""); } static const char *flash_path; @@ -95,7 +99,9 @@ flash_program_halfword (uintptr_t addr, uint16_t data) off_t offset; char buf[2]; - fprintf (stderr, "flash_program_halfword: addr=%016lx, data=%04x\n", addr, data); + if ((debug & DEBUG_FLASH)) + fprintf (stderr, "flash_program_halfword: addr=%016lx, data=%04x\n", + addr, data); offset = (off_t)(addr - (uintptr_t)flash_addr); if (offset < 0 || offset >= (off_t)flash_size) { @@ -126,7 +132,8 @@ flash_erase_page (uintptr_t addr) { off_t offset; - fprintf (stderr, "flash_erase_page: addr=%016lx\n", addr); + if ((debug & DEBUG_FLASH)) + fprintf (stderr, "flash_erase_page: addr=%016lx\n", addr); offset = (off_t)(addr - (uintptr_t)flash_addr); if (offset < 0 || offset >= (off_t)flash_size) @@ -174,7 +181,9 @@ flash_write (uintptr_t dst_addr, const uint8_t *src, size_t len) { off_t offset; - fprintf (stderr, "flash_write: addr=%016lx, %p, %zd\n", dst_addr, src, len); + if ((debug & DEBUG_FLASH)) + fprintf (stderr, "flash_write: addr=%016lx, %p, %zd\n", + dst_addr, src, len); offset = (off_t)(dst_addr - (uintptr_t)flash_addr); if (offset < 0 || offset >= (off_t)flash_size) @@ -201,7 +210,8 @@ flash_write (uintptr_t dst_addr, const uint8_t *src, size_t len) int flash_protect (void) { - fprintf (stderr, "flash_protect\n"); + if ((debug & DEBUG_FLASH)) + fprintf (stderr, "flash_protect\n"); return 0; } diff --git a/mcu/sys-gnu-linux.h b/mcu/sys-gnu-linux.h index 6fbc081..ea8a64d 100644 --- a/mcu/sys-gnu-linux.h +++ b/mcu/sys-gnu-linux.h @@ -7,6 +7,12 @@ extern const uint8_t sys_board_name[]; # define SYS_BOARD_ID BOARD_ID #endif +#define DEBUG_LED (1 << 1) +#define DEBUG_FLASH (1 << 2) +#define DEBUG_USB (1 << 3) + +extern int debug; + static inline const uint8_t * unique_device_id (void) { diff --git a/mcu/usb-usbip.c b/mcu/usb-usbip.c index 7e093b9..ee67fc4 100644 --- a/mcu/usb-usbip.c +++ b/mcu/usb-usbip.c @@ -51,6 +51,8 @@ #include <alloca.h> +#include "sys.h" /* for debug */ + static pthread_t tid_main; static pthread_t tid_usbip; @@ -304,17 +306,22 @@ hc_handle_control_urb (struct urb *urb) uint16_t remain = urb->len; uint64_t l; - puts ("hcu 0"); + if ((debug & DEBUG_USB)) + puts ("hcu 0"); usbc_ep0.urb = urb; r = control_setup_transaction (urb); if (r < 0) goto error; - puts ("hcu 1"); + if ((debug & DEBUG_USB)) + puts ("hcu 1"); + if (urb->dir == USBIP_DIR_OUT) { /* Output from host to device. */ - printf ("hcu: %d\n", r); + if ((debug & DEBUG_USB)) + printf ("hcu: %d\n", r); + while (r == 0) { if (remain > 64) @@ -340,7 +347,9 @@ hc_handle_control_urb (struct urb *urb) } else { /* Input from device to host. */ - puts ("hcu 2"); + if ((debug & DEBUG_USB)) + puts ("hcu 2"); + while (1) { if (remain > 64) @@ -353,28 +362,38 @@ hc_handle_control_urb (struct urb *urb) if (r < 0) break; - puts ("hcu 3"); + if ((debug & DEBUG_USB)) + puts ("hcu 3"); + remain -= r; urb->data_p += r; if (r < 64) break; } - puts ("hcu 4"); + + if ((debug & DEBUG_USB)) + puts ("hcu 4"); + if (r >= 0) { - puts ("hcu 5"); + if ((debug & DEBUG_USB)) + puts ("hcu 5"); + read (usbc_ep0.eventfd, &l, sizeof (l)); r = control_read_status_transaction (); if (r >= 0) r = remain; } - puts ("hcu 6"); + if ((debug & DEBUG_USB)) + puts ("hcu 6"); } if (r < 0) { error: - fprintf (stderr, "hcu 7 %d\n", r); + if ((debug & DEBUG_USB)) + printf ("hcu 7 %d\n", r); + /* recovery. */ usbc_ep0.state = USB_STATE_READY; } @@ -393,7 +412,9 @@ hc_handle_control_urb (struct urb *urb) else urb->len = 0; - printf ("hu-next: %d (%d)\n", urb->len, urb->seq); + if ((debug & DEBUG_USB)) + printf ("hu-next: %d (%d)\n", urb->len, urb->seq); + usbc_ep0.urb = NULL; return r; } @@ -452,7 +473,8 @@ usbip_finish_urb (struct urb *urb, int r) memset (&msg_rep, 0, sizeof (msg_rep)); msg_rep.len = htonl (urb->len); - fprintf (stderr, "ufu: %d (%d)\n", r, urb->seq); + if ((debug & DEBUG_USB)) + printf ("ufu: %d (%d)\n", r, urb->seq); if (r < 0) msg_rep.status = htonl (r); @@ -489,7 +511,9 @@ hc_handle_data_urb (struct usb_control *usbc_p) uint16_t count; struct urb *urb = usbc_p->urb; - puts ("hc_hdu 0"); + if ((debug & DEBUG_USB)) + puts ("hc_hdu 0"); + if (urb->remain > 64) count = 64; else @@ -497,7 +521,9 @@ hc_handle_data_urb (struct usb_control *usbc_p) if (urb->dir == USBIP_DIR_OUT) { /* Output from host to device. */ - puts ("hc_hdu 1"); + if ((debug & DEBUG_USB)) + puts ("hc_hdu 1"); + r = write_data_transaction (usbc_p, urb->ep, urb->data_p, count); if (r < 0) return r; @@ -509,14 +535,17 @@ hc_handle_data_urb (struct usb_control *usbc_p) { size_t len = urb->len - urb->remain; - fprintf (stderr, "->data: %lu\n", len); + if ((debug & DEBUG_USB)) + printf ("->data: %lu\n", len); + // successfully finished if (len) { char *s = alloca (len + 1); memcpy (s, urb->data, len); s[len] = 0; - fprintf (stderr, " : %s\n", s); + if ((debug & DEBUG_USB)) + printf (" : %s\n", s); } return 0; } @@ -525,7 +554,9 @@ hc_handle_data_urb (struct usb_control *usbc_p) } else { /* Input from device to host. */ - puts ("hc_hdu 2"); + if ((debug & DEBUG_USB)) + puts ("hc_hdu 2"); + r = read_data_transaction (usbc_p, urb->ep, urb->data_p, count); if (r < 0) return r; @@ -536,14 +567,17 @@ hc_handle_data_urb (struct usb_control *usbc_p) { size_t len = urb->len - urb->remain; - fprintf (stderr, "<-data: %lu %d\n", len, r); + if ((debug & DEBUG_USB)) + printf ("<-data: %lu %d\n", len, r); + // successfully finished if (len) { char *s = alloca (len + 1); memcpy (s, urb->data, len); s[len] = 0; - fprintf (stderr, " : %s\n", s); + if ((debug & DEBUG_USB)) + printf (" : %s\n", s); } urb->len = len; return 0; @@ -652,8 +686,9 @@ usbip_handle_urb (uint32_t seq) urb->remain = urb->len = ntohl (msg_cmd.len); urb->data_p = urb->data; - printf ("URB: dir=%s, ep=%d, len=%d\n", urb->dir==USBIP_DIR_IN? "IN": "OUT", - urb->ep, urb->len); + if ((debug & DEBUG_USB)) + printf ("URB: dir=%s, ep=%d, len=%d\n", urb->dir==USBIP_DIR_IN? "IN": "OUT", + urb->ep, urb->len); if (recv (fd, (char *)urb->setup, sizeof (urb->setup), 0) != sizeof (urb->setup)) { @@ -663,9 +698,10 @@ usbip_handle_urb (uint32_t seq) } if (urb->ep == 0) - printf ("URB: %02x %02x %02x %02x %02x %02x %02x %02x\n", - urb->setup[0], urb->setup[1], urb->setup[2], urb->setup[3], - urb->setup[4], urb->setup[5], urb->setup[6], urb->setup[7]); + if ((debug & DEBUG_USB)) + printf ("URB: %02x %02x %02x %02x %02x %02x %02x %02x\n", + urb->setup[0], urb->setup[1], urb->setup[2], urb->setup[3], + urb->setup[4], urb->setup[5], urb->setup[6], urb->setup[7]); if (urb->dir == USBIP_DIR_OUT && urb->len) { @@ -777,7 +813,9 @@ usbip_process_cmd (void) const char *device_list; size_t device_list_size; - printf ("Device List\n"); + if ((debug & DEBUG_USB)) + printf ("Device List\n"); + if (attached) { fprintf (stderr, "REQ list while attached\n"); @@ -808,7 +846,9 @@ usbip_process_cmd (void) size_t attach_size; char busid[32]; - printf ("Attach device\n"); + if ((debug & DEBUG_USB)) + printf ("Attach device\n"); + if (attached) { fprintf (stderr, "REQ attach while attached\n"); @@ -828,7 +868,8 @@ usbip_process_cmd (void) if (attach && (size_t)send (fd, attach, attach_size, 0) == attach_size) { - printf ("Attach device!\n"); + if ((debug & DEBUG_USB)) + printf ("Attach device!\n"); attached = 1; pthread_mutex_unlock (&fd_mutex); } @@ -847,7 +888,8 @@ usbip_process_cmd (void) return -1; } - printf ("URB SUBMIT! %d\n", msg.seq); + if ((debug & DEBUG_USB)) + printf ("URB SUBMIT! %d\n", msg.seq); usbip_handle_urb (msg.seq); } else if (msg.cmd == CMD_URB_UNLINK) @@ -908,7 +950,8 @@ usbip_process_cmd (void) if (found) msg_rep.status = htonl(-ECONNRESET); - printf ("URB UNLINK! %d: %s\n", seq, found?"o":"x"); + if ((debug & DEBUG_USB)) + printf ("URB UNLINK! %d: %s\n", seq, found?"o":"x"); pthread_mutex_lock (&fd_mutex); if ((size_t)send (fd, &msg, sizeof (msg), 0) != sizeof (msg)) @@ -940,7 +983,9 @@ usbip_ep_ready (struct usb_control *usbc_p) if (!usbc_p->urb) { - puts ("???usbip_ep_ready"); + if ((debug & DEBUG_USB)) + puts ("???usbip_ep_ready"); + return; } @@ -1088,7 +1133,8 @@ usbip_run_server (void *arg) if ((pollfds[0].revents & POLLNVAL) || (pollfds[0].revents & POLLERR)) { - fprintf (stderr, "Error on USBIP client socket: %d\n", pollfds[0].revents); + fprintf (stderr, "Error on USBIP client socket: %d\n", + pollfds[0].revents); exit (1); } if ((pollfds[0].revents & POLLIN)) @@ -1110,7 +1156,9 @@ usbip_run_server (void *arg) } if ((pollfds[i*2].revents & POLLIN)) { - puts ("poll in read"); + if ((debug & DEBUG_USB)) + puts ("poll in read"); + usbip_ep_ready (&usbc_ep_in[i]); } @@ -1122,7 +1170,9 @@ usbip_run_server (void *arg) } if ((pollfds[i*2+1].revents & POLLIN)) { - puts ("poll out read"); + if ((debug & DEBUG_USB)) + puts ("poll out read"); + usbip_ep_ready (&usbc_ep_out[i]); } } @@ -1163,7 +1213,8 @@ control_setup_transaction (struct urb *urb) r = -EAGAIN; else { - fprintf (stderr, "cst error %d\n", usbc_ep0.state); + if ((debug & DEBUG_USB)) + printf ("cst error %d\n", usbc_ep0.state); r = -EPIPE; } pthread_mutex_unlock (&usbc_ep0.mutex); @@ -1187,7 +1238,8 @@ control_write_data_transaction (char *buf, uint16_t count) { if (usbc_ep0.len < count) { - fprintf (stderr, "*** usbc_ep0.len < count"); + if ((debug & DEBUG_USB)) + printf ("*** usbc_ep0.len < count"); r = -EPIPE; usbc_ep0.state = USB_STATE_STALL; } @@ -1224,9 +1276,12 @@ control_write_status_transaction (void) pthread_mutex_lock (&usbc_ep0.mutex); if (usbc_ep0.state == USB_STATE_READY) { - puts ("control_write_status_transaction"); + if ((debug & DEBUG_USB)) + puts ("control_write_status_transaction"); + if (usbc_ep0.len != 0) - fprintf (stderr, "*** ACK length %d\n", usbc_ep0.len); + if ((debug & DEBUG_USB)) + printf ("*** ACK length %d\n", usbc_ep0.len); usbc_ep0.state = USB_STATE_NAK; notify_device (USB_INTR_DATA_TRANSFER, 0, USBIP_DIR_IN); r = 0; @@ -1255,7 +1310,10 @@ control_read_data_transaction (char *buf, uint16_t count) if (usbc_ep0.state == USB_STATE_READY) { if (usbc_ep0.len > count) - fprintf (stderr, "***c read: length %d > %d\n", usbc_ep0.len, count); + { + if ((debug & DEBUG_USB)) + printf ("***c read: length %d > %d\n", usbc_ep0.len, count); + } else count = usbc_ep0.len; @@ -1337,7 +1395,10 @@ read_data_transaction (struct usb_control *usbc_p, int ep_num, char *buf, uint16_t count) { if (usbc_p->len > count) - fprintf (stderr, "*** length %d > %d\n", usbc_p->len, count); + { + if ((debug & DEBUG_USB)) + printf ("*** length %d > %d\n", usbc_p->len, count); + } else count = usbc_p->len; @@ -1902,7 +1963,9 @@ static int handle_in0 (struct usb_dev *dev) == (STANDARD_REQUEST | DEVICE_RECIPIENT))) { /* XXX: record the assigned address of this device??? */ - printf ("Set Address: %d\n", dev->dev_req.value); + if ((debug & DEBUG_USB)) + printf ("Set Address: %d\n", dev->dev_req.value); + r = USB_EVENT_DEVICE_ADDRESSED; } else @@ -1917,7 +1980,9 @@ static int handle_in0 (struct usb_dev *dev) } else { - puts ("handle_in0 error"); + if ((debug & DEBUG_USB)) + puts ("handle_in0 error"); + dev->state = STALLED; pthread_mutex_lock (&usbc_ep0.mutex); usbc_ep0.state = USB_STATE_STALL; @@ -1955,7 +2020,9 @@ static void handle_out0 (struct usb_dev *dev) * Or else, unexpected state. * STALL the endpoint, until we receive the next SETUP token. */ - puts ("handle_out0 error"); + if ((debug & DEBUG_USB)) + puts ("handle_out0 error"); + dev->state = STALLED; pthread_mutex_lock (&usbc_ep0.mutex); usbc_ep0.state = USB_STATE_STALL; @@ -2077,7 +2144,9 @@ usb_lld_current_configuration (struct usb_dev *dev) void usb_lld_ctrl_error (struct usb_dev *dev) { - puts ("ctrl_error"); + if ((debug & DEBUG_USB)) + puts ("ctrl_error"); + dev->state = STALLED; pthread_mutex_lock (&usbc_ep0.mutex); usbc_ep0.state = USB_STATE_STALL; @@ -2134,7 +2203,9 @@ usb_lld_stall_tx (int ep_num) usbc_p->state = USB_STATE_STALL; notify_hostcontroller (usbc_p); pthread_mutex_unlock (&usbc_p->mutex); - printf ("stall tx %d\n", ep_num); + + if ((debug & DEBUG_USB)) + printf ("stall tx %d\n", ep_num); } void @@ -2146,7 +2217,8 @@ usb_lld_stall_rx (int ep_num) usbc_p->state = USB_STATE_STALL; notify_hostcontroller (usbc_p); pthread_mutex_unlock (&usbc_p->mutex); - printf ("stall rx %d\n", ep_num); + if ((debug & DEBUG_USB)) + printf ("stall rx %d\n", ep_num); } void @@ -2161,7 +2233,8 @@ usb_lld_rx_enable_buf (int ep_num, void *buf, size_t len) notify_hostcontroller (usbc_p); pthread_mutex_unlock (&usbc_p->mutex); - printf ("usb_lld_rx_enable_buf: %d\n", ep_num); + if ((debug & DEBUG_USB)) + printf ("usb_lld_rx_enable_buf: %d\n", ep_num); } @@ -2176,5 +2249,6 @@ usb_lld_tx_enable_buf (int ep_num, const void *buf, size_t len) usbc_p->len = len; notify_hostcontroller (usbc_p); pthread_mutex_unlock (&usbc_p->mutex); - printf ("usb_lld_tx_enable_buf: %d %ld\n", ep_num, len); + if ((debug & DEBUG_USB)) + printf ("usb_lld_tx_enable_buf: %d %ld\n", ep_num, len); } |