How to give /dev/bus/usb permissions for libusb android applications?

18,521

Solution 1

I've same problem and I solve it doing this in the ndk, changing /proc/bus/usb for where usb is mounted in your case. Hope it helps.

r = system("su -c \"chmod -R 777 /proc/bus/usb/\"");
if(r!=0) {
    LOGD("Could not grant permissions to USB\n");
    eturn CANT_GRAN_PERMISSION_USB;
}

Solution 2

There are some method from libusb developer team. for my case, I select the first one method.

from:https://github.com/libusb/libusb/blob/master/android/README

Runtime Permissions:

The default system configuration on most Android device will not allow access to USB devices. There are several options for changing this.

If you have control of the system image then you can modify the ueventd.rc used in the image to change the permissions on /dev/bus/usb//. If using this approach then it is advisable to create a new Android permission to protect access to these files. It is not advisable to give all applications read and write permissions to these files.

For rooted devices the code using libusb could be executed as root using the "su" command. An alternative would be to use the "su" command to change the permissions on the appropriate /dev/bus/usb/ files.

Users have reported success in using android.hardware.usb.UsbManager to request permission to use the UsbDevice and then opening the device. The difficulties in this method is that there is no guarantee that it will continue to work in the future Android versions, it requires invoking Java APIs and running code to match each android.hardware.usb.UsbDevice to a libusb_device.

Solution 3

Edit:

After re-reading the question, it is clear that I misunderstood that the host machine was running Android. As udev does not exist in Android, the answer below does not answer the question. I am leaving this answer here in case anyone is looking for information on getting Android devices recognized by a non-Android linux host PC.

End Edit

The default permissions on those drivers (/dev/bus/usb/###/###) are determined by udev. In particular, it parses the configuration files in /etc/udev/rules.d and /lib/udev/rules.d in numerical order to determine the defaults.

To override the defaults, you will need to create your own udev .rules file. To do so, you need the vendor ID of the device you want to grant permission to. lsusb will print out the device id for any particular device, and if you want a list of all vendors, you can try this.

So in your /etc/udev/rules.d/99-android.rules file, fill in the following line for each vendor you want to support:

SUBSYSTEM=="usb", ATTRS{idVendor}=="04e8", MODE:="0666"

Note that in the example linked to above, they use = instead of := and SYSFS instead of ATTRS. I don't know why, but I found that that file didn't work verbatim on my machine.

After writing the file,

restart usbserial 

and re-plug your device. It should now default to 666 permissions.

Solution 4

I found the solution without rooting the device.

Tested on Android 4.2.2 Kernel version 3.3.39

All you need to do is add your application in to the group "usb"

Without root you can use permissions file at /system/etc/permissions/platform.xml

Choose one permission you don't use, for example "android.permission.CAMERA".

Modify /system/etc/permissions/platform.xml like this:

<permissions>
...
    <permission name="android.permission.CAMERA" >
        <group gid="camera" />
    </permission>

    <permission name="android.permission.CAMERA" >
        <group gid="usb" />
    </permission>
...
</permissions>

So, add <uses-permission android:name="android.permission.CAMERA" /> to your application manifest file, and enjoy libusb accessible usb!

Share:
18,521
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    i am developing an Application that uses libusb via jni. this application is currently targeted only to rooted, usb-host Android 3+ machines.

    the scenario is as follows:

    <java Activity>
           loads <jni_wrapper.so>
                   which wraps <my_main_lib.so>
                           that uses <libusb.so>
                                   that needs rw access to: /dev/bus/usb/<device>
    

    all the .so native libraries are part of an infrastructure that i install (as root) on /system/lib, which then can be used by java Activities, run by the simple users.

    that means that the whole usb communication must be done from the native side.

    i got everything to work fine, except for one issue - the usb permissions: the default permissions of /dev/bus/usb/ entries are (0660, uid= root, gid= usb).

    obviously, a standard java process running a native code does not meet any of the above requirements, forcing me to "chmod 666" the corresponding /dev entry, to get this working.

    this solution is not so good, though, because it does not survive reboot, or unplugging/replugging of the device (permissions go back to 0660).

    the solution i'm looking for needs to:

    • survive reboot/replugging
    • be installed ~once~ by the root, and not involve bothering the user every time to gain permissions
    • be generic and run on all (/most) android 3+ machines
    • [not mandatory] gives the minimal credentials necessary

    directions i thought of:

    • changing permissions in /init.rc or /ueventd.rc --> both are overridden on every reboot
    • mounting usbfs /proc/bus/usb with devmode=0666 --> survives replugging, but not reboot
    • making my process join the "usb" group? --> i tried android.permission.ACCESS_USB, but it doesnt work/supported... :/

    any ideas? :-)

    thanks!

  • Hanif Hamdan
    Hanif Hamdan over 12 years
    Why the downvote? Did it not work? Not answer the question? Please leave a comment.
  • NickSoft
    NickSoft about 12 years
    Maybe downvote is because usbserial is for usb to serial converter device and he's talking about using libusb which is lower level. However the udev rules sound the right place for changing rights and ... actually that's udev's purpose. using su to change permissions soulds more like security issue.
  • Chris Stratton
    Chris Stratton about 10 years
    This answer is irrelevant as Android does not utilize udev. It is a very atypical Linux userspace.
  • DrakaSAN
    DrakaSAN almost 10 years
    Choose one permission you don't use => I m not android develloper, but it seems a REAL BAD practice to me...
  • Julian Cerruti
    Julian Cerruti over 9 years
    If you don't have root, how can you modify /system/etc/permissions/platform.xml ?
  • kangear
    kangear about 9 years
    Actually, I think this is a good method, if I change the permission of /dev/bus/usb then any app can read/write it, it is unsafe, for me a system image developer, is a good way.