source: mainline/uspace/app/mkbd/main.c@ 293de44

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 293de44 was b2995c3, checked in by Lubos Slovak <lubos.slovak@…>, 15 years ago

Fixes in mkbd (includes, removed binary)

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2011 Lubos Slovak
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 mkbd
30 * @{
31 */
32/**
33 * @file
34 * Sample application using the data from multimedia keys on keyboard
35 */
36
37#include <inttypes.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <errno.h>
41#include <str_error.h>
42#include <bool.h>
43#include <getopt.h>
44#include <devman.h>
45#include <devmap.h>
46#include <usb/dev/hub.h>
47#include <usb/host.h>
48#include <usb/driver.h>
49#include <usb/dev/pipes.h>
50
51#define NAME "mkbd"
52
53static int dev_phone = -1;
54
55//static void print_found_hc(size_t class_index, const char *path)
56//{
57// // printf(NAME ": host controller %zu is `%s'.\n", class_index, path);
58// printf("Bus %02zu: %s\n", class_index, path);
59//}
60//static void print_found_dev(usb_address_t addr, const char *path)
61//{
62// // printf(NAME ": device with address %d is `%s'.\n", addr, path);
63// printf(" Device %02d: %s\n", addr, path);
64//}
65
66//static void print_hc_devices(devman_handle_t hc_handle)
67//{
68// int rc;
69// usb_hc_connection_t conn;
70
71// usb_hc_connection_initialize(&conn, hc_handle);
72// rc = usb_hc_connection_open(&conn);
73// if (rc != EOK) {
74// printf(NAME ": failed to connect to HC: %s.\n",
75// str_error(rc));
76// return;
77// }
78// usb_address_t addr;
79// for (addr = 1; addr < 5; addr++) {
80// devman_handle_t dev_handle;
81// rc = usb_hc_get_handle_by_address(&conn, addr, &dev_handle);
82// if (rc != EOK) {
83// continue;
84// }
85// char path[MAX_PATH_LENGTH];
86// rc = devman_get_device_path(dev_handle, path, MAX_PATH_LENGTH);
87// if (rc != EOK) {
88// continue;
89// }
90// print_found_dev(addr, path);
91// }
92// usb_hc_connection_close(&conn);
93//}
94
95static bool try_parse_class_and_address(const char *path,
96 devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
97{
98 size_t class_index;
99 size_t address;
100 int rc;
101 char *ptr;
102
103 rc = str_size_t(path, &ptr, 10, false, &class_index);
104 if (rc != EOK) {
105 return false;
106 }
107 if ((*ptr == ':') || (*ptr == '.')) {
108 ptr++;
109 } else {
110 return false;
111 }
112 rc = str_size_t(ptr, NULL, 10, true, &address);
113 if (rc != EOK) {
114 return false;
115 }
116 rc = usb_ddf_get_hc_handle_by_class(class_index, out_hc_handle);
117 if (rc != EOK) {
118 return false;
119 }
120 if (out_device_address != NULL) {
121 *out_device_address = (usb_address_t) address;
122 }
123 return true;
124}
125
126static bool resolve_hc_handle_and_dev_addr(const char *devpath,
127 devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
128{
129 int rc;
130
131 /* Hack for QEMU to save-up on typing ;-). */
132 if (str_cmp(devpath, "qemu") == 0) {
133 devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1";
134 }
135
136 /* Hack for virtual keyboard. */
137 if (str_cmp(devpath, "virt") == 0) {
138 devpath = "/virt/usbhc/usb00_a1/usb00_a2";
139 }
140
141 if (try_parse_class_and_address(devpath,
142 out_hc_handle, out_device_address)) {
143 return true;
144 }
145
146 char *path = str_dup(devpath);
147 if (path == NULL) {
148 return ENOMEM;
149 }
150
151 devman_handle_t hc = 0;
152 bool hc_found = false;
153 usb_address_t addr = 0;
154 bool addr_found = false;
155
156 /* Remove suffixes and hope that we will encounter device node. */
157 while (str_length(path) > 0) {
158 /* Get device handle first. */
159 devman_handle_t dev_handle;
160 rc = devman_device_get_handle(path, &dev_handle, 0);
161 if (rc != EOK) {
162 free(path);
163 return false;
164 }
165
166 /* Try to find its host controller. */
167 if (!hc_found) {
168 rc = usb_hc_find(dev_handle, &hc);
169 if (rc == EOK) {
170 hc_found = true;
171 }
172 }
173 /* Try to get its address. */
174 if (!addr_found) {
175 addr = usb_device_get_assigned_address(dev_handle);
176 if (addr >= 0) {
177 addr_found = true;
178 }
179 }
180
181 /* Speed-up. */
182 if (hc_found && addr_found) {
183 break;
184 }
185
186 /* Remove the last suffix. */
187 char *slash_pos = str_rchr(path, '/');
188 if (slash_pos != NULL) {
189 *slash_pos = 0;
190 }
191 }
192
193 free(path);
194
195 if (hc_found && addr_found) {
196 if (out_hc_handle != NULL) {
197 *out_hc_handle = hc;
198 }
199 if (out_device_address != NULL) {
200 *out_device_address = addr;
201 }
202 return true;
203 } else {
204 return false;
205 }
206}
207
208static void print_usage(char *app_name)
209{
210#define _INDENT " "
211
212 printf(NAME ": Print out what multimedia keys were pressed.\n\n");
213 printf("Usage: %s device\n", app_name);
214 printf(_INDENT "The device is a devman path to the device.\n");
215
216#undef _OPTION
217#undef _INDENT
218}
219
220int main(int argc, char *argv[])
221{
222
223 if (argc <= 1) {
224 print_usage(argv[0]);
225 return -1;
226 }
227
228 char *devpath = argv[1];
229
230 /* The initialization is here only to make compiler happy. */
231 devman_handle_t hc_handle = 0;
232 usb_address_t dev_addr = 0;
233 bool found = resolve_hc_handle_and_dev_addr(devpath,
234 &hc_handle, &dev_addr);
235 if (!found) {
236 fprintf(stderr, NAME ": device `%s' not found "
237 "or not of USB kind. Exiting.\n", devpath);
238 return -1;
239 }
240
241 int rc;
242 usb_hc_connection_t conn;
243
244 usb_hc_connection_initialize(&conn, hc_handle);
245 rc = usb_hc_connection_open(&conn);
246 if (rc != EOK) {
247 printf(NAME ": failed to connect to HC: %s.\n",
248 str_error(rc));
249 return -1;
250 }
251 usb_address_t addr = 0;
252
253 devman_handle_t dev_handle;
254 rc = usb_hc_get_handle_by_address(&conn, addr, &dev_handle);
255 if (rc != EOK) {
256 printf(NAME ": failed getting handle to the device: %s.\n",
257 str_error(rc));
258 return -1;
259 }
260
261 usb_hc_connection_close(&conn);
262
263 rc = devman_device_connect(dev_handle, 0);
264 if (rc < 0) {
265 printf(NAME ": failed to connect to the device: %s.\n",
266 str_error(rc));
267 return -1;
268 }
269
270 dev_phone = rc;
271 printf("Got phone to the device: %d\n", dev_phone);
272
273
274// size_t class_index = 0;
275// size_t failed_attempts = 0;
276
277// while (failed_attempts < MAX_FAILED_ATTEMPTS) {
278// class_index++;
279// devman_handle_t hc_handle = 0;
280// int rc = usb_ddf_get_hc_handle_by_class(class_index, &hc_handle);
281// if (rc != EOK) {
282// failed_attempts++;
283// continue;
284// }
285// char path[MAX_PATH_LENGTH];
286// rc = devman_get_device_path(hc_handle, path, MAX_PATH_LENGTH);
287// if (rc != EOK) {
288// continue;
289// }
290// print_found_hc(class_index, path);
291// print_hc_devices(hc_handle);
292// }
293
294 return 0;
295}
296
297
298/** @}
299 */
Note: See TracBrowser for help on using the repository browser.