Monday, October 25, 2010

Creating permanent link to Your USB physical device in ubuntu

Whenever you plug in your USB device to the system it has been assigned a node in /dev directory e.g. /dev/ttyUSB1, /dev/sda1 etc.. For me problem came when i tried to use a evdo to connect to Internet. whenever i insert the evdo it creates three nodes in /dev directory with name ttyUSB"number". Now this "number" is dynamic. Dynamic means it can vary from time to time when you plug and un-plug your device. So you have to always take care of this thing that which node is actually for evdo. So what is the solution?? one solution is to use wvdial which automatically recognizes your device and starts the connection but here i'm looking for some other idea which will just create a dedicated node for my device.

The hero is UDEV!!!


I dont know much about UDEv but there are so many sites which gives a good description on udev and its use. I'm mentioning some of them at the end of this post. In udev we can create some rules that can define our devices name, links, ownership, group etc.. Here, i'll tell how to create a dedicated node for our device. rules.d The folder where we are going to put our rules is /etc/udev/rules.d/ . Here, u'll find many rules created for other devices. One more thing that u'll notice here is files starting with numbers. These numbers indicate the priority. So, what we'll do, we chose a name such that it should be executed at the end of all the rules execution such that our rules will be the final. So, we'll give name starting with alphabets with extension ".rules". e.g usb-names.rules For writing the rules, the basic idea is to first locate your device and then give any name or apply any setting. So how to locate our device?? I have a EVDO which gets registered to /dev/ttyUSB0,1 and 2. It creates three interfaces out of which only the first interface connects to Internet.

Locating our device:

udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)
Output of this command gives a hierarchal description of the device. This output is even good for them who have interest in device drivers. Output for this command for my device is:

Udevadm info starts with the device specified by the devpath and then walks up the chain of parent devices. It prints for every device found, all possible attributes in the udev rules key format. A rule to match, can be composed by the attributes of the device and the attributes from one single parent device.

looking at device '/devices/pci0000:00/0000:00:1a.1/usb4/4-2/4-2.4/4-2.4:1.0/ttyUSB0/tty/ttyUSB0':

KERNEL=="ttyUSB0"

SUBSYSTEM=="tty"

DRIVER==""


#Parent node of above device

looking at parent device '/devices/pci0000:00/0000:00:1a.1/usb4/4-2/4-2.4/4-2.4:1.0/ttyUSB0': KERNELS=="ttyUSB0"
SUBSYSTEMS=="usb-serial"
DRIVERS=="option1"
ATTRS{port_number}=="0"

#Parent of above device

looking at parent device '/devices/pci0000:00/0000:00:1a.1/usb4/4-2/4-2.4/4-2.4:1.0':

KERNELS=="4-2.4:1.0"

SUBSYSTEMS=="usb"
DRIVERS=="option"
ATTRS{bInterfaceNumber}=="00"

ATTRS{bAlternateSetting}==" 0"

ATTRS{bNumEndpoints}=="03"
ATTRS{bInterfaceClass}=="ff"
ATTRS{bInterfaceSubClass}=="ff"
ATTRS{bInterfaceProtocol}=="ff" ATTRS{modalias}=="usb:v12D1p140Bd0000dc00dsc00dp00icFFiscFFipFF"
ATTRS{supports_autosuspend}=="0"
ATTRS{interface}=="Data Interface"

looking at parent device '/devices/pci0000:00/0000:00:1a.1/usb4/4-2/4-2.4':
KERNELS=="4-2.4" SUBSYSTEMS=="usb"
DRIVERS=="usb"

ATTRS{configuration}==""
ATTRS{bNumInterfaces}==" 4"
ATTRS{bConfigurationValue}=="1"
ATTRS{bmAttributes}=="a0"

ATTRS{bMaxPower}=="500mA"

ATTRS{urbnum}=="7479"
ATTRS{idVendor}=="12d1"
ATTRS{idProduct}=="140b"
ATTRS{bcdDevice}=="0000"
ATTRS{bDeviceClass}=="00"

ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="00"

ATTRS{bNumConfigurations}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{speed}=="12"
ATTRS{busnum}=="4"

ATTRS{devnum}=="7"

ATTRS{version}==" 1.10"
ATTRS{maxchild}=="0"

ATTRS{quirks}=="0x0"

ATTRS{authorized}=="1"
ATTRS{product}=="HUAWEI Mobile"


Here, you can see the desription about your device. To distinguish my EVDO device, information that we can take is converted to bold letters in above outout. So, as we know the things that we can use to discriminate our device, it's time to write our rule

Writing rules:

create a file with name of your choice in /etc/udev/rules.d/ with .rules extension. I chose usb-names.rules .
gedit usb-names.rules

DRIVERS=="option", SUBSYSTEMS=="usb", ATTRS{bInterfaceNumber}=="00", SYSYFS{../idVendor}=="12d1", SYSFS{../idProduct}=="140b", NAME="INTERNET"

save your file and exit.
Now, to implement this rule, use following command

udevadm trigger
See your device in /dev directory u'll find that instead of /dev/ttyUSB0 your device is connected with name /dev/INTERNET. I've only changed the name of first interface out of three interfaces that is created by my EVDO. because it's only used to connect to internet. If you device only creates single node you can safely remove ATTRS{bInterfaceNumber} parameter.
To add more attributes you can directly copy paste the ATTRS from your device description. Like i can direcly take ATTRS{product}=="HUAWEI Mobile" description to match my device.
Even, if just want to create link to your device you can write like this

DRIVERS=="option", SUBSYSTEMS=="usb", ATTRS{bInterfaceNumber}=="00", SYSYFS{../idVendor}=="12d1", SYSFS{../idProduct}=="140b", SYMLINK+="INTERNET"

This will create link for your device.

This much for this post. I think this post will help you guys. If u get any query feel free to write.

References:-
1. http://fixunix.com/suse/262361-dev-ttyusb-number-physical-usb-connector-configured.html
2. http://fixunix.com/ubuntu/494414-serial-ports-ttyusb-0-9-a.html

No comments:

Post a Comment