Switching USB Modem to Serial Manually

Tags: May 12, 2014 3:56 AM

Without having to use usb-modeswitch switching the USB modem can be done easily. In this example I use USB CDMA modem with vendor ID 201e and product ID 1023 (when detected as CD-ROM storage) and product ID 1022 (when detected as USB modem or USB storage).

Requirements

Most of these module are present in all modern linux distro.
  • Kernel module: option (USB Driver for GSM modems)
  • Kernel module: usbserial (USB Serial Driver core)

Step 1: Detach the CD-ROM Storage

When you first plug your USB modem it might detected as CD-ROM storage. To identify the modem you can use dmesg and lsusb commands. Make sure running these commands as root.
$ dmesg | grep -E 'usb|scsi'
[    1.941521] sd 2:0:0:0: Attached scsi generic sg1 type 0
[  115.016532] usb 1-2: new full-speed USB device number 3 using ohci-pci
[  115.277003] usb 1-2: New USB device found, idVendor=201e, idProduct=1023
[  115.277019] usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=11
[  115.277028] usb 1-2: Product: CBP7.1
[  115.277035] usb 1-2: Manufacturer: China TeleCom
[  115.277043] usb 1-2: SerialNumber: \xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf
[  115.372632] usb-storage 1-2:1.0: USB Mass Storage device detected
[  115.372687] scsi3 : usb-storage 1-2:1.0
[  115.372967] usbcore: registered new interface driver usb-storage
[  116.391288] scsi 3:0:0:0: CD-ROM            CDROM    EVDO ReV A       7.00 PQ: 0 ANSI: 2
[  116.428033] sr1: scsi-1 drive
[  116.429719] sr 3:0:0:0: Attached scsi CD-ROM sr1
[  116.429928] sr 3:0:0:0: Attached scsi generic sg2 type 5
From the output we can see our vendor id and product id are 201e and 1023. The CD-ROM is attached on /dev/sr1. We can also see it using lsusb command.
$ lsusb
Bus 001 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 001 Device 003: ID 201e:1023
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Time to detach the CD-ROM storage from /dev/sr1.
$ eject sr1
Wait for few seconds and run the lsusb or dmesg again, the product id should change.
$ lsusb
Bus 001 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 001 Device 003: ID 201e:1022
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

$ dmesg | grep -Ei 'usb|scsi'
[ 1055.584657] usb 1-2: USB disconnect, device number 3
[ 1067.464477] usb 1-2: new full-speed USB device number 4 using ohci-pci
[ 1067.727963] usb 1-2: New USB device found, idVendor=201e, idProduct=1022
[ 1067.727976] usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=11
[ 1067.727982] usb 1-2: Product: CBP7.1
[ 1067.727987] usb 1-2: Manufacturer: China TeleCom
[ 1067.727991] usb 1-2: SerialNumber: \xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf\xffffffc3\xffffffbf\xffffffbf
[ 1067.753861] usb-storage 1-2:1.3: USB Mass Storage device detected
[ 1067.755149] scsi4 : usb-storage 1-2:1.3
[ 1068.778214] scsi 4:0:0:0: Direct-Access     SDVDISK  EVDO ReV A       7.00 PQ: 0 ANSI: 0 CCS
[ 1068.782201] sd 4:0:0:0: Attached scsi generic sg2 type 0
[ 1068.817068] sd 4:0:0:0: [sdb] Attached SCSI removable disk

Step 2: Attach The Driver to The USB Modem

Right know, the USB modem is not detected as CD-ROM storage anymore but it can not be used since no driver associated with those vendor or product. Here's how to check it.
$ lsusb -t
1-2:1.0: No such file or directory
1-2:1.1: No such file or directory
1-2:1.2: No such file or directory
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ohci-pci/8p, 12M
    |__ Port 1: Dev 2, If 0, Class=HID, Driver=usbhid, 12M
    |__ Port 2: Dev 4, If 0, Class=vend., Driver=, 12M
    |__ Port 2: Dev 4, If 1, Class=vend., Driver=, 12M
    |__ Port 2: Dev 4, If 2, Class=vend., Driver=, 12M
    |__ Port 2: Dev 4, If 3, Class=stor., Driver=usb-storage, 12M
Take a look at device number 4 that's the USB modem and no driver handle the device. We can force the kernel to attach option driver to the device just by playing around with /sys file system. All we have to do is write the vendor id and product id to the /sys/bus/usb-serial/drivers/option1/new_id. The first thing before attaching the driver is to make sure the driver itself is loaded.
$ modprobe option
$ lsmod | grep option
option                 33960  0 
usb_wwan               19721  1 option
usbserial              38859  2 option,usb_wwan
Now we need to tell the kernel to attach the driver simply by echoing to the /sys/bus/usb-serial/drivers/option1/new_id file.
$ echo 0x201e 0x1022 > /sys/bus/usb-serial/drivers/option1/new_id
Let's check again the device using the lsusb and dmesg.
$ dmesg
[ 1805.074022] usbcore: registered new interface driver usbserial
[ 1805.074465] usbcore: registered new interface driver usbserial_generic
[ 1805.074902] usbserial: USB Serial support registered for generic
[ 1805.113213] usbcore: registered new interface driver option
[ 1805.113671] usbserial: USB Serial support registered for GSM modem (1-port)
[ 2232.634820] option 1-2:1.0: GSM modem (1-port) converter detected
[ 2232.641870] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB0
[ 2232.642308] option 1-2:1.1: GSM modem (1-port) converter detected
[ 2232.642953] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB1
[ 2232.643487] option 1-2:1.2: GSM modem (1-port) converter detected
[ 2232.644293] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB2

$ lsusb -t
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ohci-pci/8p, 12M
    |__ Port 1: Dev 2, If 0, Class=HID, Driver=usbhid, 12M
    |__ Port 2: Dev 4, If 0, Class=vend., Driver=option, 12M
    |__ Port 2: Dev 4, If 1, Class=vend., Driver=option, 12M
    |__ Port 2: Dev 4, If 2, Class=vend., Driver=option, 12M
    |__ Port 2: Dev 4, If 3, Class=stor., Driver=usb-storage, 12M
Great, the device now handled by kernel option driver. There are three files created by the driver for us to talk to the modem: /dev/ttyUSB0, /dev/ttyUSB1 and /dev/ttyUSB2.

Step 3: Test The Modem

Normally the serial modem is located on the first tty* which in case is /dev/ttyUSB0. So let's try to talk to the modem by sending AT command. Since I'm using ubuntu I use the default serial communication program miniterm.py. As an example I sent "ATI" command which is command to identify the device.
miniterm.py /dev/ttyUSB0
--- Miniterm on /dev/ttyUSB0: 9600,8,N,1 ---
--- Quit: Ctrl+]  |  Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ATI
Manufacturer: +GMI: China TeleCom

Model: CBP7.1

Revision: 03.04.08P03

ESN: +GSN: 0x804512d7

+GCAP: +CEVDO, +CIS707P-A, +CIS-856-A, +CGSM, +MS, +ES, +DS, +FCLASS

OK

--- exit ---

References

Share on Facebook Twitter

0 comments:

Post a Comment