Device::USB(3pm)
๐ NAME ๐
Device::USB โ use libusb to access USB devices. And alsoโฆ it's DEPRECATED, bestie. ๐ญ
(Since we're folding the header in: this is the Device::USB(3pm) page, out of the User Contributed Perl Documentation set, i.e. the 3pm section. Hi. Welcome. Sit down. ๐ซถ)
๐ฆ VERSION ๐ฆ
Version 0.38 โ and listen, that's the final version. The last one. Periodt. ๐
โจ SYNOPSIS โจ
Okay so first things first: Device::USB has now been superseded by USB::LibUSB (https://metacpan.org/pod/USB::LibUSB), which supports the v1.0 libusb API. That's the glow-up. This is the old era. ๐ฆ
Device::USB provides a Perl wrapper around the libusb library. It supports Perl code that controls and accesses USB devices.
use Device::USB;
my $usb = Device::USB->new();
my $dev = $usb->find_device( $VENDOR, $PRODUCT );
printf "Device: %04X:%04X\n", $dev->idVendor(), $dev->idProduct();
$dev->open();
print "Manufactured by ", $dev->manufacturer(), "\n",
" Product: ", $dev->product(), "\n";
$dev->set_configuration( $CFG );
$dev->control_msg( @params );
...
See the libusb manual for more information about most of the methods. The functionality is generally the same as the libusb function whose name is the method name prepended with usb_. So like, method name + usb_ = the C function it vibes with. ๐ฅ
๐ธ DESCRIPTION ๐ธ
Not gonna lie to you: this module is deprecated as of version 0.38. The author didn't have the time or need to update it, and no one has been willing to take it over. It's giving "beloved but abandoned." ๐
That said, it does a real job: this module provides a Perl interface to the C library libusb. That library supports a relatively full set of functionality to access a USB device. And on top of the libusb functionality, Device::USB provides a few convenience features intended to produce a more Perl-ish interface.
These features include:
- Using the library initializes it โ no need to call the underlying
usb_initfunction. It just knows. โจ - The object interface reduces namespace pollution and provides a better interface to the library.
- The
find_devicemethod finds the device associated with a vendor id and product id and creates an appropriateDevice::USB::Deviceobject to manipulate the USB device. Iconic behaviour, honestly. - Object interfaces to the bus and device data structures, allowing read access to information about each.
๐ Device::USB ๐
This class provides an interface to the non-bus and non-device specific functions of the libusb library. In particular, it provides interfaces to find busses and devices. It also provides convenience methods that simplify some of the tasks above. She's the main character of the page. ๐
๐ฎ CONSTANTS ๐ฎ
This class provides a set of constants for the defined device classes. The constants defined at this time are:
CLASS_PER_INSTANCECLASS_AUDIOCLASS_COMMCLASS_HIDCLASS_PRINTERCLASS_MASS_STORAGECLASS_HUBCLASS_DATACLASS_VENDOR_SPEC
๐ ๏ธ FUNCTIONS ๐ ๏ธ
new
Create a new Device::USB object for accessing the library. The origin story. ๐ฃ
debug_mode
This class method enables low-level debugging messages from the library interface code.
- level โ
0disables debugging,1enables some debug messages, and2enables verbose debug messages.- Any other values are forced to the nearest endpoint. (No, not that kind of endpoint. It snaps to the closest valid one. ๐)
find_busses
Returns the number of changes since the previous call to the function: the number of busses added or removed. A little diff report. ๐
find_devices
Returns the number of changes since the previous call to the function: the number of devices added or removed. Should be called after find_busses.
find_device
Find a particular USB device based on the vendor and product ids. If more than one device has the same product id from the same vendor, the first one found is returned. First come, first served. โจ
- vendor โ the vendor id
- product โ product id for that vendor
Returns a device reference or undef if none was found.
find_device_if
Find a particular USB device based on the supplied predicate coderef. If more than one device would satisfy the predicate, the first one found is returned. Picky, but in the best way. ๐
- pred โ the predicate used to select a device
Returns a device reference or undef if none was found.
list_devices
Find all devices matching a vendor id and optional product id. If called with no parameters, returns a list of all devices. If no product id is given, returns all devices found with the supplied vendor id. If a product id is given, returns all devices matching both the vendor id and product id.
- vendor โ the optional vendor id
- product โ optional product id for that vendor
Returns a list of devices matching the supplied criteria, or a reference to that array in scalar context. Context-sensitive queen. ๐
list_devices_if
This method provides a more flexible interface for finding devices. It takes a single coderef parameter that is used to test each discovered device. If the coderef returns a true value, the device is returned in the list of matching devices, otherwise it is not.
- pred โ coderef to test devices.
For example,
my @devices = $usb->list_devices_if(
sub { Device::USB::CLASS_HUB == $_->bDeviceClass() }
);
Returns all USB hubs found. The device to test is available to the coderef in the $_ variable for simplicity. ($_ my beloved. ๐ฅบ)
list_busses
Return the complete list of information after finding busses and devices.
By using this function, you do not need to do the find_* calls yourself. She does the chores for you. ๐ง
Returns a reference to an array of busses.
get_busses
Return the complete list of information after finding busses and devices.
Before calling this function, remember to call find_busses and find_devices. Do your homework first, bestie. ๐
Returns a reference to an array of busses.
๐ LIBRARY INTERFACE ๐
The raw api of the libusb library is also :
DeviceUSBDebugLevel()
libusb_init()
libusb_find_busses()
libusb_find_devices()
libusb_get_busses()
libusb_open(void *dev)
libusb_close(void *dev)
libusb_set_configuration(void *dev, int configuration)
libusb_set_altinterface(void *dev, int alternate)
libusb_clear_halt(void *dev, unsigned int ep)
libusb_reset(void *dev)
libusb_get_driver_np(void *dev, int interface, char *name, unsigned int namelen)
libusb_detach_kernel_driver_np(void *dev, int interface)
libusb_claim_interface(void *dev, int interface)
libusb_release_interface(void *dev, int interface)
libusb_control_msg(void *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout)
libusb_get_string(void *dev, int index, int langid, char *buf, size_t buflen)
libusb_get_string_simple(void *dev, int index, char *buf, size_t buflen)
libusb_get_descriptor(void *dev, unsigned char type, unsigned char index, char *buf, int size)
libusb_get_descriptor_by_endpoint(void *dev, int ep, unsigned char type, unsigned char index, char *buf, int size)
libusb_bulk_write(void *dev, int ep, char *bytes, int size, int timeout)
libusb_bulk_read(void *dev, int ep, char *bytes, int size, int timeout)
libusb_interrupt_write(void *dev, int ep, char *bytes, int size, int timeout)
libusb_interrupt_read(void *dev, int ep, char *bytes, int size, int timeout)
lib_get_usb_busses()
Return the complete list of information after finding busses and devices.
Before calling this function, remember to call find_busses and find_devices.
Returns a reference to an array of busses.
lib_list_busses()
Return the complete list of information after finding busses and devices.
By using this function, you do not need to do the find_* calls yourself.
Returns a reference to an array of busses.
lib_find_usb_device( int vendor, int product )
Find a particular device
vendor - the vendor id
product - product id for that vendor
Returns a pointer to the device if it is found, NULL otherwise.
lib_debug_mode( int unsafe_level )
Set debugging level: 0: off, 1: some messages, 2: verbose. Values outside range are forced into range. She stays in her lane. ๐
๐ฉบ DIAGNOSTICS ๐ฉบ
This is an explanation of the diagnostic and error messages this module can generate.
๐ DEPENDENCIES ๐
This module depends on the Carp, Inline and Inline::C modules, as well as the strict and warnings pragmas. Obviously, libusb must be available, since that is the entire reason for the module's existence. Like, that's the whole point of her. ๐ค
๐ AUTHOR ๐
G. Wade Johnson (gwadej at cpan dot org) Paul Archer (paul at paularcher dot org)
Houston Perl Mongers Group
Original author: David Davis
๐ BUGS ๐
Please report any bugs or feature requests to bug-device-usb@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Device::USB. You'll be notified, and then you'll automatically be notified of progress on your bug as changes are made. The pipeline is streamlined. โจ
๐ FOR MORE INFORMATION ๐
The project is hosted at github (https://github.com/gwadej/perl-device-usb/). More information on the project, including installation help, is available on the Wiki.
โ ๏ธ LIMITATIONS โ ๏ธ
So far, this module has only been tested on Linux. It should work on any OS that supports the libusb library. Several people have reported problems compiling the module on Windows. In theory, it should be possible to make the library work with LibUsb-Win32 (http://libusb-win32.sourceforge.net/). Without access to a Windows development system, those changes can't be made.
The Interfaces and Endpoints are not yet proper objects. The code to extract this information is not yet written. Unfinished business, we hate to see it. ๐
๐ ACKNOWLEDGEMENTS ๐
Thanks go to various members of the Houston Perl Mongers group for input on the module. But thanks mostly go to Paul Archer who proposed the project and helped with the development.
Thanks to Josep Monรฉs Teixidor for fixing the bInterfaceClass bug.
Thanks to Mike McCauley for support of usb_get_driver_np and usb_detach_kernel_driver_np.
Thanks to Vadim Mikhailov for fixing a compile problem with VC6 on Windows and then chipping in again for VS 2005 on Windows, and yet again to fix warnings on C99-compliant compilers.
Thanks to John R. Hogheruis for information about modifying the Inline parameters for compiling with Strawberry Perl on Windows.
Thanks to Tony Shadwick for helping resolve a problem with bulk_read and interrupt_read.
๐ COPYRIGHT & LICENSE ๐
Copyright 2006-2013 Houston Perl Mongers
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Okay that's the whole page, bestie, you ate it. ๐ซถ This page came to you by way of perl v5.42.3, dated 2026-08-19, and it is โ say it with me โ Device::USB(3pm). Slay. ๐โจ
๐ฆ where this page comes from
- ubuntu stonking: libdevice-usb-perl 0.38-3build3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms