设为首页收藏本站
查看: 687|回复: 0

【米尔瑞萨RZ/G2L开发板-创新应用】qemu 虚拟 usb gadget 驱动开发环境搭建

[复制链接]

3

主题

0

回帖

39

积分

新手上路

积分
39
iysheng 发表于 2023-9-19 18:14:01 | 显示全部楼层 |阅读模式
qemu 虚拟 gadget 驱动开发环境搭建
因为我的项目主要是围绕 usb gadget 驱动进行开发的,加上最近一段时间出差手边没有板子,所以本
篇就记录下在没有板子的情况下如何进行程序开发调试。
本次,我实现的效果是使用最新版的 qemu-system-arm 模拟 orangepi PC 这个虚拟板卡,并手动编译
内核和根文件系统。同时引导启动。
环境
本次,我使用的环境:
PC 环境是 Linux fedora 6.1.14-100.fc36.x86_64
qemu 版本(手动编译的最新版本) QEMU emulator version 8.1.50 (v8.1.0-549-
gc5ea91da44)
orangepi-PC 对应的内核版本是 5.4.65
orangepi-PC 使用的根文件系统,使用最新版本的 buildroot 进行构建
过程
1. qemu 的编译过程,这里需要注意,我开始编译的时候没有生成 qemu-system-arm 工具,后来发现
是没有使能 -enable-system,同时因为后续会使用到 -nic 的 user 模式,所以这里还要在 PC 端
安装相关的软件包 libslirp-devel 。最后,完整的配置命令是 ./configure --target-
list='aarch64-linux-user arm-linux-user arm-softmmu aarch64-softmmu' --enable-
tools --enable-linux-user --enable-system --enable-slirp --enable-kvm
2. orangepi-PC 内核的编译过程相对就简单了,参数 orangepi-PC 根据OrangePi_PC_H3
用户手册
v3.2.pdf 这份文档,可以找对对应内核的仓库和对应的交叉编译器, 配置就选择仓库的
orangepi_defconfig 进行编译就行,中间出现部分错误提示有些文件(regulatory.db
regulatory.db.p7s)找不到,从 github 找到对应的文件,另外的文件(edid/asus.bin)我在配置
过程中给删除了(因为我没有从网上找到)。然后就可以顺利编译通过了。
  1. ...
  2. MODPOST 3 modules
  3. CC [M] drivers/usb/gadget/legacy/g_serial.mod.o
  4. CC [M] drivers/usb/gadget/legacy/g_zero.mod.o
  5. Kernel: arch/arm/boot/Image is ready
  6. CC [M] drivers/usb/gadget/udc/dummy_hcd.mod.o
  7. LD [M] drivers/usb/gadget/legacy/g_serial.ko
  8. LD [M] drivers/usb/gadget/legacy/g_zero.ko
  9. LZ4 arch/arm/boot/compressed/piggy_data
  10. LD [M] drivers/usb/gadget/udc/dummy_hcd.ko
  11. CC arch/arm/boot/compressed/decompress.o
  12. AS arch/arm/boot/compressed/piggy.o
  13. LD arch/arm/boot/compressed/vmlinux
  14. OBJCOPY arch/arm/boot/zImage
  15. Kernel: arch/arm/boot/zImage is ready
复制代码

3. 构建根文件系统的时候,为了加快构建,选择使用先安装好的工具链,不编译内核和bootloader,
如果本地有 buildroot 的相关下载源,那么可以加快下载构建的过程。构建成功后会生成
rootfs.ext2 文件。接着使用下面的脚本将文件转换为 ramdisk 模式(记得在内核配置过程中
也要放开选项 CONFIG_BLK_DEV_RAM , 以及 CONFIG_BLK_DEV_RAM_SIZE )
  1. ...
  2. Creating regular file
  3. /home/red/Downloads/qemu_gadget/orangpi_pc_software/buildroot/output/images/rootf
  4. s.ext2
  5. 64-bit filesystem support is not enabled. The larger fields afforded by this
  6. feature enable full-strength checksumming. Pass -O 64bit to rectify.
  7. Creating filesystem with 61440 1k blocks and 15360 inodes
  8. Filesystem UUID: 709417d3-e230-4a1e-888c-8ef34bf4f014
  9. Superblock backups stored on blocks:
  10. 8193, 24577, 40961, 57345
  11. Allocating group tables: done
  12. Writing inode tables: done
  13. Creating journal (4096 blocks): done
  14. Copying files into the device: done
  15. Writing superblocks and filesystem accounting information: done
  16. ln -sf rootfs.ext2
  17. /home/red/Downloads/qemu_gadget/orangpi_pc_software/buildroot/output/images/rootf
  18. s.ext4
复制代码

测试
接着就是完事具备,测试引导启动了,启动命令为: qemu-system-arm -M orangepi-pc -nic user -
nographic -kernel arch/arm/boot/zImage -append 'console=ttyS0,115200 nosmp
root=/dev/ram' -dtb arch/arm/boot/dts/sun8i-h3-orangepi-pc.dtb -initrd rootfs.ext2(根
文件系统的完整路径) 正常的启动打印信息如下:
  1. Booting Linux on physical CPU 0x0
  2. Linux version 5.4.65-g018b9c0bc2d5-dirty (red@fedora) (gcc version 9.2.1 20191025
  3. (GNU Toolchain for the A-profile Architecture 9.2-2019.12 (arm-9.10))) #6 SMP Sun
  4. Sep 10 20:28:01 CST 2023
  5. CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=30c5387d
  6. CPU: div instructions available: patching division code
  7. CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
  8. OF: fdt: Machine model: Xunlong Orange Pi PC
  9. Memory policy: Data cache writealloc
  10. cma: Reserved 128 MiB at 0x0000000078000000
  11. On node 0 totalpages: 262144
  12. Normal zone: 2304 pages used for memmap
  13. Normal zone: 0 pages reserved
  14. Normal zone: 196608 pages, LIFO batch:63
  15. HighMem zone: 65536 pages, LIFO batch:15
  16. ...
  17. sun8i-h3-pinctrl 1c20800.pinctrl: 1c20800.pinctrl supply vcc-pg not found, using
  18. dummy regulator
  19. ehci-platform 1c1b000.usb: EHCI Host Controller
  20. ehci-platform 1c1b000.usb: new USB bus registered, assigned bus number 5
  21. ehci-platform 1c1b000.usb: irq 28, io mem 0x01c1b000
  22. ehci-platform 1c1b000.usb: USB 2.0 started, EHCI 1.00
  23. usb usb5: New USB device found, idVendor=1d6b, idProduct=0002, bcdDevice= 5.04
  24. usb usb5: New USB device strings: Mfr=3, Product=2, SerialNumber=1
  25. usb usb5: Product: EHCI Host Controller
  26. usb usb5: Manufacturer: Linux 5.4.65-g018b9c0bc2d5-dirty ehci_hcd
  27. usb usb5: SerialNumber: 1c1b000.usb
  28. hub 5-0:1.0: USB hub found
  29. hub 5-0:1.0: 6 ports detected
  30. ehci-platform 1c1c000.usb: EHCI Host Controller
  31. ehci-platform 1c1c000.usb: new USB bus registered, assigned bus number 6
  32. ehci-platform 1c1c000.usb: irq 30, io mem 0x01c1c000
  33. ehci-platform 1c1c000.usb: USB 2.0 started, EHCI 1.00
  34. usb usb6: New USB device found, idVendor=1d6b, idProduct=0002, bcdDevice= 5.04
  35. usb usb6: New USB device strings: Mfr=3, Product=2, SerialNumber=1
  36. usb usb6: Product: EHCI Host Controller
  37. usb usb6: Manufacturer: Linux 5.4.65-g018b9c0bc2d5-dirty ehci_hcd
  38. usb usb6: SerialNumber: 1c1c000.usb
  39. hub 6-0:1.0: USB hub found
  40. hub 6-0:1.0: 6 ports detected
  41. ehci-platform 1c1d000.usb: EHCI Host Controller
  42. ehci-platform 1c1d000.usb: new USB bus registered, assigned bus number 7
  43. ehci-platform 1c1d000.usb: irq 32, io mem 0x01c1d000
  44. ehci-platform 1c1d000.usb: USB 2.0 started, EHCI 1.00
  45. usb usb7: New USB device found, idVendor=1d6b, idProduct=0002, bcdDevice= 5.04
  46. usb usb7: New USB device strings: Mfr=3, Product=2, SerialNumber=1
  47. usb usb7: Product: EHCI Host Controller
  48. usb usb7: Manufacturer: Linux 5.4.65-g018b9c0bc2d5-dirty ehci_hcd
  49. usb usb7: SerialNumber: 1c1d000.usb
  50. hub 7-0:1.0: USB hub found
  51. hub 7-0:1.0: 6 ports detected
  52. ohci-platform 1c1b400.usb: Generic Platform OHCI controller
  53. ohci-platform 1c1b400.usb: new USB bus registered, assigned bus number 8
  54. ohci-platform 1c1b400.usb: irq 29, io mem 0x01c1b400
  55. usb usb8: New USB device found, idVendor=1d6b, idProduct=0001, bcdDevice= 5.04
  56. usb usb8: New USB device strings: Mfr=3, Product=2, SerialNumber=1
  57. usb usb8: Product: Generic Platform OHCI controller
  58. usb usb8: Manufacturer: Linux 5.4.65-g018b9c0bc2d5-dirty ohci_hcd
  59. usb usb8: SerialNumber: 1c1b400.usb
  60. hub 8-0:1.0: USB hub found
  61. hub 8-0:1.0: 3 ports detected
  62. ohci-platform 1c1c400.usb: Generic Platform OHCI controller
  63. ohci-platform 1c1c400.usb: new USB bus registered, assigned bus number 9
  64. ohci-platform 1c1c400.usb: irq 31, io mem 0x01c1c400
  65. usb usb9: New USB device found, idVendor=1d6b, idProduct=0001, bcdDevice= 5.04
  66. usb usb9: New USB device strings: Mfr=3, Product=2, SerialNumber=1
  67. usb usb9: Product: Generic Platform OHCI controller
  68. usb usb9: Manufacturer: Linux 5.4.65-g018b9c0bc2d5-dirty ohci_hcd
  69. usb usb9: SerialNumber: 1c1c400.usb
  70. hub 9-0:1.0: USB hub found
  71. hub 9-0:1.0: 3 ports detected
  72. ohci-platform 1c1d400.usb: Generic Platform OHCI controller
  73. ohci-platform 1c1d400.usb: new USB bus registered, assigned bus number 10
  74. ohci-platform 1c1d400.usb: irq 33, io mem 0x01c1d400
  75. usb usb10: New USB device found, idVendor=1d6b, idProduct=0001, bcdDevice= 5.04
  76. usb usb10: New USB device strings: Mfr=3, Product=2, SerialNumber=1
  77. usb usb10: Product: Generic Platform OHCI controller
  78. usb usb10: Manufacturer: Linux 5.4.65-g018b9c0bc2d5-dirty ohci_hcd
  79. usb usb10: SerialNumber: 1c1d400.usb
  80. hub 10-0:1.0: USB hub found
  81. hub 10-0:1.0: 3 ports detected
  82. usb_phy_generic usb_phy_generic.0.auto: usb_phy_generic.0.auto supply vcc not
  83. found, using dummy regulator
  84. musb-hdrc musb-hdrc.1.auto: MUSB HDRC host driver
  85. musb-hdrc musb-hdrc.1.auto: new USB bus registered, assigned bus number 11
  86. usb usb11: New USB device found, idVendor=1d6b, idProduct=0002, bcdDevice= 5.04
  87. usb usb11: New USB device strings: Mfr=3, Product=2, SerialNumber=1
  88. usb usb11: Product: MUSB HDRC host driver
  89. usb usb11: Manufacturer: Linux 5.4.65-g018b9c0bc2d5-dirty musb-hcd
  90. usb usb11: SerialNumber: musb-hdrc.1.auto
  91. hub 11-0:1.0: USB hub found
  92. hub 11-0:1.0: 1 port detected
  93. ...
  94. EXT4-fs (ram0): mounted filesystem with ordered data mode. Opts: (null)
  95. VFS: Mounted root (ext4 filesystem) readonly on device 1:0.
  96. devtmpfs: mounted
  97. Freeing unused kernel memory: 2048K
  98. Run /sbin/init as init process
  99. EXT4-fs (ram0): re-mounted. Opts: (null)
  100. Starting syslogd: OK
  101. Starting klogd: OK
  102. Running sysctl: OK
  103. random: seedrng: uninitialized urandom read (256 bytes read)
  104. Saving 2048 bits of non-creditable seed for next boot
  105. Starting rpcbind: OK
  106. Starting network: dwmac-sun8i 1c30000.ethernet eth0: PHY [0.1:01] driver [Generic
  107. PHY]
  108. dwmac-sun8i 1c30000.ethernet eth0: No Safety Features support found
  109. dwmac-sun8i 1c30000.ethernet eth0: No MAC Management Counters available
  110. dwmac-sun8i 1c30000.ethernet eth0: PTP not supported by HW
  111. dwmac-sun8i 1c30000.ethernet eth0: configuring for phy/mii link mode
  112. dwmac-sun8i 1c30000.ethernet eth0: Link is Up - 100Mbps/Full - flow control rx/tx
  113. udhcpc: started, v1.36.1
  114. udhcpc: broadcasting discover
  115. udhcpc: broadcasting select for 10.0.2.15, server 10.0.2.2
  116. udhcpc: lease of 10.0.2.15 obtained from 10.0.2.2, lease time 86400
  117. deleting routers
  118. adding dns 10.0.2.3
  119. OK
  120. Starting NFS statd: OK
  121. Starting NFS services: OK
  122. Starting NFS daemon: NFSD: Using /var/lib/nfs/v4recovery as the NFSv4 state
  123. recovery directory
  124. NFSD: unable to find recovery directory /var/lib/nfs/v4recovery
  125. NFSD: Unable to initialize client recovery tracking! (-2)
  126. NFSD: starting 90-second grace period (net f0000049)
  127. OK
  128. Starting NFS mountd: OK
  129. Welcome to Buildroot for the Orange Pi PC By Red
  130. OrangePi_PC login: random: fast init done
  131. # uname -a
  132. Linux OrangePi_PC 5.4.65-g018b9c0bc2d5-dirty #6 SMP Sun Sep 10 20:28:01 CST 2023
  133. armv7l GNU/Linux
复制代码

手动修改 serial.c 文件如下:
  1. diff --git a/drivers/usb/gadget/legacy/serial.c
  2. b/drivers/usb/gadget/legacy/serial.c
  3. index de30d7628eef..bf7a75670c42 100644
  4. --- a/drivers/usb/gadget/legacy/serial.c
  5. +++ b/drivers/usb/gadget/legacy/serial.c
  6. @@ -21,7 +21,7 @@
  7. #define GS_VERSION_STR "v2.4"
  8. #define GS_VERSION_NUM 0x2400
  9. -#define GS_LONG_NAME "Gadget Serial"
  10. +#define GS_LONG_NAME "Gadget Serial Wow"
  11. #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
  12. /*-------------------------------------------------------------------------*/
  13. @@ -42,7 +42,7 @@ USB_GADGET_COMPOSITE_OPTIONS();
  14. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  15. static struct usb_string strings_dev[] = {
  16. - [USB_GADGET_MANUFACTURER_IDX].s = "",
  17. + [USB_GADGET_MANUFACTURER_IDX].s = "Red Inc",
  18. [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
  19. [USB_GADGET_SERIAL_IDX].s = "",
  20. [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
复制代码

进一步测试加载驱动的打印如下:
  1. [root@OrangePi_PC ~]# insmod g_serial.ko
  2. udc usbip-vudc.0: registering UDC driver [g_serial]
  3. g_serial gadget: adding 'acm'/776f8dd4 to config 'CDC ACM config'/a5c02f92
  4. g_serial gadget: acm ttyGS0: dual speed IN/ep1in OUT/ep1out NOTIFY/ep2in
  5. g_serial gadget: Gadget Serial Wow v2.4
  6. g_serial gadget: g_serial ready
复制代码

至此可以看出使用 qemu 调试 usb  gadget 驱动成为可能(备注:qemu 模拟的 origanpi-PC 和 PC 机测试使用
的 tftp 服务传输文件)。
回复

使用道具 举报

您需要登录后才可以回帖 登录

本版积分规则

Archiver|手机版|小黑屋|米尔科技论坛   

GMT+8, 2024-6-24 00:52 , Processed in 0.042100 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表