Linux gadget驅動應用

http://blog.chinaunix.net/uid-23373524-id-2426971.html

Linux內核中usb設備側驅動程序分成3個層次:UDC驅動程序、Gadget APIGadget驅動程序。UDC驅動程序(USB控制器)直接訪問硬件,控制USB設備和主機間的底層通信,向上層提供與硬件相關操作的回調函數。Gadget APIUDC驅動程序回調函數的簡單包裝,這部分程序內核都已經寫好。Gadget驅動程序具體控制USB設備功能的實現,使設備表現出“U盤”、“虛擬串口”等特性。

   簡單看個usb 虛擬串口例子

Overview
--------
The gadget serial driver is a Linux USB gadget driver, a USB device
side driver.  It runs on a Linux system that has USB device side
hardware; for example, a PDA, an embedded Linux system, or a PC
with a USB development card.
 
The gadget serial driver talks over USB to either a CDC ACM driver
or a generic USB serial driver running on a host PC.
 
   Host
   --------------------------------------
  | Host-Side   CDC ACM       USB Host   |
  | Operating |   or        | Controller |   USB
  | System    | Generic USB | Driver     |--------
  | (Linux or | Serial      | and        |        |
  | Windows)    Driver        USB Stack  |        |
   --------------------------------------         |
                                                  |
                                                  |
                                                  |
   Gadget                                         |
   --------------------------------------         |
  | Gadget                   USB Periph. |        |
  | Device-Side |  Gadget  | Controller  |        |
  | Linux       |  Serial  | Driver      |--------
  | Operating   |  Driver  | and         |
  | System                   USB Stack   |
   --------------------------------------

On the device-side Linux system, the gadget serial driver looks
like a serial device.
 
On the host-side system, the gadget serial device looks like a
CDC ACM compliant class device or a simple vendor specific device
with bulk in and bulk out endpoints, and it is treated similarly
to other serial devices.
 
The host side driver can potentially be any ACM compliant driver
or any driver that can talk to a device with a simple bulk in/out
interface.  Gadget serial has been tested with the Linux ACM driver,
the Windows usbser.sys ACM driver, and the Linux USB generic serial
driver.
 
With the gadget serial driver and the host side ACM or generic
serial driver running, you should be able to communicate between
the host and the gadget side systems as if they were connected by a
serial cable.
 
The gadget serial driver only provides simple unreliable data
communication.  It does not yet handle flow control or many other
features of normal serial devices.

 

usb-serial功能體驗

硬件:at91sam9263

內核:2.6.27

 

<*>   USB Gadget Support  --->
            USB Peripheral Controller (AT91 USB Device Port)  --->
             AT91 USB Device Port
    <M>   USB Gadget Drivers

    <M>       Gadget Zero(DEVELOPMENT) 
    <M>       Ethernet Gadget (with CDC Ethernet support)
    [*]         RNDIS support

    <M>       Gadget Filesystem(EXPERIMENTAL) 

    <M>       File-backed Storage Gadget 

    <M>       Serial Gadget 

    <M>       Printer Gadget 

    <M>       CDC Composite Device(Ethernet and ACM) 

  

簡單解釋下

  Gadget Zero, 類似於 dummy hcd, 該驅動用於測試 udc 驅動。它會幫助您通過 USB-IF 測試。

  Ethernet Gadget, 該驅動模擬以太網網口,它支持多種運行方式:

  CDC Ethernet: usb 規範規定的 Communications Device Class “Ethernet Model” protocol。

  RNDIS: 微軟公司對 CDC Ethernet 的變種實現。

   Gadget Filesystem:提供一個基於API的文件系統,可以在用戶空間訪問

  File-backed Storage Gadget最常見的 U 盤功能實現。

  Serial Gadget:虛擬串口

   Printer Gadget:打印機功能

 

這裏之所以都選擇爲模塊的形式,是爲了調試方便,有些模塊,比如U盤加載時還需要提供介質,就是說加載模塊時還需要參數,否則加載不上

編譯內核 make uImage

編譯模塊 make modules

然後把

# insmod g_serial.ko 
g_serial gadget: Gadget Serial v2.4
g_serial gadget: g_serial ready

通過連接板子和電腦

板子上提示

# g_serial gadget: full speed config #2: CDC ACM config

 

說明開發板上已經加載成功。

電腦上同時也會出現提示新硬件

 

驅動Inf文件如下

The "gserial.inf" file is given here.
 
-------------------- CUT HERE --------------------
[Version]
Signature="$Windows NT$"
Class=Ports
ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
Provider=%LINUX%
DriverVer=08/17/2004,0.0.2.0
; Copyright (C) 2004 Al Borchers ([email protected])
 
[Manufacturer]
%LINUX%=GSerialDeviceList
 
[GSerialDeviceList]
%GSERIAL%=GSerialInstall, USB\VID_0525&PID_A4A7
 
[DestinationDirs]
DefaultDestDir=10,System32\Drivers
 
[GSerialInstall]
CopyFiles=GSerialCopyFiles
AddReg=GSerialAddReg
 
[GSerialCopyFiles]
usbser.sys
 
[GSerialAddReg]
HKR,,DevLoader,,*ntkern
HKR,,NTMPDriver,,usbser.sys
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"

[GSerialInstall.Services]
AddService = usbser,0x0002,GSerialService
 
[GSerialService]
DisplayName = %GSERIAL_DISPLAY_NAME%
ServiceType = 1                  ; SERVICE_KERNEL_DRIVER
StartType = 3                    ; SERVICE_DEMAND_START
ErrorControl = 1                 ; SERVICE_ERROR_NORMAL
ServiceBinary = %10%\System32\Drivers\usbser.sys
LoadOrderGroup = Base
 
[Strings]
LINUX = "Linux"
GSERIAL = "Gadget Serial"
GSERIAL_DISPLAY_NAME = "USB Gadget Serial Driver"
-------------------- CUT HERE --------------------
 
The "usbser.sys" file comes with various versions of Windows.
For example, it can be found on Windows XP typically in
 
  C:\WINDOWS\Driver Cache\i386\driver.cab

 

安裝成功後就會發現電腦多了一個串口。操作這個串口就可以給板子讀寫數據了。

 

測試方法:

先查看

# cat /proc/devices 
Character devices:
  1 mem
  2 pty
  3 ttyp
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  7 vcs
 10 misc
 13 input
 90 mtd
128 ptm
136 pts
153 spi
254 ttyGS
 
Block devices:
  1 ramdisk
  7 loop
  8 sd
 31 mtdblock
 65 sd
 66 sd
 67 sd
 68 sd
 69 sd
 70 sd
 71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
179 mmc
#

然後

# mknod /dev/usb_serial c 254 0

然後

# cat /dev/usb_serial 

在PC上給串口發送數據,就會顯示在板子上。

# echo 12345 > /dev/usb_serial 

會發現串口收到字符"12345"

 

至此測試完畢


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章