Hello,

I am trying to run libusbx on an unrooted android phone (Sony Ericson Xperia 
Active (Android 4.0.4)). But I have hit a major problem I cannot open the 
device. After looking at the libusbx code I can see that even though I know the 
device address and vid+pid I hit an error as [libusb_open] open 1.2 returns -3. 
(see debug code below). I do not have root access to my phone and I thought 
libusbx did not need this. I have found out that the problem arises because I 
only have drwxr-xr-x access to the folder /dev/bus when I check it throug the 
adb. I can get file descriptors (fd) through both libusbx and the android 
library UsbDeviceConnection.getFileDescriptor() but I cannot figure out where 
to pass the latter fd to libusbx in order to use it to open the device.

Anyway I altered the libusbx code abit in order to figure out what is going on. 
Maybe I first should mention that I am a novice in Android coding incl. java 
and C. I only started looking into it 3 months ago. So the well dokumented 
libusbx code has been a great help and I hope I have not messed the code up. I 
hope somebody is able to give advice as I would relly like to be able to run an 
isochronous device on my phone.

One idea I have is if libusbx really need write access. I found this ex. of the 
android code 
https://android.googlesource.com/platform/system/core/+/master/libusbhost/usbhost.c
 where under the struct usb_device *usb_device_open(const char *dev_name) 
checks for only read access. Maybe that is an solution I just have not figured 
out how to change the libusbx code to fit it.

Thanks in advance,
Rasmus (RBL)

My own code:

jint Java_dk_artris_Flagermus_USBInfo_usbConnect(JNIEnv* env, jobject thiz) {
 jint USB = 0;
 __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- initialising 
libusb --");
 USB = libusb_init(NULL);
 if (USB == 0) {
 __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- libusb 
initialised --");
 } else {
 __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- libusb error: 
%i --", USB);
 }

 //try to find the device to use
 __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- trying to find 
device --");
 libusb_device **list;
 libusb_device *found = NULL;
 ssize_t cnt = libusb_get_device_list(NULL, &list);
 ssize_t i = 0;
 int err = 0;
 if (cnt < 0)
 __android_log_print(ANDROID_LOG_ERROR, "USBInfo_usbConnect", "-- No devices 
found --");

 for (i = 0; i < cnt; i++) {
 libusb_device *device = list[i];
 __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- Device found: 
%p --", device);
 if (device) {
 found = device;
 __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- %p --", 
device);
 //break;
 }
 }

 if (found) {
 libusb_device_handle *handle;

 err = libusb_open(found, &handle);
 if (err == 0)
 __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- Opened device 
(%p) so err should be 0. err is: %i --", found, err);
 else {
 // etc
 __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- Device (%p) 
not opened and err is: %i --", found, err);
 //try to directly open device
 handle = libusb_open_device_with_vid_pid(NULL, 0x0869, 0x0306);
 if (handle != 0) __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", 
"-- Opened device directly and handle is: %d --", handle);
 else __android_log_print(ANDROID_LOG_INFO, "USBInfo_usbConnect", "-- Device 
(%p) not opened and handle: %i --", found, err);
 }
 }

 libusb_free_device_list(list, 1);

 return USB;

}

The altered code:

In linux_uabfs.c:

static int check_usb_vfs(const char *dirname)
{
 DIR *dir;
 struct dirent *entry;
 int found = 0;

 usbi_dbg("Test of check_usb_vfs: %s", dirname); //RBL
 dir = opendir(dirname);
 usbi_dbg("Test of opendir in check_usb_vfs: %s", dirname); //RBL
 if (!dir) {
 usbi_dbg("No dir in check_usb_vfs"); //RBL
 return 0;
 }

 while ((entry = readdir(dir)) != NULL) {
 if (entry->d_name[0] == '.')
 continue;

 /* We assume if we find any files that it must be the right place */
 found = 1;
 break;
 }

 closedir(dir);
 usbi_dbg("Test of found in check_usb_vfs: %i", found); //RBL
 return found;
}

static const char *find_usbfs_path(void)
{
 const char *path = "/dev/bus/usb";
 const char *ret = NULL;

 if (check_usb_vfs(path)) {
 ret = path;
 } else {
 path = "/proc/bus/usb";
 if (check_usb_vfs(path))
 ret = path;
 }

 /* look for /dev/usbdev*.* if the normal places fail */
 if (ret == NULL) {
 usbi_dbg("Ret still = %i in find_usbfs_path", ret); //RBL
 struct dirent *entry;
 DIR *dir;

 path = "/dev";
 dir = opendir(path);
 if (dir != NULL) {
 usbi_dbg("Found /dev in find_usfs_path"); //RBL
 while ((entry = readdir(dir)) != NULL) {
 if (_is_usbdev_entry(entry, NULL, NULL)) {
 /* found one; that's enough */
 ret = path;
 usbi_dbg("Did find /dev/usbdev in find_usbfs_path"); //RBL
 usbdev_names = 1;
 break;
 }
 }
 closedir(dir);
 }
 }

 if (ret != NULL)
 usbi_dbg("found usbfs at %s", ret);

 ret = "/dev/bus/usb"; //RBL
 return ret;
}

In core.c:

libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
 libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
{
 struct libusb_device **devs;
 struct libusb_device *found = NULL;
 struct libusb_device *dev;
 struct libusb_device_handle *handle = NULL;
 size_t i = 0;
 int r;

 if (libusb_get_device_list(ctx, &devs) < 0)
 return NULL;

 while ((dev = devs[i++]) != NULL) {
 struct libusb_device_descriptor desc;
 r = libusb_get_device_descriptor(dev, &desc);
 usbi_dbg("r is: %i", r); //RBL
 if (r < 0)
 goto out;
 if (desc.idVendor == vendor_id && desc.idProduct == product_id) {
 found = dev;
 usbi_dbg("Found dev: %p", dev); //RBL
 break;
 }
 }

 if (found) {
 r = libusb_open(found, &handle);
 usbi_dbg("r is %i after found", r); //RBL
 if (r < 0)
 handle = NULL;
 }

out:
 libusb_free_device_list(devs, 1);
 return handle;
}

And the debug file:

D/kernel ( 140): [252422.790161] msm_otg_resume: usb exited from low power mode
D/kernel ( 140): [252422.841308] bq24185 0-006b: Set safety timer to disable
D/kernel ( 140): [252422.842559] bq24185 0-006b: Enabling boost mode
D/kernel ( 140): [252422.944488] msm_hsusb_host msm_hsusb_host.0: Qualcomm 
On-Chip EHCI Host Controller
D/kernel ( 140): [252422.944671] msm_hsusb_host msm_hsusb_host.0: new USB bus 
registered, assigned bus number 1
D/kernel ( 140): [252422.945526] msm_hsusb_host msm_hsusb_host.0: irq 60, io 
base 0xa3600000
D/kernel ( 140): [252422.945800] usb usb1: New USB device found, idVendor=1d6b, 
idProduct=0002
D/kernel ( 140): [252422.945831] usb usb1: New USB device strings: Mfr=3, 
Product=2, SerialNumber=1
D/kernel ( 140): [252422.945892] usb usb1: Product: Qualcomm On-Chip EHCI Host 
Controller
D/kernel ( 140): [252422.945922] usb usb1: Manufacturer: Linux 2.6.32.9 
http://2.6.32.9&lang=en -perf ehci_hcd
D/kernel ( 140): [252422.945983] usb usb1: SerialNumber: msm_hsusb_host.0
D/kernel ( 140): [252422.961975] usb usb1: configuration #1 chosen from 1 choice
D/kernel ( 140): [252422.970825] hub 1-0:1.0: USB hub found
D/kernel ( 140): [252422.970916] hub 1-0:1.0: 1 port detected
D/kernel ( 140): [252424.387390] usb 1-1: new full speed USB device using 
msm_hsusb_host and address 2
E/kernel ( 140): [252424.549621] usb 1-1: device v0869 p0306 is not supported
D/kernel ( 140): [252424.549713] usb 1-1: New USB device found, idVendor=0869, 
idProduct=0306
D/kernel ( 140): [252424.549743] usb 1-1: New USB device strings: Mfr=1, 
Product=2, SerialNumber=3
D/kernel ( 140): [252424.549804] usb 1-1: Product: UltraMic 250K 16 bit r2
D/kernel ( 140): [252424.549835] usb 1-1: Manufacturer: DODOTRONIC Technology .
D/kernel ( 140): [252424.562133] usb 1-1: configuration #1 chosen from 1 choice
I/ActivityManager( 283): START {flg=0x10000000 
cmp=com.android.systemui/.usb.UsbConfirmActivity (has extras)} from pid 283
I/ActivityManager( 283): Displayed 
com.android.systemui/.usb.UsbConfirmActivity: +87ms
D/dalvikvm( 1495): GC_CONCURRENT freed 399K, 29% free 7836K/11015K, paused 
3ms+3ms
I/ActivityManager( 283): No longer want dk.artris.flagermus (pid 18608): hidden 
#16
I/WindowManager( 283): WIN DEATH: Window{2c0acc30 
dk.artris.flagermus/dk.artris.flagermus.Main paused=false}
D/dalvikvm( 1495): GC_CONCURRENT freed 386K, 29% free 7886K/11015K, paused 
2ms+4ms
I/ActivityManager( 283): START 
{act=android.hardware.usb.action.USB_DEVICE_ATTACHED flg=0x10000000 
cmp=dk.artris.flagermus/.USBInfo (has extras)} from pid 366
D/dalvikvm(19632): Late-enabling CheckJNI
I/ActivityManager( 283): Start proc dk.artris.flagermus for activity 
dk.artris.flagermus/.USBInfo: pid=19632 uid=10058 gids={1015}
D/dalvikvm(19632): Trying to load lib 
/mnt/asec/dk.artris.flagermus-2/lib/libusb1.0.so http://libusb1.0.so&lang=en  
0x2bb90228
D/dalvikvm(19632): Added shared lib 
/mnt/asec/dk.artris.flagermus-2/lib/libusb1.0.so http://libusb1.0.so&lang=en  
0x2bb90228
D/USBInfo (19632): FindClass found: 50C00019
I/USBInfo_usbConnect(19632): -- initialising libusb --
D/libusb (19632): [timestamp] [threadID] facility level [function call] 
<message>
D/libusb (19632): 
--------------------------------------------------------------------------------
D/libusb (19632): [ 0.000000] [00004cb0] libusbx: debug [libusb_init] created 
default context
D/libusb (19632): [ 0.000031] [00004cb0] libusbx: debug [libusb_init] libusbx 
v1.0.17.10830
D/libusb (19632): [ 0.000061] [00004cb0] libusbx: debug [check_usb_vfs] Test of 
check_usb_vfs: /dev/bus/usb
D/libusb (19632): [ 0.000122] [00004cb0] libusbx: debug [check_usb_vfs] Test of 
opendir in check_usb_vfs: /dev/bus/usb
D/libusb (19632): [ 0.000153] [00004cb0] libusbx: debug [check_usb_vfs] No dir 
in check_usb_vfs
D/libusb (19632): [ 0.000153] [00004cb0] libusbx: debug [check_usb_vfs] Test of 
check_usb_vfs: /proc/bus/usb
D/libusb (19632): [ 0.000214] [00004cb0] libusbx: debug [check_usb_vfs] Test of 
opendir in check_usb_vfs: /proc/bus/usb
D/libusb (19632): [ 0.000214] [00004cb0] libusbx: debug [check_usb_vfs] No dir 
in check_usb_vfs
D/libusb (19632): [ 0.000244] [00004cb0] libusbx: debug [find_usbfs_path] Ret 
still = 0 in find_usbfs_path
D/libusb (19632): [ 0.000244] [00004cb0] libusbx: debug [find_usbfs_path] Found 
/dev in find_usfs_path
D/libusb (19632): [ 0.000671] [00004cb0] libusbx: debug [op_init] bulk 
continuation flag supported
D/libusb (19632): [ 0.000702] [00004cb0] libusbx: debug [op_init] zero length 
packet flag supported
D/libusb (19632): [ 0.000763] [00004cb0] libusbx: debug [op_init] sysfs can 
relate devices
D/libusb (19632): [ 0.000763] [00004cb0] libusbx: debug [op_init] sysfs has 
complete descriptors
D/libusb (19632): [ 0.001007] [00004cb0] libusbx: debug 
[linux_get_device_address] getting address for device: usb1 detached: 0
D/libusb (19632): [ 0.001007] [00004cb0] libusbx: debug 
[linux_get_device_address] scan usb1
D/libusb (19632): [ 0.001282] [00004cb0] libusbx: debug 
[linux_get_device_address] bus=1 dev=1
D/libusb (19632): [ 0.001312] [00004cb0] libusbx: debug 
[linux_enumerate_device] busnum 1 devaddr 1 session_id 257
D/libusb (19632): [ 0.001312] [00004cb0] libusbx: debug 
[linux_enumerate_device] allocating new device for 1/1 (session 257)
D/libusb (19632): [ 0.001495] [00004cb0] libusbx: debug 
[linux_get_device_address] getting address for device: 1-1 detached: 0
D/libusb (19632): [ 0.001495] [00004cb0] libusbx: debug 
[linux_get_device_address] scan 1-1
D/libusb (19632): [ 0.001648] [00004cb0] libusbx: debug 
[linux_get_device_address] bus=1 dev=2
D/libusb (19632): [ 0.001679] [00004cb0] libusbx: debug 
[linux_enumerate_device] busnum 1 devaddr 2 session_id 258
D/libusb (19632): [ 0.001679] [00004cb0] libusbx: debug 
[linux_enumerate_device] allocating new device for 1/2 (session 258)
D/libusb (19632): [ 0.001801] [00004cb0] libusbx: debug [linux_get_parent_info] 
Dev 0x2359c0 (1-1) has parent 0x1f9df0 (usb1) port 1
D/libusb (19632): [ 0.001862] [00004cb0] libusbx: debug [usbi_add_pollfd] add 
fd 58 events 1
D/libusb (19632): [ 0.001892] [00004cb0] libusbx: debug [usbi_add_pollfd] add 
fd 60 events 1
I/USBInfo_usbConnect(19632): -- libusb initialised --
I/USBInfo_usbConnect(19632): -- trying to find device --
D/libusb (19632): [ 0.001923] [00004cb0] libusbx: debug [libusb_get_device_list]
I/USBInfo_usbConnect(19632): -- Device found: 0x2359c0 --
I/USBInfo_usbConnect(19632): -- 0x2359c0 --
I/USBInfo_usbConnect(19632): -- Device found: 0x1f9df0 --
I/USBInfo_usbConnect(19632): -- 0x1f9df0 --
D/libusb (19632): [ 0.001953] [00004cb0] libusbx: debug [libusb_open] open 1.1
E/libusb (19632): [ 0.002014] [00004cb0] libusbx: error [_get_usbfs_fd] libusbx 
couldn't open USB device /dev/bus/usb/001/001: Permission denied
E/libusb (19632): [ 0.002014] [00004cb0] libusbx: error [_get_usbfs_fd] libusbx 
requires write access to USB device nodes.
D/libusb (19632): [ 0.002014] [00004cb0] libusbx: debug [libusb_open] open 1.1 
returns -3
I/USBInfo_usbConnect(19632): -- Device (0x1f9df0) not opened and err is: -3 --
D/libusb (19632): [ 0.002045] [00004cb0] libusbx: debug [libusb_get_device_list]
D/libusb (19632): [ 0.002075] [00004cb0] libusbx: debug 
[libusb_get_device_descriptor]
D/libusb (19632): [ 0.002075] [00004cb0] libusbx: debug 
[libusb_open_device_with_vid_pid] r is: 0
D/libusb (19632): [ 0.002075] [00004cb0] libusbx: debug 
[libusb_open_device_with_vid_pid] Found dev: 0x2359c0
D/libusb (19632): [ 0.002106] [00004cb0] libusbx: debug [libusb_open] open 1.2
E/libusb (19632): [ 0.002106] [00004cb0] libusbx: error [_get_usbfs_fd] libusbx 
couldn't open USB device /dev/bus/usb/001/002: Permission denied
E/libusb (19632): [ 0.002136] [00004cb0] libusbx: error [_get_usbfs_fd] libusbx 
requires write access to USB device nodes.
D/libusb (19632): [ 0.002136] [00004cb0] libusbx: debug [libusb_open] open 1.2 
returns -3
D/libusb (19632): [ 0.002136] [00004cb0] libusbx: debug 
[libusb_open_device_with_vid_pid] r is -3 after found
I/USBInfo_usbConnect(19632): -- Device (0x1f9df0) not opened and handle: -3 --
D/TextLayoutCache(19632): Using debug level: 0 - Debug Enabled: 0
D/libEGL (19632): loaded /system/lib/egl/libGLES_android.so 
http://android.so&lang=en 
D/libEGL (19632): loaded /system/lib/egl/libEGL_adreno200.so 
http://adreno200.so&lang=en 
D/libEGL (19632): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
http://adreno200.so&lang=en 
D/libEGL (19632): loaded /system/lib/egl/libGLESv2_adreno200.so 
http://adreno200.so&lang=en 
D/OpenGLRenderer(19632): Enabling debug mode 0
D/dalvikvm( 283): GC_EXPLICIT freed 939K, 19% free 18838K/23111K, paused 
6ms+12ms
W/InputManagerService( 283): Starting input on non-focused client 
com.android.internal.view.IInputMethodClient$Stub$Proxy@2beee728 (uid=10105 
pid=19603)
I/ActivityManager( 283): Displayed dk.artris.flagermus/.USBInfo: +641ms
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to