1 | /*
|
---|
2 | * Copyright (c) 2010 Vojtech Horky
|
---|
3 | * 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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup usb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Virtual USB keyboard.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <unistd.h>
|
---|
40 | #include <vfs/vfs.h>
|
---|
41 | #include <fcntl.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <str_error.h>
|
---|
44 | #include <bool.h>
|
---|
45 | #include <async.h>
|
---|
46 |
|
---|
47 | #include <usb/hcd.h>
|
---|
48 | #include <usb/device.h>
|
---|
49 | #include <usb/hid.h>
|
---|
50 | #include <usbvirt/device.h>
|
---|
51 | #include <usbvirt/hub.h>
|
---|
52 | #include <usbvirt/ids.h>
|
---|
53 |
|
---|
54 | #define LOOPS 5
|
---|
55 | #define NAME "virt-usb-kbd"
|
---|
56 |
|
---|
57 | #define DEV_HCD_NAME "hcd-virt"
|
---|
58 |
|
---|
59 | #define __QUOTEME(x) #x
|
---|
60 | #define _QUOTEME(x) __QUOTEME(x)
|
---|
61 |
|
---|
62 | #define VERBOSE_EXEC(cmd, fmt, ...) \
|
---|
63 | (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
|
---|
64 |
|
---|
65 | static int on_incoming_data(struct usbvirt_device *dev,
|
---|
66 | usb_endpoint_t endpoint, void *buffer, size_t size)
|
---|
67 | {
|
---|
68 | printf("%s: ignoring incomming data to endpoint %d\n", NAME, endpoint);
|
---|
69 |
|
---|
70 | return EOK;
|
---|
71 | }
|
---|
72 |
|
---|
73 | static int on_class_request(struct usbvirt_device *dev,
|
---|
74 | usb_device_request_setup_packet_t *request, uint8_t *data)
|
---|
75 | {
|
---|
76 | printf("%s: class request (%d)\n", NAME, (int) request->request);
|
---|
77 |
|
---|
78 | return EOK;
|
---|
79 | }
|
---|
80 |
|
---|
81 | static usb_standard_device_descriptor_t std_descriptor = {
|
---|
82 | .length = sizeof(usb_standard_device_descriptor_t),
|
---|
83 | .descriptor_type = 1,
|
---|
84 | .usb_spec_version = 0x110,
|
---|
85 | .device_class = 0x03,
|
---|
86 | .device_subclass = 0,
|
---|
87 | .device_protocol = 0,
|
---|
88 | .max_packet_size = 64,
|
---|
89 | .configuration_count = 1
|
---|
90 | };
|
---|
91 |
|
---|
92 | /** Keyboard callbacks.
|
---|
93 | * We abuse the fact that static variables are zero-filled.
|
---|
94 | */
|
---|
95 | static usbvirt_device_ops_t keyboard_ops = {
|
---|
96 | .on_devreq_class = on_class_request,
|
---|
97 | .on_data = on_incoming_data
|
---|
98 | };
|
---|
99 |
|
---|
100 | /** Keyboard device.
|
---|
101 | * Rest of the items will be initialized later.
|
---|
102 | */
|
---|
103 | static usbvirt_device_t keyboard_dev = {
|
---|
104 | .ops = &keyboard_ops,
|
---|
105 | .standard_descriptor = &std_descriptor,
|
---|
106 | .device_id_ = USBVIRT_DEV_KEYBOARD_ID
|
---|
107 | };
|
---|
108 |
|
---|
109 |
|
---|
110 | static void fibril_sleep(size_t sec)
|
---|
111 | {
|
---|
112 | while (sec-- > 0) {
|
---|
113 | async_usleep(1000*1000);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | int main(int argc, char * argv[])
|
---|
119 | {
|
---|
120 | int rc = usbvirt_connect(&keyboard_dev, DEV_HCD_NAME);
|
---|
121 | if (rc != EOK) {
|
---|
122 | printf("%s: Unable to start comunication with VHCD at usb://%s (%s).\n",
|
---|
123 | NAME, DEV_HCD_NAME, str_error(rc));
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 |
|
---|
127 | size_t i;
|
---|
128 | for (i = 0; i < LOOPS; i++) {
|
---|
129 | size_t size = 5;
|
---|
130 | char *data = (char *) "Hullo, World!";
|
---|
131 |
|
---|
132 | if (i > 0) {
|
---|
133 | fibril_sleep(2);
|
---|
134 | }
|
---|
135 |
|
---|
136 | printf("%s: Will send data to VHCD...\n", NAME);
|
---|
137 | int rc = keyboard_dev.send_data(&keyboard_dev, 0, data, size);
|
---|
138 | printf("%s: ...data sent (%s).\n", NAME, str_error(rc));
|
---|
139 | }
|
---|
140 |
|
---|
141 | fibril_sleep(1);
|
---|
142 | printf("%s: Terminating...\n", NAME);
|
---|
143 |
|
---|
144 | usbvirt_disconnect();
|
---|
145 |
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /** @}
|
---|
151 | */
|
---|