5 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * This file contains the USB template for an USB Mouse Device.
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usb_cdc.h>
56 #include <dev/usb/template/usb_template.h>
66 0x09, 0x04, /* American English */
68 #define STRING_PRODUCT \
69 'M', 0, 'o', 0, 'u', 0, 's', 0, 'e', 0, ' ', 0, \
70 'T', 0, 'e', 0, 's', 0, 't', 0, ' ', 0, \
71 'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0,
73 #define STRING_MOUSE \
74 'M', 0, 'o', 0, 'u', 0, 's', 0, 'e', 0, ' ', 0, \
75 'i', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0,
77 /* make the real string descriptors */
79 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
80 USB_MAKE_STRING_DESC(STRING_MOUSE, string_mouse);
81 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product);
85 /* The following HID descriptor was dumped from a HP mouse. */
87 static uint8_t mouse_hid_descriptor[] = {
88 0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01,
89 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
90 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
91 0x81, 0x02, 0x95, 0x05, 0x81, 0x03, 0x05, 0x01,
92 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81,
93 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06,
97 static const struct usb_temp_packet_size mouse_intr_mps = {
98 .mps[USB_SPEED_LOW] = 8,
99 .mps[USB_SPEED_FULL] = 8,
100 .mps[USB_SPEED_HIGH] = 8,
103 static const struct usb_temp_interval mouse_intr_interval = {
104 .bInterval[USB_SPEED_LOW] = 2, /* 2ms */
105 .bInterval[USB_SPEED_FULL] = 2, /* 2ms */
106 .bInterval[USB_SPEED_HIGH] = 5, /* 2ms */
109 static const struct usb_temp_endpoint_desc mouse_ep_0 = {
110 .ppRawDesc = NULL, /* no raw descriptors */
111 .pPacketSize = &mouse_intr_mps,
112 .pIntervals = &mouse_intr_interval,
113 .bEndpointAddress = UE_DIR_IN,
114 .bmAttributes = UE_INTERRUPT,
117 static const struct usb_temp_endpoint_desc *mouse_endpoints[] = {
122 static const uint8_t mouse_raw_desc[] = {
123 0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(mouse_hid_descriptor),
127 static const void *mouse_iface_0_desc[] = {
132 static const struct usb_temp_interface_desc mouse_iface_0 = {
133 .ppRawDesc = mouse_iface_0_desc,
134 .ppEndpoints = mouse_endpoints,
135 .bInterfaceClass = 3,
136 .bInterfaceSubClass = 1,
137 .bInterfaceProtocol = 2,
138 .iInterface = INDEX_MOUSE,
141 static const struct usb_temp_interface_desc *mouse_interfaces[] = {
146 static const struct usb_temp_config_desc mouse_config_desc = {
147 .ppIfaceDesc = mouse_interfaces,
148 .bmAttributes = UC_BUS_POWERED,
149 .bMaxPower = 25, /* 50 mA */
150 .iConfiguration = INDEX_PRODUCT,
153 static const struct usb_temp_config_desc *mouse_configs[] = {
158 static usb_temp_get_string_desc_t mouse_get_string_desc;
159 static usb_temp_get_vendor_desc_t mouse_get_vendor_desc;
161 const struct usb_temp_device_desc usb_template_mouse = {
162 .getStringDesc = &mouse_get_string_desc,
163 .getVendorDesc = &mouse_get_vendor_desc,
164 .ppConfigDesc = mouse_configs,
165 .idVendor = USB_TEMPLATE_VENDOR,
168 .bDeviceClass = UDCLASS_COMM,
169 .bDeviceSubClass = 0,
170 .bDeviceProtocol = 0,
172 .iProduct = INDEX_PRODUCT,
176 /*------------------------------------------------------------------------*
177 * mouse_get_vendor_desc
180 * NULL: Failure. No such vendor descriptor.
181 * Else: Success. Pointer to vendor descriptor is returned.
182 *------------------------------------------------------------------------*/
184 mouse_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
186 if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) &&
187 (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) &&
188 (req->wIndex[1] == 0) && (req->wIndex[0] == 0)) {
190 *plen = sizeof(mouse_hid_descriptor);
191 return (mouse_hid_descriptor);
196 /*------------------------------------------------------------------------*
197 * mouse_get_string_desc
200 * NULL: Failure. No such string.
201 * Else: Success. Pointer to string descriptor is returned.
202 *------------------------------------------------------------------------*/
204 mouse_get_string_desc(uint16_t lang_id, uint8_t string_index)
206 static const void *ptr[INDEX_MAX] = {
207 [INDEX_LANG] = &string_lang,
208 [INDEX_MOUSE] = &string_mouse,
209 [INDEX_PRODUCT] = &string_product,
212 if (string_index == 0) {
213 return (&string_lang);
215 if (lang_id != 0x0409) {
218 if (string_index < INDEX_MAX) {
219 return (ptr[string_index]);