Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/usb/template/usb_template_kbd.c
/* $FreeBSD$ */ | /* $FreeBSD$ */ | ||||
/*- | /*- | ||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | * 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 | * Redistribution and use in source and binary forms, with or without | ||||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
* are met: | * are met: | ||||
* 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||
* notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | ||||
* 2. Redistributions in binary form must reproduce the above copyright | * 2. Redistributions in binary form must reproduce the above copyright | ||||
* notice, this list of conditions and the following disclaimer in the | * notice, this list of conditions and the following disclaimer in the | ||||
* documentation and/or other materials provided with the distribution. | * documentation and/or other materials provided with the distribution. | ||||
Show All 36 Lines | |||||
#include <sys/callout.h> | #include <sys/callout.h> | ||||
#include <sys/malloc.h> | #include <sys/malloc.h> | ||||
#include <sys/priv.h> | #include <sys/priv.h> | ||||
#include <dev/usb/usb.h> | #include <dev/usb/usb.h> | ||||
#include <dev/usb/usbdi.h> | #include <dev/usb/usbdi.h> | ||||
#include <dev/usb/usb_core.h> | #include <dev/usb/usb_core.h> | ||||
#include <dev/usb/usb_cdc.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> | #include <dev/usb/template/usb_template.h> | ||||
#endif /* USB_GLOBAL_INCLUDE_FILE */ | #endif /* USB_GLOBAL_INCLUDE_FILE */ | ||||
enum { | enum { | ||||
INDEX_LANG, | KBD_LANG_INDEX, | ||||
INDEX_KEYBOARD, | KBD_INTERFACE_INDEX, | ||||
INDEX_PRODUCT, | KBD_PRODUCT_INDEX, | ||||
INDEX_MAX, | KBD_MAX_INDEX, | ||||
}; | }; | ||||
#define STRING_PRODUCT \ | #define KBD_DEFAULT_INTERFACE "Keyboard Interface" | ||||
"K\0e\0y\0b\0o\0a\0r\0d\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e" | #define KBD_DEFAULT_PRODUCT "Keyboard Test Device" | ||||
#define STRING_KEYBOARD \ | static struct usb_string_descriptor kbd_interface; | ||||
"K\0e\0y\0b\0o\0a\0r\0d\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" | 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 */ | /* prototypes */ | ||||
static const struct usb_temp_packet_size keyboard_intr_mps = { | static const struct usb_temp_packet_size keyboard_intr_mps = { | ||||
.mps[USB_SPEED_LOW] = 16, | .mps[USB_SPEED_LOW] = 16, | ||||
.mps[USB_SPEED_FULL] = 16, | .mps[USB_SPEED_FULL] = 16, | ||||
.mps[USB_SPEED_HIGH] = 16, | .mps[USB_SPEED_HIGH] = 16, | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
static const struct usb_temp_interface_desc keyboard_iface_0 = { | static const struct usb_temp_interface_desc keyboard_iface_0 = { | ||||
.ppRawDesc = keyboard_iface_0_desc, | .ppRawDesc = keyboard_iface_0_desc, | ||||
.ppEndpoints = keyboard_endpoints, | .ppEndpoints = keyboard_endpoints, | ||||
.bInterfaceClass = UICLASS_HID, | .bInterfaceClass = UICLASS_HID, | ||||
.bInterfaceSubClass = UISUBCLASS_BOOT, | .bInterfaceSubClass = UISUBCLASS_BOOT, | ||||
.bInterfaceProtocol = UIPROTO_BOOT_KEYBOARD, | .bInterfaceProtocol = UIPROTO_BOOT_KEYBOARD, | ||||
.iInterface = INDEX_KEYBOARD, | .iInterface = KBD_INTERFACE_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_interface_desc *keyboard_interfaces[] = { | static const struct usb_temp_interface_desc *keyboard_interfaces[] = { | ||||
&keyboard_iface_0, | &keyboard_iface_0, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
static const struct usb_temp_config_desc keyboard_config_desc = { | static const struct usb_temp_config_desc keyboard_config_desc = { | ||||
.ppIfaceDesc = keyboard_interfaces, | .ppIfaceDesc = keyboard_interfaces, | ||||
.bmAttributes = UC_BUS_POWERED, | .bmAttributes = UC_BUS_POWERED, | ||||
.bMaxPower = 25, /* 50 mA */ | .bMaxPower = 25, /* 50 mA */ | ||||
.iConfiguration = INDEX_PRODUCT, | .iConfiguration = KBD_PRODUCT_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_config_desc *keyboard_configs[] = { | static const struct usb_temp_config_desc *keyboard_configs[] = { | ||||
&keyboard_config_desc, | &keyboard_config_desc, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
static usb_temp_get_string_desc_t keyboard_get_string_desc; | static usb_temp_get_string_desc_t keyboard_get_string_desc; | ||||
static usb_temp_get_vendor_desc_t keyboard_get_vendor_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, | .getStringDesc = &keyboard_get_string_desc, | ||||
.getVendorDesc = &keyboard_get_vendor_desc, | .getVendorDesc = &keyboard_get_vendor_desc, | ||||
.ppConfigDesc = keyboard_configs, | .ppConfigDesc = keyboard_configs, | ||||
.idVendor = USB_TEMPLATE_VENDOR, | .idVendor = USB_TEMPLATE_VENDOR, | ||||
.idProduct = 0x00CB, | .idProduct = 0x00CB, | ||||
.bcdDevice = 0x0100, | .bcdDevice = 0x0100, | ||||
.bDeviceClass = UDCLASS_COMM, | .bDeviceClass = UDCLASS_COMM, | ||||
.bDeviceSubClass = 0, | .bDeviceSubClass = 0, | ||||
.bDeviceProtocol = 0, | .bDeviceProtocol = 0, | ||||
.iManufacturer = 0, | .iManufacturer = 0, | ||||
.iProduct = INDEX_PRODUCT, | .iProduct = KBD_PRODUCT_INDEX, | ||||
.iSerialNumber = 0, | .iSerialNumber = 0, | ||||
}; | }; | ||||
/*------------------------------------------------------------------------* | /*------------------------------------------------------------------------* | ||||
* keyboard_get_vendor_desc | * keyboard_get_vendor_desc | ||||
* | * | ||||
* Return values: | * Return values: | ||||
* NULL: Failure. No such vendor descriptor. | * NULL: Failure. No such vendor descriptor. | ||||
Show All 17 Lines | |||||
* | * | ||||
* Return values: | * Return values: | ||||
* NULL: Failure. No such string. | * NULL: Failure. No such string. | ||||
* Else: Success. Pointer to string descriptor is returned. | * Else: Success. Pointer to string descriptor is returned. | ||||
*------------------------------------------------------------------------*/ | *------------------------------------------------------------------------*/ | ||||
static const void * | static const void * | ||||
keyboard_get_string_desc(uint16_t lang_id, uint8_t string_index) | keyboard_get_string_desc(uint16_t lang_id, uint8_t string_index) | ||||
{ | { | ||||
static const void *ptr[INDEX_MAX] = { | static const void *ptr[KBD_MAX_INDEX] = { | ||||
[INDEX_LANG] = &usb_string_lang_en, | [KBD_LANG_INDEX] = &usb_string_lang_en, | ||||
[INDEX_KEYBOARD] = &string_keyboard, | [KBD_INTERFACE_INDEX] = &kbd_interface, | ||||
[INDEX_PRODUCT] = &string_product, | [KBD_PRODUCT_INDEX] = &kbd_product, | ||||
}; | }; | ||||
if (string_index == 0) { | if (string_index == 0) { | ||||
return (&usb_string_lang_en); | return (&usb_string_lang_en); | ||||
} | } | ||||
if (lang_id != 0x0409) { | if (lang_id != 0x0409) { | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
if (string_index < INDEX_MAX) { | if (string_index < KBD_MAX_INDEX) { | ||||
return (ptr[string_index]); | return (ptr[string_index]); | ||||
} | } | ||||
return (NULL); | 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); |