Changeset 7a6065c in mainline for uspace/srv
- 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/srv
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/hid/input/port/chardev.c
r74017ce r7a6065c 35 35 */ 36 36 37 #include <ipc/char.h>38 37 #include <async.h> 38 #include <errno.h> 39 #include <fibril.h> 40 #include <io/chardev.h> 39 41 #include <loc.h> 40 #include <errno.h>41 42 #include <stdio.h> 42 43 #include "../input.h" … … 44 45 #include "../kbd.h" 45 46 46 static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg);47 static int kbd_port_fibril(void *); 47 48 48 49 static int chardev_port_init(kbd_dev_t *); 49 static void chardev_port_write(uint8_t data);50 static void chardev_port_write(uint8_t); 50 51 51 52 kbd_port_ops_t chardev_port = { … … 56 57 static kbd_dev_t *kbd_dev; 57 58 static async_sess_t *dev_sess; 59 static chardev_t *chardev; 58 60 59 61 /** List of devices to try connecting to. */ … … 70 72 { 71 73 service_id_t service_id; 72 async_exch_t *exch;73 74 unsigned int i; 75 fid_t fid; 74 76 int rc; 75 77 … … 96 98 } 97 99 98 exch = async_exchange_begin(dev_sess);99 if ( exch == NULL) {100 printf("%s: Failed starting exchange withdevice\n", NAME);100 rc = chardev_open(dev_sess, &chardev); 101 if (rc != EOK) { 102 printf("%s: Failed opening character device\n", NAME); 101 103 async_hangup(dev_sess); 102 104 return ENOMEM; 103 105 } 104 106 105 port_id_t port; 106 rc = async_create_callback_port(exch, INTERFACE_CHAR_CB, 0, 0, 107 kbd_port_events, NULL, &port); 108 109 async_exchange_end(exch); 110 111 if (rc != 0) { 112 printf("%s: Failed to create callback from device\n", NAME); 107 fid = fibril_create(kbd_port_fibril, NULL); 108 if (fid == 0) { 109 printf("%s: Failed creating fibril\n", NAME); 110 chardev_close(chardev); 113 111 async_hangup(dev_sess); 114 return -1;112 return ENOMEM; 115 113 } 114 115 fibril_add_ready(fid); 116 116 117 117 printf("%s: Found input device '%s'\n", NAME, in_devs[i]); … … 121 121 static void chardev_port_write(uint8_t data) 122 122 { 123 async_exch_t *exch = async_exchange_begin(dev_sess); 124 if (exch == NULL) { 125 printf("%s: Failed starting exchange with device\n", NAME); 123 int 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); 126 129 return; 127 }128 129 async_msg_1(exch, CHAR_WRITE_BYTE, data);130 async_exchange_end(exch);131 }132 133 static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)134 {135 /* Ignore parameters, the connection is already opened */136 while (true) {137 138 ipc_call_t call;139 ipc_callid_t callid = async_get_call(&call);140 141 if (!IPC_GET_IMETHOD(call)) {142 /* TODO: Handle hangup */143 return;144 }145 146 int retval = EOK;147 148 switch (IPC_GET_IMETHOD(call)) {149 case CHAR_NOTIF_BYTE:150 kbd_push_data(kbd_dev, IPC_GET_ARG1(call));151 break;152 default:153 retval = ENOENT;154 }155 async_answer_0(callid, retval);156 130 } 157 131 } 158 132 133 static int kbd_port_fibril(void *arg) 134 { 135 int 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 } 159 151 160 152 /** -
uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.c
r74017ce r7a6065c 27 27 */ 28 28 29 /** @addtogroup driver_serial30 * @{31 */32 29 /** 33 30 * @file … … 37 34 */ 38 35 36 #include <async.h> 39 37 #include <ddi.h> 38 #include <errno.h> 39 #include <inttypes.h> 40 #include <io/chardev_srv.h> 40 41 #include <loc.h> 41 #include <ipc/char.h>42 #include <async.h>43 42 #include <stdio.h> 44 43 #include <stdlib.h> 45 44 #include <sysinfo.h> 46 #include <errno.h>47 #include <inttypes.h>48 45 #include "s3c24xx_uart.h" 49 46 … … 72 69 static void s3c24xx_uart_sendb(s3c24xx_uart_t *, uint8_t); 73 70 71 static int s3c24xx_uart_read(chardev_srv_t *, void *, size_t, size_t *); 72 static int s3c24xx_uart_write(chardev_srv_t *, const void *, size_t, size_t *); 73 74 static chardev_ops_t s3c24xx_uart_chardev_ops = { 75 .read = s3c24xx_uart_read, 76 .write = s3c24xx_uart_write 77 }; 78 74 79 int main(int argc, char *argv[]) 75 80 { 76 81 printf("%s: S3C24xx on-chip UART driver\n", NAME); 77 82 78 async_set_fallback_port_handler(s3c24xx_uart_connection, NULL);83 async_set_fallback_port_handler(s3c24xx_uart_connection, uart); 79 84 int rc = loc_server_register(NAME); 80 85 if (rc != EOK) { … … 111 116 void *arg) 112 117 { 113 /* Answer the IPC_M_CONNECT_ME_TO call. */ 114 async_answer_0(iid, EOK); 115 116 while (true) { 117 ipc_call_t call; 118 ipc_callid_t callid = async_get_call(&call); 119 sysarg_t method = IPC_GET_IMETHOD(call); 120 121 if (!method) { 122 /* The other side has hung up. */ 123 async_answer_0(callid, EOK); 124 return; 125 } 126 127 async_sess_t *sess = 128 async_callback_receive_start(EXCHANGE_SERIALIZE, &call); 129 if (sess != NULL) { 130 if (uart->client_sess == NULL) { 131 uart->client_sess = sess; 132 async_answer_0(callid, EOK); 133 } else 134 async_answer_0(callid, ELIMIT); 135 } else { 136 switch (method) { 137 case CHAR_WRITE_BYTE: 138 printf(NAME ": write %" PRIun " to device\n", 139 IPC_GET_ARG1(call)); 140 s3c24xx_uart_sendb(uart, (uint8_t) IPC_GET_ARG1(call)); 141 async_answer_0(callid, EOK); 142 break; 143 default: 144 async_answer_0(callid, EINVAL); 145 } 146 } 147 } 148 } 118 s3c24xx_uart_t *uart = (s3c24xx_uart_t *) arg; 119 120 chardev_conn(iid, icall, &uart->cds); 121 } 122 149 123 150 124 static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call, 151 125 void *arg) 152 126 { 127 int rc; 128 153 129 (void) iid; 154 130 (void) call; … … 159 135 uint32_t status = pio_read_32(&uart->io->uerstat); 160 136 161 if (uart->client_sess != NULL) { 162 async_exch_t *exch = async_exchange_begin(uart->client_sess); 163 async_msg_1(exch, CHAR_NOTIF_BYTE, data); 164 async_exchange_end(exch); 165 } 137 fibril_mutex_lock(&uart->buf_lock); 138 139 rc = circ_buf_push(&uart->cbuf, &data); 140 if (rc != EOK) 141 printf(NAME ": Buffer overrun\n"); 142 143 fibril_mutex_unlock(&uart->buf_lock); 144 fibril_condvar_broadcast(&uart->buf_cv); 166 145 167 146 if (status != 0) … … 176 155 sysarg_t inr; 177 156 157 circ_buf_init(&uart->cbuf, uart->buf, s3c24xx_uart_buf_size, 1); 158 fibril_mutex_initialize(&uart->buf_lock); 159 fibril_condvar_initialize(&uart->buf_cv); 160 178 161 if (sysinfo_get_value("s3c24xx_uart.address.physical", 179 162 &uart->paddr) != EOK) … … 188 171 189 172 uart->io = vaddr; 190 uart->client_sess = NULL;191 173 192 174 printf(NAME ": device at physical address %p, inr %" PRIun ".\n", … … 203 185 pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL); 204 186 187 chardev_srvs_init(&uart->cds); 188 uart->cds.ops = &s3c24xx_uart_chardev_ops; 189 uart->cds.sarg = uart; 190 205 191 return EOK; 206 192 } … … 216 202 } 217 203 204 static int s3c24xx_uart_read(chardev_srv_t *srv, void *buf, size_t size, 205 size_t *nread) 206 { 207 s3c24xx_uart_t *uart = (s3c24xx_uart_t *) srv->srvs->sarg; 208 size_t p; 209 uint8_t *bp = (uint8_t *) buf; 210 int rc; 211 212 fibril_mutex_lock(&uart->buf_lock); 213 214 while (circ_buf_nused(&uart->cbuf) == 0) 215 fibril_condvar_wait(&uart->buf_cv, &uart->buf_lock); 216 217 p = 0; 218 while (p < size) { 219 rc = circ_buf_pop(&uart->cbuf, &bp[p]); 220 if (rc != EOK) 221 break; 222 ++p; 223 } 224 225 fibril_mutex_unlock(&uart->buf_lock); 226 227 *nread = p; 228 return EOK; 229 } 230 231 static int s3c24xx_uart_write(chardev_srv_t *srv, const void *data, size_t size, 232 size_t *nwr) 233 { 234 s3c24xx_uart_t *uart = (s3c24xx_uart_t *) srv->srvs->sarg; 235 size_t i; 236 uint8_t *dp = (uint8_t *) data; 237 238 for (i = 0; i < size; i++) 239 s3c24xx_uart_sendb(uart, dp[i]); 240 241 *nwr = size; 242 return EOK; 243 } 244 245 218 246 /** @} 219 247 */ -
uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.h
r74017ce r7a6065c 38 38 #define S3C24XX_UART_H_ 39 39 40 #include <adt/circ_buf.h> 41 #include <async.h> 42 #include <fibril_synch.h> 43 #include <io/chardev_srv.h> 40 44 #include <stdint.h> 41 #include <async.h>42 45 43 46 /** S3C24xx UART I/O */ … … 76 79 #define UFCON_FIFO_ENABLE 0x01 77 80 81 enum { 82 s3c24xx_uart_buf_size = 64 83 }; 78 84 79 85 /** S3C24xx UART instance */ … … 85 91 s3c24xx_uart_io_t *io; 86 92 87 /** C allback session to the client*/88 async_sess_t *client_sess;93 /** Character device service */ 94 chardev_srvs_t cds; 89 95 90 96 /** Service ID */ 91 97 service_id_t service_id; 98 99 /** Circular buffer */ 100 circ_buf_t cbuf; 101 /** Buffer */ 102 uint8_t buf[s3c24xx_uart_buf_size]; 103 /** Buffer lock */ 104 fibril_mutex_t buf_lock; 105 /** Signal newly added data in buffer */ 106 fibril_condvar_t buf_cv; 92 107 } s3c24xx_uart_t; 93 108
Note:
See TracChangeset
for help on using the changeset viewer.