|
49 | 49 |
|
50 | 50 | #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
|
51 | 51 |
|
| 52 | +/** |
| 53 | + * dwc3_get_dr_mode - Validates and sets dr_mode |
| 54 | + * @dwc: pointer to our context structure |
| 55 | + */ |
| 56 | +static int dwc3_get_dr_mode(struct dwc3 *dwc) |
| 57 | +{ |
| 58 | + enum usb_dr_mode mode; |
| 59 | + struct device *dev = dwc->dev; |
| 60 | + unsigned int hw_mode; |
| 61 | + |
| 62 | + if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) |
| 63 | + dwc->dr_mode = USB_DR_MODE_OTG; |
| 64 | + |
| 65 | + mode = dwc->dr_mode; |
| 66 | + hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0); |
| 67 | + |
| 68 | + switch (hw_mode) { |
| 69 | + case DWC3_GHWPARAMS0_MODE_GADGET: |
| 70 | + if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) { |
| 71 | + dev_err(dev, |
| 72 | + "Controller does not support host mode.\n"); |
| 73 | + return -EINVAL; |
| 74 | + } |
| 75 | + mode = USB_DR_MODE_PERIPHERAL; |
| 76 | + break; |
| 77 | + case DWC3_GHWPARAMS0_MODE_HOST: |
| 78 | + if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) { |
| 79 | + dev_err(dev, |
| 80 | + "Controller does not support device mode.\n"); |
| 81 | + return -EINVAL; |
| 82 | + } |
| 83 | + mode = USB_DR_MODE_HOST; |
| 84 | + break; |
| 85 | + default: |
| 86 | + if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) |
| 87 | + mode = USB_DR_MODE_HOST; |
| 88 | + else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) |
| 89 | + mode = USB_DR_MODE_PERIPHERAL; |
| 90 | + } |
| 91 | + |
| 92 | + if (mode != dwc->dr_mode) { |
| 93 | + dev_warn(dev, |
| 94 | + "Configuration mismatch. dr_mode forced to %s\n", |
| 95 | + mode == USB_DR_MODE_HOST ? "host" : "gadget"); |
| 96 | + |
| 97 | + dwc->dr_mode = mode; |
| 98 | + } |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
52 | 103 | void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
|
53 | 104 | {
|
54 | 105 | u32 reg;
|
@@ -1023,17 +1074,9 @@ static int dwc3_probe(struct platform_device *pdev)
|
1023 | 1074 | goto err2;
|
1024 | 1075 | }
|
1025 | 1076 |
|
1026 |
| - if (IS_ENABLED(CONFIG_USB_DWC3_HOST) && |
1027 |
| - (dwc->dr_mode == USB_DR_MODE_OTG || |
1028 |
| - dwc->dr_mode == USB_DR_MODE_UNKNOWN)) |
1029 |
| - dwc->dr_mode = USB_DR_MODE_HOST; |
1030 |
| - else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET) && |
1031 |
| - (dwc->dr_mode == USB_DR_MODE_OTG || |
1032 |
| - dwc->dr_mode == USB_DR_MODE_UNKNOWN)) |
1033 |
| - dwc->dr_mode = USB_DR_MODE_PERIPHERAL; |
1034 |
| - |
1035 |
| - if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) |
1036 |
| - dwc->dr_mode = USB_DR_MODE_OTG; |
| 1077 | + ret = dwc3_get_dr_mode(dwc); |
| 1078 | + if (ret) |
| 1079 | + goto err3; |
1037 | 1080 |
|
1038 | 1081 | ret = dwc3_alloc_scratch_buffers(dwc);
|
1039 | 1082 | if (ret)
|
|
0 commit comments