Changes in uspace/drv/char/sun4v-con/sun4v-con.c [7aa94304:dd8ab1c] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/char/sun4v-con/sun4v-con.c
r7aa94304 rdd8ab1c 1 1 /* 2 2 * Copyright (c) 2008 Pavel Rimsky 3 * Copyright (c) 201 1Jiri Svoboda3 * Copyright (c) 2017 Jiri Svoboda 4 4 * All rights reserved. 5 5 * … … 36 36 #include <ddi.h> 37 37 #include <errno.h> 38 #include <ipc/char.h> 38 #include <str_error.h> 39 #include <io/chardev_srv.h> 39 40 #include <stdbool.h> 40 #include <sysinfo.h>41 #include <thread.h>42 41 43 42 #include "sun4v-con.h" … … 47 46 #define POLL_INTERVAL 10000 48 47 49 /* 50 * Kernel counterpart of the driver pushes characters (it has read) here. 51 * Keep in sync with the definition from 52 * kernel/arch/sparc64/src/drivers/niagara.c. 53 */ 54 #define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8) 48 static int sun4v_con_read(chardev_srv_t *, void *, size_t, size_t *); 49 static int sun4v_con_write(chardev_srv_t *, const void *, size_t, size_t *); 55 50 56 typedef volatile struct { 57 uint64_t write_ptr; 58 uint64_t read_ptr; 59 char data[INPUT_BUFFER_SIZE]; 60 } __attribute__((packed)) __attribute__((aligned(PAGE_SIZE))) *input_buffer_t; 61 62 /* virtual address of the shared buffer */ 63 static input_buffer_t input_buffer; 64 65 static void sun4v_thread_impl(void *arg); 51 static chardev_ops_t sun4v_con_chardev_ops = { 52 .read = sun4v_con_read, 53 .write = sun4v_con_write 54 }; 66 55 67 56 static void sun4v_con_putchar(sun4v_con_t *con, uint8_t data) 68 57 { 69 (void) con; 70 (void) data; 58 if (data == '\n') 59 sun4v_con_putchar(con, '\r'); 60 61 while (con->output_buffer->write_ptr == 62 (con->output_buffer->read_ptr + OUTPUT_BUFFER_SIZE - 1) 63 % OUTPUT_BUFFER_SIZE); 64 65 con->output_buffer->data[con->output_buffer->write_ptr] = data; 66 con->output_buffer->write_ptr = 67 ((con->output_buffer->write_ptr) + 1) % OUTPUT_BUFFER_SIZE; 71 68 } 72 69 73 70 /** Add sun4v console device. */ 74 int sun4v_con_add(sun4v_con_t *con )71 int sun4v_con_add(sun4v_con_t *con, sun4v_con_res_t *res) 75 72 { 76 73 ddf_fun_t *fun = NULL; 77 74 int rc; 78 75 79 input_buffer = (input_buffer_t) AS_AREA_ANY; 76 con->res = *res; 77 con->input_buffer = (niagara_input_buffer_t *) AS_AREA_ANY; 80 78 81 79 fun = ddf_fun_create(con->dev, fun_exposed, "a"); … … 86 84 } 87 85 86 chardev_srvs_init(&con->cds); 87 con->cds.ops = &sun4v_con_chardev_ops; 88 con->cds.sarg = con; 89 88 90 ddf_fun_set_conn_handler(fun, sun4v_con_connection); 89 91 90 sysarg_t paddr;91 rc = sysinfo_get_value("niagara.inbuf.address", &paddr);92 rc = physmem_map(res->in_base, 1, AS_AREA_READ | AS_AREA_WRITE, 93 (void *) &con->input_buffer); 92 94 if (rc != EOK) { 93 ddf_msg(LVL_ERROR, " niagara.inbuf.address not set (%d)", rc);95 ddf_msg(LVL_ERROR, "Error mapping memory: %s", str_error_name(rc)); 94 96 goto error; 95 97 } 96 98 97 rc = physmem_map(paddr, 1, AS_AREA_READ | AS_AREA_WRITE, 98 (void *) &input_buffer); 99 con->output_buffer = (niagara_output_buffer_t *) AS_AREA_ANY; 100 101 rc = physmem_map(res->out_base, 1, AS_AREA_READ | AS_AREA_WRITE, 102 (void *) &con->output_buffer); 99 103 if (rc != EOK) { 100 ddf_msg(LVL_ERROR, "Error mapping memory: % d", rc);101 goto error;104 ddf_msg(LVL_ERROR, "Error mapping memory: %s", str_error_name(rc)); 105 return rc; 102 106 } 103 104 thread_id_t tid;105 rc = thread_create(sun4v_thread_impl, con, "kbd_poll", &tid);106 if (rc != EOK)107 goto error;108 107 109 108 rc = ddf_fun_bind(fun); … … 113 112 } 114 113 114 ddf_fun_add_to_category(fun, "console"); 115 115 116 return EOK; 116 117 error: 117 /* XXX Clean up thread */ 118 if (con->input_buffer != (niagara_input_buffer_t *) AS_AREA_ANY) 119 physmem_unmap((void *) con->input_buffer); 118 120 119 if ( input_buffer != (input_buffer_t) AS_AREA_ANY)120 physmem_unmap((void *) input_buffer);121 if (con->output_buffer != (niagara_output_buffer_t *) AS_AREA_ANY) 122 physmem_unmap((void *) con->output_buffer); 121 123 122 124 if (fun != NULL) … … 138 140 } 139 141 140 /** 141 * Called regularly by the polling thread. Reads codes of all the 142 * pressed keys from the buffer. 143 */ 144 static void sun4v_key_pressed(sun4v_con_t *con) 142 /** Read from Sun4v console device */ 143 static int sun4v_con_read(chardev_srv_t *srv, void *buf, size_t size, 144 size_t *nread) 145 145 { 146 sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg; 147 size_t p; 148 uint8_t *bp = (uint8_t *) buf; 146 149 char c; 147 150 148 while (input_buffer->read_ptr != input_buffer->write_ptr) { 149 c = input_buffer->data[input_buffer->read_ptr]; 150 input_buffer->read_ptr = 151 ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE; 152 if (con->client_sess != NULL) { 153 async_exch_t *exch = async_exchange_begin(con->client_sess); 154 async_msg_1(exch, CHAR_NOTIF_BYTE, c); 155 async_exchange_end(exch); 156 } 157 (void) c; 151 while (con->input_buffer->read_ptr == con->input_buffer->write_ptr) 152 async_usleep(POLL_INTERVAL); 153 154 p = 0; 155 while (p < size && con->input_buffer->read_ptr != con->input_buffer->write_ptr) { 156 c = con->input_buffer->data[con->input_buffer->read_ptr]; 157 con->input_buffer->read_ptr = 158 ((con->input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE; 159 bp[p++] = c; 158 160 } 161 162 *nread = p; 163 return EOK; 159 164 } 160 165 161 /** 162 * Thread to poll Sun4v console for keypresses. 163 */ 164 static void sun4v_thread_impl(void *arg) 166 /** Write to Sun4v console device */ 167 static int sun4v_con_write(chardev_srv_t *srv, const void *data, size_t size, 168 size_t *nwr) 165 169 { 166 sun4v_con_t *con = (sun4v_con_t *) arg; 170 sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg; 171 size_t i; 172 uint8_t *dp = (uint8_t *) data; 167 173 168 while (true) { 169 sun4v_key_pressed(con); 170 thread_usleep(POLL_INTERVAL); 171 } 174 for (i = 0; i < size; i++) 175 sun4v_con_putchar(con, dp[i]); 176 177 *nwr = size; 178 return EOK; 172 179 } 173 180 … … 176 183 void *arg) 177 184 { 178 sun4v_con_t *con; 185 sun4v_con_t *con = (sun4v_con_t *) ddf_dev_data_get( 186 ddf_fun_get_dev((ddf_fun_t *) arg)); 179 187 180 /* Answer the IPC_M_CONNECT_ME_TO call. */ 181 async_answer_0(iid, EOK); 182 183 con = (sun4v_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg)); 184 185 while (true) { 186 ipc_call_t call; 187 ipc_callid_t callid = async_get_call(&call); 188 sysarg_t method = IPC_GET_IMETHOD(call); 189 190 if (!method) { 191 /* The other side has hung up. */ 192 async_answer_0(callid, EOK); 193 return; 194 } 195 196 async_sess_t *sess = 197 async_callback_receive_start(EXCHANGE_SERIALIZE, &call); 198 if (sess != NULL) { 199 if (con->client_sess == NULL) { 200 con->client_sess = sess; 201 async_answer_0(callid, EOK); 202 } else 203 async_answer_0(callid, ELIMIT); 204 } else { 205 switch (method) { 206 case CHAR_WRITE_BYTE: 207 ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n", 208 IPC_GET_ARG1(call)); 209 sun4v_con_putchar(con, (uint8_t) IPC_GET_ARG1(call)); 210 async_answer_0(callid, EOK); 211 break; 212 default: 213 async_answer_0(callid, EINVAL); 214 } 215 } 216 } 188 chardev_conn(iid, icall, &con->cds); 217 189 } 218 190
Note:
See TracChangeset
for help on using the changeset viewer.