aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/karalabe/gousb/usb/device.go
blob: b4c4131fa6d33ee74103a628f720a1d248dd01cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2013 Google Inc.  All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package usb

/*
#ifndef OS_WINDOWS
    #include "os/threads_posix.h"
#endif
#include "libusbi.h"
#include "libusb.h"
*/
import "C"

import (
    "fmt"
    "reflect"
    "sync"
    "time"
    "unsafe"
)

var DefaultReadTimeout = 1 * time.Second
var DefaultWriteTimeout = 1 * time.Second
var DefaultControlTimeout = 250 * time.Millisecond //5 * time.Second

type Device struct {
    handle *C.libusb_device_handle

    // Embed the device information for easy access
    *Descriptor

    // Timeouts
    ReadTimeout    time.Duration
    WriteTimeout   time.Duration
    ControlTimeout time.Duration

    // Claimed interfaces
    lock    *sync.Mutex
    claimed map[uint8]int

    // Detached kernel interfaces
    detached map[uint8]int
}

func newDevice(handle *C.libusb_device_handle, desc *Descriptor) (*Device, error) {
    ifaces := 0
    d := &Device{
        handle:         handle,
        Descriptor:     desc,
        ReadTimeout:    DefaultReadTimeout,
        WriteTimeout:   DefaultWriteTimeout,
        ControlTimeout: DefaultControlTimeout,
        lock:           new(sync.Mutex),
        claimed:        make(map[uint8]int, ifaces),
        detached:       make(map[uint8]int),
    }

    if err := d.detachKernelDriver(); err != nil {
        d.Close()
        return nil, err
    }

    return d, nil
}

// detachKernelDriver detaches any active kernel drivers, if supported by the platform.
// If there are any errors, like Context.ListDevices, only the final one will be returned.
func (d *Device) detachKernelDriver() (err error) {
    for _, cfg := range d.Configs {
        for _, iface := range cfg.Interfaces {
            switch activeErr := C.libusb_kernel_driver_active(d.handle, C.int(iface.Number)); activeErr {
            case C.LIBUSB_ERROR_NOT_SUPPORTED:
                // no need to do any futher checking, no platform support
                return
            case 0:
                continue
            case 1:
                switch detachErr := C.libusb_detach_kernel_driver(d.handle, C.int(iface.Number)); detachErr {
                case C.LIBUSB_ERROR_NOT_SUPPORTED:
                    // shouldn't ever get here, should be caught by the outer switch
                    return
                case 0:
                    d.detached[iface.Number]++
                case C.LIBUSB_ERROR_NOT_FOUND:
                    // this status is returned if libusb's driver is already attached to the device
                    d.detached[iface.Number]++
                default:
                    err = fmt.Errorf("usb: detach kernel driver: %s", usbError(detachErr))
                }
            default:
                err = fmt.Errorf("usb: active kernel driver check: %s", usbError(activeErr))
            }
        }
    }

    return
}

// attachKernelDriver re-attaches kernel drivers to any previously detached interfaces, if supported by the platform.
// If there are any errors, like Context.ListDevices, only the final one will be returned.
func (d *Device) attachKernelDriver() (err error) {
    for iface := range d.detached {
        switch attachErr := C.libusb_attach_kernel_driver(d.handle, C.int(iface)); attachErr {
        case C.LIBUSB_ERROR_NOT_SUPPORTED:
            // no need to do any futher checking, no platform support
            return
        case 0:
            continue
        default:
            err = fmt.Errorf("usb: attach kernel driver: %s", usbError(attachErr))
        }
    }

    return
}

func (d *Device) Reset() error {
    if errno := C.libusb_reset_device(d.handle); errno != 0 {
        return usbError(errno)
    }
    return nil
}

func (d *Device) Control(rType, request uint8, val, idx uint16, data []byte) (int, error) {
    //log.Printf("control xfer: %d:%d/%d:%d %x", idx, rType, request, val, string(data))
    dataSlice := (*reflect.SliceHeader)(unsafe.Pointer(&data))
    n := C.libusb_control_transfer(
        d.handle,
        C.uint8_t(rType),
        C.uint8_t(request),
        C.uint16_t(val),
        C.uint16_t(idx),
        (*C.uchar)(unsafe.Pointer(dataSlice.Data)),
        C.uint16_t(len(data)),
        C.uint(d.ControlTimeout/time.Millisecond))
    if n < 0 {
        return int(n), usbError(n)
    }
    return int(n), nil
}

// ActiveConfig returns the config id (not the index) of the active configuration.
// This corresponds to the ConfigInfo.Config field.
func (d *Device) ActiveConfig() (uint8, error) {
    var cfg C.int
    if errno := C.libusb_get_configuration(d.handle, &cfg); errno < 0 {
        return 0, usbError(errno)
    }
    return uint8(cfg), nil
}

// SetConfig attempts to change the active configuration.
// The cfg provided is the config id (not the index) of the configuration to set,
// which corresponds to the ConfigInfo.Config field.
func (d *Device) SetConfig(cfg uint8) error {
    if errno := C.libusb_set_configuration(d.handle, C.int(cfg)); errno < 0 {
        return usbError(errno)
    }
    return nil
}

// Close the device.
func (d *Device) Close() error {
    if d.handle == nil {
        return fmt.Errorf("usb: double close on device")
    }
    d.lock.Lock()
    defer d.lock.Unlock()
    for iface := range d.claimed {
        C.libusb_release_interface(d.handle, C.int(iface))
    }
    d.attachKernelDriver()
    C.libusb_close(d.handle)
    d.handle = nil
    return nil
}

func (d *Device) OpenEndpoint(conf, iface, setup, epoint uint8) (Endpoint, error) {
    end := &endpoint{
        Device: d,
    }

    var setAlternate bool
    for _, c := range d.Configs {
        if c.Config != conf {
            continue
        }
        debug.Printf("found conf: %#v\n", c)
        for _, i := range c.Interfaces {
            if i.Number != iface {
                continue
            }
            debug.Printf("found iface: %#v\n", i)
            for i, s := range i.Setups {
                if s.Alternate != setup {
                    continue
                }
                setAlternate = i != 0

                debug.Printf("found setup: %#v [default: %v]\n", s, !setAlternate)
                for _, e := range s.Endpoints {
                    debug.Printf("ep %02x search: %#v\n", epoint, s)
                    if e.Address != epoint {
                        continue
                    }
                    end.InterfaceSetup = s
                    end.EndpointInfo = e
                    switch tt := TransferType(e.Attributes) & TRANSFER_TYPE_MASK; tt {
                    case TRANSFER_TYPE_BULK:
                        end.xfer = bulk_xfer
                    case TRANSFER_TYPE_INTERRUPT:
                        end.xfer = interrupt_xfer
                    case TRANSFER_TYPE_ISOCHRONOUS:
                        end.xfer = isochronous_xfer
                    default:
                        return nil, fmt.Errorf("usb: %s transfer is unsupported", tt)
                    }
                    goto found
                }
                return nil, fmt.Errorf("usb: unknown endpoint %02x", epoint)
            }
            return nil, fmt.Errorf("usb: unknown setup %02x", setup)
        }
        return nil, fmt.Errorf("usb: unknown interface %02x", iface)
    }
    return nil, fmt.Errorf("usb: unknown configuration %02x", conf)

found:

    // Set the configuration
    var activeConf C.int
    if errno := C.libusb_get_configuration(d.handle, &activeConf); errno < 0 {
        return nil, fmt.Errorf("usb: getcfg: %s", usbError(errno))
    }
    if int(activeConf) != int(conf) {
        if errno := C.libusb_set_configuration(d.handle, C.int(conf)); errno < 0 {
            return nil, fmt.Errorf("usb: setcfg: %s", usbError(errno))
        }
    }

    // Claim the interface
    if errno := C.libusb_claim_interface(d.handle, C.int(iface)); errno < 0 {
        return nil, fmt.Errorf("usb: claim: %s", usbError(errno))
    }

    // Increment the claim count
    d.lock.Lock()
    d.claimed[iface]++
    d.lock.Unlock() // unlock immediately because the next calls may block

    // Choose the alternate
    if setAlternate {
        if errno := C.libusb_set_interface_alt_setting(d.handle, C.int(iface), C.int(setup)); errno < 0 {
            debug.Printf("altsetting error: %s", usbError(errno))
            return nil, fmt.Errorf("usb: setalt: %s", usbError(errno))
        }
    }

    return end, nil
}

func (d *Device) GetStringDescriptor(desc_index int) (string, error) {

    // allocate 200-byte array limited the length of string descriptor
    goBuffer := make([]byte, 200)

    // get string descriptor from libusb. if errno < 0 then there are any errors.
    // if errno >= 0; it is a length of result string descriptor
    errno := C.libusb_get_string_descriptor_ascii(
        d.handle,
        C.uint8_t(desc_index),
        (*C.uchar)(unsafe.Pointer(&goBuffer[0])),
        200)

    // if any errors occur
    if errno < 0 {
        return "", fmt.Errorf("usb: getstr: %s", usbError(errno))
    }
    // convert slice of byte to string with limited length from errno
    stringDescriptor := string(goBuffer[:errno])

    return stringDescriptor, nil
}