| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Device::USB::Device; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require 5.006; |
|
4
|
19
|
|
|
19
|
|
121
|
use warnings; |
|
|
19
|
|
|
|
|
42
|
|
|
|
19
|
|
|
|
|
773
|
|
|
5
|
19
|
|
|
19
|
|
116
|
use strict; |
|
|
19
|
|
|
|
|
40
|
|
|
|
19
|
|
|
|
|
625
|
|
|
6
|
19
|
|
|
19
|
|
110
|
use Carp; |
|
|
19
|
|
|
|
|
41
|
|
|
|
19
|
|
|
|
|
2069
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
19
|
|
|
19
|
|
132
|
use constant MAX_BUFFER_SIZE => 256; |
|
|
19
|
|
|
|
|
35
|
|
|
|
19
|
|
|
|
|
57585
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 Device::USB::Device |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
This class encapsulates the USB device structure and the methods that may be |
|
13
|
|
|
|
|
|
|
applied to it. |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Device::USB::Device - Use libusb to access USB devices. |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 VERSION |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Version 0.36 |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=cut |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our $VERSION=0.36; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Device:USB::Device provides a Perl object for accessing a USB device |
|
31
|
|
|
|
|
|
|
using the libusb library. |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use Device::USB; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $usb = Device::USB->new(); |
|
36
|
|
|
|
|
|
|
my $dev = $usb->find_device( $VENDOR, $PRODUCT ); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
printf "Device: %04X:%04X\n", $dev->idVendor(), $dev->idProduct(); |
|
39
|
|
|
|
|
|
|
print "Manufactured by ", $dev->manufacturer(), "\n", |
|
40
|
|
|
|
|
|
|
" Product: ", $dev->product(), "\n"; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$dev->set_configuration( $CFG ); |
|
43
|
|
|
|
|
|
|
$dev->control_msg( @params ); |
|
44
|
|
|
|
|
|
|
... |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
See the libusb manual for more information about most of the methods. The |
|
47
|
|
|
|
|
|
|
functionality is generally the same as the libusb function whose name is |
|
48
|
|
|
|
|
|
|
the method name prepended with "usb_". |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This module defines a Perl object that represents the data and functionality |
|
53
|
|
|
|
|
|
|
associated with a USB device. The object interface provides read-only access |
|
54
|
|
|
|
|
|
|
to the important data associated with a device. It also provides methods for |
|
55
|
|
|
|
|
|
|
almost all of the functions supplied by libusb. Where necessary, the interfaces |
|
56
|
|
|
|
|
|
|
to these methods were changed to better match Perl usage. However, most of the |
|
57
|
|
|
|
|
|
|
methods are straight-forward wrappers around their libusb counterparts. |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 METHODS |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over 4 |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item DESTROY |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Close the device connected to the object. |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub DESTROY |
|
70
|
|
|
|
|
|
|
{ |
|
71
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
72
|
0
|
0
|
|
|
|
0
|
Device::USB::libusb_close( $self->{handle} ) if $self->{handle}; |
|
73
|
0
|
|
|
|
|
0
|
return; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Make certain the device is open. |
|
77
|
|
|
|
|
|
|
sub _assert_open |
|
78
|
|
|
|
|
|
|
{ |
|
79
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
80
|
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
0
|
if(!defined $self->{handle}) |
|
82
|
|
|
|
|
|
|
{ |
|
83
|
0
|
0
|
|
|
|
0
|
$self->open() or croak "Cannot open device: $!\n"; |
|
84
|
|
|
|
|
|
|
} |
|
85
|
0
|
|
|
|
|
0
|
return; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# I need to build a lot of accessors |
|
90
|
|
|
|
|
|
|
sub _make_descr_accessor |
|
91
|
|
|
|
|
|
|
{ |
|
92
|
228
|
|
|
228
|
|
361
|
my $name = shift; |
|
93
|
|
|
|
|
|
|
## no critic (ProhibitStringyEval) |
|
94
|
|
|
|
|
|
|
|
|
95
|
228
|
|
|
0
|
1
|
17495
|
return eval <<"EOE"; |
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
0
|
1
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub $name |
|
97
|
|
|
|
|
|
|
{ |
|
98
|
|
|
|
|
|
|
my \$self = shift; |
|
99
|
|
|
|
|
|
|
return \$self->{descriptor}->{$name}; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
EOE |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item filename |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Retrieve the filename associated with the device. |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub filename |
|
111
|
|
|
|
|
|
|
{ |
|
112
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
113
|
0
|
|
|
|
|
|
return $self->{filename}; |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item config |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
In list context, return a list of the configuration structures for this device. |
|
119
|
|
|
|
|
|
|
In scalar context, return a reference to that list. This method is deprecated |
|
120
|
|
|
|
|
|
|
in favor of the two new methods: configurations and get_configuration. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub config |
|
125
|
|
|
|
|
|
|
{ |
|
126
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
127
|
0
|
0
|
|
|
|
|
return wantarray ? @{$self->{config}} : $self->{config}; |
|
|
0
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item configurations |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
In list context, return a list of the configuration structures for this device. |
|
133
|
|
|
|
|
|
|
In scalar context, return a reference to that list. |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub configurations |
|
138
|
|
|
|
|
|
|
{ |
|
139
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
140
|
0
|
0
|
|
|
|
|
return wantarray ? @{$self->{config}} : $self->{config}; |
|
|
0
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item get_configuration |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Retrieve the configuration requested by index. The legal values are from 0 |
|
146
|
|
|
|
|
|
|
to bNumConfigurations() - 1. Negative values access from the back of the list |
|
147
|
|
|
|
|
|
|
of configurations. |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=over 4 |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item index numeric index of the index to return. If not supplied, use 0. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=back |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Returns an object encapsulating the configuration on success, or C on |
|
156
|
|
|
|
|
|
|
failure. |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub get_configuration |
|
161
|
|
|
|
|
|
|
{ |
|
162
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
163
|
0
|
|
0
|
|
|
|
my $index = shift || 0; |
|
164
|
0
|
|
|
|
|
|
return $self->configurations()->[$index]; |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item accessors |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
There a several accessor methods that return data from the device and device |
|
170
|
|
|
|
|
|
|
descriptor. Each is named after the field that they return. All of the BCD |
|
171
|
|
|
|
|
|
|
fields have been changed to floating point numbers, so that you don't have to |
|
172
|
|
|
|
|
|
|
decode them yourself. |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
The methods include: |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=over 4 |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item bcdUSB |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=item bDeviceClass |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item bDeviceSubClass |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item bDeviceProtocol |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item bMaxPacketSize0 |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item idVendor |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item idProduct |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item bcdDevice |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item iManufacturer |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item iProduct |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item iSerialNumber |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=item bNumConfigurations |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=back |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=cut |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
_make_descr_accessor( 'bcdUSB' ); |
|
207
|
|
|
|
|
|
|
_make_descr_accessor( 'bDeviceClass' ); |
|
208
|
|
|
|
|
|
|
_make_descr_accessor( 'bDeviceSubClass' ); |
|
209
|
|
|
|
|
|
|
_make_descr_accessor( 'bDeviceProtocol' ); |
|
210
|
|
|
|
|
|
|
_make_descr_accessor( 'bMaxPacketSize0' ); |
|
211
|
|
|
|
|
|
|
_make_descr_accessor( 'idVendor' ); |
|
212
|
|
|
|
|
|
|
_make_descr_accessor( 'idProduct' ); |
|
213
|
|
|
|
|
|
|
_make_descr_accessor( 'bcdDevice' ); |
|
214
|
|
|
|
|
|
|
_make_descr_accessor( 'iManufacturer' ); |
|
215
|
|
|
|
|
|
|
_make_descr_accessor( 'iProduct' ); |
|
216
|
|
|
|
|
|
|
_make_descr_accessor( 'iSerialNumber' ); |
|
217
|
|
|
|
|
|
|
_make_descr_accessor( 'bNumConfigurations' ); |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item manufacturer |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Retrieve the manufacture name from the device as a string. |
|
222
|
|
|
|
|
|
|
Return undef if the device read fails. |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=cut |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
sub manufacturer |
|
227
|
|
|
|
|
|
|
{ |
|
228
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
229
|
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
return $self->get_string_simple( $self->iManufacturer() ); |
|
231
|
|
|
|
|
|
|
} |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item product |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Retrieve the product name from the device as a string. |
|
236
|
|
|
|
|
|
|
Return undef if the device read fails. |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub product |
|
241
|
|
|
|
|
|
|
{ |
|
242
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
243
|
|
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
return $self->get_string_simple( $self->iProduct() ); |
|
245
|
|
|
|
|
|
|
} |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item serial_number |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Retrieve the serial number from the device as a string. |
|
250
|
|
|
|
|
|
|
Return undef if the device read fails. |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=cut |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub serial_number |
|
255
|
|
|
|
|
|
|
{ |
|
256
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
257
|
|
|
|
|
|
|
|
|
258
|
0
|
|
|
|
|
|
return $self->get_string_simple( $self->iSerialNumber() ); |
|
259
|
|
|
|
|
|
|
} |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item open |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
Open the device. If the device is already open, close it and reopen it. |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
If the device fails to open, the reason will be available in $!. |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=cut |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
sub open ## no critic (ProhibitBuiltinHomonyms) |
|
270
|
|
|
|
|
|
|
{ |
|
271
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
272
|
0
|
0
|
|
|
|
|
Device::USB::libusb_close( $self->{handle} ) if $self->{handle}; |
|
273
|
0
|
|
|
|
|
|
local $! = 0; |
|
274
|
0
|
|
|
|
|
|
$self->{handle} = Device::USB::libusb_open( $self->{device} ); |
|
275
|
|
|
|
|
|
|
|
|
276
|
0
|
|
|
|
|
|
return 0 == $!; |
|
277
|
|
|
|
|
|
|
} |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=item set_configuration |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
Sets the active configuration of the device. |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=over 4 |
|
284
|
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=item configuration |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
the integer specified in the descriptor field bConfigurationValue. |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=back |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
returns 0 on success or <0 on error |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
When using libusb-win32 under Windows, it is important to call |
|
294
|
|
|
|
|
|
|
C after the C but before any other method calls. |
|
295
|
|
|
|
|
|
|
Without this call, other methods may not work. This call is not required under |
|
296
|
|
|
|
|
|
|
Linux. |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=cut |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
sub set_configuration |
|
301
|
|
|
|
|
|
|
{ |
|
302
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
303
|
0
|
|
|
|
|
|
my $configuration = shift; |
|
304
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
305
|
|
|
|
|
|
|
|
|
306
|
0
|
|
|
|
|
|
return Device::USB::libusb_set_configuration( $self->{handle}, $configuration ); |
|
307
|
|
|
|
|
|
|
} |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=item set_altinterface |
|
310
|
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
Sets the active alternative setting of the current interface for the device. |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=over 4 |
|
314
|
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=item alternate |
|
316
|
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
the integer specified in the descriptor field bAlternateSetting. |
|
318
|
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=back |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
returns 0 on success or <0 on error |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=cut |
|
324
|
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
sub set_altinterface |
|
326
|
|
|
|
|
|
|
{ |
|
327
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
328
|
0
|
|
|
|
|
|
my $alternate = shift; |
|
329
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
330
|
|
|
|
|
|
|
|
|
331
|
0
|
|
|
|
|
|
return Device::USB::libusb_set_altinterface( $self->{handle}, $alternate ); |
|
332
|
|
|
|
|
|
|
} |
|
333
|
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
=item clear_halt |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
Clears any halt status on the supplied endpoint. |
|
337
|
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
=over 4 |
|
339
|
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
=item alternate |
|
341
|
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
the integer specified bEndpointAddress descriptor field. |
|
343
|
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
=back |
|
345
|
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
returns 0 on success or <0 on error |
|
347
|
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
=cut |
|
349
|
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
sub clear_halt |
|
351
|
|
|
|
|
|
|
{ |
|
352
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
353
|
0
|
|
|
|
|
|
my $ep = shift; |
|
354
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
355
|
|
|
|
|
|
|
|
|
356
|
0
|
|
|
|
|
|
return Device::USB::libusb_clear_halt( $self->{handle}, $ep ); |
|
357
|
|
|
|
|
|
|
} |
|
358
|
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=item reset |
|
360
|
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
Resets the device. This also closes the handle and invalidates this device. |
|
362
|
|
|
|
|
|
|
This device will be unusable. |
|
363
|
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
=cut |
|
365
|
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
sub reset ## no critic (ProhibitBuiltinHomonyms) |
|
367
|
|
|
|
|
|
|
{ |
|
368
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
369
|
|
|
|
|
|
|
|
|
370
|
0
|
0
|
|
|
|
|
return 0 unless defined $self->{handle}; |
|
371
|
|
|
|
|
|
|
|
|
372
|
0
|
|
|
|
|
|
my $ret = Device::USB::libusb_reset( $self->{handle} ); |
|
373
|
0
|
0
|
|
|
|
|
delete $self->{handle} unless $ret; |
|
374
|
|
|
|
|
|
|
|
|
375
|
0
|
|
|
|
|
|
return $ret; |
|
376
|
|
|
|
|
|
|
} |
|
377
|
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=item claim_interface |
|
379
|
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
Claims the specified interface with the operating system. |
|
381
|
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=over 4 |
|
383
|
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
=item interface |
|
385
|
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
The interface value listed in the descriptor field bInterfaceNumber. |
|
387
|
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
=back |
|
389
|
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
Returns 0 on success, <0 on failure. |
|
391
|
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
=cut |
|
393
|
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
sub claim_interface |
|
395
|
|
|
|
|
|
|
{ |
|
396
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
397
|
0
|
|
|
|
|
|
my $interface = shift; |
|
398
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
399
|
|
|
|
|
|
|
|
|
400
|
0
|
|
|
|
|
|
return Device::USB::libusb_claim_interface( $self->{handle}, $interface ); |
|
401
|
|
|
|
|
|
|
} |
|
402
|
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
=item release_interface |
|
404
|
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
Releases the specified interface back to the operating system. |
|
406
|
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
=over 4 |
|
408
|
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
=item interface |
|
410
|
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
The interface value listed in the descriptor field bInterfaceNumber. |
|
412
|
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
=back |
|
414
|
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
Returns 0 on success, <0 on failure. |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
=cut |
|
418
|
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
sub release_interface |
|
420
|
|
|
|
|
|
|
{ |
|
421
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
422
|
0
|
|
|
|
|
|
my $interface = shift; |
|
423
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
424
|
|
|
|
|
|
|
|
|
425
|
0
|
|
|
|
|
|
return Device::USB::libusb_release_interface( $self->{handle}, $interface ); |
|
426
|
|
|
|
|
|
|
} |
|
427
|
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
=item control_msg |
|
429
|
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
Performs a control request to the default control pipe on a device. |
|
431
|
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
=over 4 |
|
433
|
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
=item requesttype |
|
435
|
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
=item request |
|
437
|
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
=item value |
|
439
|
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
=item index |
|
441
|
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
=item bytes |
|
443
|
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
Any returned data is placed here. If you don't want any returned data, |
|
445
|
|
|
|
|
|
|
pass undef. |
|
446
|
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
=item size |
|
448
|
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
Size of supplied buffer. |
|
450
|
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=item timeout |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
Milliseconds to wait for response. |
|
454
|
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
=back |
|
456
|
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
Returns number of bytes read or written on success, <0 on failure. |
|
458
|
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
=cut |
|
460
|
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
sub control_msg |
|
462
|
|
|
|
|
|
|
{ |
|
463
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
464
|
|
|
|
|
|
|
## no critic (RequireArgUnpacking) |
|
465
|
0
|
|
|
|
|
|
my ($requesttype, $request, $value, $index, $bytes, $size, $timeout) = @_; |
|
466
|
0
|
0
|
|
|
|
|
$bytes = q{} unless defined $bytes; |
|
467
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
468
|
|
|
|
|
|
|
|
|
469
|
0
|
|
|
|
|
|
my ($retval, $out) = Device::USB::libusb_control_msg( |
|
470
|
|
|
|
|
|
|
$self->{handle}, $requesttype, $request, $value, |
|
471
|
|
|
|
|
|
|
$index, $bytes, $size, $timeout |
|
472
|
|
|
|
|
|
|
); |
|
473
|
|
|
|
|
|
|
# replace the input string in $bytes. |
|
474
|
0
|
0
|
|
|
|
|
$_[4] = $out if defined $_[4]; |
|
475
|
0
|
|
|
|
|
|
return $retval; |
|
476
|
|
|
|
|
|
|
} |
|
477
|
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
=item get_string |
|
479
|
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
Retrieve a string descriptor from the device. |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
=over 4 |
|
483
|
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
=item index |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
The index of the string in the string list. |
|
487
|
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
=item langid |
|
489
|
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
The language id used to specify which of the supported languages the string |
|
491
|
|
|
|
|
|
|
should be encoded in. |
|
492
|
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
=back |
|
494
|
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
Returns a Unicode string. The function returns undef on error. |
|
496
|
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
=cut |
|
498
|
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
sub get_string |
|
500
|
|
|
|
|
|
|
{ |
|
501
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
502
|
0
|
|
|
|
|
|
my $index = shift; |
|
503
|
0
|
|
|
|
|
|
my $langid = shift; |
|
504
|
|
|
|
|
|
|
|
|
505
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
506
|
|
|
|
|
|
|
|
|
507
|
0
|
|
|
|
|
|
my $buf = "\0" x MAX_BUFFER_SIZE; |
|
508
|
|
|
|
|
|
|
|
|
509
|
0
|
|
|
|
|
|
my $retlen = Device::USB::libusb_get_string( |
|
510
|
|
|
|
|
|
|
$self->{handle}, $index, $langid, $buf, MAX_BUFFER_SIZE |
|
511
|
|
|
|
|
|
|
); |
|
512
|
|
|
|
|
|
|
|
|
513
|
0
|
0
|
|
|
|
|
return if $retlen < 0; |
|
514
|
|
|
|
|
|
|
|
|
515
|
0
|
|
|
|
|
|
return substr( $buf, 0, $retlen ); |
|
516
|
|
|
|
|
|
|
} |
|
517
|
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
=item get_string_simple |
|
519
|
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
Retrieve a string descriptor from the device. |
|
521
|
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
=over 4 |
|
523
|
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
=item index |
|
525
|
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
The index of the string in the string list. |
|
527
|
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
=back |
|
529
|
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
Returns a C-style string if successful, or undef on error. |
|
531
|
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
=cut |
|
533
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
sub get_string_simple |
|
535
|
|
|
|
|
|
|
{ |
|
536
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
537
|
0
|
|
|
|
|
|
my $index = shift; |
|
538
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
539
|
|
|
|
|
|
|
|
|
540
|
0
|
|
|
|
|
|
my $buf = "\0" x MAX_BUFFER_SIZE; |
|
541
|
|
|
|
|
|
|
|
|
542
|
0
|
|
|
|
|
|
my $retlen = Device::USB::libusb_get_string_simple( |
|
543
|
|
|
|
|
|
|
$self->{handle}, $index, $buf, MAX_BUFFER_SIZE |
|
544
|
|
|
|
|
|
|
); |
|
545
|
|
|
|
|
|
|
|
|
546
|
0
|
0
|
|
|
|
|
return if $retlen < 0; |
|
547
|
|
|
|
|
|
|
|
|
548
|
0
|
|
|
|
|
|
return substr( $buf, 0, $retlen ); |
|
549
|
|
|
|
|
|
|
} |
|
550
|
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
=item get_descriptor |
|
552
|
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
Retrieve a descriptor from the device |
|
554
|
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
=over 4 |
|
556
|
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
=item type |
|
558
|
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
The type of descriptor to retrieve. |
|
560
|
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
=item index |
|
562
|
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
The index of that descriptor in the list of descriptors of that type. |
|
564
|
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
=back |
|
566
|
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
TODO: This method needs major rewrite to be Perl-ish. |
|
568
|
|
|
|
|
|
|
I need to provide a better way to specify the type (or at least document |
|
569
|
|
|
|
|
|
|
which are available), and I need to return a Perl data structure, not |
|
570
|
|
|
|
|
|
|
a buffer of binary data. |
|
571
|
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
=cut |
|
573
|
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
sub get_descriptor |
|
575
|
|
|
|
|
|
|
{ |
|
576
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
577
|
0
|
|
|
|
|
|
my $type = shift; |
|
578
|
0
|
|
|
|
|
|
my $index = shift; |
|
579
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
580
|
|
|
|
|
|
|
|
|
581
|
0
|
|
|
|
|
|
my $buf = "\0" x MAX_BUFFER_SIZE; |
|
582
|
|
|
|
|
|
|
|
|
583
|
0
|
|
|
|
|
|
my $retlen = Device::USB::libusb_get_descriptor( |
|
584
|
|
|
|
|
|
|
$self->{handle}, $type, $index, $buf, MAX_BUFFER_SIZE |
|
585
|
|
|
|
|
|
|
); |
|
586
|
|
|
|
|
|
|
|
|
587
|
0
|
0
|
|
|
|
|
return if $retlen < 0; |
|
588
|
|
|
|
|
|
|
|
|
589
|
0
|
|
|
|
|
|
return substr( $buf, 0, $retlen ); |
|
590
|
|
|
|
|
|
|
} |
|
591
|
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
=item get_descriptor_by_endpoint |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
Retrieve an endpoint-specific descriptor from the device |
|
595
|
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
=over 4 |
|
597
|
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
=item ep |
|
599
|
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
Endpoint to query. |
|
601
|
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
=item type |
|
603
|
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
The type of descriptor to retrieve. |
|
605
|
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
=item index |
|
607
|
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
The index of that descriptor in the list of descriptors. |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
=item buf |
|
611
|
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
Buffer into which to write the requested descriptor |
|
613
|
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
=item size |
|
615
|
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
Max size to read into the buffer. |
|
617
|
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
=back |
|
619
|
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
TODO: This method needs major rewrite to be Perl-ish. |
|
621
|
|
|
|
|
|
|
I need to provide a better way to specify the type (or at least document |
|
622
|
|
|
|
|
|
|
which are available), and I need to return a Perl data structure, not |
|
623
|
|
|
|
|
|
|
a buffer of binary data. |
|
624
|
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
=cut |
|
626
|
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
sub get_descriptor_by_endpoint |
|
628
|
|
|
|
|
|
|
{ |
|
629
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
630
|
0
|
|
|
|
|
|
my $ep = shift; |
|
631
|
0
|
|
|
|
|
|
my $type = shift; |
|
632
|
0
|
|
|
|
|
|
my $index = shift; |
|
633
|
|
|
|
|
|
|
|
|
634
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
635
|
|
|
|
|
|
|
|
|
636
|
0
|
|
|
|
|
|
my $buf = "\0" x MAX_BUFFER_SIZE; |
|
637
|
|
|
|
|
|
|
|
|
638
|
0
|
|
|
|
|
|
my $retlen = Device::USB::libusb_get_descriptor_by_endpoint( |
|
639
|
|
|
|
|
|
|
$self->{handle}, $ep, $type, $index, $buf, MAX_BUFFER_SIZE |
|
640
|
|
|
|
|
|
|
); |
|
641
|
|
|
|
|
|
|
|
|
642
|
0
|
0
|
|
|
|
|
return if $retlen < 0; |
|
643
|
|
|
|
|
|
|
|
|
644
|
0
|
|
|
|
|
|
return substr( $buf, 0, $retlen ); |
|
645
|
|
|
|
|
|
|
} |
|
646
|
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
=item bulk_read |
|
648
|
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
Perform a bulk read request from the specified endpoint. |
|
650
|
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
=over 4 |
|
652
|
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
=item ep |
|
654
|
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
The number of the endpoint to read |
|
656
|
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
=item bytes |
|
658
|
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
Buffer into which to write the requested data. |
|
660
|
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=item size |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
Max size to read into the buffer. |
|
664
|
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
=item timeout |
|
666
|
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
Maximum time to wait (in milliseconds) |
|
668
|
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
=back |
|
670
|
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
The function returns the number of bytes returned or <0 on error. |
|
672
|
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
USB is packet based, not stream based. So using C to read part |
|
674
|
|
|
|
|
|
|
of the packet acts like a I. The next time you read, all of the packet |
|
675
|
|
|
|
|
|
|
is still there. |
|
676
|
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
The data is only removed when you read the entire packet. For this reason, you |
|
678
|
|
|
|
|
|
|
should always call C with the total packet size. |
|
679
|
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
=cut |
|
681
|
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
sub bulk_read |
|
683
|
|
|
|
|
|
|
{ |
|
684
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
685
|
|
|
|
|
|
|
# Don't change to shifts, I need to write back to $bytes. |
|
686
|
0
|
|
|
|
|
|
my ($ep, $bytes, $size, $timeout) = @_; |
|
687
|
0
|
0
|
|
|
|
|
$bytes = q{} unless defined $bytes; |
|
688
|
|
|
|
|
|
|
|
|
689
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
690
|
|
|
|
|
|
|
|
|
691
|
0
|
0
|
|
|
|
|
if(length $bytes < $size) |
|
692
|
|
|
|
|
|
|
{ |
|
693
|
0
|
|
|
|
|
|
$bytes .= "\0" x ($size - length $bytes); |
|
694
|
|
|
|
|
|
|
} |
|
695
|
|
|
|
|
|
|
|
|
696
|
0
|
|
|
|
|
|
my $retlen = Device::USB::libusb_bulk_read( |
|
697
|
|
|
|
|
|
|
$self->{handle}, $ep, $bytes, $size, $timeout |
|
698
|
|
|
|
|
|
|
); |
|
699
|
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
# stick back in the bytes parameter. |
|
701
|
0
|
|
|
|
|
|
$_[1] = substr( $bytes, 0, $retlen ); |
|
702
|
|
|
|
|
|
|
|
|
703
|
0
|
|
|
|
|
|
return $retlen; |
|
704
|
|
|
|
|
|
|
} |
|
705
|
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
=item interrupt_read |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
Perform a interrupt read request from the specified endpoint. |
|
709
|
|
|
|
|
|
|
|
|
710
|
|
|
|
|
|
|
=over 4 |
|
711
|
|
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
=item ep |
|
713
|
|
|
|
|
|
|
|
|
714
|
|
|
|
|
|
|
The number of the endpoint to read |
|
715
|
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
=item bytes |
|
717
|
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
Buffer into which to write the requested data. |
|
719
|
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
=item size |
|
721
|
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
Max size to read into the buffer. |
|
723
|
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
=item timeout |
|
725
|
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
Maximum time to wait (in milliseconds) |
|
727
|
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
=back |
|
729
|
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
The function returns the number of bytes returned or <0 on error. |
|
731
|
|
|
|
|
|
|
|
|
732
|
|
|
|
|
|
|
=cut |
|
733
|
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
sub interrupt_read |
|
735
|
|
|
|
|
|
|
{ |
|
736
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
737
|
|
|
|
|
|
|
# Don't change to shifts, I need to write back to $bytes. |
|
738
|
0
|
|
|
|
|
|
my ($ep, $bytes, $size, $timeout) = @_; |
|
739
|
0
|
0
|
|
|
|
|
$bytes = q{} unless defined $bytes; |
|
740
|
|
|
|
|
|
|
|
|
741
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
742
|
|
|
|
|
|
|
|
|
743
|
0
|
0
|
|
|
|
|
if(length $bytes < $size) |
|
744
|
|
|
|
|
|
|
{ |
|
745
|
0
|
|
|
|
|
|
$bytes .= "\0" x ($size - length $bytes); |
|
746
|
|
|
|
|
|
|
} |
|
747
|
|
|
|
|
|
|
|
|
748
|
0
|
|
|
|
|
|
my $retlen = Device::USB::libusb_interrupt_read( |
|
749
|
|
|
|
|
|
|
$self->{handle}, $ep, $bytes, $size, $timeout |
|
750
|
|
|
|
|
|
|
); |
|
751
|
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
# stick back in the bytes parameter. |
|
753
|
0
|
|
|
|
|
|
$_[1] = substr( $bytes, 0, $retlen ); |
|
754
|
|
|
|
|
|
|
|
|
755
|
0
|
|
|
|
|
|
return $retlen; |
|
756
|
|
|
|
|
|
|
} |
|
757
|
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
=item bulk_write |
|
759
|
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
Perform a bulk write request to the specified endpoint. |
|
761
|
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
=over 4 |
|
763
|
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
=item ep |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
The number of the endpoint to write |
|
767
|
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
=item bytes |
|
769
|
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
Buffer from which to write the requested data. |
|
771
|
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
=item timeout |
|
773
|
|
|
|
|
|
|
|
|
774
|
|
|
|
|
|
|
Maximum time to wait (in milliseconds) |
|
775
|
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
=back |
|
777
|
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
The function returns the number of bytes written or <0 on error. |
|
779
|
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
=cut |
|
781
|
|
|
|
|
|
|
|
|
782
|
|
|
|
|
|
|
sub bulk_write |
|
783
|
|
|
|
|
|
|
{ |
|
784
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
785
|
0
|
|
|
|
|
|
my $ep = shift; |
|
786
|
0
|
|
|
|
|
|
my $bytes = shift; |
|
787
|
0
|
|
|
|
|
|
my $timeout = shift; |
|
788
|
|
|
|
|
|
|
|
|
789
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
790
|
|
|
|
|
|
|
|
|
791
|
0
|
|
|
|
|
|
return Device::USB::libusb_bulk_write( |
|
792
|
|
|
|
|
|
|
$self->{handle}, $ep, $bytes, length $bytes, $timeout |
|
793
|
|
|
|
|
|
|
); |
|
794
|
|
|
|
|
|
|
} |
|
795
|
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
=item interrupt_write |
|
797
|
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
Perform a interrupt write request to the specified endpoint. |
|
799
|
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
=over 4 |
|
801
|
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
=item ep |
|
803
|
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
The number of the endpoint to write |
|
805
|
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
=item bytes |
|
807
|
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
Buffer from which to write the requested data. |
|
809
|
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
=item timeout |
|
811
|
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
Maximum time to wait (in milliseconds) |
|
813
|
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
=back |
|
815
|
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
The function returns the number of bytes written or <0 on error. |
|
817
|
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
=cut |
|
819
|
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
sub interrupt_write |
|
821
|
|
|
|
|
|
|
{ |
|
822
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
823
|
0
|
|
|
|
|
|
my $ep = shift; |
|
824
|
0
|
|
|
|
|
|
my $bytes = shift; |
|
825
|
0
|
|
|
|
|
|
my $timeout = shift; |
|
826
|
|
|
|
|
|
|
|
|
827
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
828
|
|
|
|
|
|
|
|
|
829
|
0
|
|
|
|
|
|
return Device::USB::libusb_interrupt_write( |
|
830
|
|
|
|
|
|
|
$self->{handle}, $ep, $bytes, length $bytes, $timeout |
|
831
|
|
|
|
|
|
|
); |
|
832
|
|
|
|
|
|
|
} |
|
833
|
|
|
|
|
|
|
|
|
834
|
|
|
|
|
|
|
=item get_driver_np |
|
835
|
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
This function returns the name of the driver bound to the interface |
|
837
|
|
|
|
|
|
|
specified by the parameter interface. |
|
838
|
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
=over 4 |
|
840
|
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
=item $interface |
|
842
|
|
|
|
|
|
|
|
|
843
|
|
|
|
|
|
|
The interface number of interest. |
|
844
|
|
|
|
|
|
|
|
|
845
|
|
|
|
|
|
|
=back |
|
846
|
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
Returns C on error. |
|
848
|
|
|
|
|
|
|
|
|
849
|
|
|
|
|
|
|
=cut |
|
850
|
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
sub get_driver_np |
|
852
|
|
|
|
|
|
|
{ |
|
853
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
854
|
0
|
|
|
|
|
|
my $interface = shift; |
|
855
|
0
|
|
|
|
|
|
my $name = shift; |
|
856
|
|
|
|
|
|
|
|
|
857
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
858
|
|
|
|
|
|
|
|
|
859
|
0
|
|
|
|
|
|
my $buf = "\0" x MAX_BUFFER_SIZE; |
|
860
|
|
|
|
|
|
|
|
|
861
|
0
|
|
|
|
|
|
my $retlen = Device::USB::libusb_get_driver_np( |
|
862
|
|
|
|
|
|
|
$self->{handle}, $interface, $buf, MAX_BUFFER_SIZE |
|
863
|
|
|
|
|
|
|
); |
|
864
|
|
|
|
|
|
|
|
|
865
|
0
|
0
|
|
|
|
|
return if $retlen < 0; |
|
866
|
|
|
|
|
|
|
|
|
867
|
0
|
|
|
|
|
|
return substr( $buf, 0, $retlen ); |
|
868
|
|
|
|
|
|
|
} |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
=item detach_kernel_driver_np |
|
872
|
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
This function will detach a kernel driver from the interface specified by |
|
874
|
|
|
|
|
|
|
parameter interface. Applications using libusb can then try claiming the |
|
875
|
|
|
|
|
|
|
interface. Returns 0 on success or < 0 on error. |
|
876
|
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
=cut |
|
878
|
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
sub detach_kernel_driver_np |
|
880
|
|
|
|
|
|
|
{ |
|
881
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
882
|
0
|
|
|
|
|
|
my $interface = shift; |
|
883
|
0
|
|
|
|
|
|
$self->_assert_open(); |
|
884
|
|
|
|
|
|
|
|
|
885
|
0
|
|
|
|
|
|
return Device::USB::libusb_detach_kernel_driver_np( |
|
886
|
|
|
|
|
|
|
$self->{handle}, $interface |
|
887
|
|
|
|
|
|
|
); |
|
888
|
|
|
|
|
|
|
} |
|
889
|
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
=back |
|
891
|
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
=head1 DIAGNOSTICS |
|
893
|
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
This is an explanation of the diagnostic and error messages this module |
|
895
|
|
|
|
|
|
|
can generate. |
|
896
|
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
=over 4 |
|
898
|
|
|
|
|
|
|
|
|
899
|
|
|
|
|
|
|
=item Cannot open device: I |
|
900
|
|
|
|
|
|
|
|
|
901
|
|
|
|
|
|
|
Unable to open the USB device for the reason given. |
|
902
|
|
|
|
|
|
|
|
|
903
|
|
|
|
|
|
|
=back |
|
904
|
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
|
906
|
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
This module depends on the Carp, Inline and Inline::C modules, as well as |
|
908
|
|
|
|
|
|
|
the strict and warnings pragmas. Obviously, libusb must be available since |
|
909
|
|
|
|
|
|
|
that is the entire reason for the module's existence. |
|
910
|
|
|
|
|
|
|
|
|
911
|
|
|
|
|
|
|
=head1 AUTHOR |
|
912
|
|
|
|
|
|
|
|
|
913
|
|
|
|
|
|
|
G. Wade Johnson (gwadej at cpan dot org) |
|
914
|
|
|
|
|
|
|
Paul Archer (paul at paularcher dot org) |
|
915
|
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
Houston Perl Mongers Group |
|
917
|
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
=head1 BUGS |
|
919
|
|
|
|
|
|
|
|
|
920
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
|
921
|
|
|
|
|
|
|
C, or through the web interface at |
|
922
|
|
|
|
|
|
|
L. |
|
923
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
|
924
|
|
|
|
|
|
|
your bug as I make changes. |
|
925
|
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
927
|
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
Thanks go to various members of the Houston Perl Mongers group for input |
|
929
|
|
|
|
|
|
|
on the module. But thanks mostly go to Paul Archer who proposed the project |
|
930
|
|
|
|
|
|
|
and helped with the development. |
|
931
|
|
|
|
|
|
|
|
|
932
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
933
|
|
|
|
|
|
|
|
|
934
|
|
|
|
|
|
|
Copyright 2006-2013 Houston Perl Mongers |
|
935
|
|
|
|
|
|
|
|
|
936
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
937
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
938
|
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
=cut |
|
940
|
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
1; |