Author: trasz Date: Sat Jan 20 13:58:34 2018 New Revision: 328194 URL: https://svnweb.freebsd.org/changeset/base/328194
Log: Add sysctls to control device side USB identifiers. This makes it possible to change string and numeric vendor and product identifiers, as well as anything else there might be to change for a particular device side template, eg the MAC address. Reviewed by: hselasky@ MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D13920 Modified: head/share/man/man4/usb_template.4 head/sys/dev/usb/template/usb_template.c head/sys/dev/usb/template/usb_template.h head/sys/dev/usb/template/usb_template_audio.c head/sys/dev/usb/template/usb_template_cdce.c head/sys/dev/usb/template/usb_template_kbd.c head/sys/dev/usb/template/usb_template_midi.c head/sys/dev/usb/template/usb_template_modem.c head/sys/dev/usb/template/usb_template_mouse.c head/sys/dev/usb/template/usb_template_msc.c head/sys/dev/usb/template/usb_template_mtp.c head/sys/dev/usb/template/usb_template_phone.c head/sys/dev/usb/template/usb_template_serialnet.c Modified: head/share/man/man4/usb_template.4 ============================================================================== --- head/share/man/man4/usb_template.4 Sat Jan 20 12:31:21 2018 (r328193) +++ head/share/man/man4/usb_template.4 Sat Jan 20 13:58:34 2018 (r328194) @@ -61,11 +61,12 @@ descriptors. . USB templates are selected using the .Va hw.usb.template -sysctl and tunable. -. -The -.Va hw.usb.template -value can be changed at any time, but will not +sysctl and tunable, +or by using the +.Xr usbconfig 8 +.Cm set_template +subcommand. +The sysctl values can be changed at any time, but will not have any effect until the USB device has been re-enumerated. . Available templates are: @@ -83,10 +84,32 @@ Available templates are: .It Dv 9 Ta USB MIDI .El . +.Sh SYSCTL VARIABLES +The following variables are available as both +.Xr sysctl 8 +variables and +.Xr loader 8 +tunables: +.Bl -tag -width indent +.It Va hw.usb.template +Currently selected template. +.It Va hw.usb.templates.N +Configuration for template number +.Va N . +.It Va hw.usb.templates.N.vendor_id +16-bit vendor identifier (VID), usually assigned by USB-IF. +.It Va hw.usb.templates.N.product_id +16-bit product identifier (PID). +.It Va hw.usb.templates.N.manufacturer +String containing human-readable manufacturer name. +.It Va hw.usb.templates.N.product +String containing human-readable product name. +.El .Sh SEE ALSO .Xr cfumass 4 , .Xr usb 4 , -.Xr usfs 4 +.Xr usfs 4 , +.Xr usbconfig 8 .Sh STANDARDS The .Nm Modified: head/sys/dev/usb/template/usb_template.c ============================================================================== --- head/sys/dev/usb/template/usb_template.c Sat Jan 20 12:31:21 2018 (r328193) +++ head/sys/dev/usb/template/usb_template.c Sat Jan 20 13:58:34 2018 (r328194) @@ -65,6 +65,7 @@ #include <dev/usb/usb_busdma.h> #include <dev/usb/usb_process.h> #include <dev/usb/usb_device.h> +#include <dev/usb/usb_util.h> #define USB_DEBUG_VAR usb_debug #include <dev/usb/usb_debug.h> @@ -75,6 +76,9 @@ #include <dev/usb/template/usb_template.h> #endif /* USB_GLOBAL_INCLUDE_FILE */ +SYSCTL_NODE(_hw_usb, OID_AUTO, templates, CTLFLAG_RW, 0, + "USB device side templates"); + MODULE_DEPEND(usb_template, usb, 1, 1, 1); MODULE_VERSION(usb_template, 1); @@ -112,6 +116,50 @@ static usb_error_t usb_temp_get_desc(struct usb_device static usb_error_t usb_temp_setup_by_index(struct usb_device *, uint16_t index); static void usb_temp_init(void *); + +/*------------------------------------------------------------------------* + * usb_decode_str_desc + * + * Helper function to decode string descriptors into a C string. + *------------------------------------------------------------------------*/ +void +usb_decode_str_desc(struct usb_string_descriptor *sd, char *buf, size_t buflen) +{ + size_t i; + + for (i = 0; i < buflen - 1 && i < sd->bLength / 2; i++) + buf[i] = UGETW(sd->bString[i]); + + buf[i] = '\0'; +} + +/*------------------------------------------------------------------------* + * usb_temp_sysctl + * + * Callback for SYSCTL_PROC(9), to set and retrieve template string + * descriptors. + *------------------------------------------------------------------------*/ +int +usb_temp_sysctl(SYSCTL_HANDLER_ARGS) +{ + char buf[128]; + struct usb_string_descriptor *sd = arg1; + size_t len, sdlen = arg2; + int error; + + usb_decode_str_desc(sd, buf, sizeof(buf)); + + error = sysctl_handle_string(oidp, buf, sizeof(buf), req); + if (error != 0 || req->newptr == NULL) + return (error); + + len = usb_make_str_desc(sd, sdlen, buf); + if (len == 0) + return (EINVAL); + + return (0); +} + /*------------------------------------------------------------------------* * usb_make_raw_desc Modified: head/sys/dev/usb/template/usb_template.h ============================================================================== --- head/sys/dev/usb/template/usb_template.h Sat Jan 20 12:31:21 2018 (r328193) +++ head/sys/dev/usb/template/usb_template.h Sat Jan 20 13:58:34 2018 (r328194) @@ -100,19 +100,25 @@ struct usb_temp_data { /* prototypes */ -extern const struct usb_temp_device_desc usb_template_audio; -extern const struct usb_temp_device_desc usb_template_cdce; -extern const struct usb_temp_device_desc usb_template_kbd; -extern const struct usb_temp_device_desc usb_template_modem; -extern const struct usb_temp_device_desc usb_template_mouse; -extern const struct usb_temp_device_desc usb_template_msc; -extern const struct usb_temp_device_desc usb_template_mtp; -extern const struct usb_temp_device_desc usb_template_phone; -extern const struct usb_temp_device_desc usb_template_serialnet; -extern const struct usb_temp_device_desc usb_template_midi; +extern struct usb_temp_device_desc usb_template_audio; +extern struct usb_temp_device_desc usb_template_cdce; +extern struct usb_temp_device_desc usb_template_kbd; +extern struct usb_temp_device_desc usb_template_modem; +extern struct usb_temp_device_desc usb_template_mouse; +extern struct usb_temp_device_desc usb_template_msc; +extern struct usb_temp_device_desc usb_template_mtp; +extern struct usb_temp_device_desc usb_template_phone; +extern struct usb_temp_device_desc usb_template_serialnet; +extern struct usb_temp_device_desc usb_template_midi; + +void usb_decode_str_desc(struct usb_string_descriptor *sd, + char *buf, size_t buflen); usb_error_t usb_temp_setup(struct usb_device *, const struct usb_temp_device_desc *); -void usb_temp_unsetup(struct usb_device *); +void usb_temp_unsetup(struct usb_device *); +int usb_temp_sysctl(SYSCTL_HANDLER_ARGS); + +SYSCTL_DECL(_hw_usb_templates); #endif /* _USB_TEMPLATE_H_ */ Modified: head/sys/dev/usb/template/usb_template_audio.c ============================================================================== --- head/sys/dev/usb/template/usb_template_audio.c Sat Jan 20 12:31:21 2018 (r328193) +++ head/sys/dev/usb/template/usb_template_audio.c Sat Jan 20 13:58:34 2018 (r328194) @@ -2,8 +2,13 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * - * Copyright (c) 2010 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2010 Hans Petter Selasky + * Copyright (c) 2018 The FreeBSD Foundation + * All rights reserved. * + * Portions of this software were developed by Edward Tomasz Napierala + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -56,39 +61,33 @@ #include <dev/usb/usbdi.h> #include <dev/usb/usb_core.h> #include <dev/usb/usb_cdc.h> +#include <dev/usb/usb_ioctl.h> +#include <dev/usb/usb_util.h> #include <dev/usb/template/usb_template.h> #endif /* USB_GLOBAL_INCLUDE_FILE */ enum { - INDEX_AUDIO_LANG, - INDEX_AUDIO_MIXER, - INDEX_AUDIO_RECORD, - INDEX_AUDIO_PLAYBACK, - INDEX_AUDIO_PRODUCT, - INDEX_AUDIO_MAX, + AUDIO_LANG_INDEX, + AUDIO_MIXER_INDEX, + AUDIO_RECORD_INDEX, + AUDIO_PLAYBACK_INDEX, + AUDIO_PRODUCT_INDEX, + AUDIO_MAX_INDEX, }; -#define STRING_AUDIO_PRODUCT \ - "A\0u\0d\0i\0o\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e" +#define AUDIO_DEFAULT_PRODUCT "Audio Test Device" +#define AUDIO_DEFAULT_MIXER "Mixer interface" +#define AUDIO_DEFAULT_RECORD "Record interface" +#define AUDIO_DEFAULT_PLAYBACK "Playback interface" -#define STRING_AUDIO_MIXER \ - "M\0i\0x\0e\0r\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" +static struct usb_string_descriptor audio_mixer; +static struct usb_string_descriptor audio_record; +static struct usb_string_descriptor audio_playback; +static struct usb_string_descriptor audio_product; -#define STRING_AUDIO_RECORD \ - "R\0e\0c\0o\0r\0d\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" +static struct sysctl_ctx_list audio_ctx_list; -#define STRING_AUDIO_PLAYBACK \ - "P\0l\0a\0y\0b\0a\0c\0k\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" - - -/* make the real string descriptors */ - -USB_MAKE_STRING_DESC(STRING_AUDIO_MIXER, string_audio_mixer); -USB_MAKE_STRING_DESC(STRING_AUDIO_RECORD, string_audio_record); -USB_MAKE_STRING_DESC(STRING_AUDIO_PLAYBACK, string_audio_playback); -USB_MAKE_STRING_DESC(STRING_AUDIO_PRODUCT, string_audio_product); - /* prototypes */ /* @@ -204,7 +203,7 @@ static const struct usb_temp_interface_desc audio_ifac .bInterfaceClass = UICLASS_AUDIO, .bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL, .bInterfaceProtocol = 0, - .iInterface = INDEX_AUDIO_MIXER, + .iInterface = AUDIO_MIXER_INDEX, }; static const uint8_t audio_raw_desc_20[] = { @@ -262,7 +261,7 @@ static const struct usb_temp_interface_desc audio_ifac .bInterfaceClass = UICLASS_AUDIO, .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, .bInterfaceProtocol = 0, - .iInterface = INDEX_AUDIO_PLAYBACK, + .iInterface = AUDIO_PLAYBACK_INDEX, }; static const struct usb_temp_interface_desc audio_iface_1_alt_1 = { @@ -271,7 +270,7 @@ static const struct usb_temp_interface_desc audio_ifac .bInterfaceClass = UICLASS_AUDIO, .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, .bInterfaceProtocol = 0, - .iInterface = INDEX_AUDIO_PLAYBACK, + .iInterface = AUDIO_PLAYBACK_INDEX, .isAltInterface = 1, /* this is an alternate setting */ }; @@ -320,7 +319,7 @@ static const struct usb_temp_interface_desc audio_ifac .bInterfaceClass = UICLASS_AUDIO, .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, .bInterfaceProtocol = 0, - .iInterface = INDEX_AUDIO_RECORD, + .iInterface = AUDIO_RECORD_INDEX, }; static const struct usb_temp_interface_desc audio_iface_2_alt_1 = { @@ -329,7 +328,7 @@ static const struct usb_temp_interface_desc audio_ifac .bInterfaceClass = UICLASS_AUDIO, .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, .bInterfaceProtocol = 0, - .iInterface = INDEX_AUDIO_RECORD, + .iInterface = AUDIO_RECORD_INDEX, .isAltInterface = 1, /* this is an alternate setting */ }; @@ -346,7 +345,7 @@ static const struct usb_temp_config_desc audio_config_ .ppIfaceDesc = audio_interfaces, .bmAttributes = UC_BUS_POWERED, .bMaxPower = 25, /* 50 mA */ - .iConfiguration = INDEX_AUDIO_PRODUCT, + .iConfiguration = AUDIO_PRODUCT_INDEX, }; static const struct usb_temp_config_desc *audio_configs[] = { @@ -356,7 +355,7 @@ static const struct usb_temp_config_desc *audio_config static usb_temp_get_string_desc_t audio_get_string_desc; -const struct usb_temp_device_desc usb_template_audio = { +struct usb_temp_device_desc usb_template_audio = { .getStringDesc = &audio_get_string_desc, .ppConfigDesc = audio_configs, .idVendor = USB_TEMPLATE_VENDOR, @@ -365,9 +364,7 @@ const struct usb_temp_device_desc usb_template_audio = .bDeviceClass = UDCLASS_COMM, .bDeviceSubClass = 0, .bDeviceProtocol = 0, - .iManufacturer = 0, - .iProduct = INDEX_AUDIO_PRODUCT, - .iSerialNumber = 0, + .iProduct = AUDIO_PRODUCT_INDEX, }; /*------------------------------------------------------------------------* @@ -380,12 +377,12 @@ const struct usb_temp_device_desc usb_template_audio = static const void * audio_get_string_desc(uint16_t lang_id, uint8_t string_index) { - static const void *ptr[INDEX_AUDIO_MAX] = { - [INDEX_AUDIO_LANG] = &usb_string_lang_en, - [INDEX_AUDIO_MIXER] = &string_audio_mixer, - [INDEX_AUDIO_RECORD] = &string_audio_record, - [INDEX_AUDIO_PLAYBACK] = &string_audio_playback, - [INDEX_AUDIO_PRODUCT] = &string_audio_product, + static const void *ptr[AUDIO_MAX_INDEX] = { + [AUDIO_LANG_INDEX] = &usb_string_lang_en, + [AUDIO_MIXER_INDEX] = &audio_mixer, + [AUDIO_RECORD_INDEX] = &audio_record, + [AUDIO_PLAYBACK_INDEX] = &audio_playback, + [AUDIO_PRODUCT_INDEX] = &audio_product, }; if (string_index == 0) { @@ -394,8 +391,66 @@ audio_get_string_desc(uint16_t lang_id, uint8_t string if (lang_id != 0x0409) { return (NULL); } - if (string_index < INDEX_AUDIO_MAX) { + if (string_index < AUDIO_MAX_INDEX) { return (ptr[string_index]); } return (NULL); } + +static void +audio_init(void *arg __unused) +{ + struct sysctl_oid *parent; + char parent_name[3]; + + usb_make_str_desc(&audio_mixer, sizeof(audio_mixer), + AUDIO_DEFAULT_MIXER); + usb_make_str_desc(&audio_record, sizeof(audio_record), + AUDIO_DEFAULT_RECORD); + usb_make_str_desc(&audio_playback, sizeof(audio_playback), + AUDIO_DEFAULT_PLAYBACK); + usb_make_str_desc(&audio_product, sizeof(audio_product), + AUDIO_DEFAULT_PRODUCT); + + snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_AUDIO); + sysctl_ctx_init(&audio_ctx_list); + + parent = SYSCTL_ADD_NODE(&audio_ctx_list, + SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, + parent_name, CTLFLAG_RW, + 0, "USB Audio Interface device side template"); + SYSCTL_ADD_U16(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "vendor_id", CTLFLAG_RWTUN, &usb_template_audio.idVendor, + 1, "Vendor identifier"); + SYSCTL_ADD_U16(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "product_id", CTLFLAG_RWTUN, &usb_template_audio.idProduct, + 1, "Product identifier"); +#if 0 + SYSCTL_ADD_PROC(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "mixer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + &audio_mixer, sizeof(audio_mixer), usb_temp_sysctl, + "A", "Mixer interface string"); + SYSCTL_ADD_PROC(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "record", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + &audio_record, sizeof(audio_record), usb_temp_sysctl, + "A", "Record interface string"); + SYSCTL_ADD_PROC(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "playback", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + &audio_playback, sizeof(audio_playback), usb_temp_sysctl, + "A", "Playback interface string"); +#endif + SYSCTL_ADD_PROC(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + &audio_product, sizeof(audio_product), usb_temp_sysctl, + "A", "Product string"); +} + +static void +audio_uninit(void *arg __unused) +{ + + sysctl_ctx_free(&audio_ctx_list); +} + +SYSINIT(audio_init, SI_SUB_LOCK, SI_ORDER_FIRST, audio_init, NULL); +SYSUNINIT(audio_init, SI_SUB_LOCK, SI_ORDER_FIRST, audio_uninit, NULL); Modified: head/sys/dev/usb/template/usb_template_cdce.c ============================================================================== --- head/sys/dev/usb/template/usb_template_cdce.c Sat Jan 20 12:31:21 2018 (r328193) +++ head/sys/dev/usb/template/usb_template_cdce.c Sat Jan 20 13:58:34 2018 (r328194) @@ -3,8 +3,12 @@ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2007 Hans Petter Selasky <hsela...@freebsd.org> + * Copyright (c) 2018 The FreeBSD Foundation * All rights reserved. * + * Portions of this software were developed by Edward Tomasz Napierala + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -57,55 +61,42 @@ #include <dev/usb/usbdi.h> #include <dev/usb/usb_core.h> #include <dev/usb/usb_cdc.h> +#include <dev/usb/usb_ioctl.h> +#include <dev/usb/usb_util.h> #include <dev/usb/template/usb_template.h> #endif /* USB_GLOBAL_INCLUDE_FILE */ enum { - STRING_LANG_INDEX, - STRING_MAC_INDEX, - STRING_ETH_CONTROL_INDEX, - STRING_ETH_DATA_INDEX, - STRING_ETH_CONFIG_INDEX, - STRING_ETH_VENDOR_INDEX, - STRING_ETH_PRODUCT_INDEX, - STRING_ETH_SERIAL_INDEX, - STRING_ETH_MAX, + ETH_LANG_INDEX, + ETH_MAC_INDEX, + ETH_CONTROL_INDEX, + ETH_DATA_INDEX, + ETH_CONFIGURATION_INDEX, + ETH_MANUFACTURER_INDEX, + ETH_PRODUCT_INDEX, + ETH_SERIAL_NUMBER_INDEX, + ETH_MAX_INDEX, }; -#define STRING_MAC \ - "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B" +#define ETH_DEFAULT_MAC "2A02030405060789AB" +#define ETH_DEFAULT_CONTROL "USB Ethernet Comm Interface" +#define ETH_DEFAULT_DATA "USB Ethernet Data Interface" +#define ETH_DEFAULT_CONFIG "Default Config" +#define ETH_DEFAULT_MANUFACTURER "FreeBSD foundation" +#define ETH_DEFAULT_PRODUCT "USB Ethernet Adapter" +#define ETH_DEFAULT_SERIAL_NUMBER "December 2007" -#define STRING_ETH_CONTROL \ - "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \ - "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" +static struct usb_string_descriptor eth_mac; +static struct usb_string_descriptor eth_control; +static struct usb_string_descriptor eth_data; +static struct usb_string_descriptor eth_configuration; +static struct usb_string_descriptor eth_manufacturer; +static struct usb_string_descriptor eth_product; +static struct usb_string_descriptor eth_serial_number; -#define STRING_ETH_DATA \ - "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \ - "\0I\0n\0t\0e\0r\0f\0a\0c\0e" +static struct sysctl_ctx_list eth_ctx_list; -#define STRING_ETH_CONFIG \ - "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g" - -#define STRING_ETH_VENDOR \ - "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n" - -#define STRING_ETH_PRODUCT \ - "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0A\0d\0a\0p\0t\0e\0r" - -#define STRING_ETH_SERIAL \ - "D\0e\0c\0e\0m\0b\0e\0r\0 \0002\0000\0000\0007" - -/* make the real string descriptors */ - -USB_MAKE_STRING_DESC(STRING_MAC, string_mac); -USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control); -USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data); -USB_MAKE_STRING_DESC(STRING_ETH_CONFIG, string_eth_config); -USB_MAKE_STRING_DESC(STRING_ETH_VENDOR, string_eth_vendor); -USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT, string_eth_product); -USB_MAKE_STRING_DESC(STRING_ETH_SERIAL, string_eth_serial); - /* prototypes */ static usb_temp_get_string_desc_t eth_get_string_desc; @@ -130,7 +121,7 @@ static const struct usb_cdc_ethernet_descriptor eth_en .bLength = sizeof(eth_enf_desc), .bDescriptorType = UDESC_CS_INTERFACE, .bDescriptorSubtype = UDESCSUB_CDC_ENF, - .iMacAddress = STRING_MAC_INDEX, + .iMacAddress = ETH_MAC_INDEX, .bmEthernetStatistics = {0, 0, 0, 0}, .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */ .wNumberMCFilters = {0, 0}, @@ -191,7 +182,7 @@ static const struct usb_temp_interface_desc eth_contro .bInterfaceClass = UICLASS_CDC, .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, .bInterfaceProtocol = 0, - .iInterface = STRING_ETH_CONTROL_INDEX, + .iInterface = ETH_CONTROL_INDEX, }; static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = { @@ -205,7 +196,7 @@ static const struct usb_temp_interface_desc eth_data_n .bInterfaceClass = UICLASS_CDC_DATA, .bInterfaceSubClass = 0, .bInterfaceProtocol = 0, - .iInterface = STRING_ETH_DATA_INDEX, + .iInterface = ETH_DATA_INDEX, }; static const struct usb_temp_interface_desc eth_data_interface = { @@ -213,7 +204,7 @@ static const struct usb_temp_interface_desc eth_data_i .bInterfaceClass = UICLASS_CDC_DATA, .bInterfaceSubClass = UISUBCLASS_DATA, .bInterfaceProtocol = 0, - .iInterface = STRING_ETH_DATA_INDEX, + .iInterface = ETH_DATA_INDEX, .isAltInterface = 1, /* this is an alternate setting */ }; @@ -228,7 +219,7 @@ static const struct usb_temp_config_desc eth_config_de .ppIfaceDesc = eth_interfaces, .bmAttributes = UC_BUS_POWERED, .bMaxPower = 25, /* 50 mA */ - .iConfiguration = STRING_ETH_CONFIG_INDEX, + .iConfiguration = ETH_CONFIGURATION_INDEX, }; static const struct usb_temp_config_desc *eth_configs[] = { @@ -236,7 +227,7 @@ static const struct usb_temp_config_desc *eth_configs[ NULL, }; -const struct usb_temp_device_desc usb_template_cdce = { +struct usb_temp_device_desc usb_template_cdce = { .getStringDesc = ð_get_string_desc, .ppConfigDesc = eth_configs, .idVendor = USB_TEMPLATE_VENDOR, @@ -245,9 +236,9 @@ const struct usb_temp_device_desc usb_template_cdce = .bDeviceClass = UDCLASS_COMM, .bDeviceSubClass = 0, .bDeviceProtocol = 0, - .iManufacturer = STRING_ETH_VENDOR_INDEX, - .iProduct = STRING_ETH_PRODUCT_INDEX, - .iSerialNumber = STRING_ETH_SERIAL_INDEX, + .iManufacturer = ETH_MANUFACTURER_INDEX, + .iProduct = ETH_PRODUCT_INDEX, + .iSerialNumber = ETH_SERIAL_NUMBER_INDEX, }; /*------------------------------------------------------------------------* @@ -260,15 +251,15 @@ const struct usb_temp_device_desc usb_template_cdce = static const void * eth_get_string_desc(uint16_t lang_id, uint8_t string_index) { - static const void *ptr[STRING_ETH_MAX] = { - [STRING_LANG_INDEX] = &usb_string_lang_en, - [STRING_MAC_INDEX] = &string_mac, - [STRING_ETH_CONTROL_INDEX] = &string_eth_control, - [STRING_ETH_DATA_INDEX] = &string_eth_data, - [STRING_ETH_CONFIG_INDEX] = &string_eth_config, - [STRING_ETH_VENDOR_INDEX] = &string_eth_vendor, - [STRING_ETH_PRODUCT_INDEX] = &string_eth_product, - [STRING_ETH_SERIAL_INDEX] = &string_eth_serial, + static const void *ptr[ETH_MAX_INDEX] = { + [ETH_LANG_INDEX] = &usb_string_lang_en, + [ETH_MAC_INDEX] = ð_mac, + [ETH_CONTROL_INDEX] = ð_control, + [ETH_DATA_INDEX] = ð_data, + [ETH_CONFIGURATION_INDEX] = ð_configuration, + [ETH_MANUFACTURER_INDEX] = ð_manufacturer, + [ETH_PRODUCT_INDEX] = ð_product, + [ETH_SERIAL_NUMBER_INDEX] = ð_serial_number, }; if (string_index == 0) { @@ -277,8 +268,84 @@ eth_get_string_desc(uint16_t lang_id, uint8_t string_i if (lang_id != 0x0409) { return (NULL); } - if (string_index < STRING_ETH_MAX) { + if (string_index < ETH_MAX_INDEX) { return (ptr[string_index]); } return (NULL); } + +static void +eth_init(void *arg __unused) +{ + struct sysctl_oid *parent; + char parent_name[3]; + + usb_make_str_desc(ð_mac, sizeof(eth_mac), + ETH_DEFAULT_MAC); + usb_make_str_desc(ð_control, sizeof(eth_control), + ETH_DEFAULT_CONTROL); + usb_make_str_desc(ð_data, sizeof(eth_data), + ETH_DEFAULT_DATA); + usb_make_str_desc(ð_configuration, sizeof(eth_configuration), + ETH_DEFAULT_CONFIG); + usb_make_str_desc(ð_manufacturer, sizeof(eth_manufacturer), + ETH_DEFAULT_MANUFACTURER); + usb_make_str_desc(ð_product, sizeof(eth_product), + ETH_DEFAULT_PRODUCT); + usb_make_str_desc(ð_serial_number, sizeof(eth_serial_number), + ETH_DEFAULT_SERIAL_NUMBER); + + snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCE); + sysctl_ctx_init(ð_ctx_list); + + parent = SYSCTL_ADD_NODE(ð_ctx_list, + SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, + parent_name, CTLFLAG_RW, + 0, "USB CDC Ethernet device side template"); + SYSCTL_ADD_U16(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "vendor_id", CTLFLAG_RWTUN, + &usb_template_cdce.idVendor, 1, "Vendor identifier"); + SYSCTL_ADD_U16(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "product_id", CTLFLAG_RWTUN, + &usb_template_cdce.idProduct, 1, "Product identifier"); + SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "mac", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + ð_mac, sizeof(eth_mac), usb_temp_sysctl, + "A", "MAC address string"); +#if 0 + SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "control", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + ð_control, sizeof(eth_control), usb_temp_sysctl, + "A", "Control interface string"); + SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "data", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + ð_data, sizeof(eth_data), usb_temp_sysctl, + "A", "Data interface string"); + SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + ð_configuration, sizeof(eth_configuration), usb_temp_sysctl, + "A", "Configuration string"); +#endif + SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + ð_manufacturer, sizeof(eth_manufacturer), usb_temp_sysctl, + "A", "Manufacturer string"); + SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + ð_product, sizeof(eth_product), usb_temp_sysctl, + "A", "Product string"); + SYSCTL_ADD_PROC(ð_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + ð_serial_number, sizeof(eth_serial_number), usb_temp_sysctl, + "A", "Serial number string"); +} + +static void +eth_uninit(void *arg __unused) +{ + + sysctl_ctx_free(ð_ctx_list); +} + +SYSINIT(eth_init, SI_SUB_LOCK, SI_ORDER_FIRST, eth_init, NULL); +SYSUNINIT(eth_init, SI_SUB_LOCK, SI_ORDER_FIRST, eth_uninit, NULL); Modified: head/sys/dev/usb/template/usb_template_kbd.c ============================================================================== --- head/sys/dev/usb/template/usb_template_kbd.c Sat Jan 20 12:31:21 2018 (r328193) +++ head/sys/dev/usb/template/usb_template_kbd.c Sat Jan 20 13:58:34 2018 (r328194) @@ -2,8 +2,13 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * - * Copyright (c) 2010 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2010 Hans Petter Selasky + * Copyright (c) 2018 The FreeBSD Foundation + * All rights reserved. * + * Portions of this software were developed by Edward Tomasz Napierala + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -56,28 +61,27 @@ #include <dev/usb/usbdi.h> #include <dev/usb/usb_core.h> #include <dev/usb/usb_cdc.h> +#include <dev/usb/usb_ioctl.h> +#include <dev/usb/usb_util.h> #include <dev/usb/template/usb_template.h> #endif /* USB_GLOBAL_INCLUDE_FILE */ enum { - INDEX_LANG, - INDEX_KEYBOARD, - INDEX_PRODUCT, - INDEX_MAX, + KBD_LANG_INDEX, + KBD_INTERFACE_INDEX, + KBD_PRODUCT_INDEX, + KBD_MAX_INDEX, }; -#define STRING_PRODUCT \ - "K\0e\0y\0b\0o\0a\0r\0d\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e" +#define KBD_DEFAULT_INTERFACE "Keyboard Interface" +#define KBD_DEFAULT_PRODUCT "Keyboard Test Device" -#define STRING_KEYBOARD \ - "K\0e\0y\0b\0o\0a\0r\0d\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" +static struct usb_string_descriptor kbd_interface; +static struct usb_string_descriptor kbd_product; -/* make the real string descriptors */ +static struct sysctl_ctx_list kbd_ctx_list; -USB_MAKE_STRING_DESC(STRING_KEYBOARD, string_keyboard); -USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product); - /* prototypes */ static const struct usb_temp_packet_size keyboard_intr_mps = { @@ -135,7 +139,7 @@ static const struct usb_temp_interface_desc keyboard_i .bInterfaceClass = UICLASS_HID, .bInterfaceSubClass = UISUBCLASS_BOOT, .bInterfaceProtocol = UIPROTO_BOOT_KEYBOARD, - .iInterface = INDEX_KEYBOARD, + .iInterface = KBD_INTERFACE_INDEX, }; static const struct usb_temp_interface_desc *keyboard_interfaces[] = { @@ -147,7 +151,7 @@ static const struct usb_temp_config_desc keyboard_conf .ppIfaceDesc = keyboard_interfaces, .bmAttributes = UC_BUS_POWERED, .bMaxPower = 25, /* 50 mA */ - .iConfiguration = INDEX_PRODUCT, + .iConfiguration = KBD_PRODUCT_INDEX, }; static const struct usb_temp_config_desc *keyboard_configs[] = { @@ -158,7 +162,7 @@ static const struct usb_temp_config_desc *keyboard_con static usb_temp_get_string_desc_t keyboard_get_string_desc; static usb_temp_get_vendor_desc_t keyboard_get_vendor_desc; -const struct usb_temp_device_desc usb_template_kbd = { +struct usb_temp_device_desc usb_template_kbd = { .getStringDesc = &keyboard_get_string_desc, .getVendorDesc = &keyboard_get_vendor_desc, .ppConfigDesc = keyboard_configs, @@ -169,7 +173,7 @@ const struct usb_temp_device_desc usb_template_kbd = { .bDeviceSubClass = 0, .bDeviceProtocol = 0, .iManufacturer = 0, - .iProduct = INDEX_PRODUCT, + .iProduct = KBD_PRODUCT_INDEX, .iSerialNumber = 0, }; @@ -203,10 +207,10 @@ keyboard_get_vendor_desc(const struct usb_device_reque static const void * keyboard_get_string_desc(uint16_t lang_id, uint8_t string_index) { - static const void *ptr[INDEX_MAX] = { - [INDEX_LANG] = &usb_string_lang_en, - [INDEX_KEYBOARD] = &string_keyboard, - [INDEX_PRODUCT] = &string_product, + static const void *ptr[KBD_MAX_INDEX] = { + [KBD_LANG_INDEX] = &usb_string_lang_en, + [KBD_INTERFACE_INDEX] = &kbd_interface, + [KBD_PRODUCT_INDEX] = &kbd_product, }; if (string_index == 0) { @@ -215,8 +219,54 @@ keyboard_get_string_desc(uint16_t lang_id, uint8_t str if (lang_id != 0x0409) { return (NULL); } - if (string_index < INDEX_MAX) { + if (string_index < KBD_MAX_INDEX) { return (ptr[string_index]); } return (NULL); } + +static void +kbd_init(void *arg __unused) +{ + struct sysctl_oid *parent; + char parent_name[3]; + + usb_make_str_desc(&kbd_interface, sizeof(kbd_interface), + KBD_DEFAULT_INTERFACE); + usb_make_str_desc(&kbd_product, sizeof(kbd_product), + KBD_DEFAULT_PRODUCT); + + snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_KBD); + sysctl_ctx_init(&kbd_ctx_list); + + parent = SYSCTL_ADD_NODE(&kbd_ctx_list, + SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, + parent_name, CTLFLAG_RW, + 0, "USB Keyboard device side template"); + SYSCTL_ADD_U16(&kbd_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "vendor_id", CTLFLAG_RWTUN, + &usb_template_kbd.idVendor, 1, "Vendor identifier"); + SYSCTL_ADD_U16(&kbd_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "product_id", CTLFLAG_RWTUN, + &usb_template_kbd.idProduct, 1, "Product identifier"); +#if 0 + SYSCTL_ADD_PROC(&kbd_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + &kbd_interface, sizeof(kbd_interface), usb_temp_sysctl, + "A", "Interface string"); +#endif + SYSCTL_ADD_PROC(&kbd_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + &kbd_product, sizeof(kbd_product), usb_temp_sysctl, + "A", "Product string"); +} + +static void +kbd_uninit(void *arg __unused) +{ + + sysctl_ctx_free(&kbd_ctx_list); +} + +SYSINIT(kbd_init, SI_SUB_LOCK, SI_ORDER_FIRST, kbd_init, NULL); +SYSUNINIT(kbd_init, SI_SUB_LOCK, SI_ORDER_FIRST, kbd_uninit, NULL); Modified: head/sys/dev/usb/template/usb_template_midi.c ============================================================================== --- head/sys/dev/usb/template/usb_template_midi.c Sat Jan 20 12:31:21 2018 (r328193) +++ head/sys/dev/usb/template/usb_template_midi.c Sat Jan 20 13:58:34 2018 (r328194) @@ -1,7 +1,12 @@ /* $FreeBSD$ */ /*- - * Copyright (c) 2015 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2015 Hans Petter Selasky + * Copyright (c) 2018 The FreeBSD Foundation + * All rights reserved. * + * Portions of this software were developed by Edward Tomasz Napierala + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -53,28 +58,27 @@ #include <dev/usb/usb.h> #include <dev/usb/usbdi.h> #include <dev/usb/usb_core.h> +#include <dev/usb/usb_ioctl.h> +#include <dev/usb/usb_util.h> #include <dev/usb/template/usb_template.h> #endif /* USB_GLOBAL_INCLUDE_FILE */ enum { - INDEX_MIDI_LANG, - INDEX_MIDI_IF, - INDEX_MIDI_PRODUCT, - INDEX_MIDI_MAX, + MIDI_LANG_INDEX, + MIDI_INTERFACE_INDEX, + MIDI_PRODUCT_INDEX, + MIDI_MAX_INDEX, }; -#define STRING_MIDI_PRODUCT \ - "M\0I\0D\0I\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e" +#define MIDI_DEFAULT_INTERFACE "MIDI interface" +#define MIDI_DEFAULT_PRODUCT "MIDI Test Device" -#define STRING_MIDI_IF \ - "M\0I\0D\0I\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" +static struct usb_string_descriptor midi_interface; +static struct usb_string_descriptor midi_product; -/* make the real string descriptors */ +static struct sysctl_ctx_list midi_ctx_list; -USB_MAKE_STRING_DESC(STRING_MIDI_IF, string_midi_if); -USB_MAKE_STRING_DESC(STRING_MIDI_PRODUCT, string_midi_product); - /* prototypes */ static const uint8_t midi_desc_raw_0[9] = { @@ -92,7 +96,7 @@ static const struct usb_temp_interface_desc midi_iface .bInterfaceClass = UICLASS_AUDIO, .bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL, .bInterfaceProtocol = 0, - .iInterface = INDEX_MIDI_IF, + .iInterface = MIDI_INTERFACE_INDEX, }; static const struct usb_temp_packet_size midi_mps = { @@ -174,7 +178,7 @@ static const struct usb_temp_interface_desc midi_iface .bInterfaceClass = UICLASS_AUDIO, .bInterfaceSubClass = UISUBCLASS_MIDISTREAM, .bInterfaceProtocol = 0, - .iInterface = INDEX_MIDI_IF, + .iInterface = MIDI_INTERFACE_INDEX, }; static const struct usb_temp_interface_desc *midi_interfaces[] = { @@ -187,7 +191,7 @@ static const struct usb_temp_config_desc midi_config_d .ppIfaceDesc = midi_interfaces, .bmAttributes = UC_BUS_POWERED, .bMaxPower = 25, /* 50 mA */ - .iConfiguration = INDEX_MIDI_PRODUCT, + .iConfiguration = MIDI_PRODUCT_INDEX, }; static const struct usb_temp_config_desc *midi_configs[] = { @@ -197,7 +201,7 @@ static const struct usb_temp_config_desc *midi_configs static usb_temp_get_string_desc_t midi_get_string_desc; -const struct usb_temp_device_desc usb_template_midi = { +struct usb_temp_device_desc usb_template_midi = { .getStringDesc = &midi_get_string_desc, .ppConfigDesc = midi_configs, .idVendor = USB_TEMPLATE_VENDOR, @@ -207,7 +211,7 @@ const struct usb_temp_device_desc usb_template_midi = .bDeviceSubClass = 0, .bDeviceProtocol = 0, .iManufacturer = 0, - .iProduct = INDEX_MIDI_PRODUCT, + .iProduct = MIDI_PRODUCT_INDEX, .iSerialNumber = 0, }; @@ -221,10 +225,10 @@ const struct usb_temp_device_desc usb_template_midi = static const void * midi_get_string_desc(uint16_t lang_id, uint8_t string_index) { - static const void *ptr[INDEX_MIDI_MAX] = { - [INDEX_MIDI_LANG] = &usb_string_lang_en, - [INDEX_MIDI_IF] = &string_midi_if, - [INDEX_MIDI_PRODUCT] = &string_midi_product, + static const void *ptr[MIDI_MAX_INDEX] = { + [MIDI_LANG_INDEX] = &usb_string_lang_en, + [MIDI_INTERFACE_INDEX] = &midi_interface, + [MIDI_PRODUCT_INDEX] = &midi_product, }; if (string_index == 0) { @@ -233,8 +237,54 @@ midi_get_string_desc(uint16_t lang_id, uint8_t string_ if (lang_id != 0x0409) { return (NULL); } - if (string_index < INDEX_MIDI_MAX) { + if (string_index < MIDI_MAX_INDEX) { return (ptr[string_index]); } return (NULL); } + +static void +midi_init(void *arg __unused) +{ + struct sysctl_oid *parent; + char parent_name[3]; + + usb_make_str_desc(&midi_interface, sizeof(midi_interface), + MIDI_DEFAULT_INTERFACE); + usb_make_str_desc(&midi_product, sizeof(midi_product), + MIDI_DEFAULT_PRODUCT); + + snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MIDI); + sysctl_ctx_init(&midi_ctx_list); + + parent = SYSCTL_ADD_NODE(&midi_ctx_list, + SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, + parent_name, CTLFLAG_RW, + 0, "USB MIDI device side template"); + SYSCTL_ADD_U16(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "vendor_id", CTLFLAG_RWTUN, + &usb_template_midi.idVendor, 1, "Vendor identifier"); + SYSCTL_ADD_U16(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "product_id", CTLFLAG_RWTUN, + &usb_template_midi.idProduct, 1, "Product identifier"); +#if 0 + SYSCTL_ADD_PROC(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + &midi_interface, sizeof(midi_interface), usb_temp_sysctl, + "A", "Interface string"); +#endif + SYSCTL_ADD_PROC(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, + "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, + &midi_product, sizeof(midi_product), usb_temp_sysctl, + "A", "Product string"); +} *** DIFF OUTPUT TRUNCATED AT 1000 LINES *** _______________________________________________ svn-src-all@freebsd.org mailing list https://lists.freebsd.org/mailman/listinfo/svn-src-all To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"