1 /* $FreeBSD$ */
2 /*-
3 * Copyright (c) 2010 Hans Petter Selasky
4 * Copyright (c) 2018 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * This file contains the USB template for an USB Mouse Device.
34 */
35
36 #ifdef USB_GLOBAL_INCLUDE_FILE
37 #include USB_GLOBAL_INCLUDE_FILE
38 #else
39 #include <sys/stdint.h>
40 #include <sys/stddef.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/bus.h>
47 #include <sys/module.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/sx.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
56 #include <sys/priv.h>
57
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_cdc.h>
62 #include <dev/usb/usb_ioctl.h>
63 #include <dev/usb/usb_util.h>
64
65 #include <dev/usb/template/usb_template.h>
66 #endif /* USB_GLOBAL_INCLUDE_FILE */
67
68 enum {
69 MOUSE_LANG_INDEX,
70 MOUSE_INTERFACE_INDEX,
71 MOUSE_MANUFACTURER_INDEX,
72 MOUSE_PRODUCT_INDEX,
73 MOUSE_SERIAL_NUMBER_INDEX,
74 MOUSE_MAX_INDEX,
75 };
76
77 #define MOUSE_DEFAULT_INTERFACE "Mouse interface"
78 #define MOUSE_DEFAULT_MANUFACTURER "FreeBSD foundation"
79 #define MOUSE_DEFAULT_PRODUCT "Mouse Test Interface"
80 #define MOUSE_DEFAULT_SERIAL_NUMBER "March 2008"
81
82 static struct usb_string_descriptor mouse_interface;
83 static struct usb_string_descriptor mouse_manufacturer;
84 static struct usb_string_descriptor mouse_product;
85 static struct usb_string_descriptor mouse_serial_number;
86
87 static struct sysctl_ctx_list mouse_ctx_list;
88
89 /* prototypes */
90
91 /* The following HID descriptor was dumped from a HP mouse. */
92
93 static uint8_t mouse_hid_descriptor[] = {
94 0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01,
95 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
96 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
97 0x81, 0x02, 0x95, 0x05, 0x81, 0x03, 0x05, 0x01,
98 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81,
99 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06,
100 0xc0, 0xc0
101 };
102
103 static const struct usb_temp_packet_size mouse_intr_mps = {
104 .mps[USB_SPEED_LOW] = 8,
105 .mps[USB_SPEED_FULL] = 8,
106 .mps[USB_SPEED_HIGH] = 8,
107 };
108
109 static const struct usb_temp_interval mouse_intr_interval = {
110 .bInterval[USB_SPEED_LOW] = 2, /* 2ms */
111 .bInterval[USB_SPEED_FULL] = 2, /* 2ms */
112 .bInterval[USB_SPEED_HIGH] = 5, /* 2ms */
113 };
114
115 static const struct usb_temp_endpoint_desc mouse_ep_0 = {
116 .ppRawDesc = NULL, /* no raw descriptors */
117 .pPacketSize = &mouse_intr_mps,
118 .pIntervals = &mouse_intr_interval,
119 .bEndpointAddress = UE_DIR_IN,
120 .bmAttributes = UE_INTERRUPT,
121 };
122
123 static const struct usb_temp_endpoint_desc *mouse_endpoints[] = {
124 &mouse_ep_0,
125 NULL,
126 };
127
128 static const uint8_t mouse_raw_desc[] = {
129 0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(mouse_hid_descriptor),
130 0x00
131 };
132
133 static const void *mouse_iface_0_desc[] = {
134 mouse_raw_desc,
135 NULL,
136 };
137
138 static const struct usb_temp_interface_desc mouse_iface_0 = {
139 .ppRawDesc = mouse_iface_0_desc,
140 .ppEndpoints = mouse_endpoints,
141 .bInterfaceClass = UICLASS_HID,
142 .bInterfaceSubClass = UISUBCLASS_BOOT,
143 .bInterfaceProtocol = UIPROTO_MOUSE,
144 .iInterface = MOUSE_INTERFACE_INDEX,
145 };
146
147 static const struct usb_temp_interface_desc *mouse_interfaces[] = {
148 &mouse_iface_0,
149 NULL,
150 };
151
152 static const struct usb_temp_config_desc mouse_config_desc = {
153 .ppIfaceDesc = mouse_interfaces,
154 .bmAttributes = UC_BUS_POWERED,
155 .bMaxPower = 25, /* 50 mA */
156 .iConfiguration = MOUSE_INTERFACE_INDEX,
157 };
158
159 static const struct usb_temp_config_desc *mouse_configs[] = {
160 &mouse_config_desc,
161 NULL,
162 };
163
164 static usb_temp_get_string_desc_t mouse_get_string_desc;
165 static usb_temp_get_vendor_desc_t mouse_get_vendor_desc;
166
167 struct usb_temp_device_desc usb_template_mouse = {
168 .getStringDesc = &mouse_get_string_desc,
169 .getVendorDesc = &mouse_get_vendor_desc,
170 .ppConfigDesc = mouse_configs,
171 .idVendor = USB_TEMPLATE_VENDOR,
172 .idProduct = 0x00AE,
173 .bcdDevice = 0x0100,
174 .bDeviceClass = UDCLASS_COMM,
175 .bDeviceSubClass = 0,
176 .bDeviceProtocol = 0,
177 .iManufacturer = MOUSE_MANUFACTURER_INDEX,
178 .iProduct = MOUSE_PRODUCT_INDEX,
179 .iSerialNumber = MOUSE_SERIAL_NUMBER_INDEX,
180 };
181
182 /*------------------------------------------------------------------------*
183 * mouse_get_vendor_desc
184 *
185 * Return values:
186 * NULL: Failure. No such vendor descriptor.
187 * Else: Success. Pointer to vendor descriptor is returned.
188 *------------------------------------------------------------------------*/
189 static const void *
mouse_get_vendor_desc(const struct usb_device_request * req,uint16_t * plen)190 mouse_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
191 {
192 if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) &&
193 (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) &&
194 (req->wIndex[1] == 0) && (req->wIndex[0] == 0)) {
195
196 *plen = sizeof(mouse_hid_descriptor);
197 return (mouse_hid_descriptor);
198 }
199 return (NULL);
200 }
201
202 /*------------------------------------------------------------------------*
203 * mouse_get_string_desc
204 *
205 * Return values:
206 * NULL: Failure. No such string.
207 * Else: Success. Pointer to string descriptor is returned.
208 *------------------------------------------------------------------------*/
209 static const void *
mouse_get_string_desc(uint16_t lang_id,uint8_t string_index)210 mouse_get_string_desc(uint16_t lang_id, uint8_t string_index)
211 {
212 static const void *ptr[MOUSE_MAX_INDEX] = {
213 [MOUSE_LANG_INDEX] = &usb_string_lang_en,
214 [MOUSE_INTERFACE_INDEX] = &mouse_interface,
215 [MOUSE_MANUFACTURER_INDEX] = &mouse_manufacturer,
216 [MOUSE_PRODUCT_INDEX] = &mouse_product,
217 [MOUSE_SERIAL_NUMBER_INDEX] = &mouse_serial_number,
218 };
219
220 if (string_index == 0) {
221 return (&usb_string_lang_en);
222 }
223 if (lang_id != 0x0409) {
224 return (NULL);
225 }
226 if (string_index < MOUSE_MAX_INDEX) {
227 return (ptr[string_index]);
228 }
229 return (NULL);
230 }
231
232 static void
mouse_init(void * arg __unused)233 mouse_init(void *arg __unused)
234 {
235 struct sysctl_oid *parent;
236 char parent_name[3];
237
238 usb_make_str_desc(&mouse_interface, sizeof(mouse_interface),
239 MOUSE_DEFAULT_INTERFACE);
240 usb_make_str_desc(&mouse_manufacturer, sizeof(mouse_manufacturer),
241 MOUSE_DEFAULT_MANUFACTURER);
242 usb_make_str_desc(&mouse_product, sizeof(mouse_product),
243 MOUSE_DEFAULT_PRODUCT);
244 usb_make_str_desc(&mouse_serial_number, sizeof(mouse_serial_number),
245 MOUSE_DEFAULT_SERIAL_NUMBER);
246
247 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MOUSE);
248 sysctl_ctx_init(&mouse_ctx_list);
249
250 parent = SYSCTL_ADD_NODE(&mouse_ctx_list,
251 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
252 parent_name, CTLFLAG_RW,
253 0, "USB Mouse device side template");
254 SYSCTL_ADD_U16(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
255 "vendor_id", CTLFLAG_RWTUN,
256 &usb_template_mouse.idVendor, 1, "Vendor identifier");
257 SYSCTL_ADD_U16(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
258 "product_id", CTLFLAG_RWTUN,
259 &usb_template_mouse.idProduct, 1, "Product identifier");
260 #if 0
261 SYSCTL_ADD_PROC(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
262 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
263 &mouse_interface, sizeof(mouse_interface), usb_temp_sysctl,
264 "A", "Interface string");
265 #endif
266 SYSCTL_ADD_PROC(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
267 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
268 &mouse_manufacturer, sizeof(mouse_manufacturer), usb_temp_sysctl,
269 "A", "Manufacturer string");
270 SYSCTL_ADD_PROC(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
271 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
272 &mouse_product, sizeof(mouse_product), usb_temp_sysctl,
273 "A", "Product string");
274 SYSCTL_ADD_PROC(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
275 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
276 &mouse_serial_number, sizeof(mouse_serial_number), usb_temp_sysctl,
277 "A", "Serial number string");
278 }
279
280 static void
mouse_uninit(void * arg __unused)281 mouse_uninit(void *arg __unused)
282 {
283
284 sysctl_ctx_free(&mouse_ctx_list);
285 }
286
287 SYSINIT(mouse_init, SI_SUB_LOCK, SI_ORDER_FIRST, mouse_init, NULL);
288 SYSUNINIT(mouse_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, mouse_uninit, NULL);
289