# [SOLVED] custom udev rules

## Vieri

Hi,

I'm having trouble undertsanding how udev works and accessing a USB device with a custom name/path.

I have a USB to RS-232 adapter. When I plug it in for the first time, I get /dev/ttyUSB0 and can access the device just fine (it's a GSM gateway for sending/receiving SMS with Gnokii).

I want to avoid using ttyUSB0, ttyUSB1, etc. so I created a custom udev rule for my device so that it can be named ttyUSB-GSM-1 no matter how many other USB devices I connect to the system.

In my udev rules I tried both NAME=... and SYMLINK+=...

Using Gnokii on either ttyUSB0 or ttyUSB-GSM-1 works fine.

A problem occurs when I decide to unplug the USB cable and put it back in (even if it's the only usb device connected to the system).

If I use NAME=... then after replugging the cable I get the "GNOKII ERROR" reported below. Since I don't have any /dev/ttyUSB0 then I'm forced to reboot (or maybe restarting udevd should be sufficient).

If I use SYMLINK+=... then after replugging the cable I get the "GNOKII ERROR" reported below if I tell Gnokii to use /dev/ttyUSB-GSM-1. However, if I tell Gnokii to use /dev/ttyUSB0 then it works fine.

Have I done something wrong with udev or could it be a gnokii-specific issue?

Please note below (in DMESG) that the usb connection was at address 2 and when re-connected changed to address 3...

```

UDEV RULE:

# cat /etc/udev/rules.d/01-custom.rules

# udev custom rules:

# SUBSYSTEMS=="usb", ATTRS{product}=="usb serial converter", ATTRS{manufacturer}=="FTDI", ATTRS{serial}=="FTCTWS1T", NAME="ttyUSB-GSM-1"

SUBSYSTEMS=="usb", ATTRS{product}=="usb serial converter", ATTRS{manufacturer}=="FTDI", ATTRS{serial}=="FTCTWS1T", SYMLINK+="ttyUSB-GSM-1"

DMESG:

usb 2-2: USB disconnect, address 2

ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0

ftdi_sio 2-2:1.0: device disconnected

usb 2-2: new full speed USB device using uhci_hcd and address 3

usb 2-2: configuration #1 chosen from 1 choice

ftdi_sio 2-2:1.0: FTDI USB Serial Device converter detected

drivers/usb/serial/ftdi_sio.c: Detected FT232BM

usb 2-2: FTDI USB Serial Device converter now attached to ttyUSB0

GNOKII ERROR:

Gnokii serial_open: tcgetattr: Inappropriate ioctl for device

Couldn't open ATBUS device: Inappropriate ioctl for device

```

Last edited by Vieri on Mon Sep 07, 2009 11:28 am; edited 1 time in total

----------

## Bones McCracker

http://www.reactivated.net/writing_udev_rules.html

----------

## Vieri

Thanks but do you see anything wrong with my udev rule?

Please note that I am using a USB to RS.232 adapter (virtual serial port) so there may be somethign tricky to it.

EDIT: normally the "Inappropriate ioctl" error message shows up when trying to open a serial device when it's not.

----------

## Vieri

I finally got the rule right (at least it seems to work fine now; the only thing I was missing was KERNEL=="ttyUSB*"):

```

SUBSYSTEMS=="usb", ATTRS{product}=="usb serial converter", ATTRS{manufacturer}=="FTDI", ATTRS{serial}=="FTCTWS1T", KERNEL=="ttyUSB*", SYMLINK+="ttyUSB-GSM-630725830", OWNER="root", GROUP="uucp", MODE="0660"

```

This is fine for a 1-USB to 1-RS232 adapter. However, how can I discriminate virtual serial ports in the case of a 1-USB to 4-RS232 adapter?

udevinfo is as follows:

```

# udevadm info -a -p /sys/class/usb_device/usbdev1.2

  looking at device '/class/usb_device/usbdev1.2':

    KERNEL=="usbdev1.2"

    SUBSYSTEM=="usb_device"

    DRIVER==""

  looking at parent device '/devices/pci0000:00/0000:00:02.1/usb1/1-4':

    KERNELS=="1-4"

    SUBSYSTEMS=="usb"

    DRIVERS=="usb"

    ATTRS{configuration}==""

    ATTRS{bNumInterfaces}==" 1"

    ATTRS{bConfigurationValue}=="1"

    ATTRS{bmAttributes}=="a0"

    ATTRS{bMaxPower}=="100mA"

    ATTRS{urbnum}=="22508"

    ATTRS{idVendor}=="0557"

    ATTRS{idProduct}=="2011"

    ATTRS{bcdDevice}=="0001"

    ATTRS{bDeviceClass}=="ff"

    ATTRS{bDeviceSubClass}=="00"

    ATTRS{bDeviceProtocol}=="ff"

    ATTRS{bNumConfigurations}=="1"

    ATTRS{bMaxPacketSize0}=="64"

    ATTRS{speed}=="480"

    ATTRS{busnum}=="1"

    ATTRS{devnum}=="2"

    ATTRS{version}==" 2.00"

    ATTRS{maxchild}=="0"

    ATTRS{quirks}=="0x0"

    ATTRS{authorized}=="1"

    ATTRS{manufacturer}=="ATEN                    "

    ATTRS{product}=="ATEN USB Controller  "

    ATTRS{serial}=="ATE41165"

  looking at parent device '/devices/pci0000:00/0000:00:02.1/usb1':

    KERNELS=="usb1"

    SUBSYSTEMS=="usb"

    DRIVERS=="usb"

    ATTRS{configuration}==""

    ATTRS{bNumInterfaces}==" 1"

    ATTRS{bConfigurationValue}=="1"

    ATTRS{bmAttributes}=="e0"

    ATTRS{bMaxPower}=="  0mA"

    ATTRS{urbnum}=="66"

    ATTRS{idVendor}=="1d6b"

    ATTRS{idProduct}=="0002"

    ATTRS{bcdDevice}=="0206"

    ATTRS{bDeviceClass}=="09"

    ATTRS{bDeviceSubClass}=="00"

    ATTRS{bDeviceProtocol}=="00"

    ATTRS{bNumConfigurations}=="1"

    ATTRS{bMaxPacketSize0}=="64"

    ATTRS{speed}=="480"

    ATTRS{busnum}=="1"

    ATTRS{devnum}=="1"

    ATTRS{version}==" 2.00"

    ATTRS{maxchild}=="10"

    ATTRS{quirks}=="0x0"

    ATTRS{authorized}=="1"

    ATTRS{manufacturer}=="Linux 2.6.30-gentoo-r4 ehci_hcd"

    ATTRS{product}=="EHCI Host Controller"

    ATTRS{serial}=="0000:00:02.1"

    ATTRS{authorized_default}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:02.1':

    KERNELS=="0000:00:02.1"

    SUBSYSTEMS=="pci"

    DRIVERS=="ehci_hcd"

    ATTRS{vendor}=="0x10de"

    ATTRS{device}=="0x036d"

    ATTRS{subsystem_vendor}=="0x1043"

    ATTRS{subsystem_device}=="0x8239"

    ATTRS{class}=="0x0c0320"

    ATTRS{irq}=="20"

    ATTRS{local_cpus}=="ff"

    ATTRS{local_cpulist}=="0-7"

    ATTRS{modalias}=="pci:v000010DEd0000036Dsv00001043sd00008239bc0Csc03i20"

    ATTRS{enable}=="1"

    ATTRS{broken_parity_status}=="0"

    ATTRS{msi_bus}==""

  looking at parent device '/devices/pci0000:00':

    KERNELS=="pci0000:00"

    SUBSYSTEMS==""

    DRIVERS==""

```

----------

## Vieri

I solved it with udev events and custom RUN scripts.

----------

