Bug 58925

Summary: bug in *BSD port of devel/libusb, with fix
Product: Ports & Packages Reporter: Andreas Klemm <andreas>
Component: Individual Port(s)Assignee: freebsd-ports-bugs (Nobody) <ports-bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Latest   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Andreas Klemm 2003-11-04 16:20:24 UTC
	Have a look at this bug report with fix:
	http://sourceforge.net/tracker/index.php?func=detail&aid=835645&group_id=1674&atid=101674
	Problem is:
	connect a canon powershot G5 to your USB port.
	You can talk to it in canon native mode perfectly.
	But not in ptp2 mode.

	Unfortunately the gphoto2 port autodetects ptp2 mode first.
	And in this ptp2 mode the camera locks up after transferring
	the first bytes.

	Workaround is to choose the canon native mode.
	But this surely doesn't fix libusb and if you have a 
	Camera that needs the ptp2 mode ....

	So libusb has definitively to be fixed for all BSD's.

	The fix removes the linux specific approach from *BSD code.
	The porter did his best, when reading the comments in the header ;-)
	But broke it when porting the code from Linux to bsd.

	You may ask: But why does the camera run in canon mode but not
	especially in ptp2 mode ?
	Answer: The canon driver uses an arbitrary large packets.
	PTP does not. PTP cannot determine the mmount of data that the
	device is going to send. That's why PTP calls are a read from
	an USB endpoint with a large number of bytes.

	Problems that remain after this fix are only Caono Firmware
	problems:
	After one ptp2 session the camera returns to canon native mode
	and stays in it!
	After a ptp2 session you have to powercycle the camera, to be able
	to talk in ptp2 mode again.
	Workaround: don't get listings, you may do -P or start
	a gphoto2 -shell and get listing, then get photos, etc 
	So to say: "do it all in ONE session".

How-To-Repeat: 	Try to use camera in ptp2 mode. Chokes after 1st read.
	Use truss to find it out...:
	truss -o file gphoto2 -L --debug > & debug
Comment 1 ANdrei 2003-11-18 13:27:21 UTC
i can confirm that the fix is working. i was having probelms using a HP 
Photosmart camera with the PTP driver, was receiving timeouts...
been searching for a fix for 2 weeks, when Mariusz Woloszyn told me that 
they found this bug... after editing by hand the libusb sources, and 
recompiling, everything worked ok, without any other software update or 
smtg else...

i did not notice any other problems since than, other than little 
synchronization problems when stressing the camera/USB by turning it 
off, then back on quickly and running gphoto2 --auto-detect in the same 
time on a 400MHz (old!) machine... but i think this is not at all 
related to the patch, which is working fine. i'd recommend patching the 
current port of libusb so that this problem is fixed and posting a note 
or smtg about this, as i've seen at least a dozen people having posted 
related problems in various forums, from Germany to Australia... maybe a 
notice on the gPhoto homepage would be a good idea, if there's any need 
i can write a short description/how-to on this thema, publish it on my 
page and somebody can put a link to it there...

anyway, thanks to all the people working on this!

and if i should write the how-to (only if there's interest in linking 
from the gPhoto2-page to it), just mail me and let me know... if not, 
it's futile work in my opinion, and i shall not do it... i could write a 
fairly complete tutorial on using ur digital camera through the PTP 
driver on FreeBSD, with all the probs i got into, and the fixes i 
found... there are some things which need a bit of time/experience to 
get a camera working here...



THIS mail went to:
Andreas Klemm
Mariusz Woloszyn
Freebsd-gnats-submit
Freebsd Libusb port maintainer
gphoto2 devel maillist


-- 
ANdrei R.
Software Developer
Cronon AG
Regensburg / Germany

------------------------------------[ http://www.goanga.com ]---
      _     _
    o' \.=./ `o   The only people who find what they're looking
       (o o)      for in life are the ones looking for mistakes.
--ooO--(_)--Ooo-------------------------------------------------
Comment 2 johnjen 2003-11-22 19:58:50 UTC
Well, I've received only good news related to this patch on the unofficially
rolled libusb-0.1.7_1 port that I distributed (with a call for testers). I
think it's OK to patch this and I will work with the libusb code t get the
patch into the actual package at some point.

Somebody with the commit-bit in Ports, please apply the following patch to
libusb. Note that it will create files/patch-usb.c which needs to be "cvs
add"'ed.

-Jr

diff -urN libusb.orig/Makefile libusb/Makefile
--- libusb.orig/Makefile	Tue Nov 18 22:29:36 2003
+++ libusb/Makefile	Tue Nov 18 22:30:05 2003
@@ -7,6 +7,7 @@
 
 PORTNAME=	libusb
 PORTVERSION=	0.1.7
+PORTREVISION=	1
 CATEGORIES=	devel
 MASTER_SITES=	${MASTER_SITE_SOURCEFORGE}
 MASTER_SITE_SUBDIR=	${PORTNAME}
diff -urN libusb.orig/files/patch-bsd.c libusb/files/patch-bsd.c
--- libusb.orig/files/patch-bsd.c	Wed Dec 31 17:00:00 1969
+++ libusb/files/patch-bsd.c	Tue Nov 18 22:32:25 2003
@@ -0,0 +1,62 @@
+--- bsd.c	Tue Nov  4 12:23:39 2003
++++ bsd-new.c	Tue Nov  4 12:23:21 2003
+@@ -276,7 +276,7 @@
+ int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
+                    int timeout)
+ {
+-  int fd, ret, sent = 0;
++  int fd, ret;
+ 
+   /* Ensure the endpoint address is correct */
+   ep &= ~USB_ENDPOINT_IN;
+@@ -298,8 +298,7 @@
+     USB_ERROR_STR(ret, "error setting timeout: %s",
+                   strerror(errno));
+ 
+-  do {
+-    ret = write(fd, bytes+sent, size-sent);
++    ret = write(fd, bytes, size);
+     if (ret < 0)
+ #if __FreeBSD__
+       USB_ERROR_STR(ret, "error writing to bulk endpoint %s.%d: %s",
+@@ -309,16 +308,13 @@
+                   dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
+ #endif
+ 
+-    sent += ret;
+-  } while(ret > 0 && sent < size);
+-
+-  return sent;
++  return ret;
+ }
+ 
+ int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
+                   int timeout)
+ {
+-  int fd, ret, retrieved = 0, one = 1;
++  int fd, ret, one = 1;
+ 
+   /* Ensure the endpoint address is correct */
+   ep |= USB_ENDPOINT_IN;
+@@ -345,8 +341,7 @@
+     USB_ERROR_STR(ret, "error setting short xfer: %s",
+                   strerror(errno));
+ 
+-  do {
+-    ret = read(fd, bytes+retrieved, size-retrieved);
++    ret = read(fd, bytes, size);
+     if (ret < 0)
+ #if __FreeBSD__
+       USB_ERROR_STR(ret, "error reading from bulk endpoint %s.%d: %s",
+@@ -355,10 +350,8 @@
+       USB_ERROR_STR(ret, "error reading from bulk endpoint %s.%02d: %s",
+                   dev->device->filename, UE_GET_ADDR(ep), strerror(errno));
+ #endif
+-    retrieved += ret;
+-  } while (ret > 0 && retrieved < size);
+ 
+-  return retrieved;
++  return ret;
+ }
+ 
+ int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,



-- 
John & Jennifer Reynolds  johnjen at reynoldsnet.org        www.reynoldsnet.org
Structural / Physical Design - ICG/PNG SCD     jreynold at sedona.ch.intel.com
Running FreeBSD since 2.1.5-RELEASE.               FreeBSD: The Power to Serve!
"Unix is user friendly, it's just particular about the friends it chooses."
Comment 3 andreas freebsd_committer freebsd_triage 2003-11-25 19:36:09 UTC
State Changed
From-To: open->closed

applied fix from Mariusz Woloszyn <emsi@ipartners.pl> 
has been tested under -current and -stable