Skip to content

Commit 9adfced

Browse files
jlianadamshiervani
authored andcommitted
fix: make wakeup_on_write optional for backward compatibility
Move wakeup_on_write from attrs to optionalAttrs so it is written with IgnoreErrors=true. On kernels without rv1106-system#57, the configfs attribute does not exist and writing to it would fail, breaking the entire USB gadget initialization. With this change, wakeup_on_write is silently skipped on unpatched kernels while still being set on patched ones.
1 parent cfa2ea8 commit 9adfced

5 files changed

Lines changed: 37 additions & 2 deletions

File tree

internal/usbgadget/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ type gadgetConfigItem struct {
99
order uint
1010
device string
1111
path []string
12-
attrs gadgetAttributes
13-
configAttrs gadgetAttributes
12+
attrs gadgetAttributes
13+
optionalAttrs gadgetAttributes // written with IgnoreErrors; for kernel features that may not exist
14+
configAttrs gadgetAttributes
1415
configPath []string
1516
reportDesc []byte
1617
}

internal/usbgadget/config_tx.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ func (tx *UsbGadgetTransaction) writeGadgetItemConfig(item gadgetConfigItem, dep
221221
)...)
222222
}
223223

224+
if len(item.optionalAttrs) > 0 {
225+
// write optional attributes (IgnoreErrors=true for backward compat with older kernels)
226+
files = append(files, tx.writeGadgetAttrsOptional(
227+
gadgetItemPath,
228+
item.optionalAttrs,
229+
component,
230+
beforeChange,
231+
)...)
232+
}
233+
224234
// write report descriptor if available
225235
reportDescPath := path.Join(gadgetItemPath, "report_desc")
226236
if item.reportDesc != nil {
@@ -295,6 +305,24 @@ func (tx *UsbGadgetTransaction) writeGadgetAttrs(basePath string, attrs gadgetAt
295305
return files
296306
}
297307

308+
func (tx *UsbGadgetTransaction) writeGadgetAttrsOptional(basePath string, attrs gadgetAttributes, component string, beforeChange []string) (files []string) {
309+
files = make([]string, 0)
310+
for key, val := range attrs {
311+
filePath := filepath.Join(basePath, key)
312+
tx.addFileChange(component, RequestedFileChange{
313+
Path: filePath,
314+
ExpectedState: FileStateFileContentMatch,
315+
ExpectedContent: []byte(val),
316+
Description: "write optional gadget attribute",
317+
DependsOn: []string{basePath},
318+
BeforeChange: beforeChange,
319+
IgnoreErrors: true,
320+
})
321+
files = append(files, filePath)
322+
}
323+
return files
324+
}
325+
298326
func (tx *UsbGadgetTransaction) addReorderSymlinkChange(path string, target string, deps []string) {
299327
tx.log.Trace().Str("path", path).Str("target", target).Msg("add reorder symlink change")
300328

internal/usbgadget/hid_keyboard.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var keyboardConfig = gadgetConfigItem{
2323
"subclass": "1",
2424
"report_length": "8",
2525
"no_out_endpoint": "0",
26+
},
27+
optionalAttrs: gadgetAttributes{
2628
"wakeup_on_write": "1",
2729
},
2830
reportDesc: keyboardReportDesc,

internal/usbgadget/hid_mouse_absolute.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var absoluteMouseConfig = gadgetConfigItem{
1515
"subclass": "0",
1616
"report_length": "6",
1717
"no_out_endpoint": "1",
18+
},
19+
optionalAttrs: gadgetAttributes{
1820
"wakeup_on_write": "1",
1921
},
2022
reportDesc: absoluteMouseCombinedReportDesc,

internal/usbgadget/hid_mouse_relative.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var relativeMouseConfig = gadgetConfigItem{
1515
"subclass": "1",
1616
"report_length": "5",
1717
"no_out_endpoint": "1",
18+
},
19+
optionalAttrs: gadgetAttributes{
1820
"wakeup_on_write": "1",
1921
},
2022
reportDesc: relativeMouseCombinedReportDesc,

0 commit comments

Comments
 (0)