Rethink the USB connection module in the name of the device call in Linux

How to reassign /dev/ttyUSB* to a specific name to be called by my program.
How to assign /dev/ttyUSB* to a specific device.

Reference: How do I match a ttyUSBX device to a USB serial device
Reference: http://unix.stackexchange.com/questions/81754/how-can-i-match-a-ttyusbx-device-to-a-usb-serial-device
Reference: http://ubuntuforums.org/showthread.php?t=168221
And: http://unix.stackexchange.com/questions/64266/putty-can-access-serial-port-as-dev-ttyusb0-but-not-as-named-udev-device

Quick reference/steps:

Brief Steps:

1. Command: 
   >> lsusb
   This lists your usb devices and "067b:2303",for instance, as its ID
   or:
   >> ls -l /sys/bus/usb-serial/devices
   An other locations worth exploring are /sys/class/tty/

2. Write this to /etc/udev/rules.d/50-usb.rules
  ----
   SUBSYSTEM=="tty", ATTRS{idVendor}=="19d2", ATTRS{idProduct}=="0031", SYMLINK+="ONDA"
   SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="14ac", SYMLINK+="HUAWEI"
  ----
   Reboot.
   Now you can use /dev/ONDA to get to your ONDA device.
   If you want non-root users to be able to use these, then add
  ----
   , MODE="0666"
  ----
   to the end of each line.
 
3. To get enough information to distinguish the devices try something like this for all devices:
   Command: 
   >> 
    $ udevadm info --query all -name /dev/ttyUSB0 --attribute-walk

4. To start using these new rules, you need to run the command udevstart 
   Command:
   >> sudo udevstart
   You can also restart udev using 
   Command:
   >> sudo /etc/init.d/udev restart    #you may need to reboot if this doesn't work.
   
5. Command: 
   >> 

Create your own udev rules to control removable devices

I’m sure many people who use removable devices have noticed that sometimes they don’t appear where they were before.

Plug in your USB drive, use fdisk to find the device node and then install it. USB sometimes appears as /dev/sda1, sometimes /dev/sdb1. It can depend on the order in which you plug in your USB devices, and where you plug them in. This is a real pain if you mount devices manually or if you’re trying to customize /etc/fstab.

udev allows a permanent device node, /dev/…, to be set based on a matching rule defined by your specific hardware. In other words, if a device conforming to a certain criteria is attached, it will be given its own device node, rather than being allocated to a dynamic node.

It is really easy to set up.

To get started, you need to know which dynamic device node is given to the device when it is connected for the first time. The way I can do this is by using aTail order

Code:

tail -f /var/log/messages

When a device is connected, the screen will be refreshed with the latest messages, usually imparting some knowledge about the location of the new device. For example, my USB flash disk produced this output when plugged in.

Apr 30 16:37:01 localhost kernel: [4294885.683000] USB 1-3: New full speed USB device with ohci_hcd and address 6

Apr 30 16:37:01 localhost kernel: [4294885.853000] scsi5: SCSI emulation for USB mass storage devices

Apr 30 16:37:02 localhost usb.agent[10421]: USB Storage: Already loaded

Apr 30 16:37:06 localhost kernel: [4294890.859000] Manufacturer: Model: TS128MJFLASHA Rev: 1.00

Apr 30 16:37:06 localhost kernel: [4294890.859000] Type: Direct Access ANSI SCSI Revision: 02

Apr 30 16:37:06 localhost kernel: [4294890.883000] SCSI device sdd: 253400 hdwr clips 512 bytes (130 MB)

Apr 30 16:37:06 localhost kernel: [4294890.896000] sdd: “write protection” off

Apr 30 16:37:06 localhost kernel: [4294890.924000] SCSI device sdd: 253400 hdwr clips 512 bytes (130 MB)

Apr 30 16:37:06 localhost kernel: [4294890.937000] sdd: “write protection” off

Apr 30 16:37:07 localhost kernel: [4294890.937000] /dev/scsi/host5/bus0/target0/lun0:p1

Apr 30 16:37:07 localhost kernel: [4294891.046000] attached scsi removable disk sdd In scsi5, channel 0, id 0, color 0

Apr 30 16:37:07 localhost scsi.agent[10469]:sd_mod: successfully loaded (to disk)

The output shows that the connected USB device has been set to the device node /dev/sdd. You may also want to specify the partition number using

For my USB flash disk it’s the partition /dev/sdd1.

The next thing is to find out some unique information from the device, the information that will be used in determining the udev base, and remember the match is required to set the permanent node. The next command I used is from the typing rules link at the bottom of the HOWTO

Code:

udevinfo -a -p $(udevinfo -q path -n /dev/sdd)

This command yields a lot of information about the devices it is associated with /dev/sdd. I left Many of info, leaving only the section I think is the most relevant. This section contains more specific information about my USB flash disk.

Note the bold text in the output. It is important that the information used in the udev database is contained in one section.

Code:

udevinfo starts with the device the node belongs to and then walks up the
device chain, to print for every device found, all possibly useful attributes
in the udev key format.
Only attributes within one device section may be used together in one rule,
to match the device for which the node will be created.
  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.0/usb1/1-3':
    BUS=="usb"
    ID=="1-3"
    DRIVER=="usb"
    SYSFS{bConfigurationValue}=="1"
    SYSFS{bDeviceClass}=="00"
    SYSFS{bDeviceProtocol}=="00"
    SYSFS{bDeviceSubClass}=="00"
    SYSFS{bMaxPower}=="100mA"
    SYSFS{bNumConfigurations}=="1"
    SYSFS{bNumInterfaces}==" 1"
    SYSFS{bcdDevice}=="0100"
    SYSFS{bmAttributes}=="80"
    SYSFS{configuration}==""
    SYSFS{devnum}=="6"
    SYSFS{idProduct}=="0005"
    SYSFS{idVendor}=="0c76"
    SYSFS{maxchild}=="0"
    SYSFS{product}=="TS128MJFLASHA"
    SYSFS{speed}=="12"
    SYSFS{version}==" 1.10"

Items containing specific information about the USB disk are colored red. These are the two items that interest me, the device connected toUSB bus, product is identified byTS128MJFLASHA

______

The next step is to create a udev rule regarding that device. I’ll start by creating my rules file

Code:

sudo nano /etc/udev/rules.d/10-local.rules

By naming my set of rules 10- Local rules It should be considered before the other rules specified in this volume.

The base I will be using for the flash disk looks like this.

bus == “USB”And the SYSFS {product} == “TS128MJFLASHA”And the kernel == “sd? 1”And theNAME = “Over 128MB”And theSYMLINK=”usbdevices/transcend128mb”

Quick explanation.

– the bus == “usb” And the SYSFS {product} == “TS128MJFLASHA” The options are the same as the ones you chose fromudevinfo Produce.

– Option kernel=”sd?1″ It will only match locations like /dev/sda1 and /dev/sdb1 and most importantly it will not match nodes like /dev/sda and /dev/sdb, which can be fdisk’ed. The “udev writing rules” manual also mentions this.

– options name = “128FLASH” And the SYMLINK=”usbdisc/128FLASH” It will create the persistent node in /dev/transcend128mb, and symlink /dev/usbdisc/transcend128mb points to the persistent node /dev/transcend128mb. SYMLINK option not required. The reason I included it is because all my USB devices will have a symbolic link starting with /dev/usbdevices/…

I just think it’s neater.

There are other options that can be used to create udev rules, such as GROUP = “some_group”, if you want to set the group ownership of the device node to a specific group, and MODE = “0660”, which gives the owner/group read and write permissions, such aschmod.

The Grammar Handbook has some detailed information about these other options.

______

To start using these new rules, you need to run the udevstart command

You can also restart udev with

Code:

sudo /etc/init.d/udev restart    #you may need to reboot your linux system if this doesn't work.

This should work for newer versions of udev, which don’t seem to use the udevstart command

– thanks for the Ash 211 to indicate this

Now to quickly check the new node creation for my example.

Code:

[email protected]:~$ ls -l /dev/trans*
brw-r-----  1 root plugdev 8, 49 2006-04-30 16:37 /dev/transcend128mb
[email protected]:~$ ls -l /dev/usbdevices/
lrwxrwxrwx  1 root root 17 2006-04-30 16:37 transcend128mb -> ../transcend128mb

______

Finally, fstab can be edited to include the new permanent device node, but first we’ll back it up

Code:

sudo cp /etc/fstab /etc/fstab_backup
sudo nano /etc/fstab

Then we can add an entry for a USB device as an example using the device node or symlink (if used), so in my example I would use the new device node

Code:

/dev/transcend128mb  /media/usb128mb  vfat  iocharset=utf8,umask=000   0   0

or Node symlink, which I prefer because all my USB devices have symlinks in /dev/usbdevices. I think it makes fstab look neater.

Code:

/dev/usbdevices/transcend128mb  /media/usb128mb vfat iocharset=utf8,umask=000  0  0

When the input is entered correctly, you can save the file (Ctrl + O) and exit (Ctrl + X), then mount the device, in this example my USB disk with

Code:

sudo mount /media/usb128mb

or

Once all this is complete, the example USB drive will always appear at /dev/transcend128mb so the entry in fstab will remain unchanged and will always find the device.

I’m all done!

I hope this helps some people, like it did me.

Please let me know if this works for you, and of course if there are any typos, errors, or things that need clarification.

Helpful links I needed to get this working

Kernel.org – udev

Writing udev rules

Last edited by Sutekh; May 27, 2006 in02:02 PM.

Leave a Comment