USB Gadget Device Driver Analysis (1) -- function experience

Source: Internet
Author: User

The Linux USB Gadget device driver can be used to implement some interesting functions. For example, 1. A storage device in an embedded product or a partition of a storage device, it can be used as a USB flash disk by PC. It can easily complete file interaction. This function is widely used in mobile phones, digital cameras, and other products. 2. After an embedded device connects to your PC through USB, a new network connection will appear on your PC, and a NIC will also appear on the embedded device, you can configure their IP addresses for network communication, also known as USB net.

All USB communication devices have USB device programs, which are usually called USB firmware. In some simple-function devices, you can use some dedicated programmable USB controllers. In some complex embedded systems that run Linux-like operating systems, you must be familiar with the operations of the USB device controller to complete the USB device program, be familiar with the drive architecture of the operating system.

I want to parse the compilation of the Linux USB Gadget Device Driver in four aspects: "function Experience", "Driver debugging", "Architecture Analysis of the gadget driver", and "compiling a own gadget driver ". method.

I. Implementation of the simulated USB flash drive in Linux

In the hardware environment for the far vision of the fs2410 platform, the software environment for the linux-2.6.26 Linux system, to achieve the function of analog U disk.

Add code to the kernel

# Include <ASM/ARCH/regs-gpio.h>
# Include <ASM/ARCH/regs-clock.h>
# Include <ASM/plat-s3c24xx/UDC. h>

Modify ARCH/ARM/mach-s3c2410/mach-smdk2410.c

/* Handle the pull-up resistor of the USB device */
Static voidSmdk2410_udc_pullup(Enum s3c2410_udc_1__e cmd)
{
U8 * s3c2410_pullup_info [] = {
"",
"Pull-up enable ",
"Pull-up disable ",
"UDC reset, in case"
};
Printk ("smdk2410_udc: % s/n", s3c2410_pullup_info [cmd]);
S3c2410_gpio_cfgpin (s3c2410_gpg9, s3c2410_gpg9_outp );
Switch (CMD)
{
Case s3c2410_udc_p_enable:

S3c2410_gpio_setpin (s3c2410_gpg9, 1); // set gpg9 output high
Break;
Case s3c2410_udc_p_disable:
S3c2410_gpio_setpin (s3c2410_gpg9, 0); // set gpg9 output low
Break;
Case s3c2410_udc_p_reset:
// Fixme !!!
Break;
Default:
Break;
}
}

Static struct s3c2410_udc_mach_infoSmdk2410_udc_cfg_ Initdata = {
. Udc_command = smdk2410_udc_pullup,
};

Static struct platform_device * smdk2410_devices [] _ initdata = {
...,
&Initi_device_usbgadget,/* USB Gadget DeviceDevice registration */
};

Static void _ init sdmk2410_init (void)
{
U32 upll_value;
Set_s3c2410fb_info (& smdk2410_lcdcfg );
S3c24xx_udc_set_platdata (& smdk2410_udc_cfg);/* Initialize */
Initi_device_sdi.dev.platform_data = & smdk2410_mmc_cfg;
/* Turn off suspend on both USB ports, and switch
* Selectable USB port to USB device mode .*/

S3c2410_modify_miscr (s3c2410_misccr_usbhost |
S3c2410_misccr_usbsuspnd0 |
S3c2410_misccr_usbsuspnd1, 0x0 );
/* Set the USB clock */
Upll_value = (
0x78 <s3c2410_pllcon_mdivshift)
| (0x02 <s3c2410_pllcon_pdivshift)
| (0x03 <s3c2410_pllcon_sdivshift );
While (upll_value! = Readl (s3c2410_upllcon )){
Writel (upll_value, s3c2410_upllcon );
Udelay (20 );
}
}

ModifyDrivers/USB/gadget/file_storage.c

Static void start_transfer (struct fsg_dev * FSG, struct usb_ep * EP,
Struct usb_request * req, int * pbusy,
Enum fsg_buffer_state * State)
{
Int RC;
Udelay (800 );
......
}

Configure the kernel to support U disk Simulation

<*> USB Gadget support --->
USB Peripheral Controller (S3C2410 USB device controller) --->
S3C2410 USB device controller
[*] S3C2410 UDC debug messages
<M> USB Gadget drivers
<M> file-backed storage gadget

3. Compile the kernel

# Make zimage
# Make modules

G_file_storage.ko is generated under the drivers/USB/gadget directory.

Load drivers and test functions

Use the previous kernel to start the system and load g_file_storage.ko

# Insmod g_file_storage.ko
# Insmod g_file_storage.ko file =/dev/mtdblock2 stall = 0 removable = 1
0.03 USB: usb_gadget_register_driver () 'G _ file_storage'
0.04 USB: Binding gadget driver 'G _ file_storage'
0.05 USB: s3c2410_set_selfpowered ()
G_file_storage gadget: file-backed storage gadget, version: 20 October 2004
G_file_storage gadget: Number of Luns = 1
G_file_storage gadget-lun0: RO = 0, file:/dev/mtdblock3
0.06 USB: udc_enable called
Smdk2410_udc: pull-up enable

Connect the device to Windows. Windows automatically adds the device to a new USB flash drive. Format the USB flash drive and save it to the file. After detaching a USB flash drive, perform the following operations on the target board:

# Mkdir/mnt/gadget
# Mount-T vfat/dev/mtdblock2/mnt/gadget/
# Ls

You can see the files stored in the USB flash drive in windows.

II. Implementation of USB net functions

Configure the kernel to support USB net

<*> USB Gadget support --->
USB Peripheral Controller (S3C2410 USB device controller) --->
S3C2410 USB device controller
[*] S3C2410 UDC debug messages
<M> USB Gadget drivers
<M> Ethernet gadget (with CDC Ethernet Support)
[*] Rndis support

2. Compile the kernel

# Make zimage
# Make modules

Generate g_ether.ko under the drivers/USB/gadget directory

3. Load the driver and test the function.

Use the previous kernel to start the system and load g_ether.ko.

# Insmod g_ether.ko
# Ifconfig usb0 192.168.1.120
......
Usb0 link encap: Ethernet hwaddr 5E: C5: F6: D4: 2b: 91
Inet ADDR: 192.168.1.120 bcast: 192.168.1.255 mask: 255.255.255.0
Up broadcast running Multicast MTU: 1500 Metric: 1
RX packets: 253 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 43 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 FIG: 1000
RX Bytes: 35277 (34.4 kib) TX Bytes: 10152 (9.9 kib)

Connect the device to Windows. If the Windows system prompts you to install the driver, follow the prompts to install the rndis driver. This driver can be found on the network. In this case, Windows will generate a new network connection and configure its IP address and other information. Then you can communicate with the target system through USB. (Author:Liu Hongtao, a gold medal lecturer in the far-sighted embedded School of Huaqing .)

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.