LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Hardware (https://www.linuxquestions.org/questions/linux-hardware-18/)
-   -   problem reading usb serial device ! (https://www.linuxquestions.org/questions/linux-hardware-18/problem-reading-usb-serial-device-746013/)

unikz 08-08-2009 06:24 AM

problem reading usb serial device !
 
hi everybody,

i wonder if you could help me with the problem i have with a usb serial device (ttyUSB). i have a newly designed usb token that looks like a flash memory(it is used for security issues like digitally sign or something like that).

it works when we try it under windows :
i mean for testing it, i open the device and then write 3 bytes on it ('0', '1', '2') and it should give you back 5 bytes that can be read from the device ( 'a', 'm', 'n' , ...). it can be done under windows as my colleages did.
but in linux it doesn't : i mean i can write 3 bytes but there would be nothing in the device too read after that.

i don't know what is the problem, maybe the driver is not appropriate for this device, i use usbserial generic driver ( usbserial.ko module) and and attach the device by this command to "/dev" :

(suppose i am root, and my system is ubuntu 9.4)
modprobe usbserial vendor=0xe854 product=0x1138 debug=1

and after this ttyUSB0 would appear by command "ls /dev" :
.
.
.
crw-rw---- 1 root dialout 188, 0 2009-08-08 12:14 ttyUSB0
.
.
.

and "dmesg" tells me after modprobe :

Code:

[  111.628725] usbcore: registered new interface driver usbserial
[  111.628734] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: Had to override the open usb serial operation with the generic one.
[  111.628742] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: Had to override the write usb serial operation with the generic one.
[  111.628749] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: Had to override the close usb serial operation with the generic one.
[  111.628756] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: Had to override the write_room usb serial operation with the generic one.
[  111.628763] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: Had to override the chars_in_buffer usb serial operation with the generic one.
[  111.628771] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: Had to override the read_bulk_callback usb serial operation with the generic one.
[  111.628778] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: Had to override the write_bulk_callback usb serial operation with the generic one.
[  111.628823] USB Serial support registered for generic
[  111.628883] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: static descriptor matches
[  111.628891] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: found interrupt in on endpoint 0
[  111.628898] usbserial_generic 6-1:1.0: Generic device with no bulk out, not allowed.
[  111.628910] usbserial_generic: probe of 6-1:1.0 failed with error -5
[  111.628923] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: static descriptor matches
[  111.628930] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: found bulk out on endpoint 0
[  111.628937] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: found bulk in on endpoint 1
[  111.628943] usbserial_generic 6-1:1.1: generic converter detected
[  111.628949] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: usb_serial_probe - setting up 1 port structures for this device
[  111.628959] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: get_free_serial 1
[  111.628965] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: get_free_serial - minor base = 0
[  111.628972] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: usb_serial_probe - registering ttyUSB0
[  111.629101] usb 6-1: generic converter now attached to ttyUSB0
[  111.629136] usbcore: registered new interface driver usbserial_generic
[  111.629142] usbserial: USB Serial Driver core



so then i go and test the devce by "test.c" :

Code:

#include<stdio.h>
#include<errno.h>
#include<termios.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>

int tty_set(int tty)
{
        struct termios tty_opts;
        int foo;

        foo=tcgetattr(tty,&tty_opts);
        if(foo<0) return -1;

        foo=cfsetispeed(&tty_opts,B9600);
        if(foo<0) return -2;

        foo=cfsetospeed(&tty_opts,B9600);
        if(foo<0) return -3;

        //3
        // 8N1
        tty_opts.c_cflag &= ~PARENB;
        tty_opts.c_cflag &= ~CSTOPB;
        tty_opts.c_cflag &= ~CSIZE;
        tty_opts.c_cflag |= CS8;
        // no flow control
        tty_opts.c_cflag &= ~CRTSCTS;

        tty_opts.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
        tty_opts.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl

        tty_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
        tty_opts.c_oflag &= ~OPOST; // make raw

        // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
        tty_opts.c_cc[VMIN]  = 0;
        tty_opts.c_cc[VTIME] = 0;
        //end3

        foo=tcsetattr(tty,TCSANOW, &tty_opts);
        if(foo<0) return -4;

        return 0;
}

int tty_test_connection(int tty)
{
        int foo;
        char buf[5]={0,1,2,0,0};

        foo=write(tty,buf,3);
        if(foo<0){
                perror("write");
                return foo;
        }
        if(foo==0){
                puts("nothing written");
                return foo;
        }
L0:
        foo=read(tty,buf,3); //mc should return 2 bytes p and \0
        if(foo<0){
                perror("read");
                return foo;
        }
        if(foo<2){
                puts("less then 2 bytes was red");
                return foo;
        }

        buf[foo]='\0';
        printf("ping was: %s\n",buf);
        return foo;
}

int main(int ac,char *av[])
{
        int tty_dev;
        int foo;

        tty_dev=0;
        tty_dev = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
        if(tty_dev < 0){
                perror("open");
                return -1;
        }
        puts("open: ok");

        foo=tty_set(tty_dev);
        if(foo<0){
                perror("tty_set");
                close(tty_dev);
                return -1;
        }
        puts("tty_set: ok");


        foo=tty_test_connection(tty_dev);
        if(foo<0){
                perror("tty_test_connection");
                printf("foo: %d\n",foo);
                return -1;
        }
        printf("tty_test_connection: %d characters red\n",foo);
        close(tty_dev);
        return 0;
}

it Prints:

open: ok
tty_set: ok
less then 2 bytes was red
tty_test_connection: 0 characters red

and after i run my test (test.c ) , dmesg prints this:

Code:

[  444.418593] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: serial_open
[  444.418602] /build/buildd/linux-2.6.28/drivers/usb/serial/generic.c: usb_serial_generic_open - port 0
[  444.418675] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: serial_ioctl - port 0, cmd 0x5401
[  444.418688] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: serial_ioctl - port 0, cmd 0x5401
[  444.418694] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: serial_ioctl - port 0, cmd 0x5402
[  444.418700] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: serial_set_termios - port 0
[  444.418706] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: serial_ioctl - port 0, cmd 0x5401
[  444.418720] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: serial_write - port 0, 3 byte(s)
[  444.418724] /build/buildd/linux-2.6.28/drivers/usb/serial/generic.c: usb_serial_generic_write - port 0
[  444.418729] generic ttyUSB0: usb_serial_generic_write - length = 3, data = 00 01 02
[  444.418915] /build/buildd/linux-2.6.28/drivers/usb/serial/usb-serial.c: serial_close - port 0
[  444.418920] /build/buildd/linux-2.6.28/drivers/usb/serial/generic.c: usb_serial_generic_close - port 0
[  444.418924] /build/buildd/linux-2.6.28/drivers/usb/serial/generic.c: generic_cleanup - port 0
[  444.420191] /build/buildd/linux-2.6.28/drivers/usb/serial/generic.c: usb_serial_generic_write_bulk_callback - port 0
[  444.420197] /build/buildd/linux-2.6.28/drivers/usb/serial/generic.c: usb_serial_generic_write_bulk_callback - nonzero write bulk status received: -2
[  444.421191] /build/buildd/linux-2.6.28/drivers/usb/serial/generic.c: usb_serial_generic_read_bulk_callback - port 0
[  444.421199] /build/buildd/linux-2.6.28/drivers/usb/serial/generic.c: usb_serial_generic_read_bulk_callback - nonzero read bulk status received: -2


it seems that the device will be closed before reading the data from device.



u may want to see lsusb , (the one whit ID e854:1138 is my device):

Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 007 Device 004: ID 044e:3012 Alps Electric Co., Ltd
Bus 007 Device 003: ID 044e:3013 Alps Electric Co., Ltd
Bus 007 Device 002: ID 044e:3011 Alps Electric Co., Ltd
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 002: ID e854:1138
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 002: ID 05ca:183b Ricoh Co., Ltd
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub


and u may want to see lshal (the part related to the device):

Code:

udi = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial'
  info.linux.driver = 'usb'  (string)
  info.parent = '/org/freedesktop/Hal/devices/usb_device_1d6b_1_0000_00_1d_1'  (string)
  info.product = 'Unknown (0x1138)'  (string)
  info.subsystem = 'usb_device'  (string)
  info.udi = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial'  (string)
  info.vendor = 'Unknown (0xe854)'  (string)
  linux.device_file = '/dev/bus/usb/006/002'  (string)
  linux.hotplug_type = 2  (0x2)  (int)
  linux.subsystem = 'usb'  (string)
  linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1d.1/usb6/6-1'  (string)
  usb_device.bus_number = 6  (0x6)  (int)
  usb_device.can_wake_up = false  (bool)
  usb_device.configuration_value = 1  (0x1)  (int)
  usb_device.device_class = 2  (0x2)  (int)
  usb_device.device_protocol = 0  (0x0)  (int)
  usb_device.device_revision_bcd = 272  (0x110)  (int)
  usb_device.device_subclass = 0  (0x0)  (int)
  usb_device.is_self_powered = true  (bool)
  usb_device.linux.device_number = 2  (0x2)  (int)
  usb_device.linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1d.1/usb6/6-1'  (string)
  usb_device.max_power = 0  (0x0)  (int)
  usb_device.num_configurations = 1  (0x1)  (int)
  usb_device.num_interfaces = 2  (0x2)  (int)
  usb_device.num_ports = 0  (0x0)  (int)
  usb_device.product_id = 4408  (0x1138)  (int)
  usb_device.speed = 12.0 (12) (double)
  usb_device.vendor_id = 59476  (0xe854)  (int)
  usb_device.version = 1.1 (1.1) (double)

udi = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial_if1'
  info.linux.driver = 'usbserial_generic'  (string)
  info.parent = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial'  (string)
  info.product = 'USB Data Interface'  (string)
  info.subsystem = 'usb'  (string)
  info.udi = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial_if1'  (string)
  linux.hotplug_type = 2  (0x2)  (int)
  linux.subsystem = 'usb'  (string)
  linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1d.1/usb6/6-1/6-1:1.1'  (string)
  usb.bus_number = 6  (0x6)  (int)
  usb.can_wake_up = false  (bool)
  usb.configuration_value = 1  (0x1)  (int)
  usb.device_class = 2  (0x2)  (int)
  usb.device_protocol = 0  (0x0)  (int)
  usb.device_revision_bcd = 272  (0x110)  (int)
  usb.device_subclass = 0  (0x0)  (int)
  usb.interface.class = 10  (0xa)  (int)
  usb.interface.number = 1  (0x1)  (int)
  usb.interface.protocol = 0  (0x0)  (int)
  usb.interface.subclass = 0  (0x0)  (int)
  usb.is_self_powered = true  (bool)
  usb.linux.device_number = 2  (0x2)  (int)
  usb.linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1d.1/usb6/6-1/6-1:1.1'  (string)
  usb.max_power = 0  (0x0)  (int)
  usb.num_configurations = 1  (0x1)  (int)
  usb.num_interfaces = 2  (0x2)  (int)
  usb.num_ports = 0  (0x0)  (int)
  usb.product = 'USB Data Interface'  (string)
  usb.product_id = 4408  (0x1138)  (int)
  usb.speed = 12.0 (12) (double)
  usb.vendor_id = 59476  (0xe854)  (int)
  usb.version = 1.1 (1.1) (double)

udi = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial_if1_serial_usb_0'
  access_control.file = '/dev/ttyUSB0'  (string)
  access_control.type = 'modem'  (string)
  info.callouts.add = {'hal-acl-tool --add-device'} (string list)
  info.callouts.remove = {'hal-acl-tool --remove-device'} (string list)
  info.capabilities = {'serial', 'access_control'} (string list)
  info.category = 'serial'  (string)
  info.parent = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial_if1'  (string)
  info.product = 'Unknown (0x1138)'  (string)
  info.subsystem = 'tty'  (string)
  info.udi = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial_if1_serial_usb_0'  (string)
  linux.device_file = '/dev/ttyUSB0'  (string)
  linux.hotplug_type = 2  (0x2)  (int)
  linux.subsystem = 'tty'  (string)
  linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1d.1/usb6/6-1/6-1:1.1/ttyUSB0/tty/ttyUSB0'  (string)
  serial.device = '/dev/ttyUSB0'  (string)
  serial.originating_device = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial_if1'  (string)
  serial.port = 0  (0x0)  (int)
  serial.type = 'usb'  (string)

udi = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial_if0'
  info.parent = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial'  (string)
  info.product = 'USB Communications Interface'  (string)
  info.subsystem = 'usb'  (string)
  info.udi = '/org/freedesktop/Hal/devices/usb_device_e854_1138_noserial_if0'  (string)
  linux.hotplug_type = 2  (0x2)  (int)
  linux.subsystem = 'usb'  (string)
  linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1d.1/usb6/6-1/6-1:1.0'  (string)
  usb.bus_number = 6  (0x6)  (int)
  usb.can_wake_up = false  (bool)
  usb.configuration_value = 1  (0x1)  (int)
  usb.device_class = 2  (0x2)  (int)
  usb.device_protocol = 0  (0x0)  (int)
  usb.device_revision_bcd = 272  (0x110)  (int)
  usb.device_subclass = 0  (0x0)  (int)
  usb.interface.class = 2  (0x2)  (int)
  usb.interface.number = 0  (0x0)  (int)
  usb.interface.protocol = 0  (0x0)  (int)
  usb.interface.subclass = 2  (0x2)  (int)
  usb.is_self_powered = true  (bool)
  usb.linux.device_number = 2  (0x2)  (int)
  usb.linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1d.1/usb6/6-1/6-1:1.0'  (string)
  usb.max_power = 0  (0x0)  (int)
  usb.num_configurations = 1  (0x1)  (int)
  usb.num_interfaces = 2  (0x2)  (int)
  usb.num_ports = 0  (0x0)  (int)
  usb.product = 'USB Communications Interface'  (string)
  usb.product_id = 4408  (0x1138)  (int)
  usb.speed = 12.0 (12) (double)
  usb.vendor_id = 59476  (0xe854)  (int)
  usb.version = 1.1 (1.1) (double)


i don't know what else to do,
i would really appreciate your help,
thankz in advance.

unikz 08-09-2009 05:31 AM

hi LQ friends,
I'm just running out of time ,
it would be really nice of u , if u help me as fast as u can.
thank you.


All times are GMT -5. The time now is 11:42 PM.