[未完] Linux 4.4 USB —— spiflash模拟usb大容量存储设备 调试记录 Gadget Mass Stroage
阅读原文时间:2023年07月11日阅读:3

linux 4.4 USB Gadget Mass Stroage

硬件平台: licheepi nano衍生

驱动信息

  │ This driver is a replacement for now removed File-backed                                                                                                                                                  │
  │ Storage Gadget (g_file_storage).                                                                                                                                                                          │
  │                                                                                                                                                                                                           │
  │ Say "y" to link the driver statically, or "m" to build                                                                                                                                                    │
  │ a dynamically linked module called "g_mass_storage".                                                                                                                                                      │
  │                                                                                                                                                                                                           │
  │ Symbol: USB_MASS_STORAGE [=m]                                                                                                                                                                             │
  │ Type  : tristate                                                                                                                                                                                          │
  │ Prompt: Mass Storage Gadget                                                                                                                                                                               │
  │   Location:                                                                                                                                                                                               │
  │     -> Device Drivers                                                                                                                                                                                     │
  │       -> USB support (USB_SUPPORT [=y])                                                                                                                                                                   │
  │         -> USB Gadget Support (USB_GADGET [=m])                                                                                                                                                           │
  │           -> USB Gadget precomposed configurations (<choice> [=m])                                                                                                                                        │
  │   Defined at drivers/usb/gadget/legacy/Kconfig:240                                                                                                                                                        │
  │   Depends on: <choice> && BLOCK [=y]                                                                                                                                                                      │
  │   Selects: USB_LIBCOMPOSITE [=m] && USB_F_MASS_STORAGE [=m]

config位置

[*] Device drivers
    [*] USB Support
        [M] USB Gadget Support
            [M] Mass Storage Gadget

dt node信息

// suniv.dtsi
usb_otg: usb@1c13000 {
        compatible = "allwinner,suniv-f1c100s-musb";
        reg = <0x01c13000 0x0400>;
        clocks = <&ccu CLK_BUS_OTG>;
        resets = <&ccu RST_BUS_OTG>;
        interrupts = <26>;
        interrupt-names = "mc";
        phys = <&usbphy 0>;
        phy-names = "usb";
        extcon = <&usbphy 0>;
        allwinner,sram = <&otg_sram 1>;
        status = "disabled";
};

// suniv-f1c100s-lichee-nano.dts
&usb_otg {
        dr_mode = "peripheral";
        status = "okay";
};

err log

[   40.564794] musb-sunxi 1c13000.usb: Invalid or missing 'dr_mode' property
[   40.578424] musb-sunxi: probe of 1c13000.usb failed with error -22

err log相关代码

// drivers/usb/musb/sunxi.c sunxi.ko
static int sunxi_musb_probe(struct platform_device *pdev)
{
        struct musb_hdrc_platform_data  pdata;
        struct platform_device_info     pinfo;
        struct sunxi_glue               *glue;
        struct device_node              *np = pdev->dev.of_node;
        int ret;

        if (!np) {
                dev_err(&pdev->dev, "Error no device tree node found\n");
                return -EINVAL;
        }

        glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
        if (!glue)
                return -ENOMEM;

        memset(&pdata, 0, sizeof(pdata));
        switch (usb_get_dr_mode(&pdev->dev)) {
#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
        case USB_DR_MODE_HOST:
                pdata.mode = MUSB_PORT_MODE_HOST;
                glue->phy_mode = PHY_MODE_USB_HOST;
                break;
#endif
#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_GADGET
        case USB_DR_MODE_PERIPHERAL:
                pdata.mode = MUSB_PORT_MODE_GADGET;
                glue->phy_mode = PHY_MODE_USB_DEVICE;
                break;
#endif
#ifdef CONFIG_USB_MUSB_DUAL_ROLE
        case USB_DR_MODE_OTG:
                pdata.mode = MUSB_PORT_MODE_DUAL_ROLE;
                glue->phy_mode = PHY_MODE_USB_OTG;
                break;
#endif
        default:
                dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
                return -EINVAL;
        }

查看 usb_get_dr_mode代码

dr_mode match代码

static const char *const usb_dr_modes[] = {
        [USB_DR_MODE_UNKNOWN]           = "",
        [USB_DR_MODE_HOST]              = "host",
        [USB_DR_MODE_PERIPHERAL]        = "peripheral",
        [USB_DR_MODE_OTG]               = "otg",
};

static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
{
        int ret;

        ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
        return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
}

enum usb_dr_mode usb_get_dr_mode(struct device *dev)
{
        const char *dr_mode;
        int err;

        err = device_property_read_string(dev, "dr_mode", &dr_mode);
        if (err < 0)
                return USB_DR_MODE_UNKNOWN;

        return usb_get_dr_mode_from_string(dr_mode);
}
EXPORT_SYMBOL_GPL(usb_get_dr_mode);

dt配置应该没问题

usb_get_dr_mode返回了0,导致sunxi.c return -EINVAL;

看起来没什么问题,需要修改代码进行调试。

修改打印usb dr mode序号,位于default中

[   40.909639] USB_DR_MODE: 2
[   40.915745] musb-sunxi 1c13000.usb: Invalid or missing 'dr_mode' property
[   40.929251] musb-sunxi: probe of 1c13000.usb failed with error -22

dr_mode序号是2 对应"peripheral",看来问题出在宏上

//CONFIG_USB_MUSB_DUAL_ROLE
//CONFIG_USB_MUSB_GADGET
#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_GADGET
        case USB_DR_MODE_PERIPHERAL:
                pdata.mode = MUSB_PORT_MODE_GADGET;
                glue->phy_mode = PHY_MODE_USB_DEVICE;
                break;
#endif

menuconfig中搜索相关宏

 Symbol: USB_MUSB_DUAL_ROLE [=n]                                                                                                                                                                           │
  │ Type  : boolean                                                                                                                                                                                           │
  │ Prompt: Dual Role mode                                                                                                                                                                                    │
  │   Location:                                                                                                                                                                                               │
  │     -> Device Drivers                                                                                                                                                                                     │
  │       -> USB support (USB_SUPPORT [=y])                                                                                                                                                                   │
  │         -> Inventra Highspeed Dual Role Controller (TI, ADI, AW, ...) (USB_MUSB_HDRC [=y])                                                                                                                │
  │ (1)       -> MUSB Mode Selection (<choice> [=y])                                                                                                                                                          │
  │   Defined at drivers/usb/musb/Kconfig:54                                                                                                                                                                  │
  │   Depends on: <choice> && (USB [=y]=y || USB [=y]=USB_MUSB_HDRC [=y]) && (USB_GADGET [=m]=y || USB_GADGET [=m]=USB_MUSB_HDRC [=y]) && HAS_DMA [=y]


 Symbol: USB_MUSB_GADGET [=n]                                                                                                                                                                              │
  │ Type  : boolean                                                                                                                                                                                           │
  │ Prompt: Gadget only mode                                                                                                                                                                                  │
  │   Location:                                                                                                                                                                                               │
  │     -> Device Drivers                                                                                                                                                                                     │
  │       -> USB support (USB_SUPPORT [=y])                                                                                                                                                                   │
  │         -> Inventra Highspeed Dual Role Controller (TI, ADI, AW, ...) (USB_MUSB_HDRC [=y])                                                                                                                │
  │ (1)       -> MUSB Mode Selection (<choice> [=y])                                                                                                                                                          │
  │   Defined at drivers/usb/musb/Kconfig:46                                                                                                                                                                  │
  │   Depends on: <choice> && (USB_GADGET [=m]=y || USB_GADGET [=m]=USB_MUSB_HDRC [=y]) && HAS_DMA [=y]

根据依赖关系可以得知,若要产生USB_MUSB_GADGET选项,需要满足

USB_GADGET [=m]=y || USB_GADGET [=m]=USB_MUSB_HDRC [=y]) && HAS_DMA [=y]

将CONFIG_USB_GADGET 设为y后,菜单中已经可以进行设置

dr_mode=host, peripheral, otg
( ) Host only mode
( ) Gadget only mode
(X) Dual Role mode