« Return to Thread: patch for libusb-0.9.3/libusb/os/linux_usbfs.c

Re: patch for libusb-0.9.3/libusb/os/linux_usbfs.c

by Ludovic Rousseau :: Rate this Message:

Reply to Author | View in Thread

I think I found the source of the problem.

On Sun, Nov 2, 2008 at 11:05 PM, Daniel Drake <dsd@...> wrote:
> Your application *definitely* stops all polling and calls to
> libusb_handle_events() (and variants) *before* calling libusb_close()?

With some logs I have:

libusb-compat debug: usb_bulk_io: endpoint 2 size 10 timeout 5000
libusb:debug [submit_bulk_transfer] need 1 urbs for new transfer with length 10
libusb:debug [libusb_get_next_timeout] next timeout in 0.086891s
libusb:debug [libusb_handle_events_timeout] another thread is doing
event handling
libusb:debug [handle_events] poll() returned 1
os/linux_usbfs.c:1944
libusb:debug [reap_for_handle] urb type=3 status=0 transferred=10
libusb:debug [handle_bulk_completion] handling completion status 0 of
bulk urb 1/1
libusb:debug [handle_bulk_completion] last URB in transfer --> complete!
libusb:debug [bulk_transfer_cb] actual_length=10
libusb:debug [libusb_get_next_timeout] next timeout in 0.085480s
libusb:debug [handle_events] poll() 2 fds with timeout in 86ms
libusb-compat debug: usb_bulk_io: endpoint 82 size 10 timeout 2000
libusb:debug [submit_bulk_transfer] need 1 urbs for new transfer with length 10
libusb:debug [libusb_get_next_timeout] next timeout in 0.085399s
libusb:debug [libusb_handle_events_timeout] another thread is doing
event handling
libusb:debug [handle_events] poll() returned 1
os/linux_usbfs.c:1944
libusb:debug [reap_for_handle] urb type=3 status=0 transferred=10
libusb:debug [handle_bulk_completion] handling completion status 0 of
bulk urb 1/1
libusb:debug [handle_bulk_completion] last URB in transfer --> complete!
libusb:debug [bulk_transfer_cb] actual_length=10
libusb:debug [libusb_get_next_timeout] next timeout in 0.084484s
libusb:debug [handle_events] poll() 2 fds with timeout in 85ms
00002505 <- 000000 81 00 00 00 00 00 07 01 00 00
00000015 ccid_usb.c:569:CloseUSB() Closing USB device: 002/073
00000016 ccid_usb.c:584:CloseUSB() Last slot closed. Release resources
libusb-compat debug: usb_release_interface: interface 0
libusb:debug [libusb_release_interface] interface 0
libusb-compat debug: usb_close:
libusb:debug [libusb_close]
libusb:debug [libusb_close] event_handler_active: 1
libusb:debug [usbi_remove_pollfd] remove fd 7

fd 7 removed here

00002047 readerfactory.c:1357:RFCleanupReaders() Stopping reader:
Gemplus GemPC Twin 01 00
00000013 eventhandler.c:126:EHDestroyEventHandler() Stomping thread.
00000012 ifdhandler.c:334:IFDHGetCapabilities() tag: 0xFB1,
usb:08e6/3437:libhal:/org/freedesktop/Hal/devices/usb_device_8e6_3437_noserial_0_if0
(lun: 10000)
libusb:debug [handle_events] poll() returned 1
00088932 eventhandler.c:139:EHDestroyEventHandler() Waiting polling thread
os/linux_usbfs.c:1944
POUET fd 7 not found

fd 7 not found since it has been removed earlier.

errno=Invalid argument (22)
handle: 0x80fcd0c
handle->dev: 0x1
Erreur de segmentation

and crash!!!

I also note that when libusb_close() is called an event handler is
still active. That is normal since I have two USB devices. When I call
usb_close() on the first device the second if still active.
Since libusb-compat has only one libusb-1.0 context then all the USB
devices share the same event handler.

I used a break point on usbi_remove_pollfd and have the following backtrace:
libusb-compat debug: usb_close:
libusb:debug [libusb_close]
libusb:debug [libusb_close] event_handler_active: 1

Breakpoint 2, usbi_remove_pollfd (ctx=0x80fcd00, fd=10) at io.c:1674
1674    {
(gdb) bt
#0  usbi_remove_pollfd (ctx=0x80fcd00, fd=10) at io.c:1674
#1  0xb7da0058 in op_close (dev_handle=0x81015d8) at os/linux_usbfs.c:995
#2  0xb7d9a7d5 in do_close (dev_handle=0x81015d8) at core.c:815
#3  0xb7d9a8a4 in libusb_close (dev_handle=0x81015d8) at core.c:841
#4  0xb7da6c34 in usb_close (dev=0x8101118) at core.c:630
#5  0xb7dc3b4f in CloseUSB (reader_index=0) at ccid_usb.c:592
#6  0xb7dc2ba4 in IFDHCloseChannel (Lun=0) at ifdhandler.c:249
#7  0x0804e7ac in IFDCloseIFD (rContext=0x80e6008) at ifdwrapper.c:216
#8  0x08050236 in RFUnInitializeReader (rContext=0x80e6008)
    at readerfactory.c:1145
#9  0x0805110d in RFRemoveReader (lpcReader=0xbfd6857c "Gemplus GemPC Twin",
    dwPort=2097152) at readerfactory.c:459
#10 0x08051389 in RFCleanupReaders (shouldExit=1) at readerfactory.c:1366
#11 0x0804ecc6 in SVCServiceRunLoop () at pcscdaemon.c:198
#12 0x0804f2eb in main (argc=2, argv=Cannot access memory at address 0x4
) at pcscdaemon.c:535


>From what I can understand op_close() in libusb/os/linux_usbfs.c is calling
    usbi_remove_pollfd(HANDLE_CTX(dev_handle), fd);
And this should remove any pollfd associated wit the dev_handle context.

Since libusb-compat uses only ONE libusb_context for all the devices
(declared static libusb_context *ctx = NULL; in
libusb-compat-0.1.0-beta2/libusb/core.c line 32) maybe this behavior
is expected?

Maybe libusb-compat should create one libusb_context per USB device?

Maybe the problem is in libusb 0.9.x and a pollfd of a device should
be associated to a device and not to the context of that device (since
devices can share the same context).

Is this the source of the problem? Or am I completely wrong?

Bye

--
 Dr. Ludovic Rousseau

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Libusb-devel mailing list
Libusb-devel@...
https://lists.sourceforge.net/lists/listinfo/libusb-devel

 « Return to Thread: patch for libusb-0.9.3/libusb/os/linux_usbfs.c