Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/usb/template/usb_template_mouse.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, | MOUSE_LANG_INDEX, | ||||
INDEX_MOUSE, | MOUSE_INTERFACE_INDEX, | ||||
INDEX_PRODUCT, | MOUSE_PRODUCT_INDEX, | ||||
INDEX_MAX, | MOUSE_MAX_INDEX, | ||||
}; | }; | ||||
#define STRING_PRODUCT \ | #define MOUSE_DEFAULT_INTERFACE "Mouse interface" | ||||
"M\0o\0u\0s\0e\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e" | #define MOUSE_DEFAULT_PRODUCT "Mouse Test Interface" | ||||
#define STRING_MOUSE \ | static struct usb_string_descriptor mouse_interface; | ||||
"M\0o\0u\0s\0e\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" | static struct usb_string_descriptor mouse_product; | ||||
/* make the real string descriptors */ | static struct sysctl_ctx_list mouse_ctx_list; | ||||
USB_MAKE_STRING_DESC(STRING_MOUSE, string_mouse); | |||||
USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product); | |||||
/* prototypes */ | /* prototypes */ | ||||
/* The following HID descriptor was dumped from a HP mouse. */ | /* The following HID descriptor was dumped from a HP mouse. */ | ||||
static uint8_t mouse_hid_descriptor[] = { | static uint8_t mouse_hid_descriptor[] = { | ||||
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, | 0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, | ||||
0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, | 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, | ||||
0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, | 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, | ||||
Show All 39 Lines | |||||
}; | }; | ||||
static const struct usb_temp_interface_desc mouse_iface_0 = { | static const struct usb_temp_interface_desc mouse_iface_0 = { | ||||
.ppRawDesc = mouse_iface_0_desc, | .ppRawDesc = mouse_iface_0_desc, | ||||
.ppEndpoints = mouse_endpoints, | .ppEndpoints = mouse_endpoints, | ||||
.bInterfaceClass = UICLASS_HID, | .bInterfaceClass = UICLASS_HID, | ||||
.bInterfaceSubClass = UISUBCLASS_BOOT, | .bInterfaceSubClass = UISUBCLASS_BOOT, | ||||
.bInterfaceProtocol = UIPROTO_MOUSE, | .bInterfaceProtocol = UIPROTO_MOUSE, | ||||
.iInterface = INDEX_MOUSE, | .iInterface = MOUSE_INTERFACE_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_interface_desc *mouse_interfaces[] = { | static const struct usb_temp_interface_desc *mouse_interfaces[] = { | ||||
&mouse_iface_0, | &mouse_iface_0, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
static const struct usb_temp_config_desc mouse_config_desc = { | static const struct usb_temp_config_desc mouse_config_desc = { | ||||
.ppIfaceDesc = mouse_interfaces, | .ppIfaceDesc = mouse_interfaces, | ||||
.bmAttributes = UC_BUS_POWERED, | .bmAttributes = UC_BUS_POWERED, | ||||
.bMaxPower = 25, /* 50 mA */ | .bMaxPower = 25, /* 50 mA */ | ||||
.iConfiguration = INDEX_PRODUCT, | .iConfiguration = MOUSE_INTERFACE_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_config_desc *mouse_configs[] = { | static const struct usb_temp_config_desc *mouse_configs[] = { | ||||
&mouse_config_desc, | &mouse_config_desc, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
static usb_temp_get_string_desc_t mouse_get_string_desc; | static usb_temp_get_string_desc_t mouse_get_string_desc; | ||||
static usb_temp_get_vendor_desc_t mouse_get_vendor_desc; | static usb_temp_get_vendor_desc_t mouse_get_vendor_desc; | ||||
const struct usb_temp_device_desc usb_template_mouse = { | struct usb_temp_device_desc usb_template_mouse = { | ||||
.getStringDesc = &mouse_get_string_desc, | .getStringDesc = &mouse_get_string_desc, | ||||
.getVendorDesc = &mouse_get_vendor_desc, | .getVendorDesc = &mouse_get_vendor_desc, | ||||
.ppConfigDesc = mouse_configs, | .ppConfigDesc = mouse_configs, | ||||
.idVendor = USB_TEMPLATE_VENDOR, | .idVendor = USB_TEMPLATE_VENDOR, | ||||
.idProduct = 0x00AE, | .idProduct = 0x00AE, | ||||
.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 = MOUSE_PRODUCT_INDEX, | ||||
.iSerialNumber = 0, | .iSerialNumber = 0, | ||||
}; | }; | ||||
/*------------------------------------------------------------------------* | /*------------------------------------------------------------------------* | ||||
* mouse_get_vendor_desc | * mouse_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 * | ||||
mouse_get_string_desc(uint16_t lang_id, uint8_t string_index) | mouse_get_string_desc(uint16_t lang_id, uint8_t string_index) | ||||
{ | { | ||||
static const void *ptr[INDEX_MAX] = { | static const void *ptr[MOUSE_MAX_INDEX] = { | ||||
[INDEX_LANG] = &usb_string_lang_en, | [MOUSE_LANG_INDEX] = &usb_string_lang_en, | ||||
[INDEX_MOUSE] = &string_mouse, | [MOUSE_INTERFACE_INDEX] = &mouse_interface, | ||||
[INDEX_PRODUCT] = &string_product, | [MOUSE_PRODUCT_INDEX] = &mouse_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 < MOUSE_MAX_INDEX) { | ||||
return (ptr[string_index]); | return (ptr[string_index]); | ||||
} | } | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
static void | |||||
mouse_init(void *arg __unused) | |||||
{ | |||||
struct sysctl_oid *parent; | |||||
char parent_name[3]; | |||||
usb_make_str_desc(&mouse_interface, sizeof(mouse_interface), | |||||
MOUSE_DEFAULT_INTERFACE); | |||||
usb_make_str_desc(&mouse_product, sizeof(mouse_product), | |||||
MOUSE_DEFAULT_PRODUCT); | |||||
snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MOUSE); | |||||
sysctl_ctx_init(&mouse_ctx_list); | |||||
parent = SYSCTL_ADD_NODE(&mouse_ctx_list, | |||||
SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, | |||||
parent_name, CTLFLAG_RW, | |||||
0, "USB Mouse device side template"); | |||||
SYSCTL_ADD_U16(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"vendor_id", CTLFLAG_RWTUN, | |||||
&usb_template_mouse.idVendor, 1, "Vendor identifier"); | |||||
SYSCTL_ADD_U16(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"product_id", CTLFLAG_RWTUN, | |||||
&usb_template_mouse.idProduct, 1, "Product identifier"); | |||||
#if 0 | |||||
SYSCTL_ADD_PROC(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, | |||||
&mouse_interface, sizeof(mouse_interface), usb_temp_sysctl, | |||||
"A", "Interface string"); | |||||
#endif | |||||
SYSCTL_ADD_PROC(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, | |||||
&mouse_product, sizeof(mouse_product), usb_temp_sysctl, | |||||
"A", "Product string"); | |||||
} | |||||
static void | |||||
mouse_uninit(void *arg __unused) | |||||
{ | |||||
sysctl_ctx_free(&mouse_ctx_list); | |||||
} | |||||
SYSINIT(mouse_init, SI_SUB_LOCK, SI_ORDER_FIRST, mouse_init, NULL); | |||||
SYSUNINIT(mouse_init, SI_SUB_LOCK, SI_ORDER_FIRST, mouse_uninit, NULL); |