From 5d9802388c32934a4201fa29ccaf120d2faab8a7 Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Thu, 6 Jul 2017 15:52:46 +0900
Subject: Fix USB driver on GNU/Linux.

---
 ChangeLog                   |  6 ++++++
 example-fraucheky/configure |  3 +--
 example-fraucheky/main.c    |  6 ------
 mcu/usb-usbip.c             | 28 +++++++++++++---------------
 4 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7b274d3..d6bf520 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2017-07-06  NIIBE Yutaka  <gniibe@fsij.org>
+
+	* mcu/usb-usbip.c (hc_handle_data_urb): Fix the condition of the
+	end of transaction.
+	(read_data_transaction): Allow partial read by host.
+
 2017-07-05  NIIBE Yutaka  <gniibe@fsij.org>
 
 	* example-fraucheky: New.
diff --git a/example-fraucheky/configure b/example-fraucheky/configure
index 5014a78..7bc023c 100755
--- a/example-fraucheky/configure
+++ b/example-fraucheky/configure
@@ -9,6 +9,7 @@ vidpid=none
 verbose=no
 debug=no
 with_fraucheky=yes
+with_index=./INDEX.HTM
 
 if test -d ../.git; then
   REVISION=`git describe --dirty="-modified"`
@@ -34,8 +35,6 @@ for option; do
     verbose=yes ;;
   --vidpid=*)
     vidpid=$optarg ;;
-  --with-index=*)
-    with_index=$optarg ;;
   *)
     echo "Unrecognized option \`$option'" >&2
     echo "Try \`$0 --help' for more information." >&2
diff --git a/example-fraucheky/main.c b/example-fraucheky/main.c
index efad8e1..bbb6adf 100644
--- a/example-fraucheky/main.c
+++ b/example-fraucheky/main.c
@@ -289,12 +289,6 @@ static char __process3_stack_base__[4096];
 #define main emulated_main
 #endif
 
-void
-usb_lld_write (uint8_t ep_num, const void *buf, size_t len)
-{
-  usb_lld_tx_enable_buf (ep_num, buf, len);
-}
-
 /*
  * Entry point.
  *
diff --git a/mcu/usb-usbip.c b/mcu/usb-usbip.c
index c6fac0a..47882ec 100644
--- a/mcu/usb-usbip.c
+++ b/mcu/usb-usbip.c
@@ -483,7 +483,7 @@ usbip_finish_urb (struct urb *urb, int r)
 
 
 static int
-hc_handle_data_urb  (struct usb_control *usbc_p)
+hc_handle_data_urb (struct usb_control *usbc_p)
 {
   int r;
   uint16_t count;
@@ -505,7 +505,7 @@ hc_handle_data_urb  (struct usb_control *usbc_p)
       urb->data_p += count;
       urb->remain -= count;
 
-      if (count < 64)
+      if (urb->remain == 0 || count < 64)
 	{
 	  size_t len = urb->len - urb->remain;
 
@@ -532,11 +532,11 @@ hc_handle_data_urb  (struct usb_control *usbc_p)
 
       urb->remain -= r;
       urb->data_p += r;
-      if (r < 64)
+      if (urb->remain == 0 || r < 64)
 	{
 	  size_t len = urb->len - urb->remain;
 
-	  fprintf (stderr, "<-data: %lu\n", len);
+	  fprintf (stderr, "<-data: %lu %d\n", len, r);
 	  // successfully finished
 	  if (len)
 	    {
@@ -1257,7 +1257,7 @@ control_read_data_transaction (char *buf, uint16_t count)
   if (usbc_ep0.state == USB_STATE_READY)
     {
       if (usbc_ep0.len > count)
-	fprintf (stderr, "*** length %d\n", usbc_ep0.len);
+	fprintf (stderr, "***c read: length %d > %d\n", usbc_ep0.len, count);
       else
 	count = usbc_ep0.len;
 
@@ -1339,16 +1339,14 @@ 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\n", usbc_p->len);
-      usbc_p->state = USB_STATE_STALL;
-      return -EPIPE;
-    }
+    fprintf (stderr, "*** length %d > %d\n", usbc_p->len, count);
+  else
+    count = usbc_p->len;
 
   usbc_p->state = USB_STATE_NAK;
-  memcpy (buf, usbc_p->buf, usbc_p->len);
+  memcpy (buf, usbc_p->buf, count);
   notify_device (USB_INTR_DATA_TRANSFER, ep_num, USBIP_DIR_IN);
-  return usbc_p->len;
+  return count;
 }
 
 void chx_handle_intr (uint32_t irq_num);
@@ -2135,14 +2133,14 @@ usb_lld_setup_endp (struct usb_dev *dev, int ep_num, int rx_en, int tx_en)
 void
 usb_lld_stall_tx (int ep_num)
 {
-  printf ("stall tx %d", ep_num);
+  printf ("stall tx %d\n", ep_num);
   usbc_ep_in[ep_num].state = USB_STATE_STALL;
 }
 
 void
 usb_lld_stall_rx (int ep_num)
 {
-  printf ("stall rx %d", ep_num);
+  printf ("stall rx %d\n", ep_num);
   usbc_ep_out[ep_num].state = USB_STATE_STALL;
 }
 
@@ -2175,5 +2173,5 @@ usb_lld_tx_enable_buf (int ep_num, const void *buf, size_t len)
   usbc_p->len = len;
   write (usbc_p->eventfd, &l, sizeof (l));
   pthread_mutex_unlock (&usbc_p->mutex);
-  printf ("usb_lld_tx_enable_buf: %d\n", ep_num);
+  printf ("usb_lld_tx_enable_buf: %d %ld\n", ep_num, len);
 }
-- 
cgit v1.2.3