1 /* $FreeBSD: head/sys/dev/usb/template/usb_template_mouse.c 246125 2013-01-30 16:05:54Z hselasky $ */
2 /*-
3  * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * This file contains the USB template for an USB Mouse Device.
29  */
30 
31 #ifdef USB_GLOBAL_INCLUDE_FILE
32 #include USB_GLOBAL_INCLUDE_FILE
33 #else
34 #include <sys/stdint.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>
40 #include <sys/bus.h>
41 #include <sys/module.h>
42 #include <sys/lock.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/caps.h>
49 
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usbdi.h>
52 #include <bus/u4b/usb_core.h>
53 #include <bus/u4b/usb_cdc.h>
54 
55 #include <bus/u4b/template/usb_template.h>
56 #endif			/* USB_GLOBAL_INCLUDE_FILE */
57 
58 enum {
59 	INDEX_LANG,
60 	INDEX_MOUSE,
61 	INDEX_PRODUCT,
62 	INDEX_MAX,
63 };
64 
65 #define	STRING_PRODUCT \
66   "M\0o\0u\0s\0e\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e"
67 
68 #define	STRING_MOUSE \
69   "M\0o\0u\0s\0e\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
70 
71 /* make the real string descriptors */
72 
73 USB_MAKE_STRING_DESC(STRING_MOUSE, string_mouse);
74 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product);
75 
76 /* prototypes */
77 
78 /* The following HID descriptor was dumped from a HP mouse. */
79 
80 static uint8_t mouse_hid_descriptor[] = {
81 	0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01,
82 	0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
83 	0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
84 	0x81, 0x02, 0x95, 0x05, 0x81, 0x03, 0x05, 0x01,
85 	0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81,
86 	0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06,
87 	0xc0, 0xc0
88 };
89 
90 static const struct usb_temp_packet_size mouse_intr_mps = {
91 	.mps[USB_SPEED_LOW] = 8,
92 	.mps[USB_SPEED_FULL] = 8,
93 	.mps[USB_SPEED_HIGH] = 8,
94 };
95 
96 static const struct usb_temp_interval mouse_intr_interval = {
97 	.bInterval[USB_SPEED_LOW] = 2,		/* 2ms */
98 	.bInterval[USB_SPEED_FULL] = 2,		/* 2ms */
99 	.bInterval[USB_SPEED_HIGH] = 5,		/* 2ms */
100 };
101 
102 static const struct usb_temp_endpoint_desc mouse_ep_0 = {
103 	.ppRawDesc = NULL,		/* no raw descriptors */
104 	.pPacketSize = &mouse_intr_mps,
105 	.pIntervals = &mouse_intr_interval,
106 	.bEndpointAddress = UE_DIR_IN,
107 	.bmAttributes = UE_INTERRUPT,
108 };
109 
110 static const struct usb_temp_endpoint_desc *mouse_endpoints[] = {
111 	&mouse_ep_0,
112 	NULL,
113 };
114 
115 static const uint8_t mouse_raw_desc[] = {
116 	0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(mouse_hid_descriptor),
117 	0x00
118 };
119 
120 static const void *mouse_iface_0_desc[] = {
121 	mouse_raw_desc,
122 	NULL,
123 };
124 
125 static const struct usb_temp_interface_desc mouse_iface_0 = {
126 	.ppRawDesc = mouse_iface_0_desc,
127 	.ppEndpoints = mouse_endpoints,
128 	.bInterfaceClass = 3,
129 	.bInterfaceSubClass = 1,
130 	.bInterfaceProtocol = 2,
131 	.iInterface = INDEX_MOUSE,
132 };
133 
134 static const struct usb_temp_interface_desc *mouse_interfaces[] = {
135 	&mouse_iface_0,
136 	NULL,
137 };
138 
139 static const struct usb_temp_config_desc mouse_config_desc = {
140 	.ppIfaceDesc = mouse_interfaces,
141 	.bmAttributes = UC_BUS_POWERED,
142 	.bMaxPower = 25,		/* 50 mA */
143 	.iConfiguration = INDEX_PRODUCT,
144 };
145 
146 static const struct usb_temp_config_desc *mouse_configs[] = {
147 	&mouse_config_desc,
148 	NULL,
149 };
150 
151 static usb_temp_get_string_desc_t mouse_get_string_desc;
152 static usb_temp_get_vendor_desc_t mouse_get_vendor_desc;
153 
154 const struct usb_temp_device_desc usb_template_mouse = {
155 	.getStringDesc = &mouse_get_string_desc,
156 	.getVendorDesc = &mouse_get_vendor_desc,
157 	.ppConfigDesc = mouse_configs,
158 	.idVendor = USB_TEMPLATE_VENDOR,
159 	.idProduct = 0x00AE,
160 	.bcdDevice = 0x0100,
161 	.bDeviceClass = UDCLASS_COMM,
162 	.bDeviceSubClass = 0,
163 	.bDeviceProtocol = 0,
164 	.iManufacturer = 0,
165 	.iProduct = INDEX_PRODUCT,
166 	.iSerialNumber = 0,
167 };
168 
169 /*------------------------------------------------------------------------*
170  *      mouse_get_vendor_desc
171  *
172  * Return values:
173  * NULL: Failure. No such vendor descriptor.
174  * Else: Success. Pointer to vendor descriptor is returned.
175  *------------------------------------------------------------------------*/
176 static const void *
177 mouse_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
178 {
179 	if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) &&
180 	    (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) &&
181 	    (req->wIndex[1] == 0) && (req->wIndex[0] == 0)) {
182 
183 		*plen = sizeof(mouse_hid_descriptor);
184 		return (mouse_hid_descriptor);
185 	}
186 	return (NULL);
187 }
188 
189 /*------------------------------------------------------------------------*
190  *	mouse_get_string_desc
191  *
192  * Return values:
193  * NULL: Failure. No such string.
194  * Else: Success. Pointer to string descriptor is returned.
195  *------------------------------------------------------------------------*/
196 static const void *
197 mouse_get_string_desc(uint16_t lang_id, uint8_t string_index)
198 {
199 	static const void *ptr[INDEX_MAX] = {
200 		[INDEX_LANG] = &usb_string_lang_en,
201 		[INDEX_MOUSE] = &string_mouse,
202 		[INDEX_PRODUCT] = &string_product,
203 	};
204 
205 	if (string_index == 0) {
206 		return (&usb_string_lang_en);
207 	}
208 	if (lang_id != 0x0409) {
209 		return (NULL);
210 	}
211 	if (string_index < INDEX_MAX) {
212 		return (ptr[string_index]);
213 	}
214 	return (NULL);
215 }
216