Changeset 7a6065c in mainline for uspace/drv/char
- Timestamp:
- 2017-11-23T11:56:31Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 221176c1
- Parents:
- 74017ce
- git-author:
- Jiri Svoboda <jiri@…> (2017-11-22 19:55:36)
- git-committer:
- Jiri Svoboda <jiri@…> (2017-11-23 11:56:31)
- Location:
- uspace/drv/char
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/char/msim-con/msim-con.c
r74017ce r7a6065c 37 37 #include <ddi.h> 38 38 #include <errno.h> 39 #include <i pc/char.h>39 #include <io/chardev_srv.h> 40 40 41 41 #include "msim-con.h" 42 42 43 43 static void msim_con_connection(ipc_callid_t, ipc_call_t *, void *); 44 45 static int msim_con_read(chardev_srv_t *, void *, size_t, size_t *); 46 static int msim_con_write(chardev_srv_t *, const void *, size_t, size_t *); 47 48 static chardev_ops_t msim_con_chardev_ops = { 49 .read = msim_con_read, 50 .write = msim_con_write 51 }; 44 52 45 53 static irq_cmd_t msim_cmds_proto[] = { … … 58 66 msim_con_t *con = (msim_con_t *) arg; 59 67 uint8_t c; 68 int rc; 69 70 fibril_mutex_lock(&con->buf_lock); 60 71 61 72 c = IPC_GET_ARG2(*call); 62 63 if ( con->client_sess != NULL) {64 async_exch_t *exch = async_exchange_begin(con->client_sess);65 async_msg_1(exch, CHAR_NOTIF_BYTE, c); 66 async_exchange_end(exch);67 }73 rc = circ_buf_push(&con->cbuf, &c); 74 if (rc != EOK) 75 ddf_msg(LVL_ERROR, "Buffer overrun"); 76 77 fibril_mutex_unlock(&con->buf_lock); 78 fibril_condvar_broadcast(&con->buf_cv); 68 79 } 69 80 … … 75 86 irq_cmd_t *msim_cmds = NULL; 76 87 int rc; 88 89 circ_buf_init(&con->cbuf, con->buf, msim_con_buf_size, 1); 90 fibril_mutex_initialize(&con->buf_lock); 91 fibril_condvar_initialize(&con->buf_cv); 77 92 78 93 msim_cmds = malloc(sizeof(msim_cmds_proto)); … … 106 121 async_irq_subscribe(res->irq, msim_irq_handler, con, &con->irq_code); 107 122 subscribed = true; 123 124 chardev_srvs_init(&con->cds); 125 con->cds.ops = &msim_con_chardev_ops; 126 con->cds.sarg = con; 108 127 109 128 rc = ddf_fun_bind(fun); … … 140 159 } 141 160 161 /** Read from msim console device */ 162 static int msim_con_read(chardev_srv_t *srv, void *buf, size_t size, 163 size_t *nread) 164 { 165 msim_con_t *con = (msim_con_t *) srv->srvs->sarg; 166 size_t p; 167 uint8_t *bp = (uint8_t *) buf; 168 int rc; 169 170 fibril_mutex_lock(&con->buf_lock); 171 172 while (circ_buf_nused(&con->cbuf) == 0) 173 fibril_condvar_wait(&con->buf_cv, &con->buf_lock); 174 175 p = 0; 176 while (p < size) { 177 rc = circ_buf_pop(&con->cbuf, &bp[p]); 178 if (rc != EOK) 179 break; 180 ++p; 181 } 182 183 fibril_mutex_unlock(&con->buf_lock); 184 185 *nread = p; 186 return EOK; 187 } 188 189 /** Write to msim console device */ 190 static int msim_con_write(chardev_srv_t *srv, const void *data, size_t size, 191 size_t *nwr) 192 { 193 msim_con_t *con = (msim_con_t *) srv->srvs->sarg; 194 size_t i; 195 uint8_t *dp = (uint8_t *) data; 196 197 for (i = 0; i < size; i++) 198 msim_con_putchar(con, dp[i]); 199 200 *nwr = size; 201 return EOK; 202 } 203 142 204 /** Character device connection handler. */ 143 205 static void msim_con_connection(ipc_callid_t iid, ipc_call_t *icall, 144 206 void *arg) 145 207 { 146 msim_con_t *con; 147 148 /* Answer the IPC_M_CONNECT_ME_TO call. */ 149 async_answer_0(iid, EOK); 150 151 con = (msim_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg)); 152 153 while (true) { 154 ipc_call_t call; 155 ipc_callid_t callid = async_get_call(&call); 156 sysarg_t method = IPC_GET_IMETHOD(call); 157 158 if (!method) { 159 /* The other side has hung up. */ 160 async_answer_0(callid, EOK); 161 return; 162 } 163 164 async_sess_t *sess = 165 async_callback_receive_start(EXCHANGE_SERIALIZE, &call); 166 if (sess != NULL) { 167 if (con->client_sess == NULL) { 168 con->client_sess = sess; 169 async_answer_0(callid, EOK); 170 } else 171 async_answer_0(callid, ELIMIT); 172 } else { 173 switch (method) { 174 case CHAR_WRITE_BYTE: 175 ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n", 176 IPC_GET_ARG1(call)); 177 msim_con_putchar(con, (uint8_t) IPC_GET_ARG1(call)); 178 async_answer_0(callid, EOK); 179 break; 180 default: 181 async_answer_0(callid, EINVAL); 182 } 183 } 184 } 208 msim_con_t *con = (msim_con_t *) ddf_dev_data_get( 209 ddf_fun_get_dev((ddf_fun_t *) arg)); 210 211 chardev_conn(iid, icall, &con->cds); 185 212 } 186 213 -
uspace/drv/char/msim-con/msim-con.h
r74017ce r7a6065c 36 36 #define MSIM_CON_H 37 37 38 #include <adt/circ_buf.h> 38 39 #include <async.h> 39 40 #include <ddf/driver.h> 41 #include <fibril_synch.h> 42 #include <io/chardev_srv.h> 40 43 #include <loc.h> 41 44 #include <stdint.h> 45 46 enum { 47 msim_con_buf_size = 64 48 }; 42 49 43 50 /** MSIM console resources */ … … 51 58 async_sess_t *client_sess; 52 59 ddf_dev_t *dev; 60 chardev_srvs_t cds; 53 61 msim_con_res_t res; 54 62 irq_pio_range_t irq_range[1]; 55 63 irq_code_t irq_code; 64 circ_buf_t cbuf; 65 uint8_t buf[msim_con_buf_size]; 66 fibril_mutex_t buf_lock; 67 fibril_condvar_t buf_cv; 56 68 } msim_con_t; 57 69 -
uspace/drv/char/ski-con/ski-con.c
r74017ce r7a6065c 1 1 /* 2 2 * Copyright (c) 2005 Jakub Jermar 3 * Copyright (c) 201 1Jiri Svoboda3 * Copyright (c) 2017 Jiri Svoboda 4 4 * All rights reserved. 5 5 * … … 34 34 #include <ddf/log.h> 35 35 #include <errno.h> 36 #include <ipc/char.h> 36 #include <fibril.h> 37 #include <io/chardev.h> 37 38 #include <stdint.h> 38 39 #include <stdlib.h> 39 #include <thread.h>40 40 #include <stdbool.h> 41 41 … … 46 46 #define POLL_INTERVAL 10000 47 47 48 static void ski_con_thread_impl(void *arg);48 static int ski_con_fibril(void *arg); 49 49 static int32_t ski_con_getchar(void); 50 50 static void ski_con_connection(ipc_callid_t, ipc_call_t *, void *); 51 51 52 static int ski_con_read(chardev_srv_t *, void *, size_t, size_t *); 53 static int ski_con_write(chardev_srv_t *, const void *, size_t, size_t *); 54 55 static chardev_ops_t ski_con_chardev_ops = { 56 .read = ski_con_read, 57 .write = ski_con_write 58 }; 59 52 60 /** Add ski console device. */ 53 61 int ski_con_add(ski_con_t *con) 54 62 { 55 thread_id_t tid;63 fid_t fid; 56 64 ddf_fun_t *fun = NULL; 57 65 bool bound = false; 58 66 int rc; 67 68 circ_buf_init(&con->cbuf, con->buf, ski_con_buf_size, 1); 69 fibril_mutex_initialize(&con->buf_lock); 70 fibril_condvar_initialize(&con->buf_cv); 59 71 60 72 fun = ddf_fun_create(con->dev, fun_exposed, "a"); … … 67 79 ddf_fun_set_conn_handler(fun, ski_con_connection); 68 80 81 chardev_srvs_init(&con->cds); 82 con->cds.ops = &ski_con_chardev_ops; 83 con->cds.sarg = con; 84 69 85 rc = ddf_fun_bind(fun); 70 86 if (rc != EOK) { … … 75 91 bound = true; 76 92 77 rc = thread_create(ski_con_thread_impl, con, "kbd_poll", &tid); 78 if (rc != 0) { 79 return rc; 80 } 81 93 fid = fibril_create(ski_con_fibril, con); 94 if (fid == 0) { 95 ddf_msg(LVL_ERROR, "Error creating fibril."); 96 rc = ENOMEM; 97 goto error; 98 } 99 100 fibril_add_ready(fid); 82 101 return EOK; 83 102 error: … … 102 121 } 103 122 104 /** Thread to poll Ski for keypresses. */105 static void ski_con_thread_impl(void *arg)123 /** Poll Ski for keypresses. */ 124 static int ski_con_fibril(void *arg) 106 125 { 107 126 int32_t c; 108 127 ski_con_t *con = (ski_con_t *) arg; 128 int rc; 109 129 110 130 while (1) { … … 114 134 break; 115 135 116 if (con->client_sess != NULL) { 117 async_exch_t *exch = async_exchange_begin(con->client_sess); 118 async_msg_1(exch, CHAR_NOTIF_BYTE, c); 119 async_exchange_end(exch); 120 } 136 fibril_mutex_lock(&con->buf_lock); 137 138 rc = circ_buf_push(&con->cbuf, &c); 139 if (rc != EOK) 140 ddf_msg(LVL_ERROR, "Buffer overrun"); 141 142 fibril_mutex_unlock(&con->buf_lock); 143 fibril_condvar_broadcast(&con->buf_cv); 121 144 } 122 145 123 thread_usleep(POLL_INTERVAL); 124 } 146 fibril_usleep(POLL_INTERVAL); 147 } 148 149 return 0; 125 150 } 126 151 … … 157 182 } 158 183 184 /** Read from Ski console device */ 185 static int ski_con_read(chardev_srv_t *srv, void *buf, size_t size, 186 size_t *nread) 187 { 188 ski_con_t *con = (ski_con_t *) srv->srvs->sarg; 189 size_t p; 190 uint8_t *bp = (uint8_t *) buf; 191 int rc; 192 193 fibril_mutex_lock(&con->buf_lock); 194 195 while (circ_buf_nused(&con->cbuf) == 0) 196 fibril_condvar_wait(&con->buf_cv, &con->buf_lock); 197 198 p = 0; 199 while (p < size) { 200 rc = circ_buf_pop(&con->cbuf, &bp[p]); 201 if (rc != EOK) 202 break; 203 ++p; 204 } 205 206 fibril_mutex_unlock(&con->buf_lock); 207 208 *nread = p; 209 return EOK; 210 } 211 212 /** Write to Ski console device */ 213 static int ski_con_write(chardev_srv_t *srv, const void *data, size_t size, 214 size_t *nwr) 215 { 216 ski_con_t *con = (ski_con_t *) srv->srvs->sarg; 217 size_t i; 218 uint8_t *dp = (uint8_t *) data; 219 220 for (i = 0; i < size; i++) 221 ski_con_putchar(con, dp[i]); 222 223 *nwr = size; 224 return EOK; 225 } 226 159 227 /** Character device connection handler. */ 160 228 static void ski_con_connection(ipc_callid_t iid, ipc_call_t *icall, 161 229 void *arg) 162 230 { 163 ski_con_t *con; 164 165 /* Answer the IPC_M_CONNECT_ME_TO call. */ 166 async_answer_0(iid, EOK); 167 168 con = (ski_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg)); 169 170 while (true) { 171 ipc_call_t call; 172 ipc_callid_t callid = async_get_call(&call); 173 sysarg_t method = IPC_GET_IMETHOD(call); 174 175 if (!method) { 176 /* The other side has hung up. */ 177 async_answer_0(callid, EOK); 178 return; 179 } 180 181 async_sess_t *sess = 182 async_callback_receive_start(EXCHANGE_SERIALIZE, &call); 183 if (sess != NULL) { 184 if (con->client_sess == NULL) { 185 con->client_sess = sess; 186 async_answer_0(callid, EOK); 187 } else 188 async_answer_0(callid, ELIMIT); 189 } else { 190 switch (method) { 191 case CHAR_WRITE_BYTE: 192 ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n", 193 IPC_GET_ARG1(call)); 194 ski_con_putchar(con, (uint8_t) IPC_GET_ARG1(call)); 195 async_answer_0(callid, EOK); 196 break; 197 default: 198 async_answer_0(callid, EINVAL); 199 } 200 } 201 } 231 ski_con_t *con = (ski_con_t *) ddf_dev_data_get( 232 ddf_fun_get_dev((ddf_fun_t *) arg)); 233 234 chardev_conn(iid, icall, &con->cds); 202 235 } 203 236 -
uspace/drv/char/ski-con/ski-con.h
r74017ce r7a6065c 36 36 #define SKI_CON_H 37 37 38 #include <adt/circ_buf.h> 38 39 #include <async.h> 39 40 #include <ddf/driver.h> 41 #include <io/chardev_srv.h> 40 42 #include <loc.h> 41 43 #include <stdint.h> 44 45 enum { 46 ski_con_buf_size = 64 47 }; 42 48 43 49 /** Ski console */ … … 45 51 async_sess_t *client_sess; 46 52 ddf_dev_t *dev; 53 chardev_srvs_t cds; 54 circ_buf_t cbuf; 55 uint8_t buf[ski_con_buf_size]; 56 fibril_mutex_t buf_lock; 57 fibril_condvar_t buf_cv; 47 58 } ski_con_t; 48 59 -
uspace/drv/char/sun4v-con/sun4v-con.c
r74017ce r7a6065c 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 <i pc/char.h>38 #include <io/chardev_srv.h> 39 39 #include <stdbool.h> 40 40 #include <thread.h> … … 62 62 static input_buffer_t input_buffer; 63 63 64 static void sun4v_thread_impl(void *arg); 64 static int sun4v_con_read(chardev_srv_t *, void *, size_t, size_t *); 65 static int sun4v_con_write(chardev_srv_t *, const void *, size_t, size_t *); 66 67 static chardev_ops_t sun4v_con_chardev_ops = { 68 .read = sun4v_con_read, 69 .write = sun4v_con_write 70 }; 65 71 66 72 static void sun4v_con_putchar(sun4v_con_t *con, uint8_t data) … … 86 92 } 87 93 94 chardev_srvs_init(&con->cds); 95 con->cds.ops = &sun4v_con_chardev_ops; 96 con->cds.sarg = con; 97 88 98 ddf_fun_set_conn_handler(fun, sun4v_con_connection); 89 99 … … 94 104 goto error; 95 105 } 96 97 thread_id_t tid;98 rc = thread_create(sun4v_thread_impl, con, "kbd_poll", &tid);99 if (rc != EOK)100 goto error;101 106 102 107 rc = ddf_fun_bind(fun); … … 131 136 } 132 137 133 /** 134 * Called regularly by the polling thread. Reads codes of all the 135 * pressed keys from the buffer. 136 */ 137 static void sun4v_key_pressed(sun4v_con_t *con) 138 /** Read from Sun4v console device */ 139 static int sun4v_con_read(chardev_srv_t *srv, void *buf, size_t size, 140 size_t *nread) 138 141 { 142 size_t p; 143 uint8_t *bp = (uint8_t *) buf; 139 144 char c; 140 145 141 while (input_buffer->read_ptr != input_buffer->write_ptr) { 146 while (input_buffer->read_ptr == input_buffer->write_ptr) 147 fibril_usleep(POLL_INTERVAL); 148 149 p = 0; 150 while (p < size && input_buffer->read_ptr != input_buffer->write_ptr) { 142 151 c = input_buffer->data[input_buffer->read_ptr]; 143 152 input_buffer->read_ptr = 144 153 ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE; 145 if (con->client_sess != NULL) { 146 async_exch_t *exch = async_exchange_begin(con->client_sess); 147 async_msg_1(exch, CHAR_NOTIF_BYTE, c); 148 async_exchange_end(exch); 149 } 150 (void) c; 154 bp[p++] = c; 151 155 } 156 157 *nread = p; 158 return EOK; 152 159 } 153 160 154 /** 155 * Thread to poll Sun4v console for keypresses. 156 */ 157 static void sun4v_thread_impl(void *arg) 161 /** Write to Sun4v console device */ 162 static int sun4v_con_write(chardev_srv_t *srv, const void *data, size_t size, 163 size_t *nwr) 158 164 { 159 sun4v_con_t *con = (sun4v_con_t *) arg; 165 sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg; 166 size_t i; 167 uint8_t *dp = (uint8_t *) data; 160 168 161 while (true) { 162 sun4v_key_pressed(con); 163 thread_usleep(POLL_INTERVAL); 164 } 169 for (i = 0; i < size; i++) 170 sun4v_con_putchar(con, dp[i]); 171 172 *nwr = size; 173 return EOK; 165 174 } 166 175 … … 169 178 void *arg) 170 179 { 171 sun4v_con_t *con; 180 sun4v_con_t *con = (sun4v_con_t *) ddf_dev_data_get( 181 ddf_fun_get_dev((ddf_fun_t *) arg)); 172 182 173 /* Answer the IPC_M_CONNECT_ME_TO call. */ 174 async_answer_0(iid, EOK); 175 176 con = (sun4v_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg)); 177 178 while (true) { 179 ipc_call_t call; 180 ipc_callid_t callid = async_get_call(&call); 181 sysarg_t method = IPC_GET_IMETHOD(call); 182 183 if (!method) { 184 /* The other side has hung up. */ 185 async_answer_0(callid, EOK); 186 return; 187 } 188 189 async_sess_t *sess = 190 async_callback_receive_start(EXCHANGE_SERIALIZE, &call); 191 if (sess != NULL) { 192 if (con->client_sess == NULL) { 193 con->client_sess = sess; 194 async_answer_0(callid, EOK); 195 } else 196 async_answer_0(callid, ELIMIT); 197 } else { 198 switch (method) { 199 case CHAR_WRITE_BYTE: 200 ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n", 201 IPC_GET_ARG1(call)); 202 sun4v_con_putchar(con, (uint8_t) IPC_GET_ARG1(call)); 203 async_answer_0(callid, EOK); 204 break; 205 default: 206 async_answer_0(callid, EINVAL); 207 } 208 } 209 } 183 chardev_conn(iid, icall, &con->cds); 210 184 } 211 185 -
uspace/drv/char/sun4v-con/sun4v-con.h
r74017ce r7a6065c 38 38 #include <async.h> 39 39 #include <ddf/driver.h> 40 #include <io/chardev_srv.h> 40 41 #include <loc.h> 41 42 #include <stdint.h> … … 50 51 async_sess_t *client_sess; 51 52 ddf_dev_t *dev; 53 chardev_srvs_t cds; 52 54 sun4v_con_res_t res; 53 55 } sun4v_con_t;
Note:
See TracChangeset
for help on using the changeset viewer.