A little research on Linux gadget u disk and USB virtual serial __linux

Source: Internet
Author: User
Tags types of functions
origin:https://blog.csdn.net/wuyuwei45/article/details/8926858

Linux kernel2.6 in the above version, the USB device-driven interface changed to Gadget, in the Kernel/driver/usb/gadget directory mainly includes platform USB UDC drive and gadget interface driver.

Kernel/driver/usb/gadget SERIAL.C is a commonly used driver file that can be configured for bulk transmission drive or CDC ACM Drive (USB to serial port drive). There are two ways to configure it, one can manually change the default value of the variable "USE_ACM" in the driver file before compiling, and now the default value is "true", which corresponds to the CDC ACM Drive, which corresponds to "false" for the bulk drive; Two can be driven into a module, Then it's OK to pass a parameter on the insmod.

Run make Menuconfig to see:


The "USB Gadget Support-->" option is compiled into a module, which facilitates dynamic load driving.

To perform the "USB Gadget Support-->" Configuration submenu:


A module that is configured with a red box callout.

Run command: Make M=driver/usb/gagdet modules

After compiling, the production of G_file_storage.ko and G_serial.ko under Driver/usb/gagdet, respectively, for U disk drive and USB serial port drive.

The author's embedded board has an SD card, Linux driver mount after the main directory is/dev/mmcblk0, the partition directory is/DEV/MMCBLK0P1,/DEV/MMCBLK0P2,/DEV/MMCBLK0P3, there are three partitions.

Perform:

[Plain] View Plain Copy root@rfodncc:/mnt# insmod g_file_storage.ko file=/dev/mmcblk0 stall=0  removable=1   [   69.783477] g_file_storage gadget: no  serial-number string provided!   [   69.798980] g_file_storage  gadget: file-backed storage gadget, version: 1 september 2010   [    69.807495] g_file_storage gadget: Number of LUNs=1   [    69.812622] g_file_storage gadget-lun0: ro=0, nofua=0, file: / dev/mmcblk0   [   69.819763] MUSB-HDRC MUSB-HDRC.0: MUSB HDRC  host driver   [   69.825622] musb-hdrc musb-hdrc.0: new  USB bus registered, assigned bus number 2   [    69.833801] usb usb2:&nbSp new usb device found, idvendor=1d6b, idproduct=0002   [    69.840942] usb usb2: new usb device strings: mfr=3, product=2,  serialnumber=1   [   69.848510] USB USB2: PRODUCT: MUSB HDRC  host driver   [   69.853668] usb usb2: manufacturer:  linux 2.6.37-05branch musb-hcd   [   69.860137] usb usb2:  serialnumber: musb-hdrc.0   [   69.872558] hub 2-0:1.0: usb  hub found   [   69.876647] hub 2-0:1.0: 1 port detected    Root@rfodncc:/mnt# [   70.169555] g_file_storage gadget: high  speed config  #1   

Connect USB to PC, you can see the PC side of the SD card partition directory.

Ps:insmod G_file_storage.ko file=/dev/mmcblk0 stall=0 removable=1 and Insmod G_file_storage.ko a little different, Follow-up can be found to understand the specific role of stall and removable.

Then make an embedded device-side FAT32 file partition and mount it on your PC:

1. Set up the FAT32 file image on the host Ubuntu, the size is 2M.

#dd If=/dev/zero of=fat32.img bs=1k count=2048
#mkfs. VFAT fat32.img

2. Write some files to the fat32.img to test:
#mkdir FAT32
#sudo mount-t Vfat-o Loop fat32.img FAT32
#cd FAT32
#touch Hello.txt
#echo Hello,wolrd>hello.txt
#sync

3. Copy the fat32.img from the host Ubuntu to the root file system of the embedded device.

4. Load the G_file_storage driver in the embedded device

Insmod G_file_storage.ko file=/opt/fat32.img stall=0 removable=1//I'll put fat32.img under/opt

5.USB line connection device and PC, eject Discovery Mobile disk, open disk, there is a hello.txt file.

6. Open u disk on PC side, add a file system, such as Text.txt, and then mount the image by command on the device side, command:

MOUNT-T vfat-o Loop/opt/fat32.img/media//Mount to media directory

To media directory, Cd/media

See that/media has two files Hello.txt and Test.txt, and the content is consistent with what you see on the PC side.

Note: If the file is modified at this time on the PC side. The device side cannot immediately see the results of a PC modification. That is, the PC and device side are not synchronized.

You can now umount/media the device and then mount it again to see the results of your PC changes. Also on the device side of the modified file, the PC side can not immediately see the changes, you need to reseat the USB cable to see the update. As for the reason, I do not know for a moment.

Uninstall G_file_storage

Execute load USB serial port command

[Plain] View plain copy root@rfodncc:/mnt# insmod g_serial.ko    [  239.675933] g_ serial gadget: gadget serial v2.4   [  239.680786] g_serial  gadget: g_serial ready   [  239.685241] musb-hdrc musb-hdrc.0:  musb hdrc host driver   [  239.691009] musb-hdrc musb-hdrc.0:  new USB bus registered, assigned bus number 2   [   239.707153] usb usb2: New USB device found, idVendor=1d6b,  idproduct=0002   [  239.714324] usb usb2: new usb device  strings: mfr=3, product=2, serialnumber=1   [  239.721893] usb  usb2: product: musb hdrc host driver   [  239.727111] usb  usb2: manufacturer: linux 2.6.37-05branch musb-hcd   [  239.733581] usb usb2:  serialnumber: musb-hdrc.0   [  239.747467] hub 2-0:1.0: usb hub  found   [  239.751434] hub 2-0:1.0: 1 port detected    root@rfodncc:/mnt# [  240.044830] g_serial gadget: high speed  config  #2: cdc acm config   PC-side installed USB port driver, you can see from the PC Device Manager on a serial port.

To enable the device to communicate with the PC end via USB to the serial port, the device side also need to manually create the device file node, refer to the kernel document (Documents/usb/gagdet_serial.txt) part of the content:

This'll also automatically load the underlying gadget peripheral
Controller driver. This must is done the reboot the gadget
Side Linux System. Can add this to the "Start up scripts, if


Desired.


Your system should use Mdev (from BusyBox) or udev to make the
Device nodes. After this gadget driver has been set up you should
Then A/DEV/TTYGS0 node:


# ls-l/dev/ttygs0 | Cat
CRW-RW----1 root 253, 0 May 8 14:10/dev/ttygs0
#


The major number (253, above) is system-specific. If
You are need to create/dev nodes by hand and the right numbers to use
would be in The/sys/class/tty/ttygs0/dev file.

The author creates a device node: First Cat/sys/class/tty/ttygs0/dev, after the main device number, Mknod/dev/ttygs0 C Major_num 0

Then the PC opens a serial port software to open the corresponding serial ports.

The device side performs the echo "Hello" >/dev/ttygs0,pc port serial software to receive the corresponding data.

PS:UDC (Device Controller) driver is mainly related to the hardware platform, it will implement gadget function-driven interface required, the general UDC only support the registration of a gadget function driver, so the above is after the G_file_storage driver uninstall to reload G_ Serial.ko. Otherwise, a failure will occur.

look at some of the programs in the UDC driver code that are registered gadget drive functions Usb_gadget_probe_driver:

[plain] view plain copy spin_lock_irqsave (&udc->lock, flags);       if (udc->driver) {Spin_unlock_irqrestore (&udc->lock, flags);   Return-ebusy; The Udc->driver is true after the first registration of the gadget driver, and an unregistered error occurs when gadget is registered again without unloading.

"The following is reproduced: http://blog.csdn.net/embededswordman/article/details/6689593"

Linux supports connecting a variety of USB from devices, while also supporting itself as a device plugged into other hosts. The most typical example is the Android OS phone, which can be identified as a device such as a USB disk.

For Soc, this part directly corresponds to the operation of the USB device part.

To avoid confusion with the term "device-driven (USB Device Driver)" That is supported as a host, Linux names this part of the implementation as "Gadget", a small toy. The kernel source directory is \drivers\usb\gadget, which contains the different types of USB Device Controller (UDC)-driven implementations supported by the kernel, as well as the implementation of frameworks and different gadget.

Take AT91 ARM9 As an example, the bottom driver is at91_udc.c (for the SOC that supports high Speed USB 2.0 is ATMEL_USBA_UDC.C), it implements the unified interface defined by Gadget.h, and then various gadget on the upper layer Driver (such as SERIAL.C, etc.) call this set of unified interface to achieve different types of functions, such as USB serial, u disk, USB ethernet and so on.

Atmel USB Serial Port frame diagram:


The description and usage of USB serial port are detailed and clearly described in the kernel directory \documentation\usb\gadget_serial.txt, no longer repeat here. Once the module is loaded, the device file is automatically created under/dev/, and the program can open it and write, even if the USB cable is not connected. Have been entangled in why G_serial did not provide the upper layer of USB cable Connect/disconnect events, and then think in the use of the serial port is the same case, open a string even if there is no connection line, you can send data, but no one will receive it. However, the only difference with the serial port is that if the communication is in progress to unplug the USB cable, then again connected to the USB cable after the need to reopen the ttyGS0 device file can be sent again, otherwise write can not work as normal serial port return error.


Reference section:

Description of the official gadget framework: http://www.linux-usb.org/gadget/

9263-based configuration process: http://www.cublog.cn/u3/111925/showart_2278264.html

Huaqing lecturer Liu Hongtao's blog:http://blog.csdn.net/hongtao_liu/article/details/4555645

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.