xref: /freebsd/sys/dev/usb/template/usb_template_mouse.c (revision 97cb52fa9aefd90fad38790fded50905aeeb9b9e)
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * This file contains the USB template for an USB Mouse Device.
31  */
32 
33 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
35 #else
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/bus.h>
44 #include <sys/module.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/sx.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
53 #include <sys/priv.h>
54 
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usb_core.h>
58 #include <dev/usb/usb_cdc.h>
59 
60 #include <dev/usb/template/usb_template.h>
61 #endif			/* USB_GLOBAL_INCLUDE_FILE */
62 
63 enum {
64 	INDEX_LANG,
65 	INDEX_MOUSE,
66 	INDEX_PRODUCT,
67 	INDEX_MAX,
68 };
69 
70 #define	STRING_PRODUCT \
71   "M\0o\0u\0s\0e\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e"
72 
73 #define	STRING_MOUSE \
74   "M\0o\0u\0s\0e\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
75 
76 /* make the real string descriptors */
77 
78 USB_MAKE_STRING_DESC(STRING_MOUSE, string_mouse);
79 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product);
80 
81 /* prototypes */
82 
83 /* The following HID descriptor was dumped from a HP mouse. */
84 
85 static uint8_t mouse_hid_descriptor[] = {
86 	0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01,
87 	0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
88 	0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
89 	0x81, 0x02, 0x95, 0x05, 0x81, 0x03, 0x05, 0x01,
90 	0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81,
91 	0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06,
92 	0xc0, 0xc0
93 };
94 
95 static const struct usb_temp_packet_size mouse_intr_mps = {
96 	.mps[USB_SPEED_LOW] = 8,
97 	.mps[USB_SPEED_FULL] = 8,
98 	.mps[USB_SPEED_HIGH] = 8,
99 };
100 
101 static const struct usb_temp_interval mouse_intr_interval = {
102 	.bInterval[USB_SPEED_LOW] = 2,		/* 2ms */
103 	.bInterval[USB_SPEED_FULL] = 2,		/* 2ms */
104 	.bInterval[USB_SPEED_HIGH] = 5,		/* 2ms */
105 };
106 
107 static const struct usb_temp_endpoint_desc mouse_ep_0 = {
108 	.ppRawDesc = NULL,		/* no raw descriptors */
109 	.pPacketSize = &mouse_intr_mps,
110 	.pIntervals = &mouse_intr_interval,
111 	.bEndpointAddress = UE_DIR_IN,
112 	.bmAttributes = UE_INTERRUPT,
113 };
114 
115 static const struct usb_temp_endpoint_desc *mouse_endpoints[] = {
116 	&mouse_ep_0,
117 	NULL,
118 };
119 
120 static const uint8_t mouse_raw_desc[] = {
121 	0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(mouse_hid_descriptor),
122 	0x00
123 };
124 
125 static const void *mouse_iface_0_desc[] = {
126 	mouse_raw_desc,
127 	NULL,
128 };
129 
130 static const struct usb_temp_interface_desc mouse_iface_0 = {
131 	.ppRawDesc = mouse_iface_0_desc,
132 	.ppEndpoints = mouse_endpoints,
133 	.bInterfaceClass = UICLASS_HID,
134 	.bInterfaceSubClass = UISUBCLASS_BOOT,
135 	.bInterfaceProtocol = UIPROTO_MOUSE,
136 	.iInterface = INDEX_MOUSE,
137 };
138 
139 static const struct usb_temp_interface_desc *mouse_interfaces[] = {
140 	&mouse_iface_0,
141 	NULL,
142 };
143 
144 static const struct usb_temp_config_desc mouse_config_desc = {
145 	.ppIfaceDesc = mouse_interfaces,
146 	.bmAttributes = UC_BUS_POWERED,
147 	.bMaxPower = 25,		/* 50 mA */
148 	.iConfiguration = INDEX_PRODUCT,
149 };
150 
151 static const struct usb_temp_config_desc *mouse_configs[] = {
152 	&mouse_config_desc,
153 	NULL,
154 };
155 
156 static usb_temp_get_string_desc_t mouse_get_string_desc;
157 static usb_temp_get_vendor_desc_t mouse_get_vendor_desc;
158 
159 const struct usb_temp_device_desc usb_template_mouse = {
160 	.getStringDesc = &mouse_get_string_desc,
161 	.getVendorDesc = &mouse_get_vendor_desc,
162 	.ppConfigDesc = mouse_configs,
163 	.idVendor = USB_TEMPLATE_VENDOR,
164 	.idProduct = 0x00AE,
165 	.bcdDevice = 0x0100,
166 	.bDeviceClass = UDCLASS_COMM,
167 	.bDeviceSubClass = 0,
168 	.bDeviceProtocol = 0,
169 	.iManufacturer = 0,
170 	.iProduct = INDEX_PRODUCT,
171 	.iSerialNumber = 0,
172 };
173 
174 /*------------------------------------------------------------------------*
175  *      mouse_get_vendor_desc
176  *
177  * Return values:
178  * NULL: Failure. No such vendor descriptor.
179  * Else: Success. Pointer to vendor descriptor is returned.
180  *------------------------------------------------------------------------*/
181 static const void *
182 mouse_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
183 {
184 	if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) &&
185 	    (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) &&
186 	    (req->wIndex[1] == 0) && (req->wIndex[0] == 0)) {
187 
188 		*plen = sizeof(mouse_hid_descriptor);
189 		return (mouse_hid_descriptor);
190 	}
191 	return (NULL);
192 }
193 
194 /*------------------------------------------------------------------------*
195  *	mouse_get_string_desc
196  *
197  * Return values:
198  * NULL: Failure. No such string.
199  * Else: Success. Pointer to string descriptor is returned.
200  *------------------------------------------------------------------------*/
201 static const void *
202 mouse_get_string_desc(uint16_t lang_id, uint8_t string_index)
203 {
204 	static const void *ptr[INDEX_MAX] = {
205 		[INDEX_LANG] = &usb_string_lang_en,
206 		[INDEX_MOUSE] = &string_mouse,
207 		[INDEX_PRODUCT] = &string_product,
208 	};
209 
210 	if (string_index == 0) {
211 		return (&usb_string_lang_en);
212 	}
213 	if (lang_id != 0x0409) {
214 		return (NULL);
215 	}
216 	if (string_index < INDEX_MAX) {
217 		return (ptr[string_index]);
218 	}
219 	return (NULL);
220 }
221