source: mainline/uspace/app/virtusbkbd/virtusbkbd.c@ 0e41957

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0e41957 was 4971812, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Standard requests in virtual USB device

The virtual USB device framework can handle some standard requests
alone. Again, this is more about bones than about meat. Meat will
come later.

Update `vuk' app to provide standard device descriptor.

  • Property mode set to 100644
File size: 3.7 KB
Line 
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 <usbvirt/device.h>
50#include <usbvirt/hub.h>
51#include <usbvirt/ids.h>
52
53#define LOOPS 5
54#define NAME "virt-usb-kbd"
55
56#define DEV_HCD_NAME "hcd-virt"
57
58#define __QUOTEME(x) #x
59#define _QUOTEME(x) __QUOTEME(x)
60
61#define VERBOSE_EXEC(cmd, fmt, ...) \
62 (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
63
64static int on_incoming_data(struct usbvirt_device *dev,
65 usb_endpoint_t endpoint, void *buffer, size_t size)
66{
67 printf("%s: ignoring incomming data to endpoint %d\n", NAME, endpoint);
68
69 return EOK;
70}
71
72static usb_standard_device_descriptor_t std_descriptor = {
73 .length = sizeof(usb_standard_device_descriptor_t),
74 .descriptor_type = 1,
75 .usb_spec_version = 0x110,
76 .device_class = 0x03,
77 .device_subclass = 0,
78 .device_protocol = 0,
79 .max_packet_size = 64,
80 .configuration_count = 1
81};
82
83/** Keyboard callbacks.
84 * We abuse the fact that static variables are zero-filled.
85 */
86static usbvirt_device_ops_t keyboard_ops = {
87 .on_data = on_incoming_data
88};
89
90/** Keyboard device.
91 * Rest of the items will be initialized later.
92 */
93static usbvirt_device_t keyboard_dev = {
94 .ops = &keyboard_ops,
95 .standard_descriptor = &std_descriptor,
96 .device_id_ = USBVIRT_DEV_KEYBOARD_ID
97};
98
99
100static void fibril_sleep(size_t sec)
101{
102 while (sec-- > 0) {
103 async_usleep(1000*1000);
104 }
105}
106
107
108int main(int argc, char * argv[])
109{
110 int rc = usbvirt_connect(&keyboard_dev, DEV_HCD_NAME);
111 if (rc != EOK) {
112 printf("%s: Unable to start comunication with VHCD at usb://%s (%s).\n",
113 NAME, DEV_HCD_NAME, str_error(rc));
114 return rc;
115 }
116
117 size_t i;
118 for (i = 0; i < LOOPS; i++) {
119 size_t size = 5;
120 char *data = (char *) "Hullo, World!";
121
122 if (i > 0) {
123 fibril_sleep(2);
124 }
125
126 printf("%s: Will send data to VHCD...\n", NAME);
127 int rc = keyboard_dev.send_data(&keyboard_dev, 0, data, size);
128 printf("%s: ...data sent (%s).\n", NAME, str_error(rc));
129 }
130
131 fibril_sleep(1);
132 printf("%s: Terminating...\n", NAME);
133
134 usbvirt_disconnect();
135
136 return 0;
137}
138
139
140/** @}
141 */
Note: See TracBrowser for help on using the repository browser.