diff options
author | alfred <alfred@FreeBSD.org> | 2008-11-04 02:31:03 +0000 |
---|---|---|
committer | alfred <alfred@FreeBSD.org> | 2008-11-04 02:31:03 +0000 |
commit | effcf5d59c063e4380b3ca7098356d4bfa8b6633 (patch) | |
tree | f7c8289a089d1c56007a55463a568b1ccd445e5c /usr.sbin/usbconfig | |
parent | c472e6126ea8321c622e67625e038abef748b7d3 (diff) | |
download | FreeBSD-src-effcf5d59c063e4380b3ca7098356d4bfa8b6633.zip FreeBSD-src-effcf5d59c063e4380b3ca7098356d4bfa8b6633.tar.gz |
Bring in USB4BSD, Hans Petter Selasky rework of the USB stack
that includes significant features and SMP safety.
This commit includes a more or less complete rewrite of the *BSD USB
stack, including Host Controller and Device Controller drivers and
updating all existing USB drivers to use the new USB API:
1) A brief feature list:
- A new and mutex enabled USB API.
- Many USB drivers are now running Giant free.
- Linux USB kernel compatibility layer.
- New UGEN backend and libusb library, finally solves the "driver
unloading" problem. The new BSD licensed libusb20 library is fully
compatible with libusb-0.1.12 from sourceforge.
- New "usbconfig" utility, for easy configuration of USB.
- Full support for Split transactions, which means you can use your
full speed USB audio device on a high speed USB HUB.
- Full support for HS ISOC transactions, which makes writing drivers
for various HS webcams possible, for example.
- Full support for USB on embedded platforms, mostly cache flushing
and buffer invalidating stuff.
- Safer parsing of USB descriptors.
- Autodetect of annoying USB install disks.
- Support for USB device side mode, also called USB gadget mode,
using the same API like the USB host side. In other words the new
USB stack is symmetric with regard to host and device side.
- Support for USB transfers like I/O vectors, means more throughput
and less interrupts.
- ... see the FreeBSD quarterly status reports under "USB project"
2) To enable the driver in the default kernel build:
2.a) Remove all existing USB device options from your kernel config
file.
2.b) Add the following USB device options to your kernel configuration
file:
# USB core support
device usb2_core
# USB controller support
device usb2_controller
device usb2_controller_ehci
device usb2_controller_ohci
device usb2_controller_uhci
# USB mass storage support
device usb2_storage
device usb2_storage_mass
# USB ethernet support, requires miibus
device usb2_ethernet
device usb2_ethernet_aue
device usb2_ethernet_axe
device usb2_ethernet_cdce
device usb2_ethernet_cue
device usb2_ethernet_kue
device usb2_ethernet_rue
device usb2_ethernet_dav
# USB wireless LAN support
device usb2_wlan
device usb2_wlan_rum
device usb2_wlan_ral
device usb2_wlan_zyd
# USB serial device support
device usb2_serial
device usb2_serial_ark
device usb2_serial_bsa
device usb2_serial_bser
device usb2_serial_chcom
device usb2_serial_cycom
device usb2_serial_foma
device usb2_serial_ftdi
device usb2_serial_gensa
device usb2_serial_ipaq
device usb2_serial_lpt
device usb2_serial_mct
device usb2_serial_modem
device usb2_serial_moscom
device usb2_serial_plcom
device usb2_serial_visor
device usb2_serial_vscom
# USB bluetooth support
device usb2_bluetooth
device usb2_bluetooth_ng
# USB input device support
device usb2_input
device usb2_input_hid
device usb2_input_kbd
device usb2_input_ms
# USB sound and MIDI device support
device usb2_sound
2) To enable the driver at runtime:
2.a) Unload all existing USB modules. If USB is compiled into the
kernel then you might have to build a new kernel.
2.b) Load the "usb2_xxx.ko" modules under /boot/kernel having the same
base name like the kernel device option.
Submitted by: Hans Petter Selasky hselasky at c2i dot net
Reviewed by: imp, alfred
Diffstat (limited to 'usr.sbin/usbconfig')
-rw-r--r-- | usr.sbin/usbconfig/Makefile | 9 | ||||
-rw-r--r-- | usr.sbin/usbconfig/dump.c | 459 | ||||
-rw-r--r-- | usr.sbin/usbconfig/dump.h | 37 | ||||
-rw-r--r-- | usr.sbin/usbconfig/usbconfig.8 | 53 | ||||
-rw-r--r-- | usr.sbin/usbconfig/usbconfig.c | 683 |
5 files changed, 1241 insertions, 0 deletions
diff --git a/usr.sbin/usbconfig/Makefile b/usr.sbin/usbconfig/Makefile new file mode 100644 index 0000000..6356963 --- /dev/null +++ b/usr.sbin/usbconfig/Makefile @@ -0,0 +1,9 @@ +# +# $FreeBSD$ +# +PROG= usbconfig +MAN= usbconfig.8 +SRCS= usbconfig.c dump.c +LDADD+= -lusb20 + +.include <bsd.prog.mk> diff --git a/usr.sbin/usbconfig/dump.c b/usr.sbin/usbconfig/dump.c new file mode 100644 index 0000000..89a47ac --- /dev/null +++ b/usr.sbin/usbconfig/dump.c @@ -0,0 +1,459 @@ +/* $FreeBSD$ */ +/*- + * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <err.h> +#include <string.h> +#include <pwd.h> +#include <grp.h> +#include <ctype.h> + +#include <libusb20.h> +#include <libusb20_desc.h> + +#include "dump.h" + +#define DUMP0(n,type,field,...) dump_field(pdev, " ", #field, n->field); +#define DUMP1(n,type,field,...) dump_field(pdev, " ", #field, n->field); +#define DUMP2(n,type,field,...) dump_field(pdev, " ", #field, n->field); +#define DUMP3(n,type,field,...) dump_field(pdev, " ", #field, n->field); + +const char * +dump_mode(uint8_t value) +{ + if (value == LIBUSB20_MODE_HOST) + return ("HOST"); + return ("DEVICE"); +} + +const char * +dump_speed(uint8_t value) +{ + ; /* style fix */ + switch (value) { + case LIBUSB20_SPEED_LOW: + return ("LOW (1.5Mbps)"); + case LIBUSB20_SPEED_FULL: + return ("FULL (12Mbps)"); + case LIBUSB20_SPEED_HIGH: + return ("HIGH (480Mbps)"); + case LIBUSB20_SPEED_VARIABLE: + return ("VARIABLE (52-480Mbps)"); + case LIBUSB20_SPEED_SUPER: + return ("SUPER (4.8Gbps)"); + default: + break; + } + return ("unknown"); +} + +const char * +dump_power_mode(uint8_t value) +{ + ; /* style fix */ + switch (value) { + case LIBUSB20_POWER_OFF: + return ("OFF"); + case LIBUSB20_POWER_ON: + return ("ON"); + case LIBUSB20_POWER_SAVE: + return ("SAVE"); + case LIBUSB20_POWER_SUSPEND: + return ("SUSPEND"); + case LIBUSB20_POWER_RESUME: + return ("RESUME"); + default: + return ("UNKNOWN"); + } +} + +static void +dump_field(struct libusb20_device *pdev, const char *plevel, + const char *field, uint32_t value) +{ + struct LIBUSB20_CONTROL_SETUP_DECODED req; + uint16_t lang_id; + uint8_t index; + uint8_t temp_string[256]; + + printf("%s%s = 0x%04x ", plevel, field, value); + + if ((field[0] != 'i') || (field[1] == 'd')) { + printf("\n"); + return; + } + if (value == 0) { + printf(" <no string> \n"); + return; + } + LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req); + + lang_id = 0; + index = 0; + + req.bmRequestType = + LIBUSB20_REQUEST_TYPE_STANDARD | + LIBUSB20_RECIPIENT_DEVICE | + LIBUSB20_ENDPOINT_IN; + req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR; + req.wValue = (256 * LIBUSB20_DT_STRING) | index; + req.wIndex = lang_id; + req.wLength = 4; /* bytes */ + + if (libusb20_dev_request_sync(pdev, &req, + temp_string, NULL, 1000, 0)) { + goto done; + } + lang_id = temp_string[2] | (temp_string[3] << 8); + + printf(" LangId:0x%04x <", lang_id); + + index = value; + + req.wValue = (256 * LIBUSB20_DT_STRING) | index; + req.wIndex = lang_id; + req.wLength = 4; /* bytes */ + + if (libusb20_dev_request_sync(pdev, &req, + temp_string, NULL, 1000, 0)) { + printf("ERROR>\n"); + goto done; + } + req.wValue = (256 * LIBUSB20_DT_STRING) | index; + req.wIndex = lang_id; + req.wLength = temp_string[0]; /* bytes */ + + if (libusb20_dev_request_sync(pdev, &req, + temp_string, NULL, 1000, 0)) { + printf("ERROR>\n"); + goto done; + } + req.wLength /= 2; + + for (index = 1; index != req.wLength; index++) { + if (isprint(temp_string[(2 * index) + 0])) { + printf("%c", temp_string[(2 * index) + 0]); + } else if (isprint(temp_string[(2 * index) + 1])) { + printf("%c", temp_string[(2 * index) + 1]); + } else { + printf("?"); + } + } + printf(">\n"); +done: + return; +} + +static void +dump_extra(struct libusb20_me_struct *str, const char *plevel) +{ + const uint8_t *ptr; + uint8_t x; + + ptr = NULL; + + while ((ptr = libusb20_desc_foreach(str, ptr))) { + printf("\n" "%sAdditional Descriptor\n\n", plevel); + printf("%sbLength = 0x%02x\n", plevel, ptr[0]); + printf("%sbDescriptorType = 0x%02x\n", plevel, ptr[1]); + if (ptr[0] > 1) + printf("%sbDescriptorSubType = 0x%02x\n", + plevel, ptr[2]); + printf("%s RAW dump: ", plevel); + for (x = 0; x != ptr[0]; x++) { + if ((x % 8) == 0) { + printf("\n%s 0x%02x | ", plevel, x); + } + printf("0x%02x%s", ptr[x], + (x != (ptr[0] - 1)) ? ", " : (x % 8) ? "\n" : ""); + } + printf("\n"); + } + return; +} + +static void +dump_endpoint(struct libusb20_device *pdev, + struct libusb20_endpoint *ep) +{ + struct LIBUSB20_ENDPOINT_DESC_DECODED *edesc; + + edesc = &ep->desc; + LIBUSB20_ENDPOINT_DESC(DUMP3, edesc); + dump_extra(&ep->extra, " " " " " "); + return; +} + +static void +dump_iface(struct libusb20_device *pdev, + struct libusb20_interface *iface) +{ + struct LIBUSB20_INTERFACE_DESC_DECODED *idesc; + uint8_t z; + + idesc = &iface->desc; + LIBUSB20_INTERFACE_DESC(DUMP2, idesc); + dump_extra(&iface->extra, " " " " " "); + + for (z = 0; z != iface->num_endpoints; z++) { + printf("\n Endpoint %u\n", z); + dump_endpoint(pdev, iface->endpoints + z); + } + return; +} + +void +dump_device_info(struct libusb20_device *pdev) +{ + printf("%s, cfg=%u md=%s spd=%s pwr=%s\n", + libusb20_dev_get_desc(pdev), + libusb20_dev_get_config_index(pdev), + dump_mode(libusb20_dev_get_mode(pdev)), + dump_speed(libusb20_dev_get_speed(pdev)), + dump_power_mode(libusb20_dev_get_power_mode(pdev))); + return; +} + +void +dump_be_quirk_names(struct libusb20_backend *pbe) +{ + struct libusb20_quirk q; + uint16_t x; + int err; + + memset(&q, 0, sizeof(q)); + + printf("\nDumping list of supported quirks:\n\n"); + + for (x = 0; x != 0xFFFF; x++) { + + err = libusb20_be_get_quirk_name(pbe, x, &q); + if (err) { + if (x == 0) { + printf("No quirk names - maybe the USB quirk " + "module has not been loaded.\n"); + } + break; + } + if (strcmp(q.quirkname, "UQ_NONE")) + printf("%s\n", q.quirkname); + } + printf("\n"); + return; +} + +void +dump_be_dev_quirks(struct libusb20_backend *pbe) +{ + struct libusb20_quirk q; + uint16_t x; + int err; + + memset(&q, 0, sizeof(q)); + + printf("\nDumping current device quirks:\n\n"); + + for (x = 0; x != 0xFFFF; x++) { + + err = libusb20_be_get_dev_quirk(pbe, x, &q); + if (err) { + if (x == 0) { + printf("No device quirks - maybe the USB quirk " + "module has not been loaded.\n"); + } + break; + } + if (strcmp(q.quirkname, "UQ_NONE")) { + printf("VID=0x%04x PID=0x%04x REVLO=0x%04x " + "REVHI=0x%04x QUIRK=%s\n", + q.vid, q.pid, q.bcdDeviceLow, + q.bcdDeviceHigh, q.quirkname); + } + } + printf("\n"); + return; +} + +void +dump_be_access(struct libusb20_backend *pbe) +{ + struct group *gr; + struct passwd *pw; + const char *owner; + const char *group; + uid_t uid; + gid_t gid; + mode_t mode; + + if (libusb20_be_get_owner(pbe, &uid, &gid)) { + err(1, "could not get owner"); + } + if (libusb20_be_get_perm(pbe, &mode)) { + err(1, "could not get permission"); + } + owner = (pw = getpwuid(uid)) ? pw->pw_name : "UNKNOWN"; + group = (gr = getgrgid(gid)) ? gr->gr_name : "UNKNOWN"; + + if (mode || 1) { + printf("Global Access: %s:%s 0%o\n", owner, group, mode); + } else { + printf("Global Access: <not set>\n"); + } + return; +} + +void +dump_device_access(struct libusb20_device *pdev, uint8_t iface) +{ + struct group *gr; + struct passwd *pw; + const char *owner; + const char *group; + uid_t uid; + gid_t gid; + mode_t mode; + + if (libusb20_dev_get_owner(pdev, &uid, &gid)) { + err(1, "could not get owner"); + } + if (libusb20_dev_get_perm(pdev, &mode)) { + err(1, "could not get permission"); + } + if (mode) { + owner = (pw = getpwuid(uid)) ? pw->pw_name : "UNKNOWN"; + group = (gr = getgrgid(gid)) ? gr->gr_name : "UNKNOWN"; + + printf(" " "Device Access: %s:%s 0%o\n", owner, group, mode); + + } else { + printf(" " "Device Access: <not set>\n"); + } + + if (iface == 0xFF) { + for (iface = 0; iface != 0xFF; iface++) { + if (dump_device_iface_access(pdev, iface)) { + break; + } + } + } else { + if (dump_device_iface_access(pdev, iface)) { + err(1, "could not get interface access info"); + } + } + return; +} + +int +dump_device_iface_access(struct libusb20_device *pdev, uint8_t iface) +{ + struct group *gr; + struct passwd *pw; + const char *owner; + const char *group; + uid_t uid; + gid_t gid; + mode_t mode; + int error; + + if ((error = libusb20_dev_get_iface_owner(pdev, iface, &uid, &gid))) { + return (error); + } + if ((error = libusb20_dev_get_iface_perm(pdev, iface, &mode))) { + return (error); + } + if (mode) { + + owner = (pw = getpwuid(uid)) ? pw->pw_name : "UNKNOWN"; + group = (gr = getgrgid(gid)) ? gr->gr_name : "UNKNOWN"; + + printf(" " "Interface %u Access: %s:%s 0%o\n", iface, owner, group, mode); + } else { + printf(" " "Interface %u Access: <not set>\n", iface); + } + + return (0); +} + +void +dump_device_desc(struct libusb20_device *pdev) +{ + struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; + + ddesc = libusb20_dev_get_device_desc(pdev); + LIBUSB20_DEVICE_DESC(DUMP0, ddesc); + return; +} + +void +dump_config(struct libusb20_device *pdev, uint8_t all_cfg) +{ + struct LIBUSB20_CONFIG_DESC_DECODED *cdesc; + struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; + struct libusb20_config *pcfg = NULL; + uint8_t cfg_index; + uint8_t cfg_index_end; + uint8_t x; + uint8_t y; + + ddesc = libusb20_dev_get_device_desc(pdev); + + if (all_cfg) { + cfg_index = 0; + cfg_index_end = ddesc->bNumConfigurations; + } else { + cfg_index = libusb20_dev_get_config_index(pdev); + cfg_index_end = cfg_index + 1; + } + + for (; cfg_index != cfg_index_end; cfg_index++) { + + pcfg = libusb20_dev_alloc_config(pdev, cfg_index); + if (!pcfg) { + continue; + } + printf("\n Configuration index %u\n\n", cfg_index); + cdesc = &(pcfg->desc); + LIBUSB20_CONFIG_DESC(DUMP1, cdesc); + dump_extra(&(pcfg->extra), " " " "); + + for (x = 0; x != pcfg->num_interface; x++) { + printf("\n Interface %u\n", x); + dump_iface(pdev, pcfg->interface + x); + printf("\n"); + for (y = 0; y != (pcfg->interface + x)->num_altsetting; y++) { + printf("\n Interface %u Alt %u\n", x, y + 1); + dump_iface(pdev, + (pcfg->interface + x)->altsetting + y); + printf("\n"); + } + } + printf("\n"); + free(pcfg); + } + return; +} diff --git a/usr.sbin/usbconfig/dump.h b/usr.sbin/usbconfig/dump.h new file mode 100644 index 0000000..95ef720 --- /dev/null +++ b/usr.sbin/usbconfig/dump.h @@ -0,0 +1,37 @@ +/* $FreeBSD$ */ +/*- + * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +const char *dump_mode(uint8_t value); +const char *dump_speed(uint8_t value); +const char *dump_power_mode(uint8_t value); +void dump_device_info(struct libusb20_device *pdev); +void dump_be_access(struct libusb20_backend *pbe); +void dump_be_quirk_names(struct libusb20_backend *pbe); +void dump_be_dev_quirks(struct libusb20_backend *pbe); +void dump_device_access(struct libusb20_device *pdev, uint8_t iface); +int dump_device_iface_access(struct libusb20_device *pdev, uint8_t iface); +void dump_device_desc(struct libusb20_device *pdev); +void dump_config(struct libusb20_device *pdev, uint8_t all_cfg); diff --git a/usr.sbin/usbconfig/usbconfig.8 b/usr.sbin/usbconfig/usbconfig.8 new file mode 100644 index 0000000..ffdb4ce --- /dev/null +++ b/usr.sbin/usbconfig/usbconfig.8 @@ -0,0 +1,53 @@ +.\" $FreeBSD$ +.\" +.\" Copyright (c) 2008 Hans Petter Selasky. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd Sep 28, 2008 +.Dt USBCONFIG 8 +.Os +.Sh NAME +.Nm usbconfig +.Nd configure the USB subsystem +.Sh SYNOPSIS +.Nm +.Op Fl u Ar unit +.Op Fl a Ar addr +.Op cmds... +.Sh DESCRIPTION +The +.Nm +utility is used to configure and dump information about the USB subsystem. +.Pp +The options are as follows: +.Bl -tag -width " " +.It Fl u Ar unit +Limit device range to USB devices connected to the given USBUS unit. +.It Fl a Ar addr +Limit device range to the given USB device index. +Should only be used in conjunction with the unit argument. +.It Fl h +Show help and available commands. +.El +.Sh SEE ALSO +.Xr usb2_core 4 diff --git a/usr.sbin/usbconfig/usbconfig.c b/usr.sbin/usbconfig/usbconfig.c new file mode 100644 index 0000000..eb10b5f --- /dev/null +++ b/usr.sbin/usbconfig/usbconfig.c @@ -0,0 +1,683 @@ +/* $FreeBSD$ */ +/*- + * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <err.h> +#include <string.h> +#include <pwd.h> +#include <grp.h> +#include <errno.h> + +#include <libusb20_desc.h> +#include <libusb20.h> + +#include "dump.h" + +struct options { + const char *quirkname; + gid_t gid; + uid_t uid; + mode_t mode; + uint32_t got_any; + uint16_t bus; + uint16_t addr; + uint16_t iface; + uint16_t vid; + uint16_t pid; + uint16_t lo_rev; /* inclusive */ + uint16_t hi_rev; /* inclusive */ + uint8_t config_index; + uint8_t alt_index; + uint8_t got_list:1; + uint8_t got_bus:1; + uint8_t got_addr:1; + uint8_t got_iface:1; + uint8_t got_set_config:1; + uint8_t got_set_alt:1; + uint8_t got_set_owner:1; + uint8_t got_set_perm:1; + uint8_t got_suspend:1; + uint8_t got_resume:1; + uint8_t got_reset:1; + uint8_t got_power_off:1; + uint8_t got_power_save:1; + uint8_t got_power_on:1; + uint8_t got_dump_device_quirks:1; + uint8_t got_dump_quirk_names:1; + uint8_t got_dump_device_desc:1; + uint8_t got_dump_curr_config:1; + uint8_t got_dump_all_config:1; + uint8_t got_dump_info:1; + uint8_t got_dump_access:1; + uint8_t got_remove_device_quirk:1; + uint8_t got_add_device_quirk:1; +}; + +struct token { + const char *name; + uint8_t value; + uint8_t narg; +}; + +enum { + T_UNIT, + T_ADDR, + T_IFACE, + T_SET_CONFIG, + T_SET_ALT, + T_SET_OWNER, + T_SET_PERM, + T_ADD_DEVICE_QUIRK, + T_REMOVE_DEVICE_QUIRK, + T_DUMP_QUIRK_NAMES, + T_DUMP_DEVICE_QUIRKS, + T_DUMP_DEVICE_DESC, + T_DUMP_CURR_CONFIG_DESC, + T_DUMP_ALL_CONFIG_DESC, + T_DUMP_ACCESS, + T_DUMP_INFO, + T_SUSPEND, + T_RESUME, + T_POWER_OFF, + T_POWER_SAVE, + T_POWER_ON, + T_RESET, + T_LIST, +}; + +static struct options options; + +static const struct token token[] = { + {"-u", T_UNIT, 1}, + {"-a", T_ADDR, 1}, + {"-i", T_IFACE, 1}, + {"set_config", T_SET_CONFIG, 1}, + {"set_alt", T_SET_ALT, 1}, + {"set_owner", T_SET_OWNER, 1}, + {"set_perm", T_SET_PERM, 1}, + {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5}, + {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5}, + {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0}, + {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0}, + {"dump_device_desc", T_DUMP_DEVICE_DESC, 0}, + {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0}, + {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0}, + {"dump_access", T_DUMP_ACCESS, 0}, + {"dump_info", T_DUMP_INFO, 0}, + {"suspend", T_SUSPEND, 0}, + {"resume", T_RESUME, 0}, + {"power_off", T_POWER_OFF, 0}, + {"power_save", T_POWER_SAVE, 0}, + {"power_on", T_POWER_ON, 0}, + {"reset", T_RESET, 0}, + {"list", T_LIST, 0}, +}; + +static void +be_dev_remove_quirk(struct libusb20_backend *pbe, + uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev, + const char *str) +{ + struct libusb20_quirk q; + int err; + + memset(&q, 0, sizeof(q)); + + q.vid = vid; + q.pid = pid; + q.bcdDeviceLow = lorev; + q.bcdDeviceHigh = hirev; + strlcpy(q.quirkname, str, sizeof(q.quirkname)); + + err = libusb20_be_remove_dev_quirk(pbe, &q); + if (err) { + printf("Removing quirk '%s' failed, continuing.\n", str); + } + return; +} + +static void +be_dev_add_quirk(struct libusb20_backend *pbe, + uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev, + const char *str) +{ + struct libusb20_quirk q; + int err; + + memset(&q, 0, sizeof(q)); + + q.vid = vid; + q.pid = pid; + q.bcdDeviceLow = lorev; + q.bcdDeviceHigh = hirev; + strlcpy(q.quirkname, str, sizeof(q.quirkname)); + + err = libusb20_be_add_dev_quirk(pbe, &q); + if (err) { + printf("Adding quirk '%s' failed, continuing.\n", str); + } + return; +} + +static uint8_t +get_token(const char *str, uint8_t narg) +{ + uint8_t n; + + for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) { + if (strcasecmp(str, token[n].name) == 0) { + if (token[n].narg > narg) { + /* to little arguments */ + break; + } + return (token[n].value); + } + } + return (0 - 1); +} + +static uid_t +num_id(const char *name, const char *type) +{ + uid_t val; + char *ep; + + errno = 0; + val = strtoul(name, &ep, 0); + if (errno) { + err(1, "%s", name); + } + if (*ep != '\0') { + errx(1, "%s: illegal %s name", name, type); + } + return (val); +} + +static gid_t +a_gid(const char *s) +{ + struct group *gr; + + if (*s == '\0') { + /* empty group ID */ + return ((gid_t)-1); + } + return ((gr = getgrnam(s)) ? gr->gr_gid : num_id(s, "group")); +} + +static uid_t +a_uid(const char *s) +{ + struct passwd *pw; + + if (*s == '\0') { + /* empty user ID */ + return ((uid_t)-1); + } + return ((pw = getpwnam(s)) ? pw->pw_uid : num_id(s, "user")); +} + +static mode_t +a_mode(const char *s) +{ + uint16_t val; + char *ep; + + errno = 0; + val = strtoul(s, &ep, 8); + if (errno) { + err(1, "%s", s); + } + if (*ep != '\0') { + errx(1, "illegal permissions: %s", s); + } + return val; +} + +static void +usage(void) +{ + printf("" + "usbconfig - configure the USB subsystem" "\n" + "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n" + "commands:" "\n" + " set_config <num>" "\n" + " set_alt <altno>" "\n" + " set_owner <user:group>" "\n" + " set_perm <mode>" "\n" + " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n" + " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n" + " dump_quirk_names" "\n" + " dump_device_quirks" "\n" + " dump_device_desc" "\n" + " dump_curr_config_desc" "\n" + " dump_all_config_desc" "\n" + " dump_access" "\n" + " dump_info" "\n" + " suspend" "\n" + " resume" "\n" + " power_off" "\n" + " power_save" "\n" + " power_on" "\n" + " reset" "\n" + " list" "\n" + ); + exit(1); +} + +static void +reset_options(struct options *opt) +{ + memset(opt, 0, sizeof(*opt)); + return; +} + +static void +flush_command(struct libusb20_backend *pbe, struct options *opt) +{ + struct libusb20_device *pdev = NULL; + uint32_t matches = 0; + uint8_t dump_any; + + /* check for invalid option combinations */ + if ((opt->got_suspend + + opt->got_resume + + opt->got_reset + + opt->got_set_config + + opt->got_set_alt + + opt->got_power_save + + opt->got_power_on + + opt->got_power_off) > 1) { + err(1, "cannot only specify one of 'set_config', " + "'set_alt', 'reset', 'suspend', 'resume', " + "'power_save', 'power_on' and 'power_off' " + "at the same time!"); + } + if (opt->got_dump_access) { + opt->got_any--; + dump_be_access(pbe); + } + if (opt->got_dump_quirk_names) { + opt->got_any--; + dump_be_quirk_names(pbe); + } + if (opt->got_dump_device_quirks) { + opt->got_any--; + dump_be_dev_quirks(pbe); + } + if (opt->got_remove_device_quirk) { + opt->got_any--; + be_dev_remove_quirk(pbe, + opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname); + } + if (opt->got_add_device_quirk) { + opt->got_any--; + be_dev_add_quirk(pbe, + opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname); + } + if (opt->got_any == 0) { + /* + * do not scan through all the devices if there are no valid + * options + */ + goto done; + } + while ((pdev = libusb20_be_device_foreach(pbe, pdev))) { + + if (opt->got_bus && + (libusb20_dev_get_bus_number(pdev) != opt->bus)) { + continue; + } + if (opt->got_addr && + (libusb20_dev_get_address(pdev) != opt->addr)) { + continue; + } + matches++; + + /* do owner and permissions first */ + + if (opt->got_bus && opt->got_addr && opt->got_iface) { + if (opt->got_set_owner) { + if (libusb20_dev_set_iface_owner(pdev, + opt->iface, opt->uid, opt->gid)) { + err(1, "setting owner and group failed\n"); + } + } + if (opt->got_set_perm) { + if (libusb20_dev_set_iface_perm(pdev, + opt->iface, opt->mode)) { + err(1, "setting mode failed\n"); + } + } + } else if (opt->got_bus && opt->got_addr) { + if (opt->got_set_owner) { + if (libusb20_dev_set_owner(pdev, + opt->uid, opt->gid)) { + err(1, "setting owner and group failed\n"); + } + } + if (opt->got_set_perm) { + if (libusb20_dev_set_perm(pdev, + opt->mode)) { + err(1, "setting mode failed\n"); + } + } + } else if (opt->got_bus) { + if (opt->got_set_owner) { + if (libusb20_bus_set_owner(pbe, opt->bus, + opt->uid, opt->gid)) { + err(1, "setting owner and group failed\n"); + } + } + if (opt->got_set_perm) { + if (libusb20_bus_set_perm(pbe, opt->bus, + opt->mode)) { + err(1, "setting mode failed\n"); + } + } + } else { + if (opt->got_set_owner) { + if (libusb20_be_set_owner(pbe, + opt->uid, opt->gid)) { + err(1, "setting owner and group failed\n"); + } + } + if (opt->got_set_perm) { + if (libusb20_be_set_perm(pbe, + opt->mode)) { + err(1, "setting mode failed\n"); + } + } + } + + if (libusb20_dev_open(pdev, 0)) { + err(1, "could not open device"); + } + if (opt->got_set_config) { + if (libusb20_dev_set_config_index(pdev, + opt->config_index)) { + err(1, "could not set config index"); + } + } + if (opt->got_set_alt) { + if (libusb20_dev_set_alt_index(pdev, opt->iface, opt->alt_index)) { + err(1, "could not set alternate setting"); + } + } + if (opt->got_reset) { + if (libusb20_dev_reset(pdev)) { + err(1, "could not reset device"); + } + } + if (opt->got_suspend) { + if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_SUSPEND)) { + err(1, "could not set suspend"); + } + } + if (opt->got_resume) { + if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_RESUME)) { + err(1, "could not set resume"); + } + } + if (opt->got_power_off) { + if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_OFF)) { + err(1, "could not set power OFF"); + } + } + if (opt->got_power_save) { + if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_SAVE)) { + err(1, "could not set power SAVE"); + } + } + if (opt->got_power_on) { + if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_ON)) { + err(1, "could not set power ON"); + } + } + dump_any = + (opt->got_dump_device_desc || + opt->got_dump_curr_config || + opt->got_dump_all_config || + opt->got_dump_info || + opt->got_dump_access); + + if (opt->got_list || dump_any) { + dump_device_info(pdev); + } + if (opt->got_dump_access) { + printf("\n"); + dump_device_access(pdev, opt->got_iface ? + opt->iface : 0xFF); + } + if (opt->got_dump_device_desc) { + printf("\n"); + dump_device_desc(pdev); + } + if (opt->got_dump_all_config) { + printf("\n"); + dump_config(pdev, 1); + } else if (opt->got_dump_curr_config) { + printf("\n"); + dump_config(pdev, 0); + } + if (dump_any) { + printf("\n"); + } + if (libusb20_dev_close(pdev)) { + err(1, "could not close device"); + } + } + + if (matches == 0) { + printf("No device match\n"); + } +done: + reset_options(opt); + + return; +} + +int +main(int argc, char **argv) +{ + struct libusb20_backend *pbe; + struct options *opt = &options; + char *cp; + int n; + int t; + + if (argc < 1) { + usage(); + } + pbe = libusb20_be_alloc_default(); + if (pbe == NULL) + err(1, "could not access USB backend\n"); + + for (n = 1; n != argc; n++) { + + /* get number of additional options */ + t = (argc - n - 1); + if (t > 255) + t = 255; + switch (get_token(argv[n], t)) { + case T_ADD_DEVICE_QUIRK: + if (opt->got_add_device_quirk) { + flush_command(pbe, opt); + } + opt->vid = num_id(argv[n + 1], "Vendor ID"); + opt->pid = num_id(argv[n + 2], "Product ID"); + opt->lo_rev = num_id(argv[n + 3], "Low Revision"); + opt->hi_rev = num_id(argv[n + 4], "High Revision"); + opt->quirkname = argv[n + 5]; + n += 5; + + opt->got_add_device_quirk = 1; + opt->got_any++; + break; + + case T_REMOVE_DEVICE_QUIRK: + if (opt->got_remove_device_quirk) { + flush_command(pbe, opt); + } + opt->vid = num_id(argv[n + 1], "Vendor ID"); + opt->pid = num_id(argv[n + 2], "Product ID"); + opt->lo_rev = num_id(argv[n + 3], "Low Revision"); + opt->hi_rev = num_id(argv[n + 4], "High Revision"); + opt->quirkname = argv[n + 5]; + n += 5; + opt->got_remove_device_quirk = 1; + opt->got_any++; + break; + + case T_DUMP_QUIRK_NAMES: + opt->got_dump_quirk_names = 1; + opt->got_any++; + break; + + case T_DUMP_DEVICE_QUIRKS: + opt->got_dump_device_quirks = 1; + opt->got_any++; + break; + + case T_UNIT: + if (opt->got_any) { + /* allow multiple commands on the same line */ + flush_command(pbe, opt); + } + opt->bus = num_id(argv[n + 1], "busnum"); + opt->got_bus = 1; + n++; + break; + case T_ADDR: + opt->addr = num_id(argv[n + 1], "addr"); + opt->got_addr = 1; + n++; + break; + case T_IFACE: + opt->iface = num_id(argv[n + 1], "iface"); + opt->got_iface = 1; + n++; + break; + case T_SET_CONFIG: + opt->config_index = num_id(argv[n + 1], "confindex"); + opt->got_set_config = 1; + opt->got_any++; + n++; + break; + case T_SET_ALT: + opt->alt_index = num_id(argv[n + 1], "confindex"); + opt->got_set_alt = 1; + opt->got_any++; + n++; + break; + case T_SET_OWNER: + cp = argv[n + 1]; + cp = strchr(cp, ':'); + if (cp == NULL) { + err(1, "missing colon in '%s'!", argv[n + 1]); + } + *(cp++) = '\0'; + opt->gid = a_gid(cp); + opt->uid = a_uid(argv[n + 1]); + opt->got_set_owner = 1; + opt->got_any++; + n++; + break; + case T_SET_PERM: + opt->mode = a_mode(argv[n + 1]); + opt->got_set_perm = 1; + opt->got_any++; + n++; + break; + case T_DUMP_DEVICE_DESC: + opt->got_dump_device_desc = 1; + opt->got_any++; + break; + case T_DUMP_CURR_CONFIG_DESC: + opt->got_dump_curr_config = 1; + opt->got_any++; + break; + case T_DUMP_ALL_CONFIG_DESC: + opt->got_dump_all_config = 1; + opt->got_any++; + break; + case T_DUMP_INFO: + opt->got_dump_info = 1; + opt->got_any++; + break; + case T_DUMP_ACCESS: + opt->got_dump_access = 1; + opt->got_any++; + break; + case T_SUSPEND: + opt->got_suspend = 1; + opt->got_any++; + break; + case T_RESUME: + opt->got_resume = 1; + opt->got_any++; + break; + case T_POWER_OFF: + opt->got_power_off = 1; + opt->got_any++; + break; + case T_POWER_SAVE: + opt->got_power_save = 1; + opt->got_any++; + break; + case T_POWER_ON: + opt->got_power_on = 1; + opt->got_any++; + break; + case T_RESET: + opt->got_reset = 1; + opt->got_any++; + break; + case T_LIST: + opt->got_list = 1; + opt->got_any++; + break; + default: + usage(); + break; + } + } + if (opt->got_any) { + /* flush out last command */ + flush_command(pbe, opt); + } else { + /* list all the devices */ + opt->got_list = 1; + opt->got_any++; + flush_command(pbe, opt); + } + /* release data */ + libusb20_be_free(pbe); + + return (0); +} |