首页 > 代码库 > Linux usb gadget configfs用法

Linux usb gadget configfs用法

Linux 3.0新的USB gadget configfs.c驱动的用法如下所示:

挂载configfs文件系统:

# mount -t configfs none /sys/kernel/config

# cd /sys/kernel/config

# ls

usb_gadget

# cd usb_gadget


创建g1目录,实例化一个新的gadget模板:

# mkdir g1

# cd g1

# ls

UDC  bDeviceProtocol    bMaxPacketSize0    bcdUSB    functions    idVendor    

bDeviceClass     bDeviceSubClass    bcdDevice    configs    idProduct    strings


定义产品的VendorID和ProductID

# echo "0xabcd"  > idVendor

# echo "0x1234" > idProduct


实例化英语ID:

# mkdir strings/0x409

# ls strings/0x409

manufacturer    product    serialnumber


将开发商、产品和序列号字符串写入内核:

# echo "0123456789ABCDEF" > strings/0x409/serialnumber

# echo "Master"  > strings/0x409/manufacturer

# echo "Demo"  > strings/0x409/product


创建功能实例,需要注意的是,一个功能如果有多个实例的话,扩展名必须用数字编号:

# mkdir functions/acm.GS0

# mkdir functions/acm.GS1

# mkdir functions/ecm.usb0


创建一个USB配置实例:

# mkdir configs/c.1

# ls configs/c.1

MaxPower bmAttributes strings


定义配置描述符使用的字符串

# mkdir configs/c.1/string/0x409

# ls configs/c.1/strings/0x409/

configuration

# echo "2XACM+ECM" > configfs/c.1/strings/0x409/configuration


捆绑功能实例到配置c.1

# ln -s functions/acm.GS0 configs/c.1

# ln -s functions/acm.GS1 configs/c.1

# ln -s functions/ecm.usb0 configs/c.1


查找本机可获得的UDC实例

# ls /sys/class/udc/

msm_hsusb


将gadget驱动注册到UDC上,插上USB线到电脑上,电脑就会枚举USB设备。

# echo "msm_hsusb" > UDC