LinuxQuestions.org

LinuxQuestions.org (http://www.linuxquestions.org/questions/index.php)
-   Linux - Hardware (http://www.linuxquestions.org/questions/forumdisplay.php?f=18)
-   -   force usb to use same dev id using udev rule (http://www.linuxquestions.org/questions/showthread.php?t=4175434928)

srikanth007m 10-31-2012 11:56 AM

force usb to use same dev id using udev rule
 
I need some help regarding the usb devices(Android based smartphones), more over i am new to linux. After googling a lot i heard there is trick using udev rule. Does this udev rule solved my problem?

how to create a rule for my below problem. Any help would really helpful..

I am testing some android based mobiles in linux machine. The automation script uses the device id under "/dev/bus/usb/001/"053" it will be always under bus 001 only.. But the dev id will be random like if i insert one mobile then the dev id will be 053, if remove and insert it again then the dev id will be 054..

The problem is, when some tests runs on the device and if device gets rebooted then new dev id is showing for the rebooted one and my scripts failing due to new dev id..

Is there any way to force usb devices to use same dev id instead of new one. So that there will be no issues to my tests even after device reboots.

Thanks in advance.

fbianconi 10-31-2012 06:37 PM

Quote:

Is there any way to force usb devices to use same dev id instead of new one
No, the name of a device node can not be changed by udev, only additional symlinks can be created.

Here is the documentation for writing udev rules, and here and here are OLD guides to all of this.

What you can do is something like this:
1) Make a new file '/etc/udev/rules.d/40-myphone.rules' with the content:
Code:

SUBSYSTEMS=="usb", ATTRS{busnum}=="1",ACTION=="add",SYMLINK+="myphone"
This should make a symbolic link to the device when plugged under /dev/myphone

2) Try unplugging and pugging the phone and check if it worked.
3) If it did then change the automation script to point to "/dev/myphone".

For this case both SUBSYSTEMS=="usb" and ATTRS{busnum}=="1" means something like "/dev/bus/usb/001/*" (except it only matches the highest link_priority one). The bus number will change whenever you switch ports. You can try making a rule using other attribute, just use "udevadm monitor" to get udev events printed to the terminal as they happen, and then use "udevadm info -a -p <device>" for a device name udevadm just spout. ATTRS{idVendor} and ATTRS{idProduct} are very commonly used to match a single device, but a phone might act as many devices depending on the mode it is connected: eg. modem, storage device, flash mode, etc. all of this usually have different idProduct attributes.

Good luck and try not to write over a raw disk partition.

jschiwal 10-31-2012 07:06 PM

Using a different ID type for the filesystem may work.
Select one that is constant such as the UUID of the filesystem.

You may not even need to use udev rules. The KDE devices applet may be enough. The same may be true for Gnome. Also if you use the UUID of the filesystem, it can be used in an fstab entry. The user, uid, gid, noauto, fmask and dmask options will allow you to fix the mount location, ownership and permissions.

On some distros the udev rules have been migrated to polkit and systemd targets. A user ACL is used to grant permission rather than changing the group ownership.

I'd suggest researching how your disto and desktop handle devices now before jumping in and creating udev rules. There may be an easier way that doesn't fight with the system.

srikanth007m 10-31-2012 11:11 PM

@fbianconi - Thank you for the detailed info and it'a valuable suggestion for me as a biggner in linux. I'll make a try and let you know the output.. Here i am connecting 14 devices to a linux box. Do i need to append 14 entries to the same rule with corresponding values?

@jschiwal - I am totally new to linux environment. I don't know much about the types of destros and Gnome/kde etc..

srikanth007m 10-31-2012 11:12 PM

Quote:

Originally Posted by fbianconi (Post 4819287)
No, the name of a device node can not be changed by udev, only additional symlinks can be created.

Here is the documentation for writing udev rules, and here and here are OLD guides to all of this.

What you can do is something like this:
1) Make a new file '/etc/udev/rules.d/40-myphone.rules' with the content:
Code:

SUBSYSTEMS=="usb", ATTRS{busnum}=="1",ACTION=="add",SYMLINK+="myphone"
This should make a symbolic link to the device when plugged under /dev/myphone

2) Try unplugging and pugging the phone and check if it worked.
3) If it did then change the automation script to point to "/dev/myphone".

For this case both SUBSYSTEMS=="usb" and ATTRS{busnum}=="1" means something like "/dev/bus/usb/001/*" (except it only matches the highest link_priority one). The bus number will change whenever you switch ports. You can try making a rule using other attribute, just use "udevadm monitor" to get udev events printed to the terminal as they happen, and then use "udevadm info -a -p <device>" for a device name udevadm just spout. ATTRS{idVendor} and ATTRS{idProduct} are very commonly used to match a single device, but a phone might act as many devices depending on the mode it is connected: eg. modem, storage device, flash mode, etc. all of this usually have different idProduct attributes.

Good luck and try not to write over a raw disk partition.

Thank you for the detailed info and it'a valuable suggestion for me as a biggner in linux. I'll make a try and let you know the output.. Here i am connecting 14 devices to a linux box. Do i need to append 14 entries to the same rule with corresponding values?

srikanth007m 11-01-2012 08:57 AM

fbianconi - When i used the below rule. It was created lots of myphone1/2/3/4/5 etc.. but i want to refer only to /dev/bus/usb/001/XXX.. the symlink is also not point to /dev/bus/usb/001/XXX

SUBSYSTEMS=="usb", ATTRS{busnum}=="1",ACTION=="add",SYMLINK+="myphone%n"

fbianconi 11-02-2012 01:26 AM

Making udev rules is one of the things that you never should have to do, unless you're dealing with experimental hardware and/or software. I don't think we can help you further without you explaining what are you trying to do. Can we see the script you are using?

Quote:

Do i need to append 14 entries to the same rule with corresponding values?
That depends on what you're doing and the rules you write, eg. if you are trying to connect more than one device at the time you might want to look at PROGRAM and %result.
Quote:

When i used the below rule. It was created lots of myphone1/2/3/4/5 etc.. but i want to refer only to /dev/bus/usb/001/XXX..
The numbering should be expected, since you added %n to the symlink string.
Quote:

the symlink is also not point to /dev/bus/usb/001/XXX
Where does it points to?


All times are GMT -5. The time now is 12:47 AM.