source: mainline/uspace/srv/hid/input/port/chardev.c@ 5f97ef44

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5f97ef44 was 5f97ef44, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Sleep is more natural as part of the fibril API.
(the implementation will move later)

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
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 kbd_port
30 * @ingroup kbd
31 * @{
32 */
33/** @file
34 * @brief Chardev keyboard port driver.
35 */
36
37#include <async.h>
38#include <errno.h>
39#include <fibril.h>
40#include <io/chardev.h>
41#include <loc.h>
42#include <stdio.h>
43#include "../input.h"
44#include "../kbd_port.h"
45#include "../kbd.h"
46
47static errno_t kbd_port_fibril(void *);
48
49static errno_t chardev_port_init(kbd_dev_t *);
50static void chardev_port_write(uint8_t);
51
52kbd_port_ops_t chardev_port = {
53 .init = chardev_port_init,
54 .write = chardev_port_write
55};
56
57static kbd_dev_t *kbd_dev;
58static async_sess_t *dev_sess;
59static chardev_t *chardev;
60
61/** List of devices to try connecting to. */
62static const char *in_devs[] = {
63 /** S3C24xx UART - Openmoko debug console */
64 "char/s3c24xx_uart",
65 /** Ski console, MSIM console, Sun4v console */
66 "devices/\\hw\\console\\a"
67};
68
69static const unsigned int num_devs = sizeof(in_devs) / sizeof(in_devs[0]);
70
71static errno_t chardev_port_init(kbd_dev_t *kdev)
72{
73 service_id_t service_id;
74 unsigned int i;
75 fid_t fid;
76 errno_t rc;
77
78 kbd_dev = kdev;
79again:
80 for (i = 0; i < num_devs; i++) {
81 rc = loc_service_get_id(in_devs[i], &service_id, 0);
82 if (rc == EOK)
83 break;
84 }
85
86 if (i >= num_devs) {
87 /* XXX This is just a hack. */
88 printf("%s: No input device found, sleep for retry.\n", NAME);
89 fibril_usleep(1000 * 1000);
90 goto again;
91 }
92
93 dev_sess = loc_service_connect(service_id, INTERFACE_DDF,
94 IPC_FLAG_BLOCKING);
95 if (dev_sess == NULL) {
96 printf("%s: Failed connecting to device\n", NAME);
97 return ENOENT;
98 }
99
100 rc = chardev_open(dev_sess, &chardev);
101 if (rc != EOK) {
102 printf("%s: Failed opening character device\n", NAME);
103 async_hangup(dev_sess);
104 return ENOMEM;
105 }
106
107 fid = fibril_create(kbd_port_fibril, NULL);
108 if (fid == 0) {
109 printf("%s: Failed creating fibril\n", NAME);
110 chardev_close(chardev);
111 async_hangup(dev_sess);
112 return ENOMEM;
113 }
114
115 fibril_add_ready(fid);
116
117 printf("%s: Found input device '%s'\n", NAME, in_devs[i]);
118 return 0;
119}
120
121static void chardev_port_write(uint8_t data)
122{
123 errno_t rc;
124 size_t nwr;
125
126 rc = chardev_write(chardev, &data, sizeof(data), &nwr);
127 if (rc != EOK || nwr != sizeof(data)) {
128 printf("%s: Failed writing to character device\n", NAME);
129 return;
130 }
131}
132
133static errno_t kbd_port_fibril(void *arg)
134{
135 errno_t rc;
136 size_t nread;
137 uint8_t b;
138
139 while (true) {
140 rc = chardev_read(chardev, &b, sizeof(b), &nread);
141 if (rc != EOK || nread != sizeof(b)) {
142 printf("%s: Error reading data", NAME);
143 continue;
144 }
145
146 kbd_push_data(kbd_dev, b);
147 }
148
149 return 0;
150}
151
152/**
153 * @}
154 */
Note: See TracBrowser for help on using the repository browser.