Skip to content

Commit

Permalink
Added 'zlp-send'
Browse files Browse the repository at this point in the history
This quirk, if enabled, sets LIBUSB_TRANSFER_ADD_ZERO_PACKET flag on
each outgoing transfer, which causes libusb to terminate outgoing
transfers that a multiple of the endpoint's packet size win an
extra zero length packet.

Note, there is actually no known device that actually requires this
quirk to be set. It is added just for for completeness.
  • Loading branch information
alexpevzner committed Dec 8, 2024
1 parent 165bf17 commit 2b88dca
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ipp-usb.8
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ Delay between subsequent requests\.
\fBusb\-max\-interfaces = N\fR
.br
Don't use more that N USB interfaces, even if more is available\.
.IP "\(bu" 4
\fBzlp\-send = true | false\fR
.br
Terminate outgoing transfers that a multiple of the endpoint's packet size win an extra zero length packet\.
.IP "" 0
.P
The DELAY parameter can be specified either as an unsigned integer (in milliseconds) or as a sequence of decimal numbers with an optional fraction and a unit suffix, such as "300ms," "0\.5s," or "2m30s\." Valid time units are "ns," "us" (or "µs"), "ms," "s," "m," and "h\."
Expand Down
4 changes: 4 additions & 0 deletions ipp-usb.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ The following parameters are defined:
* `usb-max-interfaces = N`<br>
Don't use more that N USB interfaces, even if more is available.

* `zlp-send = true | false`<br>
Terminate outgoing transfers that a multiple of the endpoint's
packet size win an extra zero length packet.

The DELAY parameter can be specified either as an unsigned integer (in
milliseconds) or as a sequence of decimal numbers with an optional
fraction and a unit suffix, such as "300ms," "0.5s," or "2m30s." Valid
Expand Down
9 changes: 9 additions & 0 deletions quirks.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
QuirkNmInitTimeout = "init-timeout"
QuirkNmRequestDelay = "request-delay"
QuirkNmUsbMaxInterfaces = "usb-max-interfaces"
QuirkNmZlpSend = "zlp-send"
)

// quirkParse maps quirk names into appropriate parsing methods,
Expand All @@ -58,6 +59,7 @@ var quirkParse = map[string]func(*Quirk) error{
QuirkNmInitTimeout: (*Quirk).parseDuration,
QuirkNmRequestDelay: (*Quirk).parseDuration,
QuirkNmUsbMaxInterfaces: (*Quirk).parseUint,
QuirkNmZlpSend: (*Quirk).parseBool,
}

// quirkDefaultStrings contains default values for quirks, in
Expand All @@ -72,6 +74,7 @@ var quirkDefaultStrings = map[string]string{
QuirkNmInitTimeout: DevInitTimeout.String(),
QuirkNmRequestDelay: "0",
QuirkNmUsbMaxInterfaces: "0",
QuirkNmZlpSend: "false",
}

// quirkDefault contains default values for quirks, precompiled.
Expand Down Expand Up @@ -344,6 +347,12 @@ func (quirks Quirks) GetUsbMaxInterfaces() uint {
return quirks.Get(QuirkNmUsbMaxInterfaces).Parsed.(uint)
}

// GetZlpSend returns effective "zlp-send" parameter,
// taking the whole set into consideration.
func (quirks Quirks) GetZlpSend() bool {
return quirks.Get(QuirkNmZlpSend).Parsed.(bool)
}

// QuirksSet represents collection of quirks
type QuirksSet []*Quirks

Expand Down
11 changes: 11 additions & 0 deletions quirks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ func TestQuirksLookup(t *testing.T) {
origin: "default",
},

{
model: "Unknown Device",
param: QuirkNmZlpSend,
get: func(quirks Quirks) interface{} {
return quirks.GetZlpSend()
},
match: "*",
value: false,
origin: "default",
},

// Quirks for some known devices
{
model: "HP ScanJet Pro 4500 fn1",
Expand Down
4 changes: 4 additions & 0 deletions usbio_libusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,10 @@ func (iface *UsbInterface) Send(ctx context.Context,
0,
)

if iface.quirks.GetZlpSend() {
xfer.flags |= C.LIBUSB_TRANSFER_ADD_ZERO_PACKET
}

// Submit transfer
rc := C.libusb_submit_transfer(xfer)
if rc < 0 {
Expand Down

0 comments on commit 2b88dca

Please sign in to comment.