usb4bsd: FWIW, port usb_template and hook it into the build.
[dragonfly.git] / sys / bus / u4b / template / usb_template_kbd.c
1 /* $FreeBSD: head/sys/dev/usb/template/usb_template_kbd.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 Keyboard 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/priv.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_KEYBOARD,
61         INDEX_PRODUCT,
62         INDEX_MAX,
63 };
64
65 #define STRING_PRODUCT \
66   "K\0e\0y\0b\0o\0a\0r\0d\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e"
67
68 #define STRING_KEYBOARD \
69   "K\0e\0y\0b\0o\0a\0r\0d\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
70
71 /* make the real string descriptors */
72
73 USB_MAKE_STRING_DESC(STRING_KEYBOARD, string_keyboard);
74 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product);
75
76 /* prototypes */
77
78 static const struct usb_temp_packet_size keyboard_intr_mps = {
79         .mps[USB_SPEED_LOW] = 16,
80         .mps[USB_SPEED_FULL] = 16,
81         .mps[USB_SPEED_HIGH] = 16,
82 };
83
84 static const struct usb_temp_interval keyboard_intr_interval = {
85         .bInterval[USB_SPEED_LOW] = 2,  /* 2 ms */
86         .bInterval[USB_SPEED_FULL] = 2, /* 2 ms */
87         .bInterval[USB_SPEED_HIGH] = 5, /* 2 ms */
88 };
89
90 /* The following HID descriptor was dumped from a HP keyboard. */
91
92 static uint8_t keyboard_hid_descriptor[] = {
93         0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07,
94         0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01,
95         0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01,
96         0x75, 0x08, 0x81, 0x01, 0x95, 0x03, 0x75, 0x01,
97         0x05, 0x08, 0x19, 0x01, 0x29, 0x03, 0x91, 0x02,
98         0x95, 0x05, 0x75, 0x01, 0x91, 0x01, 0x95, 0x06,
99         0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
100         0x07, 0x19, 0x00, 0x2a, 0xff, 0x00, 0x81, 0x00,
101         0xc0
102 };
103
104 static const struct usb_temp_endpoint_desc keyboard_ep_0 = {
105         .ppRawDesc = NULL,              /* no raw descriptors */
106         .pPacketSize = &keyboard_intr_mps,
107         .pIntervals = &keyboard_intr_interval,
108         .bEndpointAddress = UE_DIR_IN,
109         .bmAttributes = UE_INTERRUPT,
110 };
111
112 static const struct usb_temp_endpoint_desc *keyboard_endpoints[] = {
113         &keyboard_ep_0,
114         NULL,
115 };
116
117 static const uint8_t keyboard_raw_desc[] = {
118         0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(keyboard_hid_descriptor),
119         0x00
120 };
121
122 static const void *keyboard_iface_0_desc[] = {
123         keyboard_raw_desc,
124         NULL,
125 };
126
127 static const struct usb_temp_interface_desc keyboard_iface_0 = {
128         .ppRawDesc = keyboard_iface_0_desc,
129         .ppEndpoints = keyboard_endpoints,
130         .bInterfaceClass = 3,
131         .bInterfaceSubClass = 1,
132         .bInterfaceProtocol = 1,
133         .iInterface = INDEX_KEYBOARD,
134 };
135
136 static const struct usb_temp_interface_desc *keyboard_interfaces[] = {
137         &keyboard_iface_0,
138         NULL,
139 };
140
141 static const struct usb_temp_config_desc keyboard_config_desc = {
142         .ppIfaceDesc = keyboard_interfaces,
143         .bmAttributes = UC_BUS_POWERED,
144         .bMaxPower = 25,                /* 50 mA */
145         .iConfiguration = INDEX_PRODUCT,
146 };
147
148 static const struct usb_temp_config_desc *keyboard_configs[] = {
149         &keyboard_config_desc,
150         NULL,
151 };
152
153 static usb_temp_get_string_desc_t keyboard_get_string_desc;
154 static usb_temp_get_vendor_desc_t keyboard_get_vendor_desc;
155
156 const struct usb_temp_device_desc usb_template_kbd = {
157         .getStringDesc = &keyboard_get_string_desc,
158         .getVendorDesc = &keyboard_get_vendor_desc,
159         .ppConfigDesc = keyboard_configs,
160         .idVendor = USB_TEMPLATE_VENDOR,
161         .idProduct = 0x00CB,
162         .bcdDevice = 0x0100,
163         .bDeviceClass = UDCLASS_COMM,
164         .bDeviceSubClass = 0,
165         .bDeviceProtocol = 0,
166         .iManufacturer = 0,
167         .iProduct = INDEX_PRODUCT,
168         .iSerialNumber = 0,
169 };
170
171 /*------------------------------------------------------------------------*
172  *      keyboard_get_vendor_desc
173  *
174  * Return values:
175  * NULL: Failure. No such vendor descriptor.
176  * Else: Success. Pointer to vendor descriptor is returned.
177  *------------------------------------------------------------------------*/
178 static const void *
179 keyboard_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
180 {
181         if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) &&
182             (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) &&
183             (req->wIndex[1] == 0) && (req->wIndex[0] == 0)) {
184
185                 *plen = sizeof(keyboard_hid_descriptor);
186                 return (keyboard_hid_descriptor);
187         }
188         return (NULL);
189 }
190
191 /*------------------------------------------------------------------------*
192  *      keyboard_get_string_desc
193  *
194  * Return values:
195  * NULL: Failure. No such string.
196  * Else: Success. Pointer to string descriptor is returned.
197  *------------------------------------------------------------------------*/
198 static const void *
199 keyboard_get_string_desc(uint16_t lang_id, uint8_t string_index)
200 {
201         static const void *ptr[INDEX_MAX] = {
202                 [INDEX_LANG] = &usb_string_lang_en,
203                 [INDEX_KEYBOARD] = &string_keyboard,
204                 [INDEX_PRODUCT] = &string_product,
205         };
206
207         if (string_index == 0) {
208                 return (&usb_string_lang_en);
209         }
210         if (lang_id != 0x0409) {
211                 return (NULL);
212         }
213         if (string_index < INDEX_MAX) {
214                 return (ptr[string_index]);
215         }
216         return (NULL);
217 }