LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2012, 10:24 AM   #1
elazarik
LQ Newbie
 
Registered: May 2011
Distribution: Ubuntu, Fedora
Posts: 17

Rep: Reputation: Disabled
Question How to find out which /dev/ttyUSB* can be used for 3G modem connection?


Hi

I am writing a Perl script that detects USB 3G modem (of various types), switches it and then creates a connection with the ISP provider.

My problem is that several /dev/ttyUSB* files are created, and most cannot be used to connect.
I managed to filter some out by using modem command such as "at+csq" or "ati" (I receive a "timeout" on unconnected ttyUSBs).
But in some cases I am left with more than one /dev/ttyUSB* that respond correctly to these commands, but only one ttyUSB will actually work correctly with pppd.

For a more concrete example: USB stick Alcatel X220 (Vendor 1bbb product 0017). Both ttyUSB3 and ttyUSB4 respond to ati commands, but only ttyUSB4 will actually create the ppp connection (on ttyUSB3 pppd sends configuration requests but no answers are received).

Is there a way I can find out which ttyUSB is usable through a script?
This way should be generic for all USB modem types

The only way I can think of now is to add specific udev rules for ALL supported USB modems. And every time we add another supported modem, to add another rule.
Will be very thankful if there is an easier way to find out which ttyUSB to use

Thank you
Karin
 
Old 01-02-2012, 10:27 AM   #2
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 202

Rep: Reputation: 36
Have you tried using lsusb to locate the right one?.
 
Old 01-03-2012, 12:29 AM   #3
elazarik
LQ Newbie
 
Registered: May 2011
Distribution: Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: Disabled
Can you please elaborate?
lsusb returns "Bus 001 Device 003: ID 1bbb:0017"

How can I use this output to locate the correct /dev/ttyUSB*?

(I understand it helps me locate the /proc/bus/usb file that I can use for usbreset, but how else can I use this?)

Thank you
Karin
 
Old 01-03-2012, 01:37 AM   #4
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 202

Rep: Reputation: 36
Is /dev/ttyUSB* a file or a link to a file in /dev/bus/usb/00?

If it is not a link then maybe you can get the information you need from dmesg, otherwise you can use udev rules (create your own in /dev/udev/rules.d) to map your device to the same filename when connected to the system.

You will have to read de documentation to create your own rule (check this link), but you can also look inside the ones already created by the system (the one for the CD could help) and you will get a notion about how you should create your own.

Last edited by rodrifra; 01-03-2012 at 01:58 AM.
 
Old 01-03-2012, 02:32 AM   #5
elazarik
LQ Newbie
 
Registered: May 2011
Distribution: Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: Disabled
/dev/ttyUSB* are files and not symbolic links
Also dmesg offers no help

I guess I will have to create a rule for each supported modem type (sadly - a few hundreds... :-( ). I guess I will create a script for that ... ;-)

Thank you for your help
K
 
Old 01-25-2013, 01:00 AM   #6
alvin_cai
LQ Newbie
 
Registered: Jan 2013
Posts: 12

Rep: Reputation: Disabled
Smile

I met the same problem ,did not have a way to find the true ttyUSB? between them.
And have you find a way to distinguish ?
 
Old 01-29-2013, 06:45 AM   #7
elazarik
LQ Newbie
 
Registered: May 2011
Distribution: Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: Disabled
Yes I solved this

In a nutshell - I cannibalized usb_modeswitch way of switching:
I added to the udev rules a reference if it finds a tty file to call my script
I modified the usb_modeswitch scripts to my specific needs and it ended by creating a symbolic link (e.g /dev/modem3g) to the specific ttyUsb that is active
So far it has not failed me.
it has been a while since I did it and the code is no longer available to me immediately
Try to have a look at the way usb_modeswitch does it. If u still have trouble, I will try to dig around and find exactly what I did
 
Old 01-30-2013, 02:59 AM   #8
alvin_cai
LQ Newbie
 
Registered: Jan 2013
Posts: 12

Rep: Reputation: Disabled
Did you use the script usb_modeswitch.tcl?
I find that it seems to this script can make one a symbolic link to the specific ttyUSB, but I did not use the PC linux ,it based on the embedded Linux. So I can not use the tcl .
Thank you .
 
Old 01-31-2013, 09:44 AM   #9
hamlindsza
Member
 
Registered: Aug 2012
Distribution: Debian, CentOS
Posts: 74

Rep: Reputation: Disabled
Have u tried wvdial?

wvdialconf will automatically chooses which ttyUSB to use.
 
Old 01-31-2013, 07:40 PM   #10
alvin_cai
LQ Newbie
 
Registered: Jan 2013
Posts: 12

Rep: Reputation: Disabled
Yes ,I have tried wvdial
And I also find that wvdialconf will create a ttyUSB in the config file.
but this ttyUSB was not the PPP always ,it is just the first port between the AT and ppp.
For example , the at port is ttyUSB1 and the ppp port is ttyUSB2 ,the wvdialconf will create ttyUSB1 in the conf.
However , I'm now trying to porting the wvdial in my board , it will help me find the two ports, although will not find the true ppp port .
 
Old 02-01-2013, 11:16 AM   #11
elazarik
LQ Newbie
 
Registered: May 2011
Distribution: Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: Disabled
I did not use tcl, I rewrote it in perl

I do not know tcl and was too lazy to learn, although I could read it well enough
I took the idea and re-wrote it in Perl (slim possibility of bash, I do not remember)
I will try to upload the script on Monday
 
Old 02-01-2013, 11:35 PM   #12
alvin_cai
LQ Newbie
 
Registered: Jan 2013
Posts: 12

Rep: Reputation: Disabled
Thank you !
I will try to debug the script in order to use in my board.
Also , I'm trying to change the tcl into C .
 
Old 02-04-2013, 05:02 AM   #13
elazarik
LQ Newbie
 
Registered: May 2011
Distribution: Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: Disabled
The code I used:

udev rules:
=====================
kernel=="ttyUSB*", ATTRS{bNumConfigurations}=="*", PROGRAM="/etc/prysm/create_usb_symlink.pl %p %s{idVendor} %s{idProduct}", SYMLINK="%c"
kernel=="ttyACM*", ATTRS{bNumConfigurations}=="*", PROGRAM="/etc/prysm/create_usb_symlink.pl %p %s{idVendor} %s{idProduct}", SYMLINK="%c"
=====================
Perl script:
=====================
#!/usr/bin/perl
package links::3G;
use Sys::Syslog qw(:standard :macros);

openlog "Links:3G", "perror,pid", $LOCAL;

# This is not the first time - mode switch is already accomplished

my $path = "/sys$ARGV[0]/device";
syslog(LOG_DEBUG, "Executed with Vendor %s Product %s path %s real %s",$ARGV[1], $ARGV[2],$ARGV[0], $path) ;
# Make sure it is not a dialup modem
if ($ARGV[1] eq "0572" and $ARGV[2] eq "1329") {
syslog(LOG_DEBUG, "Executed for dialup modem");
print "";
closelog();
exit 0;
}
if (! -e $path) {
syslog(LOG_ERR, "Path %s does not exist", $path);
print"";
closelog();
exit 0;
}
if (-l $path) {
$link = readlink "$path";
syslog(LOG_DEBUG, "Using link %s", $link);
$link =~ s/\.\.//g;
$path = "/sys$link";
syslog(LOG_DEBUG, "Using path %s", $path);
}
my $ttyUSB;
if ($path =~ /(ttyUSB[0-9]+)/) {
$ttyUSB = $1;
syslog(LOG_DEBUG, "Using usb %s", $ttyUSB);
} else {
$ttyUSB = "unknown";
syslog(LOG_WARNING, "Path %s is not a ttyUSB port", $path);
#closelog();
#exit 0;
}
# remove ttyusb from path
@parts = split ("/", $path);
$count = 0;
foreach (@parts) {
if (/ttyUSB/) {
last;
}
$count++;
}
$path = "";
for ($i = 0; $i < $count; $i++) {
$path = $path."/".$parts[$i];
}
syslog(LOG_DEBUG, "Interrupt path %s", $path);
my $rc = &is_inter($path);
if ($rc == 0) {
syslog(LOG_DEBUG, "USB %s is not Interruptable", $ttyUSB);
print "";
closelog();
exit 0;
}
if ($ttyUSB eq "unknown") {
syslog(LOG_DEBUG, "USB %s is Interruptable", $ttyUSB);
print "gsmmodem";
closelog();
exit 0;
}
@numbers = split (/\./, $path);
$last = scalar(@numbers)-1;
$port = $numbers[$last];
if ($port > 0) {
syslog(LOG_DEBUG, "Looking for lower ports with inter");
for ($i = 0; $i < $port; $i++) {
$numbers[$last] = $i;
$path = join(".", @numbers);
$rc = &is_inter($path);
if ($rc == 1) {
syslog(LOG_DEBUG, "found an interrupt interface below %s", $ttyUSB);
print "";
closelog();
exit 0;
}
}
}

print "gsmmodem";
closelog();
exit 0;
sub is_inter($) {
my $path = $_[0];
if (! opendir(DIR, "$path")) {
syslog(LOG_ERR,"Could not open dir %s : %m", $path);
return 0;
}
@files = readdir(DIR);
closedir(DIR);
foreach $file (@files) {
if ($file =~ /^ep_/) {
$ep = "$path/$file/type";
if (!open(TYPE, $ep)) {
syslog(LOG_ERR,"Could not open file %s : %m", $ep);
next;
}
@lines = (<TYPE>);
close TYPE;
$count = grep (/Interrupt/, @lines);
if ($count > 0) {
syslog(LOG_DEBUG, "Interrupt path %s tty %s", $ep,$ttyUSB);
return 1;
}
}
}

return 0;
}
 
  


Reply

Tags
3g, modem, ttyusb



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No /dev/ttyUSB<*> in CentOS 5.4 baig Linux - Newbie 1 03-05-2010 03:02 PM
/dev/ttyUSB* is getting locked shariefbe Linux - Software 1 02-09-2010 10:55 PM
CentOS: No /dev/ttyUSB chris24300 Fedora 4 08-05-2009 12:29 PM
no /dev/ttyUSB* with FC3 please help derddera Fedora 3 12-30-2004 07:43 AM
/dev/ttyUSB? disappaers on SuSE 9.1 with pilot-link gevard Linux - Laptop and Netbook 0 08-29-2004 02:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration