1 /*
2  * Copyright (c) 2008-2015 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #pragma once
24 
25 #include <lk/compiler.h>
26 #include <lk/list.h>
27 #include <sys/types.h>
28 
29 /* LK specific calls to register to get input/output of the main console */
30 
31 __BEGIN_CDECLS
32 
33 typedef struct __print_callback print_callback_t;
34 struct __print_callback {
35     struct list_node entry;
36     void (*print)(print_callback_t *cb, const char *str, size_t len);
37     void (*commit)(print_callback_t *cb);
38     void *context;
39 };
40 
41 /* register callback to receive debug prints */
42 void register_print_callback(print_callback_t *cb);
43 void unregister_print_callback(print_callback_t *cb);
44 
45 /* the underlying handle to talk to io devices */
46 struct io_handle;
47 typedef struct io_handle_hooks {
48     ssize_t (*write)(struct io_handle *handle, const char *buf, size_t len);
49     void (*write_commit)(struct io_handle *handle);
50     ssize_t (*read)(struct io_handle *handle, char *buf, size_t len);
51     void (*lock)(struct io_handle *handle);
52     void (*unlock)(struct io_handle *handle);
53 } io_handle_hooks_t;
54 
55 #define IO_HANDLE_MAGIC (0x696f6820)  // "ioh "
56 
57 typedef struct io_handle {
58     uint32_t magic;
59     const io_handle_hooks_t *hooks;
60 } io_handle_t;
61 
62 /* routines to call through the io handle */
63 ssize_t io_write(io_handle_t *io, const char *buf, size_t len);
64 ssize_t io_read(io_handle_t *io, char *buf, size_t len);
65 
66 /* lower-level io operations */
67 ssize_t io_write_partial(io_handle_t *io, const char *buf, size_t len);
68 void io_write_commit(io_handle_t *io);
69 void io_lock(io_handle_t *io);
70 void io_unlock(io_handle_t *io);
71 
72 /* initialization routine */
73 #define IO_HANDLE_INITIAL_VALUE(_hooks) { .magic = IO_HANDLE_MAGIC, .hooks = _hooks }
74 
io_handle_init(io_handle_t * io,io_handle_hooks_t * hooks)75 static inline void io_handle_init(io_handle_t *io, io_handle_hooks_t *hooks)
76 {
77     *io = (io_handle_t)IO_HANDLE_INITIAL_VALUE(hooks);
78 }
79 
80 /* the main console io handle */
81 extern io_handle_t console_io;
82 
83 #ifndef CONSOLE_HAS_INPUT_BUFFER
84 #define CONSOLE_HAS_INPUT_BUFFER 0
85 #endif
86 
87 #if CONSOLE_HAS_INPUT_BUFFER
88 /* main input circular buffer that acts as the default input queue */
89 typedef struct cbuf cbuf_t;
90 extern cbuf_t console_input_cbuf;
91 #endif
92 
93 __END_CDECLS
94